123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\model\Category as CategoryModel;
- use fast\Tree;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 保险产品
- *
- * @icon fa fa-circle-o
- */
- class Product extends Backend
- {
-
- /**
- * Product模型对象
- * @var \app\admin\model\Product
- */
- protected $model = null;
- protected $c_model = null;
- protected $categorylist = [];
- protected $relationSearch = true;
- protected $searchFields = ["name"];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Product;
- $this->view->assign("attributeList", $this->model->getAttributeList());
- $this->c_model = model('app\common\model\Category');
- $tree = Tree::instance();
- $tree->init(collection($this->c_model->order('weigh desc,id desc')->select())->toArray(), 'pid');
- $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
- $categorydata = [0 => ['type' => 'test', 'name' => __('None')]];
- foreach ($this->categorylist as $k => $v) {
- $categorydata[$v['id']] = $v;
- }
- $typeList = CategoryModel::getTypeList();
- $this->view->assign("flagList", $this->c_model->getFlagList());
- $this->view->assign("typeList", $typeList);
- $this->view->assign("parentList", $categorydata);
- $group_id = $this->auth->getGroupIds();
- $this->gid = $group_id[0];
- $this->view->assign("group_id", $group_id[0]);
- }
- public function import()
- {
- parent::import();
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
-
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
-
- $list = $this->model
- ->with(['company','category'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- /*if($list){
- foreach ($list as $key=>$value){
- $list[$key]['category_name'] = \app\common\model\Category::where(array('id'=>$value['category_id']))->value('name');
- $list[$key]['company_name'] = \app\admin\model\Company::where(array('id'=>$value['company_id']))->value('name');
- }
- }*/
- foreach ($list as $row) {
- //$row->visible(['id','name','category_id','category_name','company_name','company_id','sort','createtime','p_image','status','attribute']);
-
-
-
- $row->getRelation('company')->visible(['name']);
-
- $row->getRelation('category')->visible(['name']);
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 获取分类数据
- */
- public function params(){
- $id = $this->request->post("id",0);
- $content = \app\common\model\Category::where(array('id'=>$id))->value('content');
- if(!empty($content)){
- return json(array('code'=>1,'msg'=>'成功','data'=>$content));
- }else{
- return json(array('code'=>0,'msg'=>'成功','data'=>$content));
- }
- }
- public function getCjson(){
- $list = $this->categorylist;
- return json($list);
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- //print_r($params['key1']);exit;
- //处理追加数组
- if(!empty($params['key1']) && !empty($params['name1'])){
- $new_array = array();
- $key1 = $params['key1'];
- $name1 = $params['name1'];
- foreach ($key1 as $key=>$value){
- $new_array[$key]['key'] = $value;
- }
- foreach ($name1 as $key=>$value){
- $new_array[$key]['name'] = $value;
- }
- $k = count($new_array);;
- if(!empty($params['content'])){
- $content = json_decode($params['content'],true);
- foreach ($new_array as $key=>$value){
- if(isset($content[$key]['key']) && isset($content[$key]['name'])){
- $new_array[$k]['key'] = $content[$key]['key'];
- $new_array[$k]['name'] = $content[$key]['name'];
- $k++;
- }else{
- break;
- }
- }
- // if(!empty($new_array)){
- // array_walk($new_array,function($item) use (&$content) {
- // array_unshift($content, $item);
- // });
- // }
- $content = $new_array;
- }else{
- $content = $new_array;
- }
- $params['content'] = json_encode($content,true);
- }
- // print_r($params);exit;
- if ($params) {
- $params = $this->preExcludeFields($params);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- // $row = $this->model->get(1);
- // $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- if($this->gid == 6 && $row['status']==0)
- {
- $this->error('上架商品无法修改哦');
- // $this->success('上架商品无法修改哦');
- }
-
-
- //固定模板属性和追加属性分开
- $rss = db('Category')->where('id',$row['category_id'])->find();//分类模板属性
- $content = json_decode($row['content'],true); //当前全部属性
- $content_one = json_decode($rss['content'],true); //模板属性
- $cha = [];//客户顾定模板设置
- $cha_arr = []; //自由追加的数据
- if($content)
- {
-
- foreach ($content as $key=>$val)
- {
- if(!isset($val['key'])) continue;
- if(isset($content_one[$key]))
- { //print_r($content_one[$key]);
- /// var_dump($val['key']);
- if ($val['key'] == $content_one[$key]['key'])
- {
- $cha[] = $val;
- }
- else
- {
- $cha_arr[] = $val;
- }
-
- } else {
- $cha_arr[] = $val;
- }
-
-
- }
-
- }
- //exit;
- // print_r($cha);
-
- $row['content'] = json_encode($cha_arr,JSON_UNESCAPED_UNICODE);
-
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
-
- if($this->gid == 7 || $this->gid == 6)
- {
- unset($params['sort']);
- }
- if(count($params) == 1 && isset($params['sort'])) //证明是更新排序的
- {
-
- $rows = Db::name('Product')->where('id',$row['id'])->setField('sort', $params['sort']);
- $this->success();exit;
- }
- //处理追加数组
- if(!empty($params['key1']) && !empty($params['name1'])){
- $new_array = array();
- $key1 = $params['key1'];
- $name1 = $params['name1'];
- foreach ($key1 as $key=>$value){
- $new_array[$key]['key'] = $value;
- }
- foreach ($name1 as $key=>$value){
- $new_array[$key]['name'] = $value;
- }
- $k = count($new_array);;
- if(!empty($params['content'])){
- $content = json_decode($params['content'],true);
- foreach ($new_array as $key=>$value){
- if(isset($content[$key]['key']) && isset($content[$key]['name'])){
- $new_array[$k]['key'] = $content[$key]['key'];
- $new_array[$k]['name'] = $content[$key]['name'];
- $k++;
- }else{
- break;
- }
- }
- // if(!empty($new_array)){
- // array_walk($new_array,function($item) use (&$content) {
- // array_unshift($content, $item);
- // });
- // }
- $content = $new_array;
- }else{
- $content = $new_array;
- }
- $params['content'] = json_encode($content,true);
- }
- if ($params) {
- $params = $this->preExcludeFields($params);
-
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("content_one", $cha);
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- }
|