SystemStore.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\store;
  12. use app\services\order\StoreOrderServices;
  13. use app\services\product\branch\StoreBranchProductServices;
  14. use app\services\store\LoginServices;
  15. use app\services\store\SystemStoreStaffServices;
  16. use app\services\system\attachment\SystemAttachmentServices;
  17. use think\facade\App;
  18. use app\controller\admin\AuthController;
  19. use app\services\store\SystemStoreServices;
  20. /**
  21. * 门店管理控制器
  22. * Class SystemStore
  23. * @package app\controller\admin\v1\store
  24. */
  25. class SystemStore extends AuthController
  26. {
  27. /**
  28. * 构造方法
  29. * SystemStore constructor.
  30. * @param App $app
  31. * @param SystemStoreServices $services
  32. */
  33. public function __construct(App $app, SystemStoreServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * 获取门店列表1
  40. * @return mixed
  41. */
  42. public function index()
  43. {
  44. $where = $this->request->getMore([
  45. ['name', '', '', 'keywords'],
  46. [['type', 'd'], 0],
  47. ['id', 0, '', 'order_id'],
  48. ['cate_id', ''],
  49. ]);
  50. if ($where['type'] == 'all') $where['type'] = '';
  51. $where['is_del'] = 0;
  52. $field = ['id', 'cate_id', 'name', 'phone', 'address', 'detailed_address', 'image', 'is_show', 'day_time', 'day_start', 'day_end'];
  53. return $this->success($this->services->getStoreList($where, $field, '', '', 0, ['categoryName']));
  54. }
  55. /**
  56. * 获取门店头部
  57. * @return mixed
  58. */
  59. public function get_header()
  60. {
  61. $count = $this->services->getStoreData();
  62. return $this->success(compact('count'));
  63. }
  64. /**
  65. * 门店设置
  66. * @return mixed
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function get_info()
  72. {
  73. [$id] = $this->request->getMore([
  74. [['id', 'd'], 0],
  75. ], true);
  76. $info = $this->services->getStoreDispose($id);
  77. $info['cate_id'] = $info['cate_com'] ? array_map('intval', explode(',', $info['cate_com'])) : [];
  78. return $this->success(compact('info'));
  79. }
  80. /**
  81. * 位置选择
  82. * @return mixed
  83. */
  84. public function select_address()
  85. {
  86. $key = sys_config('tengxun_map_key');
  87. if (!$key) return $this->fail('提示:前往设置->系统设置->第三方接口 配置腾讯地图KEY');
  88. return $this->success(compact('key'));
  89. }
  90. /**
  91. * 设置单个门店是否显示
  92. * @param string $is_show
  93. * @param string $id
  94. * @return json
  95. */
  96. public function set_show($is_show = '', $id = '')
  97. {
  98. ($is_show == '' || $id == '') && $this->fail('缺少参数');
  99. $res = $this->services->update((int)$id, ['is_show' => (int)$is_show]);
  100. if ($res) {
  101. /** @var SystemStoreStaffServices $storeStaffServices */
  102. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  103. if ($is_show) {
  104. $storeStaffServices->update(['store_id' => $id, 'is_del' => 0, 'status' => 0], ['status' => 1]);
  105. $this->services->cacheSaveValue($id, 'is_show', $is_show);
  106. } else {
  107. $storeStaffServices->update(['store_id' => $id, 'is_del' => 0, 'status' => 1], ['status' => 0]);
  108. $this->services->cacheDelById($id);
  109. }
  110. event('store.status', [$id, $is_show]);
  111. return $this->success('设置成功');
  112. } else {
  113. return $this->fail('设置失败');
  114. }
  115. }
  116. /**
  117. * 获取重置账号密码表单
  118. * @param $id
  119. * @return mixed
  120. */
  121. public function resetAdminForm($id)
  122. {
  123. if (!$id) {
  124. return $this->fail('缺少参数');
  125. }
  126. return $this->success($this->services->storeAdminAccountForm($id));
  127. }
  128. /**
  129. * 重置门店超级管理员账号密码
  130. * @param $id
  131. * @return mixed
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function resetAdmin($id)
  137. {
  138. if (!$id) {
  139. return $this->fail('缺少参数');
  140. }
  141. $data = $this->request->postMore([
  142. ['staff_id', 0],
  143. ['phone', ''],
  144. ['account', ''],
  145. ['password', ''],
  146. ['true_password', ''],
  147. ]);
  148. $this->validate(['store_account' => $data['account'], 'true_password' => $data['true_password'], 'store_password' => $data['password']], \app\validate\admin\merchant\SystemStoreValidate::class, 'reset');
  149. if (!$data['staff_id']) {
  150. return $this->fail('参数错误');
  151. }
  152. if (!$data['password']) {
  153. return $this->fail('请输入密码');
  154. }
  155. if (!$data['true_password']) {
  156. return $this->fail('请输入确认密码');
  157. }
  158. if ($data['password'] != $data['true_password']) {
  159. return $this->fail('两次输入的密码不正确');
  160. }
  161. $this->services->resetAdmin((int)$id, $data);
  162. return $this->success('操作成功!');
  163. }
  164. /**
  165. * 保存修改门店信息
  166. * param int $id
  167. * */
  168. public function save($id = 0)
  169. {
  170. $data = $this->request->postMore([
  171. ['type', 1],//门店类型1:自营2加盟
  172. ['cate_id', []],
  173. ['erp_shop_id', 0],
  174. ['name', ''],
  175. ['introduction', ''],
  176. ['is_show', 1],
  177. ['is_store', 1],
  178. ['image', ''],
  179. ['phone', ''],
  180. ['address', ''],
  181. ['detailed_address', ''],
  182. ['province', ''],
  183. ['city', ''],
  184. ['area', ''],
  185. ['street', ''],
  186. ['longitude', ''],
  187. ['latitude', ''],
  188. ['day_time', []],
  189. ['valid_range', 0],
  190. ['store_account', ''],
  191. ['store_password', ''],
  192. ['business', 0],
  193. ['product_status', 1],
  194. ['product_verify_status', 0],
  195. ['applicable_type', 1],//1;全部商品 2部分商品
  196. ['product_id', []],//同步商品Ids
  197. ]);
  198. $this->validate($data, \app\validate\admin\merchant\SystemStoreValidate::class, $id ? 'update' : 'save');
  199. if (!!sys_config('erp_open') && !$data['erp_shop_id']) {
  200. return $this->fail('开启ERP时,店铺编号必须填写');
  201. }
  202. if (!isset($data['longitude']) || !isset($data['latitude'])) {
  203. return $this->fail('请选择门店位置');
  204. }
  205. if (!check_phone($data['phone'])) {
  206. return $this->fail('请输入正确的手机号');
  207. }
  208. if (!$id && (!$data['store_account'] || !$data['store_password'])) {
  209. return $this->fail('请填写门店管理员账号密码');
  210. }
  211. if ($data['is_show'] && (!$data['day_time'] || count($data['day_time']) != 2)) {
  212. return $this->fail('请选择门店营业时间');
  213. }
  214. if (!floatval(trim($data['valid_range']))) {
  215. return $this->fail('请输入有效的配送范围');
  216. }
  217. if ($data['applicable_type'] == 2 && !$data['product_id']) {
  218. return $this->fail('请选择同步商品');
  219. }
  220. if ($data['day_time'] && count($data['day_time']) == 2) {
  221. [$data['day_start'], $data['day_end']] = $data['day_time'];
  222. $data['day_time'] = implode(' - ', $data['day_time']);
  223. }
  224. if ($data['image'] && strstr($data['image'], 'http') === false) {
  225. $site_url = sys_config('site_url');
  226. $data['image'] = $site_url . $data['image'];
  227. }
  228. $data['address'] = str_replace([' ', '/', '\\'], '', $data['address']);
  229. $data['detailed_address'] = str_replace([' ', '/', '\\'], '', $data['detailed_address']);
  230. $staff_data = [
  231. 'staff_name' => $data['name'],
  232. 'avatar' => $data['image'],
  233. 'phone' => $data['phone'],
  234. 'account' => $data['store_account'],
  235. 'pwd' => $data['store_password']
  236. ];
  237. $data['valid_range'] = bcmul($data['valid_range'], '1000', 0);
  238. $data['cate_com'] = $data['cate_id'] ? implode(',', $data['cate_id']) : '';
  239. $data['cate_id'] = $data['cate_id'] ? end($data['cate_id']) : 0;
  240. $product_ids = [];
  241. if ($data['applicable_type'] == 2) $product_ids = $data['product_id'];
  242. if ($data['type'] == 1) {//自营门店不允许自主上传商品
  243. $data['product_status'] = 0;
  244. $data['product_verify_status'] = 0;
  245. }
  246. unset($data['store_account'], $data['store_password'], $data['product_id'], $data['applicable_type']);
  247. [$id, $is_new] = $this->services->saveStore((int)$id, $data, $staff_data);
  248. event('store.create', [$data, $id, $is_new, $product_ids]);
  249. $storeInfo = $this->services->get((int)$id);
  250. $this->services->cacheUpdate($storeInfo->toArray());
  251. return $this->success('操作成功!');
  252. }
  253. /**
  254. * 删除恢复门店
  255. * @param $id
  256. */
  257. public function delete($id)
  258. {
  259. $id = (int)$id;
  260. if (!$id) return $this->fail('数据不存在');
  261. $storeInfo = $this->services->get($id);
  262. if (!$storeInfo) {
  263. return $this->fail('数据不存在');
  264. }
  265. /** @var SystemStoreStaffServices $storeStaffServices */
  266. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  267. if ($storeInfo->is_del == 1) {
  268. $storeInfo->is_del = 0;
  269. if (!$storeInfo->save()) {
  270. return $this->fail('恢复失败,请稍候再试!');
  271. } else {
  272. $storeStaffServices->update(['store_id' => $id, 'is_del' => 1], ['is_del' => 0]);
  273. $this->services->cacheUpdate($storeInfo->toArray());
  274. return $this->success('恢复门店成功!');
  275. }
  276. } else {
  277. /** @var StoreOrderServices $storeOrderServices */
  278. $storeOrderServices = app()->make(StoreOrderServices::class);
  279. $orderCount = $storeOrderServices->count(['store_id' => $id, 'status' => 0]);
  280. if (!$orderCount) {
  281. $orderCount = $storeOrderServices->count(['store_id' => $id, 'status' => 1]);
  282. if (!$orderCount) {
  283. $orderCount = $storeOrderServices->count(['store_id' => $id, 'status' => 5]);
  284. }
  285. }
  286. if ($orderCount) {
  287. return $this->fail('删除失败,该门店还有待处理订单');
  288. }
  289. $storeInfo->is_del = 1;
  290. if (!$storeInfo->save()) {
  291. return $this->fail('删除失败,请稍候再试!');
  292. } else {
  293. $storeStaffServices->update(['store_id' => $id, 'is_del' => 0], ['is_del' => 1]);
  294. $this->services->cacheDelById($id);
  295. /** @var StoreBranchProductServices $storeBranchProducesServices */
  296. $storeBranchProducesServices = app()->make(StoreBranchProductServices::class);
  297. //删除门店商品
  298. $storeBranchProducesServices->deleteProducts([], 1, $id);
  299. /** @var SystemAttachmentServices $attach */
  300. $attach = app()->make(SystemAttachmentServices::class);
  301. //删除附件
  302. $attach->delAttachment([], 2, $id);
  303. event('store.delete', [$id]);
  304. return $this->success('删除门店成功!');
  305. }
  306. }
  307. }
  308. /**
  309. * 门店登录
  310. * @param SystemStoreStaffServices $staffServices
  311. * @param LoginServices $services
  312. * @param $id
  313. * @return mixed
  314. * @throws \think\db\exception\DataNotFoundException
  315. * @throws \think\db\exception\ModelNotFoundException
  316. */
  317. public function storeLogin(SystemStoreStaffServices $staffServices, LoginServices $services, $id)
  318. {
  319. $storeInfo = $this->services->get($id);
  320. if (!$storeInfo) {
  321. return $this->fail('登录的门店不存在');
  322. }
  323. $staffAdmin = $staffServices->getOne(['store_id' => $id, 'level' => 0, 'is_admin' => 1]);
  324. if (!$staffAdmin) {
  325. return $this->fail('门店超级管理员异常');
  326. }
  327. return $this->success($services->login($staffAdmin['account'], '', 'store', $id));
  328. }
  329. /**
  330. * @return mixed
  331. * @throws \Exception
  332. */
  333. public function getErpShop()
  334. {
  335. return $this->success($this->services->erpShopList());
  336. }
  337. }