StoreStaff.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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\store\staff;
  12. use app\services\order\store\BranchOrderServices;
  13. use app\services\store\finance\StoreFinanceFlowServices;
  14. use app\services\store\StoreUserServices;
  15. use app\services\store\SystemStoreServices;
  16. use app\services\user\UserServices;
  17. use crmeb\exceptions\AdminException;
  18. use think\facade\App;
  19. use app\controller\store\AuthController;
  20. use app\services\store\SystemStoreStaffServices;
  21. /**
  22. * 店员
  23. * Class SystemStoreStaff
  24. * @package app\controller\store\staff
  25. */
  26. class StoreStaff extends AuthController
  27. {
  28. protected $level = null;
  29. /**
  30. * 构造方法
  31. * SystemStoreStaff constructor.
  32. * @param App $app
  33. * @param SystemStoreStaffServices $services
  34. */
  35. public function __construct(App $app, SystemStoreStaffServices $services)
  36. {
  37. parent::__construct($app);
  38. $this->services = $services;
  39. $this->level = $this->storeStaffInfo['level'] ?? 0;
  40. }
  41. /**
  42. * 获取店员列表
  43. * @return mixed
  44. */
  45. public function index()
  46. {
  47. $where = $this->request->getMore([
  48. ['keyword', ''],
  49. ['field_key', ''],
  50. ]);
  51. if ($where['field_key'] == 'all') $where['field_key'] = '';
  52. $where['store_id'] = $this->storeId;
  53. if ($this->level) {
  54. $where['level'] = $this->level + 1;
  55. }
  56. $where['is_store'] = 1;
  57. $where['is_del'] = 0;
  58. return app('json')->success($this->services->getStoreStaffList($where));
  59. }
  60. /**
  61. * 获取店员select
  62. * @param SystemStoreStaffServices $services
  63. * @return mixed
  64. */
  65. public function getStaffSelect()
  66. {
  67. $where['store_id'] = $this->storeId;
  68. $where['is_del'] = 0;
  69. $where['status'] = 1;
  70. return app('json')->success($this->services->getSelectList($where));
  71. }
  72. /**
  73. * 获取某个店员信息
  74. * @param $id
  75. * @return mixed
  76. */
  77. public function read($id)
  78. {
  79. if (!$id) {
  80. return app('json')->fail('缺少店员id');
  81. }
  82. return app('json')->success($this->services->read((int)$id));
  83. }
  84. /**
  85. * 店员
  86. * @param $id
  87. * @return mixed
  88. */
  89. public function staffDetail($id)
  90. {
  91. $data = $this->request->getMore([
  92. ['type', ''],
  93. ]);
  94. $id = (int)$id;
  95. if ($data['type'] == '' || !$id) return $this->fail('缺少参数');
  96. return $this->success($this->services->staffDetail($id, $data['type']));
  97. }
  98. /**
  99. * 店员新增表单
  100. * @return mixed
  101. * @throws \FormBuilder\Exception\FormBuilderException
  102. */
  103. public function create()
  104. {
  105. return app('json')->success($this->services->createStoreStaffForm((int)$this->storeId, $this->storeStaffInfo['level'] + 1));
  106. }
  107. /**
  108. * 店员修改表单
  109. * @return mixed
  110. * @throws \FormBuilder\Exception\FormBuilderException
  111. */
  112. public function edit()
  113. {
  114. [$id] = $this->request->getMore([
  115. [['id', 'd'], 0],
  116. ], true);
  117. return app('json')->success($this->services->updateStoreStaffForm($id, $this->storeStaffInfo['level'] + 1));
  118. }
  119. /**
  120. * 保存店员信息
  121. */
  122. public function save($id = 0)
  123. {
  124. $data = $this->request->postMore([
  125. ['image', ''],
  126. ['account', ''],
  127. ['uid', 0],
  128. ['avatar', ''],
  129. ['staff_name', ''],
  130. ['roles', []],
  131. ['phone', ''],
  132. ['verify_status', 1],
  133. ['order_status', 1],
  134. ['is_manager', 0],
  135. ['is_cashier', 0],
  136. ['status', 1],
  137. ['conf_pwd', ''],
  138. ['pwd', ''],
  139. ['notify', 0],
  140. ['is_customer', 0],
  141. ['customer_phone', ''],
  142. ['customer_url', '']
  143. ]);
  144. $data['store_id'] = $this->storeId;
  145. $data['is_store'] = 1;
  146. if ($data['image'] == '') return $this->fail('请选择用户');
  147. $data['uid'] = $data['image']['uid'];
  148. if ($data['staff_name'] == '') {
  149. return app('json')->fail('请填店员名称');
  150. }
  151. if ($data['phone'] == '') {
  152. return app('json')->fail('请填写店员电话');
  153. }
  154. if (!check_phone($data['phone'])) {
  155. return app('json')->fail('请输入正确的手机号');
  156. }
  157. if ($data['conf_pwd'] != $data['pwd']) {
  158. return app('json')->fail('两次输入的密码不相同');
  159. }
  160. if ($this->services->count(['account' => $data['account'], 'is_del' => 0])) {
  161. return app('json')->fail('该员工账号已经存在');
  162. }
  163. $staff = $this->services->getOne(['store_id' => $this->storeId, 'phone' => $data['phone'], 'is_del' => 0]);
  164. if ($staff && $staff['is_store']) {
  165. return app('json')->fail('该手机号已经存在');
  166. }
  167. /** @var SystemStoreServices $storeServices */
  168. $storeServices = app()->make(SystemStoreServices::class);
  169. //是客服验证
  170. if ($data['is_customer']) {
  171. $storeInfo = $storeServices->getStoreInfo((int)$this->storeId);
  172. if ($storeInfo['customer_type'] == 1 && !$data['customer_phone']) {
  173. return app('json')->fail('请输入客服电话');
  174. }
  175. if ($storeInfo['customer_type'] == 2 && !$data['customer_url']) {
  176. return app('json')->fail('请选择客服二维码');
  177. }
  178. }
  179. $data['avatar'] = $data['image']['image'];
  180. $userStaff = $this->services->getOne(['uid' => $data['uid'], 'is_del' => 0]);
  181. if ($userStaff) {
  182. $store = $storeServices->get($userStaff['store_id']);
  183. return $this->fail($store['id'] == $this->storeId ? '该用户已存在!' : '该用户已在(' . ($store['name'] ?? '') . ')门店存在!');
  184. }
  185. unset($data['image']);
  186. unset($data['conf_pwd'], $data['image']);
  187. $data['level'] = $this->storeStaffInfo['level'] + 1;
  188. $data['pwd'] = $this->services->passwordHash($data['pwd']);
  189. $data['add_time'] = time();
  190. if ($staff) {//修改
  191. $res = $this->services->update($staff['id'], $data);
  192. } else {
  193. $res = $this->services->save($data);
  194. }
  195. if ($res) {
  196. return app('json')->success('添加成功');
  197. } else {
  198. return app('json')->fail('添加失败,请稍后再试');
  199. }
  200. }
  201. /**
  202. * 保存店员信息
  203. */
  204. public function update($id = 0)
  205. {
  206. $data = $this->request->postMore([
  207. ['image', ''],
  208. ['account', ''],
  209. ['avatar', ''],
  210. ['staff_name', ''],
  211. ['phone', ''],
  212. ['roles', []],
  213. ['verify_status', 1],
  214. ['order_status', 1],
  215. ['is_manager', 0],
  216. ['is_cashier', 0],
  217. ['status', 1],
  218. ['conf_pwd', ''],
  219. ['pwd', ''],
  220. ['notify', 0],
  221. ['is_customer', 0],
  222. ['customer_phone', ''],
  223. ['customer_url', '']
  224. ]);
  225. $data['store_id'] = $this->storeId;
  226. if ($data['image']) {
  227. $data['uid'] = $data['image']['uid'];
  228. $data['avatar'] = $data['image']['image'];
  229. }
  230. if ($data['staff_name'] == '') {
  231. return app('json')->fail('请填店员名称');
  232. }
  233. if ($data['phone'] == '') {
  234. return app('json')->fail('请填写店员电话');
  235. }
  236. if (!check_phone($data['phone'])) {
  237. return app('json')->fail('请输入正确的手机号');
  238. }
  239. $staff = $this->services->get(['store_id' => $this->storeId, 'account' => $data['account'], 'is_del' => 0]);
  240. if ($staff && $staff['id'] != $id) {
  241. return app('json')->fail('该员工账号已经存在');
  242. }
  243. $staff = $this->services->getOne(['store_id' => $this->storeId, 'phone' => $data['phone'], 'is_del' => 0]);
  244. if ($staff && $staff['is_store'] && $staff['id'] != $id) {
  245. return app('json')->fail('该手机号已经存在');
  246. }
  247. if ($data['pwd']) {
  248. if (!$data['conf_pwd']) {
  249. return app('json')->fail('请输入确认密码');
  250. }
  251. if ($data['pwd'] != $data['conf_pwd']) {
  252. return app('json')->fail('两次输入的密码不一致');
  253. }
  254. $data['pwd'] = $this->services->passwordHash($data['pwd']);
  255. } else {
  256. unset($data['pwd']);
  257. }
  258. //是客服验证
  259. if ($data['is_customer']) {
  260. /** @var SystemStoreServices $storeServices */
  261. $storeServices = app()->make(SystemStoreServices::class);
  262. $storeInfo = $storeServices->getStoreInfo((int)$this->storeId);
  263. if ($storeInfo['customer_type'] == 1 && !$data['customer_phone']) {
  264. return app('json')->fail('请输入客服电话');
  265. }
  266. if ($storeInfo['customer_type'] == 2 && !$data['customer_url']) {
  267. return app('json')->fail('请选择客服二维码');
  268. }
  269. }
  270. unset($data['conf_pwd']);
  271. $data['is_store'] = 1;
  272. $res = $this->services->update($id, $data);
  273. if ($res) {
  274. return app('json')->success('编辑成功');
  275. } else {
  276. return app('json')->fail('编辑失败');
  277. }
  278. }
  279. /**
  280. * 设置单个店员是否开启
  281. * @param string $is_show
  282. * @param string $id
  283. * @return mixed
  284. */
  285. public function set_show($is_show = '', $id = '')
  286. {
  287. if ($is_show == '' || $id == '') {
  288. $this->fail('缺少参数');
  289. }
  290. $res = $this->services->update($id, ['status' => (int)$is_show]);
  291. if ($res) {
  292. return app('json')->success($is_show == 1 ? '开启成功' : '关闭成功');
  293. } else {
  294. return app('json')->fail($is_show == 1 ? '开启失败' : '关闭失败');
  295. }
  296. }
  297. /**
  298. * 修改当前登陆店员信息
  299. * @return mixed
  300. */
  301. public function updateStaffPwd()
  302. {
  303. $data = $this->request->postMore([
  304. ['real_name', ''],
  305. ['pwd', ''],
  306. ['new_pwd', ''],
  307. ['conf_pwd', ''],
  308. ['avatar', ''],
  309. ]);
  310. if (!preg_match('/^(?![^a-zA-Z]+$)(?!\D+$).{6,}$/', $data['new_pwd'])) {
  311. return $this->fail('设置的密码过于简单(不小于六位包含数字字母)');
  312. }
  313. if ($this->services->updateStaffPwd($this->storeStaffId, $data))
  314. return $this->success('修改成功');
  315. else
  316. return $this->fail('修改失败');
  317. }
  318. /**
  319. * 删除店员
  320. * @param $id
  321. */
  322. public function delete($id)
  323. {
  324. if (!$id) return app('json')->fail('数据不存在');
  325. $staff = $this->services->getStaffInfo((int)$id);
  326. if (!$staff['level']) {
  327. return app('json')->fail('门店超级管理员账号不能删除');
  328. }
  329. if (!$this->services->update($id, ['is_del' => 1]))
  330. return app('json')->fail('删除失败,请稍候再试!');
  331. else
  332. return app('json')->success('删除成功!');
  333. }
  334. /**
  335. * 店员绑定uid
  336. * @param UserServices $userServices
  337. * @return mixed
  338. * @throws \think\db\exception\DataNotFoundException
  339. * @throws \think\db\exception\DbException
  340. * @throws \think\db\exception\ModelNotFoundException
  341. */
  342. public function bandingUser(UserServices $userServices, StoreUserServices $storeUserServices)
  343. {
  344. [$uid, $staff_id] = $this->request->postMore([
  345. ['uid', 0],
  346. ['staff_id', ''],
  347. ], true);
  348. if (!$uid || !$staff_id) {
  349. return app('json')->fail('缺少参数');
  350. }
  351. if (!$userServices->count(['uid' => $uid])) {
  352. return app('json')->fail('用户不存在');
  353. }
  354. $staffInfo = $this->services->getStaffInfo((int)$staff_id);
  355. if (!$staffInfo) {
  356. return app('json')->fail('店员不存在');
  357. }
  358. $res = $this->services->transaction(function () use ($storeUserServices, $staffInfo, $uid, $staff_id) {
  359. //清空该门店uid绑定其他店员
  360. $re = $this->services->update(['store_id' => $staffInfo['store_id'], 'uid' => $uid, 'is_del' => 0], ['uid' => 0]);
  361. $re = $re && $this->services->update($staff_id, ['uid' => $uid]);
  362. //写入门店用户
  363. return $re && $storeUserServices->setStoreUser((int)$uid, (int)$this->storeId);
  364. });
  365. if (!$res) {
  366. return app('json')->fail('设置失败');
  367. }
  368. return app('json')->success('修改成功');
  369. }
  370. /**
  371. * 店员交易统计
  372. * @param StoreFinanceFlowServices $services
  373. * @return mixed
  374. * @throws \think\db\exception\DataNotFoundException
  375. * @throws \think\db\exception\DbException
  376. * @throws \think\db\exception\ModelNotFoundException
  377. */
  378. public function statistics(StoreFinanceFlowServices $services, BranchOrderServices $orderServices)
  379. {
  380. $where = $this->request->getMore([
  381. ['staff_id', -1],
  382. ['time_key', 'add_time'],
  383. ['data', '', '', 'time'],
  384. ['type', 0]
  385. ]);
  386. if ($where['time_key'] && !in_array($where['time_key'], ['add_time', 'delivery_time', 'write_time'])) {
  387. return app('json')->fail('参数错误');
  388. }
  389. $where['time_key'] = 'add_time';
  390. $where['staff_id'] = $where['staff_id'] ?: -1;
  391. $where['store_id'] = $this->storeId;
  392. $where['trade_type'] = 2;
  393. if (!$where['type']) {
  394. $where['type'] = [7, 8, 9, 10, 11, 12, 13];
  395. } elseif ($where['type'] == 11) {
  396. $where['type'] = [11, 12, 13];
  397. }
  398. $where['time'] = $orderServices->timeHandle($where['time']);
  399. $where['is_del'] = 0;
  400. return app('json')->success($services->getList($where));
  401. }
  402. /**
  403. * 店员交易统计头部数据
  404. * @return mixed
  405. */
  406. public function statisticsHeader(StoreFinanceFlowServices $services, BranchOrderServices $orderServices)
  407. {
  408. $where = $this->request->getMore([
  409. ['staff_id', -1],
  410. ['time_key', 'add_time'],
  411. ['data', '', '', 'time'],
  412. ['type', 0]
  413. // ['group', '']
  414. ]);
  415. if ($where['time_key'] && !in_array($where['time_key'], ['add_time', 'delivery_time', 'write_time'])) {
  416. return app('json')->fail('参数错误');
  417. }
  418. $where['time_key'] = 'add_time';
  419. $where['staff_id'] = $where['staff_id'] ?: -1;
  420. $where['store_id'] = $this->storeId;
  421. $where['trade_type'] = 2;
  422. if (!$where['type']) {
  423. $where['type'] = [7, 8, 9, 10, 11, 12, 13];
  424. } elseif ($where['type'] == 11) {
  425. $where['type'] = [11, 12, 13];
  426. }
  427. $where['is_del'] = 0;
  428. if ($where['staff_id'] == -1) {
  429. $where['time'] = $orderServices->timeHandle($where['time']);
  430. $data = $services->getStatisticsHeader($where);
  431. } else {
  432. $time = $orderServices->timeHandle($where['time'], true);
  433. $data = $services->getTypeHeader($where, $time);
  434. }
  435. return app('json')->success($data);
  436. }
  437. /**
  438. * 获取登录店员详情
  439. * @return mixed
  440. */
  441. public function info()
  442. {
  443. return app('json')->success($this->storeStaffInfo);
  444. }
  445. /**
  446. * 登录收银台
  447. * @param \app\services\cashier\LoginServices $services
  448. * @param $id
  449. * @return mixed
  450. * @throws \think\db\exception\DataNotFoundException
  451. * @throws \think\db\exception\DbException
  452. * @throws \think\db\exception\ModelNotFoundException
  453. */
  454. public function loginCashier(\app\services\cashier\LoginServices $services, $id)
  455. {
  456. $storeStaffInfo = $services->get($id);
  457. if (!$storeStaffInfo) {
  458. return app('json')->fail('账号不存在!');
  459. }
  460. if ($storeStaffInfo->is_del) {
  461. return app('json')->fail('账号不存在');
  462. }
  463. return app('json')->success($services->getLoginResult($id, 'cashier', $storeStaffInfo));
  464. }
  465. }