InsuranceInfo.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 InsuranceInfo extends Backend
  15. {
  16. /**
  17. * InsuranceInfo模型对象
  18. * @var \app\admin\model\InsuranceInfo
  19. */
  20. protected $model = null;
  21. protected $c_model = null;
  22. protected $categorylist = [];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\InsuranceInfo();
  27. $this->c_model = new \app\admin\model\InsuranceInfo;
  28. $this->c_model = model('app\common\model\Category');
  29. $tree = Tree::instance();
  30. $tree->init(collection($this->c_model->order('weigh desc,id desc')->where(['type'=>'tuijianchaxunyijifenlei'])->select())->toArray(), 'pid');
  31. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  32. $categorydata = [0 => ['type' => 'test', 'name' => __('None')]];
  33. foreach ($this->categorylist as $k => $v) {
  34. $categorydata[$v['id']] = $v;
  35. }
  36. $typeList = CategoryModel::getTypeList();
  37. $this->view->assign("flagList", $this->c_model->getFlagList());
  38. $this->view->assign("typeList", $typeList);
  39. $this->view->assign("parentList", $categorydata);
  40. }
  41. public function import()
  42. {
  43. parent::import();
  44. }
  45. /**
  46. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  47. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  48. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  49. */
  50. /**
  51. * 查看
  52. */
  53. public function index()
  54. {
  55. //当前是否为关联查询
  56. $this->relationSearch = false;
  57. //设置过滤方法
  58. $this->request->filter(['strip_tags', 'trim']);
  59. if ($this->request->isAjax()) {
  60. //如果发送的来源是Selectpage,则转发到Selectpage
  61. if ($this->request->request('keyField')) {
  62. return $this->selectpage();
  63. }
  64. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  65. $list = $this->model
  66. ->where($where)
  67. ->order($sort, $order)
  68. ->paginate($limit);
  69. foreach ($list as $row) {
  70. $row->visible(['id','name','category_id','i_image','createtime','sort','age_limit','maximum_coverage','female_price','buy']);
  71. }
  72. $result = array("total" => $list->total(), "rows" => $list->items());
  73. return json($result);
  74. }
  75. return $this->view->fetch();
  76. }
  77. /**
  78. * 编辑
  79. */
  80. public function edit($ids = null)
  81. {
  82. $row = $this->model->get($ids);
  83. if (!$row) {
  84. $this->error(__('No Results were found'));
  85. }
  86. $adminIds = $this->getDataLimitAdminIds();
  87. if (is_array($adminIds)) {
  88. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  89. $this->error(__('You have no permission'));
  90. }
  91. }
  92. if ($this->request->isPost()) {
  93. $params = $this->request->post("row/a");
  94. if ($params) {
  95. $params = $this->preExcludeFields($params);
  96. $result = false;
  97. Db::startTrans();
  98. try {
  99. //是否采用模型验证
  100. if ($this->modelValidate) {
  101. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  102. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  103. $row->validateFailException(true)->validate($validate);
  104. }
  105. $result = $row->allowField(true)->save($params);
  106. Db::commit();
  107. } catch (ValidateException $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. } catch (PDOException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (Exception $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. }
  117. if ($result !== false) {
  118. $this->success();
  119. } else {
  120. $this->error(__('No rows were updated'));
  121. }
  122. }
  123. $this->error(__('Parameter %s can not be empty', ''));
  124. }
  125. $this->view->assign("row", $row);
  126. return $this->view->fetch();
  127. }
  128. }