Address.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\shop\Area;
  9. use app\common\Enum\StatusEnum;
  10. /**
  11. * 收货地址
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Address extends Backend
  16. {
  17. /**
  18. * Address模型对象
  19. * @var \app\admin\model\shop\Address
  20. */
  21. protected $model = null;
  22. protected $relationSearch = true;
  23. protected $noNeedRight = ['area'];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\shop\Address;
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. $this->assignconfig('statusList', json_encode($this->model->getStatusList()));
  30. $this->assignconfig('isdefaultList', json_encode(StatusEnum::getYesNoMap()));
  31. }
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $total = $this->model
  46. ->with(['user', 'province', 'city', 'area'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->count();
  50. $list = $this->model
  51. ->with(['user', 'province', 'city', 'area'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->limit($offset, $limit)
  55. ->select();
  56. $result = array("total" => $total, "rows" => $list);
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 添加
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. if ($params) {
  69. $params['province_id'] = $this->request->post('province');
  70. $params['city_id'] = $this->request->post('city');
  71. $params['area_id'] = $this->request->post('area');
  72. $params = $this->preExcludeFields($params);
  73. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  74. $params[$this->dataLimitField] = $this->auth->id;
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException(true)->validate($validate);
  84. }
  85. $result = $this->model->allowField(true)->save($params);
  86. Db::commit();
  87. } catch (ValidateException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (PDOException $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. } catch (Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result !== false) {
  98. $this->success();
  99. } else {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. return $this->view->fetch();
  106. }
  107. /**
  108. * 编辑
  109. */
  110. public function edit($ids = null)
  111. {
  112. $row = $this->model->get($ids, ['Province', 'City', 'Area']);
  113. if (!$row) {
  114. $this->error(__('No Results were found'));
  115. }
  116. $adminIds = $this->getDataLimitAdminIds();
  117. if (is_array($adminIds)) {
  118. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  119. $this->error(__('You have no permission'));
  120. }
  121. }
  122. if ($this->request->isPost()) {
  123. $params = $this->request->post("row/a");
  124. if ($params) {
  125. $params['province_id'] = $this->request->post('province');
  126. $params['city_id'] = $this->request->post('city');
  127. $params['area_id'] = $this->request->post('area');
  128. $params = $this->preExcludeFields($params);
  129. $result = false;
  130. Db::startTrans();
  131. try {
  132. //是否采用模型验证
  133. if ($this->modelValidate) {
  134. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  135. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  136. $row->validateFailException(true)->validate($validate);
  137. }
  138. $result = $row->allowField(true)->save($params);
  139. Db::commit();
  140. } catch (ValidateException $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. } catch (PDOException $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. } catch (Exception $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. }
  150. if ($result !== false) {
  151. $this->success();
  152. } else {
  153. $this->error(__('No rows were updated'));
  154. }
  155. }
  156. $this->error(__('Parameter %s can not be empty', ''));
  157. }
  158. $this->view->assign("row", $row);
  159. return $this->view->fetch();
  160. }
  161. /**
  162. * 读取省市区数据,联动列表
  163. */
  164. public function area()
  165. {
  166. $params = $this->request->get("row/a");
  167. if (!empty($params)) {
  168. $province = isset($params['province']) ? $params['province'] : '';
  169. $city = isset($params['city']) ? $params['city'] : '';
  170. } else {
  171. $province = $this->request->get('province', '');
  172. $city = $this->request->get('city', '');
  173. }
  174. $where = ['pid' => 0, 'level' => 1];
  175. $provincelist = null;
  176. if ($province !== '') {
  177. $where['pid'] = $province;
  178. $where['level'] = 2;
  179. if ($city !== '') {
  180. $where['pid'] = $city;
  181. $where['level'] = 3;
  182. }
  183. }
  184. $provincelist = Area::where($where)->field('id as value,name')->where('status', 'normal')->select();
  185. $this->success('', '', $provincelist);
  186. }
  187. }