StoreService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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');
  63. return app('json')->success(compact('list', 'count'));
  64. }
  65. /**
  66. * 添加客服表单
  67. * @return mixed
  68. * @throws \FormBuilder\Exception\FormBuilderException
  69. */
  70. public function add()
  71. {
  72. return app('json')->success($this->services->create());
  73. }
  74. /**
  75. * 保存新建的资源
  76. * @return mixed
  77. */
  78. public function save()
  79. {
  80. $data = $this->request->postMore([
  81. ['image', ''],
  82. ['uid', 0],
  83. ['avatar', ''],
  84. ['customer', ''],
  85. ['notify', ''],
  86. ['phone', ''],
  87. ['account', ''],
  88. ['password', ''],
  89. ['true_password', ''],
  90. ['phone', ''],
  91. ['nickname', ''],
  92. ['status', 1],
  93. ]);
  94. if ($data['image'] == '') return app('json')->fail(400250);
  95. $data['uid'] = $data['image']['uid'];
  96. /** @var UserServices $userService */
  97. $userService = app()->make(UserServices::class);
  98. $userInfo = $userService->get($data['uid']);
  99. if ($data['phone'] == '') {
  100. if (!$userInfo['phone']) {
  101. throw new AdminException(400251);
  102. } else {
  103. $data['phone'] = $userInfo['phone'];
  104. }
  105. } else {
  106. if (!check_phone($data['phone'])) {
  107. throw new AdminException(400252);
  108. }
  109. }
  110. if ($data['nickname'] == '') $data['nickname'] = $userInfo['nickname'];
  111. $data['avatar'] = $data['image']['image'];
  112. if ($this->services->count(['uid' => $data['uid']])) {
  113. return app('json')->fail(400253);
  114. }
  115. unset($data['image']);
  116. $data['add_time'] = time();
  117. if (!$data['account']) {
  118. return app('json')->fail(400254);
  119. }
  120. if (!preg_match('/^[a-zA-Z0-9]{4,30}$/', $data['account'])) {
  121. return app('json')->fail(400255);
  122. }
  123. if (!$data['password']) {
  124. return app('json')->fail(400256);
  125. }
  126. if (!preg_match('/^[0-9a-z_$]{6,20}$/i', $data['password'])) {
  127. return app('json')->fail(400257);
  128. }
  129. if ($this->services->count(['phone' => $data['phone']])) {
  130. return app('json')->fail(400258);
  131. }
  132. if ($this->services->count(['account' => $data['account']])) {
  133. return app('json')->fail(400259);
  134. }
  135. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  136. $res = $this->services->save($data);
  137. if ($res) {
  138. return app('json')->success(400260);
  139. } else {
  140. return app('json')->fail(400261);
  141. }
  142. }
  143. /**
  144. * 显示编辑资源表单页
  145. * @param $id
  146. * @return mixed
  147. * @throws \FormBuilder\Exception\FormBuilderException
  148. */
  149. public function edit($id)
  150. {
  151. return app('json')->success($this->services->edit((int)$id));
  152. }
  153. /**
  154. * 保存新建的资源
  155. * @param $id
  156. * @return mixed
  157. */
  158. public function update($id)
  159. {
  160. $data = $this->request->postMore([
  161. ['avatar', ''],
  162. ['nickname', ''],
  163. ['account', ''],
  164. ['phone', ''],
  165. ['status', 1],
  166. ['notify', 1],
  167. ['customer', 1],
  168. ['password', ''],
  169. ['true_password', ''],
  170. ]);
  171. $customer = $this->services->get((int)$id);
  172. if (!$customer) {
  173. return app('json')->fail(100026);
  174. }
  175. if ($data["nickname"] == '') {
  176. return app('json')->fail(400262);
  177. }
  178. if (!check_phone($data['phone'])) {
  179. return app('json')->fail(400252);
  180. }
  181. if ($customer['phone'] != $data['phone'] && $this->services->count(['phone' => $data['phone']])) {
  182. return app('json')->fail(400258);
  183. }
  184. if ($data['password']) {
  185. if (!preg_match('/^[0-9a-z_$]{6,16}$/i', $data['password'])) {
  186. return app('json')->fail(400257);
  187. }
  188. if (!$data['true_password']) {
  189. return app('json')->fail(400263);
  190. }
  191. if ($data['password'] != $data['true_password']) {
  192. return app('json')->fail(400264);
  193. }
  194. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  195. } else {
  196. unset($data['password']);
  197. }
  198. $this->services->update($id, $data);
  199. return app('json')->success(100001);
  200. }
  201. /**
  202. * 删除指定资源
  203. * @param int $id
  204. * @return \think\Response
  205. */
  206. public function delete($id)
  207. {
  208. if (!$this->services->delete($id))
  209. return app('json')->fail(100008);
  210. else
  211. return app('json')->success(100002);
  212. }
  213. /**
  214. * 修改状态
  215. * @param UserServices $services
  216. * @param $id
  217. * @param $status
  218. * @return mixed
  219. */
  220. public function set_status(UserServices $services, $id, $status)
  221. {
  222. if ($status == '' || $id == 0) return app('json')->fail(100100);
  223. $info = $this->services->get($id, ['status', 'uid']);
  224. if (!$services->count(['uid' => $info['uid']])) {
  225. $info->status = 1;
  226. $info->save();
  227. return app('json')->fail(400265);
  228. }
  229. $info->status = $status;
  230. $info->save();
  231. return app('json')->success(100014);
  232. }
  233. /**
  234. * 聊天记录
  235. * @param $id
  236. * @return mixed
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\DbException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. */
  241. public function chat_user($id)
  242. {
  243. $uid = $this->services->value(['id' => $id], 'uid');
  244. if (!$uid) {
  245. return app('json')->fail(100026);
  246. }
  247. return app('json')->success($this->services->getChatUser((int)$uid));
  248. }
  249. /**
  250. * 聊天记录
  251. * @param StoreServiceLogServices $services
  252. * @return mixed
  253. * @throws \think\db\exception\DataNotFoundException
  254. * @throws \think\db\exception\DbException
  255. * @throws \think\db\exception\ModelNotFoundException
  256. */
  257. public function chat_list(StoreServiceLogServices $services)
  258. {
  259. $data = $this->request->getMore([
  260. ['uid', 0],
  261. ['to_uid', 0],
  262. ['id', 0]
  263. ]);
  264. if ($data['uid']) {
  265. CacheService::set('admin_chat_list' . $this->adminId, $data);
  266. }
  267. $data = CacheService::get('admin_chat_list' . $this->adminId);
  268. if ($data['uid']) {
  269. $where = [
  270. 'chat' => [$data['uid'], $data['to_uid']],
  271. ];
  272. } else {
  273. $where = [];
  274. }
  275. $list = $services->getChatLogList($where);
  276. return app('json')->success($list);
  277. }
  278. /**
  279. * 客服登录
  280. * @param LoginServices $services
  281. * @param $id
  282. * @return mixed
  283. * @throws \think\db\exception\DataNotFoundException
  284. * @throws \think\db\exception\DbException
  285. * @throws \think\db\exception\ModelNotFoundException
  286. */
  287. public function keufLogin(LoginServices $services, $id)
  288. {
  289. $serviceInfo = $services->get($id);
  290. if (!$serviceInfo) {
  291. return app('json')->fail(400266);
  292. }
  293. if (!$serviceInfo->account || !$serviceInfo->password) {
  294. return app('json')->fail(400267);
  295. }
  296. return app('json')->success($services->authLogin($serviceInfo->account));
  297. }
  298. }