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