Company.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 门店
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Company extends Backend
  11. {
  12. /**
  13. * Company模型对象
  14. * @var \app\admin\model\Company
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Company;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. $this->view->assign("isOpenList", $this->model->getIsOpenList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 添加
  31. */
  32. public function add()
  33. {
  34. if ($this->request->isPost()) {
  35. $params = $this->request->post("row/a");
  36. $params = $this->preExcludeFields($params);
  37. if (!$params) {
  38. $this->error(__('Parameter %s can not be empty', ''));
  39. }
  40. $result = false;
  41. try {
  42. //是否采用模型验证
  43. if ($this->modelValidate) {
  44. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  45. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  46. $this->model->validateFailException(true)->validate($validate);
  47. }
  48. if(isset($params['area_id'])) {
  49. $areaWhere['id'] = $params['area_id'];
  50. $areaData = Db::name('shopro_area')->where($areaWhere)->find();
  51. $params['province_name'] = isset($areaData['province_name']) ? $areaData['province_name'] : '';
  52. $params['city_name'] = isset($areaData['city_name']) ? $areaData['city_name'] : '';
  53. $params['area_name'] = isset($areaData['name']) ? $areaData['name'] : '';
  54. $params['full_address'] = $params['province_name'].$params['city_name'].$params['area_name'].$params['address'];
  55. }
  56. $result = $this->model->allowField(true)->save($params);
  57. } catch (ValidateException|PDOException|Exception $e) {
  58. $this->error($e->getMessage());
  59. }
  60. if ($result == false) {
  61. $this->error(__('No rows were inserted'));
  62. }
  63. $this->success();
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 编辑
  69. */
  70. public function edit($ids = null)
  71. {
  72. $row = $this->model->get($ids);
  73. if (!$row) {
  74. $this->error(__('No Results were found'));
  75. }
  76. $adminIds = $this->getDataLimitAdminIds();
  77. if (is_array($adminIds)) {
  78. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  79. $this->error(__('You have no permission'));
  80. }
  81. }
  82. if ($this->request->isPost()) {
  83. $params = $this->request->post("row/a");
  84. if (!$params) {
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. $params = $this->preExcludeFields($params);
  88. $result = false;
  89. try {
  90. //是否采用模型验证
  91. if ($this->modelValidate) {
  92. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  93. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  94. $row->validateFailException(true)->validate($validate);
  95. }
  96. if(isset($params['area_id'])) {
  97. $areaWhere['id'] = $params['area_id'];
  98. $areaData = Db::name('shopro_area')->where($areaWhere)->find();
  99. $params['province_name'] = isset($areaData['province_name']) ? $areaData['province_name'] : '';
  100. $params['city_name'] = isset($areaData['city_name']) ? $areaData['city_name'] : '';
  101. $params['area_name'] = isset($areaData['name']) ? $areaData['name'] : '';
  102. $params['full_address'] = $params['province_name'].$params['city_name'].$params['area_name'].$params['address'];
  103. }
  104. $result = $row->allowField(true)->save($params);
  105. } catch (ValidateException|PDOException|Exception $e) {
  106. $this->error($e->getMessage());
  107. }
  108. if ($result == false) {
  109. $this->error(__('No rows were updated'));
  110. }
  111. $this->success();
  112. }
  113. $this->view->assign("row", $row);
  114. return $this->view->fetch();
  115. }
  116. }