Store.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\model\Area;
  4. use app\api\service\BDService;
  5. use app\common\controller\Backend;
  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 Store extends Backend
  15. {
  16. protected $noNeedRight = ["fetchWhere", "profit_rate"];
  17. /**
  18. * Store模型对象
  19. * @var \app\admin\model\store\Store
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\store\Store;
  26. $this->assignconfig(["admin" => $this->auth->getUserInfo()]);
  27. $this->assign("is_edit_name", $this->auth->getUserInfo()["type"] == \E_IDENTITY_TYPE::Platform);
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. }
  30. private function fetchWhere()
  31. {
  32. $admin = $this->auth->getUserInfo();
  33. if (!$admin)
  34. $this->error("error");
  35. $p_where = [];
  36. if (\E_ADMIN_TYPE::Store === $admin["type"]) {
  37. array_push($p_where, ["id", "=", $admin["store_id"]]);
  38. }
  39. if (\E_ADMIN_TYPE::Agency === $admin["type"]) {
  40. array_push($p_where, ["city_code", "in", is_null($admin["city_codes"]) ? [] : explode(",", $admin["city_codes"])]);
  41. }
  42. return $p_where;
  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->relationSearch = false;
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags', 'trim']);
  58. if ($this->request->isAjax()) {
  59. //如果发送的来源是Selectpage,则转发到Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->c_selectpage($this->fetchWhere());
  62. }
  63. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  64. $query = $this->model
  65. ->where($where);
  66. foreach ($this->fetchWhere() as $item) {
  67. $query->where($item[0], $item[1], $item[2]);
  68. }
  69. $list = $query->order($sort, $order)
  70. ->paginate($limit);
  71. foreach ($list as $row) {
  72. $row->visible(['id', 'name', 'legal_person_name', 'invite_qr_code', 'profit_amount', 'photo_images', 'tags', 'star', 'phone', 'email', 'area_city', 'address', 'status', 'hot', 'sort', 'updatetime', 'city_code']);
  73. }
  74. $result = array("total" => $list->total(), "rows" => $list->items());
  75. return json($result);
  76. }
  77. return $this->view->fetch();
  78. }
  79. public function add()
  80. {
  81. if (false === $this->request->isPost()) {
  82. return $this->view->fetch();
  83. }
  84. $params = $this->request->post('row/a');
  85. if (empty($params)) {
  86. $this->error(__('Parameter %s can not be empty', ''));
  87. }
  88. $params = $this->preExcludeFields($params);
  89. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  90. $params[$this->dataLimitField] = $this->auth->id;
  91. }
  92. if(mb_strlen($params["area_city"]) > 0) {
  93. $area_city = join(',', explode("/", $params["area_city"]));
  94. } else {
  95. $area_city = "湖南省,长沙市,长沙县";
  96. }
  97. $area = (new Area())->where("mergename", "中国,{$area_city}")->find();
  98. if (!$area)
  99. $this->error("地区选择错误!");
  100. $params["county_code"] = $area["area_code"];
  101. $params["city_code"] = (int)($area["area_code"] / 100) * 100;
  102. $params["p_code"] = (int)($area["area_code"] / 1000) * 1000;
  103. if ((($area["area_code"] / 100) - (int)($area["area_code"] / 100)) == 0) {
  104. $this->error("请选择区/县");
  105. }
  106. $params["lng"] = $area["lng"];
  107. $params["lat"] = $area["lat"];
  108. $s_res = BDService::geocoding($params["address"] ?? '');
  109. if ($s_res) {
  110. $params["lng"] = $s_res->data()["lng"];
  111. $params["lat"] = $s_res->data()["lat"];
  112. }
  113. $params["status"] = $this->auth->getUserInfo()["type"] == \E_IDENTITY_TYPE::Platform ? \E_BASE_STATUS::Normal : \E_BASE_STATUS::Checking;
  114. $result = false;
  115. Db::startTrans();
  116. try {
  117. //是否采用模型验证
  118. if ($this->modelValidate) {
  119. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  120. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  121. $this->model->validateFailException()->validate($validate);
  122. }
  123. $store_id = $this->model->allowField(true)->insertGetId($params);
  124. $qr_code = \qrcodeBase64("https://pbh5.xunsoftware.com/pages/user/login", ["invite_store_id" => $store_id]);
  125. $this->model->update([
  126. "invite_qr_code" => $qr_code
  127. ], ["id" => $store_id]);
  128. $result = true;
  129. Db::commit();
  130. } catch (ValidateException | PDOException | Exception $e) {
  131. Db::rollback();
  132. $this->error($e->getMessage());
  133. }
  134. if ($result === false) {
  135. $this->error(__('No rows were inserted'));
  136. }
  137. $this->success();
  138. }
  139. public function edit($ids = null)
  140. {
  141. $admin = $this->auth->getUserInfo();
  142. $row = $this->model->get($ids);
  143. if (!$row) {
  144. $this->error(__('No Results were found'));
  145. }
  146. $adminIds = $this->getDataLimitAdminIds();
  147. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  148. $this->error(__('You have no permission'));
  149. }
  150. if (false === $this->request->isPost()) {
  151. $this->view->assign([
  152. 'row' => $row,
  153. 'admin_type' => $admin["type"]
  154. ]);
  155. return $this->view->fetch();
  156. }
  157. $params = $this->request->post('row/a');
  158. if (empty($params)) {
  159. $this->error(__('Parameter %s can not be empty', ''));
  160. }
  161. $params = $this->preExcludeFields($params);
  162. $area_city = join(',', explode("/", $params["area_city"]));
  163. $area = (new Area())->where("mergename", "中国,{$area_city}")->find();
  164. if (!$area)
  165. $this->error("地区选择错误!");
  166. $params["lng"] = $area["lng"];
  167. $params["lat"] = $area["lat"];
  168. $params["county_code"] = $area["area_code"];
  169. $params["city_code"] = (int)($area["area_code"] / 100) * 100;
  170. $params["p_code"] = (int)($area["area_code"] / 1000) * 1000;
  171. if ((($area["area_code"] / 100) - (int)($area["area_code"] / 100)) == 0) {
  172. $this->error("请选择区/县");
  173. }
  174. $s_res = BDService::geocoding($params["address"] ?? '');
  175. if ($s_res) {
  176. $params["lng"] = $s_res->data()["lng"];
  177. $params["lat"] = $s_res->data()["lat"];
  178. }
  179. if ($admin["type"] !== \E_IDENTITY_TYPE::Platform) {
  180. unset($params["status"]);
  181. }
  182. $result = false;
  183. Db::startTrans();
  184. try {
  185. //是否采用模型验证
  186. if ($this->modelValidate) {
  187. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  188. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  189. $row->validateFailException()->validate($validate);
  190. }
  191. if (null === $row["invite_qr_code"]) {
  192. $params["invite_qr_code"] = \qrcodeBase64("https://pbh5.xunsoftware.com/pages/user/login", ["invite_store_id" => $ids]);
  193. }
  194. $result = $row->allowField(true)->save($params);
  195. Db::commit();
  196. } catch (ValidateException | PDOException | Exception $e) {
  197. Db::rollback();
  198. $this->error($e->getMessage());
  199. }
  200. if (false === $result) {
  201. $this->error(__('No rows were updated'));
  202. }
  203. $this->success();
  204. }
  205. public function profit_rate($ids = null)
  206. {
  207. $admin = $this->auth->getUserInfo();
  208. $row = $this->model->get($ids);
  209. if (!$row) {
  210. $this->error(__('No Results were found'));
  211. }
  212. if (false === $this->request->isPost()) {
  213. $this->view->assign([
  214. 'row' => $row,
  215. 'admin_type' => $admin["type"]
  216. ]);
  217. return $this->view->fetch();
  218. }
  219. $params = $this->request->param();
  220. $platform_profit_rate = isset($params["platform_profit_rate"]) ? $params["platform_profit_rate"] : $row["platform_profit_rate"];
  221. $agency_profit_rate = isset($params["agency_profit_rate"]) ? $params["agency_profit_rate"] : $row["agency_profit_rate"];
  222. $this->model->update([
  223. "platform_profit_rate" => $platform_profit_rate,
  224. "agency_profit_rate" => $agency_profit_rate
  225. ], ["id" => $row["id"]]);
  226. $this->success();
  227. }
  228. }