DeliveryService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\order;
  12. use app\controller\admin\AuthController;
  13. use app\services\store\DeliveryServiceServices;
  14. use app\services\user\UserServices;
  15. use app\services\user\UserWechatuserServices;
  16. use crmeb\exceptions\AdminException;
  17. use think\facade\App;
  18. /**
  19. * 配送员
  20. * Class StoreService
  21. * @package app\controller\admin\v1\store
  22. */
  23. class DeliveryService extends AuthController
  24. {
  25. /**
  26. * DeliveryService constructor.
  27. * @param App $app
  28. * @param DeliveryServiceServices $services
  29. */
  30. public function __construct(App $app, DeliveryServiceServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. /**
  36. * 显示资源列表
  37. *
  38. * @return \think\Response
  39. */
  40. public function index()
  41. {
  42. return $this->success($this->services->getServiceList(['type' => 0, 'is_del' => 0]));
  43. }
  44. /**
  45. * 显示创建资源表单页.
  46. *
  47. * @return \think\Response
  48. */
  49. public function create(UserWechatuserServices $services)
  50. {
  51. $where = $this->request->getMore([
  52. ['nickname', ''],
  53. ['data', '', '', 'time'],
  54. ['type', '', '', 'user_type'],
  55. ]);
  56. [$list, $count] = $services->getWhereUserList($where, 'u.nickname,u.uid,u.avatar as headimgurl,w.subscribe,w.province,w.country,w.city,w.sex');
  57. return $this->success(compact('list', 'count'));
  58. }
  59. /**
  60. * 添加客服表单
  61. * @return mixed
  62. * @throws \FormBuilder\Exception\FormBuilderException
  63. */
  64. public function add()
  65. {
  66. return $this->success($this->services->create());
  67. }
  68. /*
  69. * 保存新建的资源
  70. */
  71. public function save()
  72. {
  73. $data = $this->request->postMore([
  74. ['image', ''],
  75. ['uid', 0],
  76. ['avatar', ''],
  77. ['phone', ''],
  78. ['nickname', ''],
  79. ['status', 1],
  80. ]);
  81. if ($data['image'] == '') return $this->fail('请选择用户');
  82. $data['uid'] = $data['image']['uid'];
  83. /** @var UserServices $userService */
  84. $userService = app()->make(UserServices::class);
  85. $userInfo = $userService->get($data['uid']);
  86. if ($data['phone'] == '') {
  87. if (!$userInfo['phone']) {
  88. throw new AdminException('该用户没有绑定手机号,请手动填写');
  89. } else {
  90. $data['phone'] = $userInfo['phone'];
  91. }
  92. } else {
  93. if (!check_phone($data['phone'])) {
  94. return $this->fail('请输入正确的手机号!');
  95. }
  96. }
  97. if ($data['nickname'] == '') $data['nickname'] = $userInfo['nickname'];
  98. $data['avatar'] = $data['image']['image'];
  99. if ($this->services->count(['uid' => $data['uid'], 'type' => 0, 'is_del' => 0])) {
  100. return $this->fail('配送员已存在!');
  101. }
  102. if ($this->services->count(['phone' => $data['phone'], 'type' => 0, 'is_del' => 0])) {
  103. return $this->fail('同一个手机号的配送员只能添加一个!');
  104. }
  105. unset($data['image']);
  106. $data['type'] = 0;
  107. $data['add_time'] = time();
  108. $res = $this->services->save($data);
  109. if ($res) {
  110. return $this->success('配送员添加成功');
  111. } else {
  112. return $this->fail('配送员添加失败,请稍后再试');
  113. }
  114. }
  115. /**
  116. * 显示编辑资源表单页.
  117. *
  118. * @param int $id
  119. * @return \think\Response
  120. */
  121. public function edit($id)
  122. {
  123. return $this->success($this->services->edit((int)$id));
  124. }
  125. /**
  126. * 保存新建的资源
  127. *
  128. * @param \think\Request $request
  129. * @return \think\Response
  130. */
  131. public function update($id)
  132. {
  133. $data = $this->request->postMore([
  134. ['avatar', ''],
  135. ['nickname', ''],
  136. ['phone', ''],
  137. ['status', 1],
  138. ]);
  139. $delivery = $this->services->get((int)$id);
  140. if (!$delivery) {
  141. return $this->fail('数据不存在');
  142. }
  143. if ($data["nickname"] == '') {
  144. return $this->fail("配送员名称不能为空!");
  145. }
  146. if (!$data['phone']) {
  147. return $this->fail("手机号不能为空!");
  148. }
  149. if (!check_phone($data['phone'])) {
  150. return $this->fail('请输入正确的手机号!');
  151. }
  152. if ($delivery['phone'] != $data['phone'] && $this->services->count(['phone' => $data['phone'], 'type' => 0, 'is_del' => 0])) {
  153. return $this->fail('同一个手机号的配送员只能添加一个!');
  154. }
  155. $this->services->update($id, $data);
  156. return $this->success('修改成功!');
  157. }
  158. /**
  159. * 删除指定资源
  160. *
  161. * @param int $id
  162. * @return \think\Response
  163. */
  164. public function delete($id)
  165. {
  166. if (!$this->services->delete($id))
  167. return $this->fail('删除失败,请稍候再试!');
  168. else
  169. return $this->success('删除成功!');
  170. }
  171. /**
  172. * 修改状态
  173. * @param $id
  174. * @param $status
  175. * @return mixed
  176. */
  177. public function set_status($id, $status)
  178. {
  179. if ($status == '' || $id == 0) return $this->fail('参数错误');
  180. $this->services->update($id, ['status' => $status]);
  181. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  182. }
  183. /**
  184. *获取所有配送员列表
  185. */
  186. public function get_delivery_list()
  187. {
  188. $data = $this->services->getDeliveryList();
  189. return $this->success($data);
  190. }
  191. }