DeliveryServiceServices.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\services\order;
  12. use app\dao\order\DeliveryServiceDao;
  13. use app\services\BaseServices;
  14. use app\services\kefu\service\StoreServiceLogServices;
  15. use app\services\user\UserServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\FormBuilder;
  18. /**
  19. * 配送
  20. * Class DeliveryServiceServices
  21. * @package app\services\order
  22. * @method getStoreServiceOrderNotice() 获取接受通知的客服
  23. */
  24. class DeliveryServiceServices extends BaseServices
  25. {
  26. /**
  27. * 创建form表单
  28. * @var Form
  29. */
  30. protected $builder;
  31. /**构造方法
  32. * DeliveryServiceServices constructor.
  33. * @param DeliveryServiceDao $dao
  34. * @param FormBuilder $builder
  35. */
  36. public function __construct(DeliveryServiceDao $dao, FormBuilder $builder)
  37. {
  38. $this->dao = $dao;
  39. $this->builder = $builder;
  40. }
  41. /**
  42. * 获取配送员列表
  43. * @param array $where
  44. * @return array
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function getServiceList(array $where)
  50. {
  51. [$page, $limit] = $this->getPageValue();
  52. $list = $this->dao->getServiceList($where, $page, $limit);
  53. $count = $this->dao->count($where);
  54. return compact('list', 'count');
  55. }
  56. /**
  57. *获取配送员列表
  58. */
  59. public function getDeliveryList()
  60. {
  61. [$page, $limit] = $this->getPageValue();
  62. [$list, $count] = $this->dao->getList($page, $limit);
  63. return compact('list', 'count');
  64. }
  65. /**
  66. * 创建配送员表单
  67. * @param array $formData
  68. * @return mixed
  69. * @throws \FormBuilder\Exception\FormBuilderException
  70. */
  71. public function createServiceForm(array $formData = [])
  72. {
  73. if ($formData) {
  74. $field[] = $this->builder->frameImage('avatar', '配送员头像', $this->url(config('app.admin_prefix', 'admin') . '/widget.images/index', ['fodder' => 'avatar'], true), $formData['avatar'] ?? '')->icon('el-icon-user')->width('950px')->height('560px')->props(['footer' => false]);
  75. } else {
  76. $field[] = $this->builder->frameImage('image', '商城用户', $this->url(config('app.admin_prefix', 'admin') . '/system.user/list', ['fodder' => 'image'], true))->icon('el-icon-user')->width('950px')->height('560px')->Props(['srcKey' => 'image', 'footer' => false]);
  77. $field[] = $this->builder->hidden('uid', 0);
  78. $field[] = $this->builder->hidden('avatar', '');
  79. }
  80. $field[] = $this->builder->input('nickname', '配送员名称', $formData['nickname'] ?? '')->required('请填写名称')->col(24);
  81. $field[] = $this->builder->input('phone', '手机号码', $formData['phone'] ?? '')->required('请填写电话')->col(24)->maxlength(11);
  82. $field[] = $this->builder->radio('status', '配送员状态', $formData['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  83. return $field;
  84. }
  85. /**
  86. * 创建配送员获取表单
  87. * @return array
  88. * @throws \FormBuilder\Exception\FormBuilderException
  89. */
  90. public function create()
  91. {
  92. return create_form('添加配送员', $this->createServiceForm(), $this->url('/order/delivery/save'), 'POST');
  93. }
  94. /**
  95. * 编辑获取表单
  96. * @param int $id
  97. * @return array
  98. * @throws \FormBuilder\Exception\FormBuilderException
  99. */
  100. public function edit(int $id)
  101. {
  102. $serviceInfo = $this->dao->get($id);
  103. if (!$serviceInfo) {
  104. throw new AdminException(100026);
  105. }
  106. return create_form('编辑配送员', $this->createServiceForm($serviceInfo->toArray()), $this->url('/order/delivery/update/' . $id), 'PUT');
  107. }
  108. /**
  109. * 获取某人的聊天记录用户列表
  110. * @param int $uid
  111. * @return array|array[]
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function getChatUser(int $uid)
  117. {
  118. /** @var StoreServiceLogServices $serviceLog */
  119. $serviceLog = app()->make(StoreServiceLogServices::class);
  120. /** @var UserServices $serviceUser */
  121. $serviceUser = app()->make(UserServices::class);
  122. $uids = $serviceLog->getChatUserIds($uid);
  123. if (!$uids) {
  124. return [];
  125. }
  126. return $serviceUser->getUserList(['uid' => $uids], 'nickname,uid,avatar as headimgurl');
  127. }
  128. /**
  129. * 检查用户是否是配送员
  130. * @param int $uid
  131. * @return bool
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function checkoutIsService(int $uid)
  137. {
  138. return (bool)$this->dao->count(['uid' => $uid, 'status' => 1]);
  139. }
  140. /**
  141. * 保存新建的资源
  142. * @param array $data
  143. * @return void
  144. */
  145. public function saveDeliveryService(array $data)
  146. {
  147. if ($data['image'] == '') throw new AdminException(400250);
  148. $data['uid'] = $data['image']['uid'];
  149. /** @var UserServices $userService */
  150. $userService = app()->make(UserServices::class);
  151. $userInfo = $userService->get($data['uid']);
  152. if ($data['phone'] == '') {
  153. if (!$userInfo['phone']) {
  154. throw new AdminException(400132);
  155. } else {
  156. $data['phone'] = $userInfo['phone'];
  157. }
  158. } else {
  159. if (!check_phone($data['phone'])) {
  160. throw new AdminException(400252);
  161. }
  162. }
  163. if ($data['nickname'] == '') $data['nickname'] = $userInfo['nickname'];
  164. $data['avatar'] = $data['image']['image'];
  165. if ($this->dao->count(['uid' => $data['uid']])) {
  166. throw new AdminException(400467);
  167. }
  168. if ($this->dao->count(['phone' => $data['phone']])) {
  169. throw new AdminException(400468);
  170. }
  171. unset($data['image']);
  172. $data['add_time'] = time();
  173. $res = $this->dao->save($data);
  174. if (!$res) throw new AdminException(100006);
  175. return true;
  176. }
  177. /**
  178. * 更新资源
  179. * @param int $id
  180. * @param array $data
  181. * @return void
  182. */
  183. public function updateDeliveryService(int $id, array $data)
  184. {
  185. $delivery = $this->dao->get($id);
  186. if (!$delivery) {
  187. throw new AdminException(100026);
  188. }
  189. if ($data["nickname"] == '') {
  190. throw new AdminException(400469);
  191. }
  192. if (!$data['phone']) {
  193. throw new AdminException(400132);
  194. }
  195. if (!check_phone($data['phone'])) {
  196. throw new AdminException(400252);
  197. }
  198. if ($delivery['phone'] != $data['phone'] && $this->dao->count(['phone' => $data['phone']])) {
  199. throw new AdminException(400468);
  200. }
  201. $res = $this->dao->update($id, $data);
  202. if (!$res) throw new AdminException(100007);
  203. return true;
  204. }
  205. }