StoreService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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\message\service;
  12. use app\controller\admin\AuthController;
  13. use app\services\kefu\LoginServices;
  14. use app\services\message\service\StoreServiceLogServices;
  15. use app\services\message\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\controller\admin\v1\application\wechat
  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. *
  41. * @return \think\Response
  42. */
  43. public function index()
  44. {
  45. return $this->success($this->services->getServiceList([]));
  46. }
  47. /**
  48. * 显示创建资源表单页.
  49. *
  50. * @return \think\Response
  51. */
  52. public function create(UserWechatuserServices $services)
  53. {
  54. $where = $this->request->getMore([
  55. ['nickname', ''],
  56. ['data', '', '', 'time'],
  57. ['type', '', '', 'user_type'],
  58. ]);
  59. $where['is_filter_del'] = 1; //是否过滤删除用户
  60. [$list, $count] = $services->getWhereUserList($where, 'u.nickname,u.uid,u.avatar as headimgurl,u.user_type,w.subscribe,w.province,w.country,w.city,w.sex');
  61. foreach ($list as &$item) {
  62. $item['subscribe'] = is_null($item['subscribe']) ? 0 : $item['subscribe'];
  63. $item['country'] = is_null($item['country']) ? '' : $item['country'];
  64. $item['province'] = is_null($item['province']) ? '' : $item['province'];
  65. $item['city'] = is_null($item['city']) ? '' : $item['city'];
  66. $item['sex'] = is_null($item['sex']) ? 0 : $item['sex'];
  67. }
  68. return $this->success(compact('list', 'count'));
  69. }
  70. /**
  71. * 添加客服表单
  72. * @return mixed
  73. * @throws \FormBuilder\Exception\FormBuilderException
  74. */
  75. public function add()
  76. {
  77. return $this->success($this->services->create());
  78. }
  79. /*
  80. * 保存新建的资源
  81. */
  82. public function save()
  83. {
  84. $data = $this->request->postMore([
  85. ['image', ''],
  86. ['uid', 0],
  87. ['avatar', ''],
  88. ['customer', ''],
  89. ['notify', ''],
  90. ['phone', ''],
  91. ['account', ''],
  92. ['password', ''],
  93. ['true_password', ''],
  94. ['phone', ''],
  95. ['nickname', ''],
  96. ['status', 1],
  97. ['account_status', 1],
  98. ]);
  99. if ($data['image'] == '') return $this->fail('请选择用户');
  100. $data['uid'] = $data['image']['uid'];
  101. /** @var UserServices $userService */
  102. $userService = app()->make(UserServices::class);
  103. $userInfo = $userService->get($data['uid']);
  104. if ($data['phone'] == '') {
  105. if (!$userInfo['phone']) {
  106. throw new AdminException('该用户没有绑定手机号,请手动填写');
  107. } else {
  108. $data['phone'] = $userInfo['phone'];
  109. }
  110. } else {
  111. if (!check_phone($data['phone'])) {
  112. throw new AdminException('请输入正确的手机号');
  113. }
  114. }
  115. if ($data['nickname'] == '') $data['nickname'] = $userInfo['nickname'];
  116. $data['avatar'] = $data['image']['image'];
  117. if ($this->services->count(['uid' => $data['uid']])) {
  118. return $this->fail('客服已存在!');
  119. }
  120. unset($data['image']);
  121. $data['add_time'] = time();
  122. if (!$data['account']) {
  123. return $this->fail('请输入账号');
  124. }
  125. if (!preg_match('/^[a-zA-Z0-9]{4,30}$/', $data['account'])) {
  126. return $this->fail('账号必须为数字或者字母的组合4-30位');
  127. }
  128. if (!$data['password']) {
  129. return $this->fail('请输入密码');
  130. }
  131. if (!preg_match('/^[0-9a-z_$]{6,20}$/i', $data['password'])) {
  132. return $this->fail('密码必须为数字或者字母的组合6-20位');
  133. }
  134. if ($this->services->count(['phone' => $data['phone']])) {
  135. return $this->fail('该手机号的客服已存在!');
  136. }
  137. if ($this->services->count(['account' => $data['account']])) {
  138. return $this->fail('该客服账号已存在!');
  139. }
  140. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  141. $res = $this->services->save($data);
  142. if ($res) {
  143. return $this->success('客服添加成功');
  144. } else {
  145. return $this->fail('客服添加失败,请稍后再试');
  146. }
  147. }
  148. /**
  149. * 显示编辑资源表单页.
  150. *
  151. * @param int $id
  152. * @return \think\Response
  153. */
  154. public function edit($id)
  155. {
  156. return $this->success($this->services->edit((int)$id));
  157. }
  158. /**
  159. * 保存新建的资源
  160. *
  161. * @param \think\Request $request
  162. * @return \think\Response
  163. */
  164. public function update($id)
  165. {
  166. $data = $this->request->postMore([
  167. ['avatar', ''],
  168. ['nickname', ''],
  169. ['account', ''],
  170. ['phone', ''],
  171. ['status', 1],
  172. ['account_status', 1],
  173. ['notify', 1],
  174. ['customer', 1],
  175. ['password', ''],
  176. ['true_password', ''],
  177. ]);
  178. $customer = $this->services->get((int)$id);
  179. if (!$customer) {
  180. return $this->fail('数据不存在');
  181. }
  182. if ($data["nickname"] == '') {
  183. return $this->fail("客服名称不能为空!");
  184. }
  185. if (!check_phone($data['phone'])) {
  186. return $this->fail('请输入正确的手机号');
  187. }
  188. if ($customer['phone'] != $data['phone'] && $this->services->count(['phone' => $data['phone']])) {
  189. return $this->fail('该手机号的客服已存在!');
  190. }
  191. if ($data['password']) {
  192. if (!preg_match('/^[0-9a-z_$]{6,16}$/i', $data['password'])) {
  193. return $this->fail('密码必须为数字或者字母的组合');
  194. }
  195. if (!$data['true_password']) {
  196. return $this->fail('请输入确认密码');
  197. }
  198. if ($data['password'] != $data['true_password']) {
  199. return $this->fail('两次输入的密码不正确');
  200. }
  201. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  202. } else {
  203. unset($data['password']);
  204. }
  205. $this->services->update($id, $data);
  206. return $this->success('修改成功!');
  207. }
  208. /**
  209. * 删除指定资源
  210. *
  211. * @param int $id
  212. * @return \think\Response
  213. */
  214. public function delete($id)
  215. {
  216. if (!$this->services->delete($id))
  217. return $this->fail('删除失败,请稍候再试!');
  218. else
  219. return $this->success('删除成功!');
  220. }
  221. /**
  222. * 修改状态
  223. * @param $id
  224. * @param $status
  225. * @return mixed
  226. */
  227. public function set_status(UserServices $services, $id, $status)
  228. {
  229. if ($status == '' || $id == 0) return $this->fail('参数错误');
  230. $info = $this->services->get($id, ['account_status', 'uid']);
  231. if (!$services->count(['uid' => $info['uid']])) {
  232. $info->account_status = 1;
  233. $info->save();
  234. return $this->fail('用户不存在,客服将强制禁止登录');
  235. }
  236. $info->account_status = $status;
  237. $info->save();
  238. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  239. }
  240. /**
  241. * 聊天记录
  242. *
  243. * @return \think\Response
  244. */
  245. public function chat_user($id)
  246. {
  247. $uid = $this->services->value(['id' => $id], 'uid');
  248. if (!$uid) {
  249. return $this->fail('数据不存在!');
  250. }
  251. return $this->success($this->services->getChatUser((int)$uid));
  252. }
  253. /**
  254. * 聊天记录
  255. * @param StoreServiceLogServices $services
  256. * @return mixed
  257. * @throws \think\db\exception\DataNotFoundException
  258. * @throws \think\db\exception\DbException
  259. * @throws \think\db\exception\ModelNotFoundException
  260. */
  261. public function chat_list(StoreServiceLogServices $services)
  262. {
  263. $data = $this->request->getMore([
  264. ['uid', 0],
  265. ['to_uid', 0],
  266. ['id', 0]
  267. ]);
  268. if ($data['uid']) {
  269. CacheService::set('admin_chat_list' . $this->adminId, $data);
  270. }
  271. $data = CacheService::get('admin_chat_list' . $this->adminId);
  272. if ($data['uid']) {
  273. $where = [
  274. 'chat' => [$data['uid'], $data['to_uid']],
  275. ];
  276. } else {
  277. $where = [];
  278. }
  279. $list = $services->getChatLogList($where);
  280. return $this->success($list);
  281. }
  282. /**
  283. * 客服登录
  284. * @param LoginServices $services
  285. * @param $id
  286. * @return mixed
  287. * @throws \think\db\exception\DataNotFoundException
  288. * @throws \think\db\exception\DbException
  289. * @throws \think\db\exception\ModelNotFoundException
  290. */
  291. public function keufLogin(LoginServices $services, $id)
  292. {
  293. $serviceInfo = $services->get($id);
  294. if (!$serviceInfo) {
  295. return $this->fail('登录的客服不存在');
  296. }
  297. if (!$serviceInfo->account || !$serviceInfo->password) {
  298. return $this->fail('请先填写客服账号和密码再尝试进入客服平台');
  299. }
  300. if (!$serviceInfo->account_status || !$serviceInfo->status) {
  301. return $this->fail('客服帐号已被禁用');
  302. }
  303. return $this->success($services->authLogin($serviceInfo->account));
  304. }
  305. }