StoreService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\merchant\store\service;
  12. use app\common\repositories\store\service\StoreServiceLogRepository;
  13. use app\common\repositories\store\service\StoreServiceRepository;
  14. use app\common\repositories\user\UserRepository;
  15. use app\validate\merchant\StoreServiceValidate;
  16. use crmeb\basic\BaseController;
  17. use FormBuilder\Exception\FormBuilderException;
  18. use think\App;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\exception\ValidateException;
  23. /**
  24. * Class StoreService
  25. * @package app\controller\merchant\store\service
  26. * @author xaboy
  27. * @day 2020/5/29
  28. */
  29. class StoreService extends BaseController
  30. {
  31. /**
  32. * @var StoreServiceRepository
  33. */
  34. protected $repository;
  35. /**
  36. * @var StoreServiceLogRepository
  37. */
  38. protected $logRepository;
  39. /**
  40. * StoreService constructor.
  41. * @param App $app
  42. * @param StoreServiceRepository $repository
  43. */
  44. public function __construct(App $app, StoreServiceRepository $repository, StoreServiceLogRepository $logRepository)
  45. {
  46. parent::__construct($app);
  47. $this->repository = $repository;
  48. $this->logRepository = $logRepository;
  49. }
  50. /**
  51. * 获取列表数据
  52. *
  53. * @return \Psr\Http\Message\ResponseInterface
  54. */
  55. public function lst()
  56. {
  57. // 从请求参数中获取关键字和状态
  58. $where = $this->request->params(['keyword', 'status']);
  59. // 获取分页信息
  60. [$page, $limit] = $this->getPage();
  61. // 设置商家ID
  62. $where['mer_id'] = $this->request->merId();
  63. // 调用仓库的获取列表方法并返回结果
  64. return app('json')->success($this->repository->getList($where, $page, $limit));
  65. }
  66. /**
  67. * 创建表单数据
  68. *
  69. * @return \Psr\Http\Message\ResponseInterface
  70. */
  71. public function createForm()
  72. {
  73. // 调用仓库的表单方法并返回结果
  74. return app('json')->success(formToData($this->repository->form($this->request->merId())));
  75. }
  76. /**
  77. * 创建客服账号
  78. *
  79. * @param StoreServiceValidate $validate 验证器实例
  80. * @return \Psr\Http\Message\ResponseInterface 返回JSON格式的响应结果
  81. */
  82. public function create(StoreServiceValidate $validate)
  83. {
  84. // 获取参数并进行校验
  85. $data = $this->checkParams($validate);
  86. // 设置商家ID
  87. $data['mer_id'] = $this->request->merId();
  88. // 判断该用户是否已经绑定了客服
  89. if ($this->repository->issetService($data['mer_id'], $data['uid']))
  90. return app('json')->fail('该用户已绑定客服');
  91. // 判断账号是否已存在
  92. if ($this->repository->fieldExists('account', $data['account'])) {
  93. return app('json')->fail('账号已存在');
  94. }
  95. // 对密码进行加密处理
  96. $data['pwd'] = password_hash($data['pwd'], PASSWORD_BCRYPT);
  97. // 创建客服账号
  98. $this->repository->create($data);
  99. // 返回操作成功的提示信息
  100. return app('json')->success('添加成功');
  101. }
  102. /**
  103. * 检查参数并返回处理后的数据
  104. *
  105. * @param StoreServiceValidate $validate 商家服务验证器实例
  106. * @param bool $isUpdate 是否为更新操作,默认为false
  107. * @return array 处理后的数据
  108. * @throws ValidateException 当客服密码与确认密码不一致时抛出异常
  109. */
  110. public function checkParams(StoreServiceValidate $validate, $isUpdate = false)
  111. {
  112. // 获取请求参数
  113. $data = $this->request->params([['uid', []], 'nickname', 'account', 'pwd', 'confirm_pwd', 'is_open', 'status', 'customer', 'is_verify', 'is_goods', 'notify', 'avatar', 'phone', ['sort', 0]]);
  114. // 如果是更新操作,则调用验证器的update方法
  115. if ($isUpdate) {
  116. $validate->update();
  117. }
  118. // 如果没有传入merId,则将is_verify、customer、is_goods和notify设置为0,phone设置为空字符串
  119. if (!$this->request->merId()) {
  120. $data['is_verify'] = 0;
  121. $data['customer'] = 0;
  122. $data['is_goods'] = 0;
  123. }
  124. // 对数据进行验证
  125. $validate->check($data);
  126. // 如果没有上传头像,则将其设置为uid的src属性值
  127. if (!$data['avatar']) $data['avatar'] = $data['uid']['src'];
  128. // 如果密码和确认密码不一致,则抛出异常
  129. if ($data['pwd'] && $data['pwd'] != $data['confirm_pwd']) {
  130. throw new ValidateException('客服密码与确认密码不一致');
  131. }
  132. // 将uid设置为uid的id属性值,并删除confirm_pwd属性
  133. $data['uid'] = $data['uid']['id'];
  134. unset($data['confirm_pwd']);
  135. // 返回处理后的数据
  136. return $data;
  137. }
  138. /**
  139. * 更新表单
  140. *
  141. * @param int $id 表单ID
  142. * @return \Illuminate\Http\JsonResponse
  143. */
  144. public function updateForm($id)
  145. {
  146. // 判断表单是否存在
  147. if (!$this->repository->merExists($this->request->merId(), $id))
  148. return app('json')->fail('数据不存在');
  149. // 调用 repository 的 updateForm 方法更新表单,并将结果转换为数据格式返回
  150. return app('json')->success(formToData($this->repository->updateForm($id)));
  151. }
  152. /**
  153. * 更新客服信息
  154. *
  155. * @param int $id 客服ID
  156. * @param StoreServiceValidate $validate 验证器实例
  157. * @return \think\response\Json
  158. */
  159. public function update($id, StoreServiceValidate $validate)
  160. {
  161. // 获取验证通过的数据
  162. $data = $this->checkParams($validate, true);
  163. // 判断客服是否存在
  164. if (!$this->repository->merExists($merId = $this->request->merId(), $id))
  165. return app('json')->fail('数据不存在');
  166. // 判断该用户是否已经绑定了客服
  167. if ($this->repository->issetService($merId, $data['uid'], $id))
  168. return app('json')->fail('该用户已绑定客服');
  169. // 判断账号是否已存在
  170. if ($this->repository->fieldExists('account', $data['account'], $id)) {
  171. return app('json')->fail('账号已存在');
  172. }
  173. // 对密码进行加密处理
  174. if ($data['pwd']) {
  175. $data['pwd'] = password_hash($data['pwd'], PASSWORD_BCRYPT);
  176. } else {
  177. unset($data['pwd']);
  178. }
  179. // 更新客服信息
  180. $this->repository->update($id, $data);
  181. // 返回操作结果
  182. return app('json')->success('修改成功');
  183. }
  184. /**
  185. * 修改状态
  186. *
  187. * @param int $id 商品ID
  188. * @return \think\response\Json
  189. */
  190. public function changeStatus($id)
  191. {
  192. // 获取请求参数中的状态值
  193. $status = $this->request->param('status');
  194. // 判断商品是否存在
  195. if (!$this->repository->merExists($this->request->merId(), $id))
  196. return app('json')->fail('数据不存在');
  197. // 更新商品状态
  198. $this->repository->update($id, ['is_open' => $status == 1 ? 1 : 0]);
  199. // 返回操作结果
  200. return app('json')->success('修改成功');
  201. }
  202. /**
  203. * 根据ID删除数据
  204. *
  205. * @param int $id 数据ID
  206. * @return \Illuminate\Http\JsonResponse 返回JSON格式的响应结果
  207. */
  208. public function delete($id)
  209. {
  210. // 判断数据是否存在
  211. if (!$this->repository->merExists($this->request->merId(), $id))
  212. return app('json')->fail('数据不存在');
  213. // 删除数据
  214. $this->repository->delete($id);
  215. // 返回删除成功的响应结果
  216. return app('json')->success('删除成功');
  217. }
  218. /**
  219. * 客服的全部用户
  220. * @param $id
  221. * @return mixed
  222. * @author Qinii
  223. * @day 2020-06-18
  224. */
  225. public function serviceUserList($id)
  226. {
  227. if (!$this->repository->merExists($this->request->merId(), $id))
  228. return app('json')->fail('数据不存在');
  229. [$page, $limit] = $this->getPage();
  230. return app('json')->success($this->logRepository->getServiceUserList($id, $page, $limit));
  231. }
  232. /**
  233. * 商户的全部用户列表
  234. * @return mixed
  235. * @author Qinii
  236. * @day 2020-06-19
  237. */
  238. public function merchantUserList()
  239. {
  240. [$page, $limit] = $this->getPage();
  241. return app('json')->success($this->logRepository->getMerchantUserList($this->request->merId(), $page, $limit));
  242. }
  243. /**
  244. * 用户与客服聊天记录
  245. * @param $id
  246. * @param $uid
  247. * @return mixed
  248. * @author Qinii
  249. * @day 2020-06-19
  250. */
  251. public function getUserMsnByService($id, $uid)
  252. {
  253. [$page, $limit] = $this->getPage();
  254. if (!$this->repository->getWhereCount(['service_id' => $id, 'mer_id' => $this->request->merId()]))
  255. return app('json')->fail('客服不存在');
  256. return app('json')->success($this->logRepository->getUserMsn($uid, $page, $limit, $this->request->merId(), $id));
  257. }
  258. /**
  259. * 用户与商户聊天记录
  260. * @param $id
  261. * @return mixed
  262. * @author Qinii
  263. * @day 2020-06-19
  264. */
  265. public function getUserMsnByMerchant($id)
  266. {
  267. [$page, $limit] = $this->getPage();
  268. return app('json')->success($this->logRepository->getUserMsn($id, $page, $limit, $this->request->merId()));
  269. }
  270. /**
  271. * 获取用户列表
  272. *
  273. * @return \think\response\Json
  274. */
  275. public function getUserList()
  276. {
  277. // 获取分页参数
  278. [$page, $limit] = $this->getPage();
  279. // 获取查询条件
  280. $where = $this->request->params(['keyword']);
  281. $where['status'] = 1;
  282. // 调用 UserRepository 类的 getPulbicLst 方法获取用户列表
  283. $data = app()->make(UserRepository::class)->getPulbicLst($where, $page, $limit);
  284. // 返回 JSON 格式的数据
  285. return app('json')->success($data);
  286. }
  287. /**
  288. * 登录方法
  289. *
  290. * @param int $id 管理员ID
  291. * @return \think\response\Json
  292. */
  293. public function login($id)
  294. {
  295. // 判断商家和管理员是否存在
  296. if (!$this->repository->merExists($this->request->merId(), $id))
  297. return app('json')->fail('数据不存在');
  298. // 获取管理员信息
  299. $adminInfo = $this->repository->get($id);
  300. // 创建令牌
  301. $tokenInfo = $this->repository->createToken($adminInfo);
  302. // 构造返回数据
  303. $admin = $adminInfo->toArray();
  304. unset($admin['pwd']);
  305. $data = [
  306. 'token' => $tokenInfo['token'],
  307. 'exp' => $tokenInfo['out'],
  308. 'admin' => $admin,
  309. 'url' => '/' . config('admin.service_prefix')
  310. ];
  311. // 返回成功响应
  312. return app('json')->success($data);
  313. }
  314. }