StoreService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\kefu;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\kefu\LoginServices;
  14. use app\services\kefu\service\StoreServiceLogServices;
  15. use app\services\kefu\service\StoreServiceServices;
  16. use app\services\user\UserServices;
  17. use app\services\user\UserWechatuserServices;
  18. use crmeb\exceptions\AdminException;
  19. use crmeb\services\CacheService;
  20. use think\facade\App;
  21. /**
  22. * 客服管理
  23. * Class StoreService
  24. * @package app\admin\controller\store
  25. */
  26. class StoreService extends AuthController
  27. {
  28. /**
  29. * StoreService constructor.
  30. * @param App $app
  31. * @param StoreServiceServices $services
  32. */
  33. public function __construct(App $app, StoreServiceServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * 显示资源列表
  40. * @return mixed
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function index()
  46. {
  47. return app('json')->success($this->services->getServiceList([]));
  48. }
  49. /**
  50. * 显示创建资源表单页
  51. * @param UserWechatuserServices $services
  52. * @return mixed
  53. */
  54. public function create(UserWechatuserServices $services)
  55. {
  56. $where = $this->request->getMore([
  57. ['nickname', ''],
  58. ['data', '', '', 'time'],
  59. ['type', '', '', 'user_type'],
  60. ]);
  61. $where['is_del'] = 0;
  62. [$list, $count] = $services->getWhereUserList($where, 'u.nickname,u.uid,u.avatar as headimgurl,w.subscribe,w.province,w.country,w.city,w.sex,u.user_type,u.is_del,u.phone,u.add_time');
  63. foreach ($list as &$item) {
  64. $item['add_time'] = date('Y-m-d', $item['add_time']);
  65. }
  66. return app('json')->success(compact('list', 'count'));
  67. }
  68. /**
  69. * 添加客服表单
  70. * @return mixed
  71. * @throws \FormBuilder\Exception\FormBuilderException
  72. */
  73. public function add()
  74. {
  75. return app('json')->success($this->services->create());
  76. }
  77. /**
  78. * 保存新建的资源
  79. * @return mixed
  80. */
  81. public function save()
  82. {
  83. $data = $this->request->postMore([
  84. ['image', ''],
  85. ['uid', 0],
  86. ['avatar', ''],
  87. ['customer', ''],
  88. ['notify', ''],
  89. ['phone', ''],
  90. ['account', ''],
  91. ['password', ''],
  92. ['true_password', ''],
  93. ['phone', ''],
  94. ['nickname', ''],
  95. ['status', 1],
  96. ]);
  97. if ($data['image'] == '') return app('json')->fail(400250);
  98. $data['uid'] = $data['image']['uid'];
  99. /** @var UserServices $userService */
  100. $userService = app()->make(UserServices::class);
  101. $userInfo = $userService->get($data['uid']);
  102. if ($data['phone'] == '') {
  103. if (!$userInfo['phone']) {
  104. throw new AdminException(400251);
  105. } else {
  106. $data['phone'] = $userInfo['phone'];
  107. }
  108. } else {
  109. if (!check_phone($data['phone'])) {
  110. throw new AdminException(400252);
  111. }
  112. }
  113. if ($data['nickname'] == '') $data['nickname'] = $userInfo['nickname'];
  114. $data['avatar'] = $data['image']['image'];
  115. if ($this->services->count(['uid' => $data['uid']])) {
  116. return app('json')->fail(400253);
  117. }
  118. unset($data['image']);
  119. $data['add_time'] = time();
  120. if (!$data['account']) {
  121. return app('json')->fail(400254);
  122. }
  123. if (!preg_match('/^[a-zA-Z0-9]{4,30}$/', $data['account'])) {
  124. return app('json')->fail(400255);
  125. }
  126. if (!$data['password']) {
  127. return app('json')->fail(400256);
  128. }
  129. if (!preg_match('/^[0-9a-z_$]{6,20}$/i', $data['password'])) {
  130. return app('json')->fail(400257);
  131. }
  132. if ($this->services->count(['phone' => $data['phone']])) {
  133. return app('json')->fail(400258);
  134. }
  135. if ($this->services->count(['account' => $data['account']])) {
  136. return app('json')->fail(400259);
  137. }
  138. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  139. $res = $this->services->save($data);
  140. if ($res) {
  141. return app('json')->success(400260);
  142. } else {
  143. return app('json')->fail(400261);
  144. }
  145. }
  146. /**
  147. * 显示编辑资源表单页
  148. * @param $id
  149. * @return mixed
  150. * @throws \FormBuilder\Exception\FormBuilderException
  151. */
  152. public function edit($id)
  153. {
  154. return app('json')->success($this->services->edit((int)$id));
  155. }
  156. /**
  157. * 保存新建的资源
  158. * @param $id
  159. * @return mixed
  160. */
  161. public function update($id)
  162. {
  163. $data = $this->request->postMore([
  164. ['avatar', ''],
  165. ['nickname', ''],
  166. ['account', ''],
  167. ['phone', ''],
  168. ['status', 1],
  169. ['notify', 1],
  170. ['customer', 1],
  171. ['password', ''],
  172. ['true_password', ''],
  173. ]);
  174. $customer = $this->services->get((int)$id);
  175. if (!$customer) {
  176. return app('json')->fail(100026);
  177. }
  178. if ($data["nickname"] == '') {
  179. return app('json')->fail(400262);
  180. }
  181. if (!check_phone($data['phone'])) {
  182. return app('json')->fail(400252);
  183. }
  184. if ($customer['phone'] != $data['phone'] && $this->services->count(['phone' => $data['phone']])) {
  185. return app('json')->fail(400258);
  186. }
  187. if ($data['password']) {
  188. if (!preg_match('/^[0-9a-z_$]{6,16}$/i', $data['password'])) {
  189. return app('json')->fail(400257);
  190. }
  191. if (!$data['true_password']) {
  192. return app('json')->fail(400263);
  193. }
  194. if ($data['password'] != $data['true_password']) {
  195. return app('json')->fail(400264);
  196. }
  197. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  198. } else {
  199. unset($data['password']);
  200. }
  201. $this->services->update($id, $data);
  202. return app('json')->success(100001);
  203. }
  204. /**
  205. * 删除指定资源
  206. * @param int $id
  207. * @return \think\Response
  208. */
  209. public function delete($id)
  210. {
  211. if (!$this->services->delete($id))
  212. return app('json')->fail(100008);
  213. else
  214. return app('json')->success(100002);
  215. }
  216. /**
  217. * 修改状态
  218. * @param UserServices $services
  219. * @param $id
  220. * @param $status
  221. * @return mixed
  222. */
  223. public function set_status(UserServices $services, $id, $status)
  224. {
  225. if ($status == '' || $id == 0) return app('json')->fail(100100);
  226. $info = $this->services->get($id, ['status', 'uid']);
  227. if (!$services->count(['uid' => $info['uid']])) {
  228. $info->status = 1;
  229. $info->save();
  230. return app('json')->fail(400265);
  231. }
  232. $info->status = $status;
  233. $info->save();
  234. return app('json')->success(100014);
  235. }
  236. /**
  237. * 聊天记录
  238. * @param $id
  239. * @return mixed
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\DbException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. */
  244. public function chat_user($id)
  245. {
  246. $uid = $this->services->value(['id' => $id], 'uid');
  247. if (!$uid) {
  248. return app('json')->fail(100026);
  249. }
  250. return app('json')->success($this->services->getChatUser((int)$uid));
  251. }
  252. /**
  253. * 聊天记录
  254. * @param StoreServiceLogServices $services
  255. * @return mixed
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\DbException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. */
  260. public function chat_list(StoreServiceLogServices $services)
  261. {
  262. $data = $this->request->getMore([
  263. ['uid', 0],
  264. ['to_uid', 0],
  265. ['id', 0]
  266. ]);
  267. if ($data['uid']) {
  268. CacheService::set('admin_chat_list' . $this->adminId, $data);
  269. }
  270. $data = CacheService::get('admin_chat_list' . $this->adminId);
  271. if ($data['uid']) {
  272. $where = [
  273. 'chat' => [$data['uid'], $data['to_uid']],
  274. ];
  275. } else {
  276. $where = [];
  277. }
  278. $list = $services->getChatLogList($where);
  279. return app('json')->success($list);
  280. }
  281. /**
  282. * 客服登录
  283. * @param LoginServices $services
  284. * @param $id
  285. * @return mixed
  286. * @throws \think\db\exception\DataNotFoundException
  287. * @throws \think\db\exception\DbException
  288. * @throws \think\db\exception\ModelNotFoundException
  289. */
  290. public function keufLogin(LoginServices $services, $id)
  291. {
  292. $serviceInfo = $services->get($id);
  293. if (!$serviceInfo) {
  294. return app('json')->fail(400266);
  295. }
  296. if (!$serviceInfo->account || !$serviceInfo->password) {
  297. return app('json')->fail(400267);
  298. }
  299. return app('json')->success($services->authLogin($serviceInfo->account));
  300. }
  301. }