Service.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\api\store\service;
  12. use app\common\repositories\store\order\StoreOrderRepository;
  13. use app\common\repositories\store\service\StoreServiceLogRepository;
  14. use app\common\repositories\store\service\StoreServiceRepository;
  15. use app\common\repositories\store\service\StoreServiceUserRepository;
  16. use app\common\repositories\system\ExtendRepository;
  17. use app\common\repositories\system\merchant\MerchantRepository;
  18. use crmeb\basic\BaseController;
  19. use think\App;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\facade\Cache;
  24. /**
  25. * Class Service
  26. * @package app\controller\api\store\service
  27. * @author xaboy
  28. * @day 2020/5/29
  29. */
  30. class Service extends BaseController
  31. {
  32. /**
  33. * @var StoreServiceRepository
  34. */
  35. protected $repository;
  36. /**
  37. * Service constructor.
  38. * @param App $app
  39. * @param StoreServiceRepository $repository
  40. */
  41. public function __construct(App $app, StoreServiceRepository $repository)
  42. {
  43. parent::__construct($app);
  44. $this->repository = $repository;
  45. }
  46. /**
  47. * @param $id
  48. * @param StoreServiceLogRepository $repository
  49. * @return mixed
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. * @author xaboy
  54. * @day 2020/6/15
  55. */
  56. public function chatHistory($id, StoreServiceLogRepository $repository)
  57. {
  58. [$page, $limit] = $this->getPage();
  59. return app('json')->success($repository->userList($id, $this->request->uid(), $page, $limit));
  60. }
  61. /**
  62. * @param StoreServiceLogRepository $repository
  63. * @return mixed
  64. * @throws DataNotFoundException
  65. * @throws DbException
  66. * @throws ModelNotFoundException
  67. * @author xaboy
  68. * @day 2020/6/16
  69. */
  70. public function getList(StoreServiceUserRepository $repository)
  71. {
  72. [$page, $limit] = $this->getPage();
  73. return app('json')->success($repository->userMerchantList($this->request->uid(), $page, $limit));
  74. }
  75. /**
  76. * @param StoreServiceLogRepository $repository
  77. * @return mixed
  78. * @throws DataNotFoundException
  79. * @throws DbException
  80. * @throws ModelNotFoundException
  81. * @author xaboy
  82. * @day 2020/6/16
  83. */
  84. public function serviceUserList($merId, StoreServiceUserRepository $repository)
  85. {
  86. [$page, $limit] = $this->getPage();
  87. return app('json')->success($repository->merUserList($merId, $this->request->uid(), $page, $limit));
  88. }
  89. /**
  90. * @param $merId
  91. * @param $id
  92. * @param StoreServiceLogRepository $repository
  93. * @return mixed
  94. * @throws DataNotFoundException
  95. * @throws DbException
  96. * @throws ModelNotFoundException
  97. * @author xaboy
  98. * @day 2020/6/15
  99. */
  100. public function serviceHistory($merId, $id, StoreServiceLogRepository $repository)
  101. {
  102. [$page, $limit] = $this->getPage();
  103. return app('json')->success($repository->merList($merId, $id, $this->request->uid(), $page, $limit));
  104. }
  105. public function user($merId, $uid, StoreServiceUserRepository $serviceUserRepository)
  106. {
  107. if (!$service = $this->repository->search([
  108. 'uid' => $this->request->uid(),
  109. 'mer_id' => (int)$merId,
  110. 'status' => 1
  111. ])->find()) {
  112. return app('json')->fail('没有权限');
  113. }
  114. $user = $serviceUserRepository->search(['uid' => $uid, 'service_user_id' => $service->service_user_id])->with(['user' => function ($query) use ($merId) {
  115. $query->field('uid,avatar,nickname,is_promoter' . (!$merId ? ',phone' : ''));
  116. }, 'mark' => function ($query) use ($merId) {
  117. $query->where('mer_id', $merId)->bind(['mark' => 'extend_value']);
  118. }])->find();
  119. if (!$user) {
  120. return app('json')->fail('用户不存在');
  121. }
  122. return app('json')->success($user->toArray());
  123. }
  124. public function mark($merId, $uid, StoreServiceUserRepository $serviceUserRepository, ExtendRepository $extendRepository)
  125. {
  126. $data = $this->request->params(['mark']);
  127. if (!$service = $this->repository->search([
  128. 'uid' => $this->request->uid(),
  129. 'mer_id' => (int)$merId,
  130. 'status' => 1
  131. ])->find()) {
  132. return app('json')->fail('没有权限');
  133. }
  134. if ($service->mer_id && !$serviceUserRepository->existsWhere(['uid' => (int)$uid, 'mer_id' => $service->mer_id])) {
  135. return app('json')->fail('用户不存在');
  136. }
  137. $extendRepository->updateInfo(ExtendRepository::TYPE_SERVICE_USER_MARK, (int)$uid, $service->mer_id, (string)$data['mark']);
  138. return app('json')->success('备注成功');
  139. }
  140. public function merchantInfo($id)
  141. {
  142. if ($id) {
  143. $merchant = app()->make(MerchantRepository::class)->get((int)$id);
  144. if (!$merchant)
  145. return app('json')->fail('商户不存在');
  146. $data = [
  147. 'mer_id' => $merchant['mer_id'],
  148. 'avatar' => $merchant['mer_avatar'],
  149. 'name' => $merchant['mer_name'],
  150. ];
  151. } else {
  152. $config = systemConfig(['site_logo', 'site_name']);
  153. $data = [
  154. 'mer_id' => 0,
  155. 'avatar' => $config['site_logo'],
  156. 'name' => $config['site_name'],
  157. ];
  158. }
  159. return app('json')->success($data);
  160. }
  161. public function scanLogin($key)
  162. {
  163. $serviceId = (int)$this->request->param('service_id');
  164. if (!$serviceId || !$service = $this->repository->search([
  165. 'uid' => $this->request->uid(),
  166. 'service_id' => $serviceId,
  167. ])->find()) {
  168. return app('json')->fail('用户不存在');
  169. }
  170. if (!$service['is_open'] || !$service['status']) {
  171. return app('json')->fail('账号已被关闭');
  172. }
  173. if (Cache::has('_scan_ser_login' . $key))
  174. Cache::set('_scan_ser_login' . $key, $serviceId);
  175. else
  176. return app('json')->fail('操作超时');
  177. return app('json')->success('登录成功');
  178. }
  179. public function orderOverTime()
  180. {
  181. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  182. $storeOrderRepository->expireOrder();
  183. }
  184. }