User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\cashier;
  12. use app\Request;
  13. use app\services\cashier\UserServices;
  14. use app\services\order\OtherOrderServices;
  15. use app\services\store\StoreUserServices;
  16. use app\services\order\StoreCartServices;
  17. use app\services\store\SystemStoreStaffServices;
  18. use app\services\user\level\SystemUserLevelServices;
  19. use app\services\user\member\MemberCardServices;
  20. use app\webscoket\SocketPush;
  21. use crmeb\services\CacheService;
  22. use think\exception\ValidateException;
  23. /**
  24. * 收银台用户控制器
  25. */
  26. class User extends AuthController
  27. {
  28. /**
  29. * 修改收银员信息
  30. * @param Request $request
  31. * @param SystemStoreStaffServices $services
  32. * @return mixed
  33. */
  34. public function updatePwd(Request $request, SystemStoreStaffServices $services)
  35. {
  36. $data = $request->postMore([
  37. ['real_name', ''],
  38. ['pwd', ''],
  39. ['new_pwd', ''],
  40. ['conf_pwd', ''],
  41. ['avatar', ''],
  42. ]);
  43. if ($data['pwd'] && !preg_match('/^(?![^a-zA-Z]+$)(?!\D+$).{6,}$/', $data['new_pwd'])) {
  44. return $this->fail('设置的密码过于简单(不小于六位包含数字字母)');
  45. }
  46. if ($services->updateStaffPwd($this->cashierId, $data))
  47. return $this->success('修改成功');
  48. else
  49. return $this->fail('修改失败');
  50. }
  51. /**
  52. * 获取登录店员详情
  53. * @return mixed
  54. */
  55. public function getCashierInfo()
  56. {
  57. return $this->success($this->cashierInfo);
  58. }
  59. /**
  60. * 收银台选择用户列表
  61. * @param Request $request
  62. * @param UserServices $services
  63. * @return mixed
  64. */
  65. public function getUserList(Request $request, StoreUserServices $storeUserservices, \app\services\user\UserServices $services)
  66. {
  67. $data = $request->getMore([
  68. ['keyword', ''],
  69. ['field_key', '']
  70. ]);
  71. if ($data['keyword']) {
  72. if ($data['field_key'] == 'all') {
  73. $data['field_key'] = '';
  74. }
  75. if ($data['field_key'] && in_array($data['field_key'], ['uid', 'phone', 'bar_code'])) {
  76. $where[$data['field_key']] = trim($data['keyword']);
  77. } else {
  78. $where['store_like'] = trim($data['keyword']);
  79. }
  80. $where['is_filter_del'] = 1;
  81. $list = $services->getUserList($where);
  82. if (isset($list['list']) && $list['list']) {
  83. foreach ($list['list'] as &$item) {
  84. //用户类型
  85. if ($item['user_type'] == 'routine') {
  86. $item['user_type'] = '小程序';
  87. } else if ($item['user_type'] == 'wechat') {
  88. $item['user_type'] = '公众号';
  89. } else if ($item['user_type'] == 'h5') {
  90. $item['user_type'] = 'H5';
  91. } else if ($item['user_type'] == 'pc') {
  92. $item['user_type'] = 'PC';
  93. } else if ($item['user_type'] == 'app') {
  94. $item['user_type'] = 'APP';
  95. } else $item['user_type'] = '其他';
  96. }
  97. }
  98. return $this->success($list);
  99. } else {
  100. return app('json')->success($storeUserservices->index($data, $this->storeId));
  101. }
  102. }
  103. /**
  104. * 获取当前门店店员列表和店员信息
  105. * @param Request $request
  106. * @param SystemStoreStaffServices $services
  107. * @return mixed
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public function getCashierList(Request $request, SystemStoreStaffServices $services)
  113. {
  114. $where = $request->getMore([
  115. ['keyword', '']
  116. ]);
  117. $where['store_id'] = $this->storeId;
  118. $where['is_del'] = 0;
  119. return $this->success([
  120. 'staffInfo' => $request->cashierInfo(),
  121. 'staffList' => $services->getStoreStaff($where),
  122. 'count' => $services->count($where)
  123. ]);
  124. }
  125. /**
  126. * 游客切换到用户
  127. * @param Request $request
  128. * @param StoreCartServices $services
  129. * @param $cashierId
  130. * @return mixed
  131. */
  132. public function switchCartUser(Request $request, StoreCartServices $services, $cashierId)
  133. {
  134. [$uid, $toUid, $isTourist] = $request->postMore([
  135. ['uid', 0],
  136. ['to_uid', 0],
  137. ['is_tourist', 0]
  138. ], true);
  139. if ($isTourist && $uid) {
  140. $where = ['tourist_uid' => $uid, 'store_id' => $this->storeId, 'staff_id' => $cashierId];
  141. $touristCart = $services->getCartList($where);
  142. if ($touristCart) {
  143. $userWhere = ['uid' => $toUid, 'store_id' => $this->storeId, 'staff_id' => $cashierId];
  144. $userCarts = $services->getCartList($userWhere);
  145. if ($userCarts) {
  146. foreach ($touristCart as $cart) {
  147. foreach ($userCarts as $userCart) {
  148. //游客商品 存在用户购物车商品中
  149. if ($cart['product_id'] == $userCart['product_id'] && $cart['product_attr_unique'] == $userCart['product_attr_unique']) {
  150. //修改用户商品数量 删除游客购物车这条数据
  151. $services->update(['id' => $userCart['id']], ['cart_num' => bcadd((string)$cart['cart_num'], (string)$userCart['cart_num'])]);
  152. $services->delete(['id' => $cart['id']]);
  153. }
  154. }
  155. }
  156. }
  157. //发送消息
  158. try {
  159. SocketPush::instance()
  160. ->to($this->cashierId)
  161. ->setUserType('cashier')
  162. ->type('changCart')
  163. ->data(['uid' => $uid])
  164. ->push();
  165. } catch (\Throwable $e) {
  166. }
  167. }
  168. $services->update($where, ['uid' => $toUid, 'tourist_uid' => '']);
  169. }
  170. return $this->success('修改成功');
  171. }
  172. /**
  173. * 用户信息
  174. * @param Request $request
  175. * @param \app\services\user\UserServices $services
  176. * @param StoreCartServices $cartServices
  177. * @return mixed
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public function getUserInfo(Request $request, \app\services\user\UserServices $services, StoreCartServices $cartServices)
  183. {
  184. $code = $request->post('code', '');
  185. $uid = $request->post('uid', '');
  186. if (!$code && !$uid) {
  187. return $this->fail('缺少参数');
  188. }
  189. $field = ['uid', 'avatar', 'phone', 'nickname', 'now_money', 'integral', 'level', 'is_money_level', 'is_ever_level', 'overdue_time'];
  190. if ($uid) {
  191. $userInfo = $services->getUserInfo($uid, $field);
  192. } elseif ($code) {
  193. $userInfo = $services->get(['uniqid' => $code], $field);
  194. }
  195. $userInfo['vip_name'] = '';
  196. if ($userInfo['level']) {
  197. /** @var SystemUserLevelServices $levelServices */
  198. $levelServices = app()->make(SystemUserLevelServices::class);
  199. $levelInfo = $levelServices->getOne(['id' => $userInfo['level']], 'id,name');
  200. $userInfo['vip_name'] = $levelInfo['name'] ?? '';
  201. }
  202. $userInfo['overdue_time'] = date('Y-m-d H:i:s', $userInfo['overdue_time']);
  203. if (!isset($userInfo) && !$userInfo) {
  204. return $this->fail('用户不存在');
  205. }
  206. $cart = $request->post('cart', []);
  207. if ($cart) {
  208. $cartServices->batchAddCart($cart, $this->storeId, $userInfo->uid);
  209. }
  210. return $this->success($userInfo->toArray());
  211. }
  212. /**
  213. * 收银台获取当前用户信息
  214. * @param \app\services\user\UserServices $userServices
  215. * @param $uid
  216. * @return mixed
  217. */
  218. public function getUidInfo(\app\services\user\UserServices $userServices, $uid)
  219. {
  220. return $this->success($userServices->read((int)$uid));
  221. }
  222. /**
  223. * 收银台用户记录
  224. * @param Request $request
  225. * @param \app\services\user\UserServices $userServices
  226. * @param $uid
  227. * @return mixed
  228. */
  229. public function userRecord(Request $request, \app\services\user\UserServices $userServices, $uid)
  230. {
  231. $type = $request->get('type', '');
  232. return $this->success($userServices->oneUserInfo((int)$uid, $type));
  233. }
  234. /**
  235. * 切换用户、用户切换到其他用户、用户切换到游客
  236. * @param Request $request
  237. * @return mixed
  238. */
  239. public function swithUser(Request $request)
  240. {
  241. $uid = $request->post('uid', 0);
  242. $touristUid = $request->post('tourist_uid', 0);
  243. $cashierId = $request->post('cashier_id', 0);
  244. $changePrice = $request->post('change_price', 0);
  245. $changCartRemove = $request->post('chang_cart_remove', 0);
  246. if (!$uid && !$touristUid && !$cashierId && !$changePrice && !$changCartRemove) {
  247. return $this->fail('缺少参数');
  248. }
  249. $res = CacheService::redisHandler()->get('aux_screen_' . $this->cashierId);
  250. if ($uid) {
  251. if ($res && is_array($res)) {
  252. $res['uid'] = $uid;
  253. $res['tourist'] = false;
  254. $res['tourist_uid'] = 0;
  255. CacheService::redisHandler(CacheService::CASHIER_AUX_SCREEN_TAG . '_' . $this->storeId)
  256. ->set('aux_screen_' . $this->cashierId, $res);
  257. } else {
  258. //游客切换到用户。或者用户之间切换
  259. CacheService::redisHandler(CacheService::CASHIER_AUX_SCREEN_TAG . '_' . $this->storeId)
  260. ->set('aux_screen_' . $this->cashierId, [
  261. 'uid' => $uid,
  262. 'cashier_id' => 0,
  263. 'tourist_uid' => 0,
  264. 'tourist' => false
  265. ]);
  266. }
  267. //发送消息
  268. try {
  269. SocketPush::instance()->to($this->cashierId)->setUserType('cashier')->type('changUser')->data(['uid' => $uid])->push();
  270. } catch (\Throwable $e) {
  271. }
  272. } else if ($touristUid) {
  273. if ($res && is_array($res)) {
  274. $res['tourist_uid'] = $touristUid;
  275. $res['tourist'] = true;
  276. $res['uid'] = 0;
  277. CacheService::redisHandler(CacheService::CASHIER_AUX_SCREEN_TAG . '_' . $this->storeId)
  278. ->set('aux_screen_' . $this->cashierId, $res);
  279. } else {
  280. //用户切换到游客
  281. CacheService::redisHandler(CacheService::CASHIER_AUX_SCREEN_TAG . '_' . $this->storeId)
  282. ->set('aux_screen_' . $this->cashierId, [
  283. 'uid' => 0,
  284. 'cashier_id' => 0,
  285. 'tourist_uid' => $touristUid,
  286. 'tourist' => true
  287. ]);
  288. }
  289. //发送消息
  290. try {
  291. SocketPush::instance()->to($this->cashierId)->setUserType('cashier')->type('changUser')->data(['tourist_uid' => $touristUid])->push();
  292. } catch (\Throwable $e) {
  293. }
  294. } else if ($cashierId) {
  295. //切换店员
  296. if ($res && is_array($res)) {
  297. $res['cashier_id'] = $cashierId;
  298. CacheService::redisHandler(CacheService::CASHIER_AUX_SCREEN_TAG . '_' . $this->storeId)
  299. ->set('aux_screen_' . $this->cashierId, $res);
  300. } else {
  301. CacheService::redisHandler(CacheService::CASHIER_AUX_SCREEN_TAG . '_' . $this->storeId)
  302. ->set('aux_screen_' . $this->cashierId, [
  303. 'uid' => 0,
  304. 'cashier_id' => $cashierId,
  305. 'tourist_uid' => 0,
  306. 'tourist' => true
  307. ]);
  308. }
  309. //发送消息
  310. try {
  311. SocketPush::instance()->to($this->cashierId)->setUserType('cashier')->type('changUser')->data(['cashier_id' => $cashierId])->push();
  312. } catch (\Throwable $e) {
  313. }
  314. } else if ($changePrice) {
  315. //发送消息
  316. try {
  317. SocketPush::instance()->to($this->cashierId)->setUserType('cashier')->type('changUser')->data(['change_price' => $changePrice])->push();
  318. } catch (\Throwable $e) {
  319. }
  320. } else if ($changCartRemove) {
  321. //发送消息
  322. try {
  323. SocketPush::instance()->to($this->cashierId)->setUserType('cashier')->type('changCartRemove')->push();
  324. } catch (\Throwable $e) {
  325. }
  326. }
  327. return $this->success();
  328. }
  329. /**
  330. * 获取副屏用户信息
  331. * @return mixed
  332. */
  333. public function getAuxScreenInfo()
  334. {
  335. $res = CacheService::redisHandler()->get('aux_screen_' . $this->cashierId);
  336. $data = [];
  337. $key = ['cashier_id' => 0, 'tourist_uid' => 0, 'uid' => 0, 'tourist' => false];
  338. foreach ($key as $k => $v) {
  339. $data[$k] = $res[$k] ?? $v;
  340. }
  341. return $this->success($data);
  342. }
  343. /**
  344. * 获取会员类型
  345. * @param Request $request
  346. * @return mixed
  347. */
  348. public function getMemberCard(Request $request)
  349. {
  350. [$is_money_level, $overdue_time] = $request->getMore([
  351. ['is_money_level', 0],
  352. ['overdue_time', 0],
  353. ], true);
  354. /** @var MemberCardServices $memberCardServices */
  355. $memberCardServices = app()->make(MemberCardServices::class);
  356. $member_type = $memberCardServices->DoMemberType(0, false);
  357. if (!$is_money_level) $overdue_time = time();
  358. foreach ($member_type as $key => &$item) {
  359. if (!$overdue_time || $item['type'] == 'ever' && $item['vip_day'] == -1) {
  360. $item['overdue_time'] = '';
  361. } else {
  362. $item['overdue_time'] = date('Y-m-d H:i:s', $overdue_time + $item['vip_day'] * 86400);
  363. }
  364. }
  365. return $this->success($member_type);
  366. }
  367. /**
  368. * 购买会员
  369. * @param Request $request
  370. * @return mixed
  371. * @throws \think\db\exception\DataNotFoundException
  372. * @throws \think\db\exception\ModelNotFoundException
  373. */
  374. public function payMember(Request $request)
  375. {
  376. [$uid, $price, $memberId, $payType, $authCode] = $request->postMore([
  377. ['uid', 0],
  378. ['price', 0],
  379. ['merber_id', 0],
  380. [['pay_type', 'd'], 2], //2=用户扫码支付,3=付款码扫码支付, 4=现金支付
  381. ['auth_code', '']
  382. ], true);
  383. if (!(int)$memberId) {
  384. return $this->fail('缺少购买会员类型ID');
  385. }
  386. if (!$authCode && $payType == 3) {
  387. return $this->fail('缺少付款码二维码CODE');
  388. }
  389. if (!$price || $price <= 0) {
  390. return $this->fail('充值金额不能为0元!');
  391. }
  392. $storeMinRecharge = sys_config('store_user_min_recharge');
  393. if ($price < $storeMinRecharge) return $this->fail('充值金额不能低于' . $storeMinRecharge);
  394. /** @var OtherOrderServices $OtherOrderServices */
  395. $OtherOrderServices = app()->make(OtherOrderServices::class);
  396. $re = $OtherOrderServices->payMember($uid, (int)$memberId, (float)$price, (int)$payType, 'store', $this->cashierInfo, $authCode);
  397. if ($re) {
  398. $msg = $re['msg'];
  399. unset($re['msg']);
  400. return $this->success($msg, $re);
  401. }
  402. return $this->fail('充值失败');
  403. }
  404. /**显示指定的资源
  405. * @param $id
  406. * @param \app\services\user\UserServices $services
  407. * @return mixed
  408. */
  409. public function read($id, \app\services\user\UserServices $services)
  410. {
  411. if (is_string($id)) {
  412. $id = (int)$id;
  413. }
  414. return $this->success($services->read($id));
  415. }
  416. /**获取单个用户信息
  417. * @param Request $request
  418. * @param $id
  419. * @param \app\services\user\UserServices $services
  420. * @return mixed
  421. */
  422. public function oneUserInfo(Request $request, $id, \app\services\user\UserServices $services)
  423. {
  424. $data = $request->getMore([
  425. ['type', ''],
  426. ]);
  427. $id = (int)$id;
  428. if ($data['type'] == '') return $this->fail('缺少参数');
  429. return $this->success($services->oneUserInfo($id, $data['type']));
  430. }
  431. }