Product.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\Category as CategoryModel;
  5. use fast\Tree;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 保险产品
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Product extends Backend
  15. {
  16. /**
  17. * Product模型对象
  18. * @var \app\admin\model\Product
  19. */
  20. protected $model = null;
  21. protected $c_model = null;
  22. protected $categorylist = [];
  23. protected $relationSearch = true;
  24. protected $searchFields = ["name"];
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = new \app\admin\model\Product;
  29. $this->view->assign("attributeList", $this->model->getAttributeList());
  30. $this->c_model = model('app\common\model\Category');
  31. $tree = Tree::instance();
  32. $tree->init(collection($this->c_model->order('weigh desc,id desc')->select())->toArray(), 'pid');
  33. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  34. $categorydata = [0 => ['type' => 'test', 'name' => __('None')]];
  35. foreach ($this->categorylist as $k => $v) {
  36. $categorydata[$v['id']] = $v;
  37. }
  38. $typeList = CategoryModel::getTypeList();
  39. $this->view->assign("flagList", $this->c_model->getFlagList());
  40. $this->view->assign("typeList", $typeList);
  41. $this->view->assign("parentList", $categorydata);
  42. $group_id = $this->auth->getGroupIds();
  43. $this->gid = $group_id[0];
  44. $this->view->assign("group_id", $group_id[0]);
  45. }
  46. public function import()
  47. {
  48. parent::import();
  49. }
  50. /**
  51. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  52. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  53. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  54. */
  55. /**
  56. * 查看
  57. */
  58. public function index()
  59. {
  60. //当前是否为关联查询
  61. $this->relationSearch = true;
  62. //设置过滤方法
  63. $this->request->filter(['strip_tags', 'trim']);
  64. if ($this->request->isAjax()) {
  65. //如果发送的来源是Selectpage,则转发到Selectpage
  66. if ($this->request->request('keyField')) {
  67. return $this->selectpage();
  68. }
  69. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  70. $list = $this->model
  71. ->with(['company','category'])
  72. ->where($where)
  73. ->order($sort, $order)
  74. ->paginate($limit);
  75. /*if($list){
  76. foreach ($list as $key=>$value){
  77. $list[$key]['category_name'] = \app\common\model\Category::where(array('id'=>$value['category_id']))->value('name');
  78. $list[$key]['company_name'] = \app\admin\model\Company::where(array('id'=>$value['company_id']))->value('name');
  79. }
  80. }*/
  81. foreach ($list as $row) {
  82. //$row->visible(['id','name','category_id','category_name','company_name','company_id','sort','createtime','p_image','status','attribute']);
  83. $row->getRelation('company')->visible(['name']);
  84. $row->getRelation('category')->visible(['name']);
  85. }
  86. $result = array("total" => $list->total(), "rows" => $list->items());
  87. return json($result);
  88. }
  89. return $this->view->fetch();
  90. }
  91. /**
  92. * 获取分类数据
  93. */
  94. public function params(){
  95. $id = $this->request->post("id",0);
  96. $content = \app\common\model\Category::where(array('id'=>$id))->value('content');
  97. if(!empty($content)){
  98. return json(array('code'=>1,'msg'=>'成功','data'=>$content));
  99. }else{
  100. return json(array('code'=>0,'msg'=>'成功','data'=>$content));
  101. }
  102. }
  103. public function getCjson(){
  104. $list = $this->categorylist;
  105. return json($list);
  106. }
  107. /**
  108. * 添加
  109. */
  110. public function add()
  111. {
  112. if ($this->request->isPost()) {
  113. $params = $this->request->post("row/a");
  114. //print_r($params['key1']);exit;
  115. //处理追加数组
  116. if(!empty($params['key1']) && !empty($params['name1'])){
  117. $new_array = array();
  118. $key1 = $params['key1'];
  119. $name1 = $params['name1'];
  120. foreach ($key1 as $key=>$value){
  121. $new_array[$key]['key'] = $value;
  122. }
  123. foreach ($name1 as $key=>$value){
  124. $new_array[$key]['name'] = $value;
  125. }
  126. $k = count($new_array);;
  127. if(!empty($params['content'])){
  128. $content = json_decode($params['content'],true);
  129. foreach ($new_array as $key=>$value){
  130. if(isset($content[$key]['key']) && isset($content[$key]['name'])){
  131. $new_array[$k]['key'] = $content[$key]['key'];
  132. $new_array[$k]['name'] = $content[$key]['name'];
  133. $k++;
  134. }else{
  135. break;
  136. }
  137. }
  138. // if(!empty($new_array)){
  139. // array_walk($new_array,function($item) use (&$content) {
  140. // array_unshift($content, $item);
  141. // });
  142. // }
  143. $content = $new_array;
  144. }else{
  145. $content = $new_array;
  146. }
  147. $params['content'] = json_encode($content,true);
  148. }
  149. // print_r($params);exit;
  150. if ($params) {
  151. $params = $this->preExcludeFields($params);
  152. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  153. $params[$this->dataLimitField] = $this->auth->id;
  154. }
  155. $result = false;
  156. Db::startTrans();
  157. try {
  158. //是否采用模型验证
  159. if ($this->modelValidate) {
  160. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  161. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  162. $this->model->validateFailException(true)->validate($validate);
  163. }
  164. $result = $this->model->allowField(true)->save($params);
  165. Db::commit();
  166. } catch (ValidateException $e) {
  167. Db::rollback();
  168. $this->error($e->getMessage());
  169. } catch (PDOException $e) {
  170. Db::rollback();
  171. $this->error($e->getMessage());
  172. } catch (Exception $e) {
  173. Db::rollback();
  174. $this->error($e->getMessage());
  175. }
  176. if ($result !== false) {
  177. $this->success();
  178. } else {
  179. $this->error(__('No rows were inserted'));
  180. }
  181. }
  182. $this->error(__('Parameter %s can not be empty', ''));
  183. }
  184. // $row = $this->model->get(1);
  185. // $this->view->assign("row", $row);
  186. return $this->view->fetch();
  187. }
  188. /**
  189. * 编辑
  190. */
  191. public function edit($ids = null)
  192. {
  193. $row = $this->model->get($ids);
  194. if (!$row) {
  195. $this->error(__('No Results were found'));
  196. }
  197. if($this->gid == 6 && $row['status']==0)
  198. {
  199. $this->error('上架商品无法修改哦');
  200. // $this->success('上架商品无法修改哦');
  201. }
  202. //固定模板属性和追加属性分开
  203. $rss = db('Category')->where('id',$row['category_id'])->find();//分类模板属性
  204. $content = json_decode($row['content'],true); //当前全部属性
  205. $content_one = json_decode($rss['content'],true); //模板属性
  206. $cha = [];//客户顾定模板设置
  207. $cha_arr = []; //自由追加的数据
  208. if($content)
  209. {
  210. foreach ($content as $key=>$val)
  211. {
  212. if(!isset($val['key'])) continue;
  213. if(isset($content_one[$key]))
  214. { //print_r($content_one[$key]);
  215. /// var_dump($val['key']);
  216. if ($val['key'] == $content_one[$key]['key'])
  217. {
  218. $cha[] = $val;
  219. }
  220. else
  221. {
  222. $cha_arr[] = $val;
  223. }
  224. } else {
  225. $cha_arr[] = $val;
  226. }
  227. }
  228. }
  229. //exit;
  230. // print_r($cha);
  231. $row['content'] = json_encode($cha_arr,JSON_UNESCAPED_UNICODE);
  232. $adminIds = $this->getDataLimitAdminIds();
  233. if (is_array($adminIds)) {
  234. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  235. $this->error(__('You have no permission'));
  236. }
  237. }
  238. if ($this->request->isPost()) {
  239. $params = $this->request->post("row/a");
  240. if($this->gid == 7 || $this->gid == 6)
  241. {
  242. unset($params['sort']);
  243. }
  244. if(count($params) == 1 && isset($params['sort'])) //证明是更新排序的
  245. {
  246. $rows = Db::name('Product')->where('id',$row['id'])->setField('sort', $params['sort']);
  247. $this->success();exit;
  248. }
  249. //处理追加数组
  250. if(!empty($params['key1']) && !empty($params['name1'])){
  251. $new_array = array();
  252. $key1 = $params['key1'];
  253. $name1 = $params['name1'];
  254. foreach ($key1 as $key=>$value){
  255. $new_array[$key]['key'] = $value;
  256. }
  257. foreach ($name1 as $key=>$value){
  258. $new_array[$key]['name'] = $value;
  259. }
  260. $k = count($new_array);;
  261. if(!empty($params['content'])){
  262. $content = json_decode($params['content'],true);
  263. foreach ($new_array as $key=>$value){
  264. if(isset($content[$key]['key']) && isset($content[$key]['name'])){
  265. $new_array[$k]['key'] = $content[$key]['key'];
  266. $new_array[$k]['name'] = $content[$key]['name'];
  267. $k++;
  268. }else{
  269. break;
  270. }
  271. }
  272. // if(!empty($new_array)){
  273. // array_walk($new_array,function($item) use (&$content) {
  274. // array_unshift($content, $item);
  275. // });
  276. // }
  277. $content = $new_array;
  278. }else{
  279. $content = $new_array;
  280. }
  281. $params['content'] = json_encode($content,true);
  282. }
  283. if ($params) {
  284. $params = $this->preExcludeFields($params);
  285. $result = false;
  286. Db::startTrans();
  287. try {
  288. //是否采用模型验证
  289. if ($this->modelValidate) {
  290. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  291. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  292. $row->validateFailException(true)->validate($validate);
  293. }
  294. $result = $row->allowField(true)->save($params);
  295. Db::commit();
  296. } catch (ValidateException $e) {
  297. Db::rollback();
  298. $this->error($e->getMessage());
  299. } catch (PDOException $e) {
  300. Db::rollback();
  301. $this->error($e->getMessage());
  302. } catch (Exception $e) {
  303. Db::rollback();
  304. $this->error($e->getMessage());
  305. }
  306. if ($result !== false) {
  307. $this->success();
  308. } else {
  309. $this->error(__('No rows were updated'));
  310. }
  311. }
  312. $this->error(__('Parameter %s can not be empty', ''));
  313. }
  314. $this->view->assign("content_one", $cha);
  315. $this->view->assign("row", $row);
  316. return $this->view->fetch();
  317. }
  318. }