DeliveryStation.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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\delivery;
  12. use app\common\repositories\system\serve\ServeOrderRepository;
  13. use crmeb\services\DeliverySevices;
  14. use think\App;
  15. use crmeb\basic\BaseController;
  16. use app\common\repositories\delivery\DeliveryStationRepository;
  17. use app\validate\merchant\DeliveryStationValidate;
  18. use think\exception\ValidateException;
  19. class DeliveryStation extends BaseController
  20. {
  21. protected $repository;
  22. /**
  23. * 构造函数
  24. * @param App $app 应用实例
  25. * @param DeliveryStationRepository $repository 配送站点仓库实例
  26. */
  27. public function __construct(App $app, DeliveryStationRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 获取配送站点列表
  34. * @return mixed
  35. */
  36. public function lst()
  37. {
  38. // 获取分页参数
  39. [$page, $limit] = $this->getPage();
  40. // 获取查询条件
  41. $where = $this->request->params(['keyword', 'station_name', 'status', 'type', 'date']);
  42. $where['mer_id'] = $this->request->merId();
  43. // 调用仓库方法获取数据
  44. $data = $this->repository->merList($where, $page, $limit);
  45. // 返回成功响应
  46. return app('json')->success($data);
  47. }
  48. /**
  49. * 获取配送站点类型列表
  50. * @return mixed
  51. */
  52. public function getTypeList()
  53. {
  54. // 判断同城配送是否开启
  55. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  56. // 获取分页参数
  57. [$page, $limit] = $this->getPage();
  58. $where = $this->request->params(['keyword', 'station_name']);
  59. $where['mer_id'] = $this->request->merId();
  60. $where['type'] = systemConfig('delivery_type');
  61. $where['status'] = 1;
  62. // 调用仓库方法获取数据
  63. $data = $this->repository->merList($where, $page, $limit);
  64. // 返回成功响应
  65. return app('json')->success($data);
  66. }
  67. /**
  68. * 获取配送站点详情
  69. * @param int $id 配送站点ID
  70. * @return mixed
  71. */
  72. public function detail($id)
  73. {
  74. $data = $this->repository->detail($id, $this->request->merId());
  75. // 返回成功响应
  76. return app('json')->success($data);
  77. }
  78. /**
  79. * 创建配送站点
  80. * @return mixed
  81. */
  82. public function create()
  83. {
  84. // 判断同城配送是否开启
  85. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  86. // 校验参数
  87. $data = $this->checkParams();
  88. $data['mer_id'] = $this->request->merId();
  89. // 调用仓库方法保存数据
  90. $this->repository->save($data);
  91. return app('json')->success('添加成功');
  92. }
  93. /**
  94. * 更新配送站点信息
  95. * @param int $id 配送站点ID
  96. * @return \think\response\Json
  97. * @throws \app\common\exception\ValidateException
  98. */
  99. public function update($id)
  100. {
  101. // 判断同城配送是否开启
  102. // 判断同城配送是否开启
  103. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  104. // 校验参数
  105. // 校验参数
  106. $data = $this->checkParams();
  107. $this->repository->edit($id, $this->request->merId(), $data);
  108. return app('json')->success('编辑成功');
  109. }
  110. /**
  111. * 删除配送站点信息
  112. * @param int $id 配送站点ID
  113. * @return \think\response\Json
  114. * @throws \app\common\exception\ValidateException
  115. */
  116. public function delete($id)
  117. {
  118. // 判断同城配送是否开启
  119. // 判断同城配送是否开启
  120. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  121. $this->repository->destory($id, $this->request->merId());
  122. return app('json')->success('删除成功');
  123. }
  124. /**
  125. * 切换配送站点状态
  126. * @param int $id 配送站点ID
  127. * @return \think\response\Json
  128. * @throws \app\common\exception\ValidateException
  129. */
  130. public function switchWithStatus($id)
  131. {
  132. // 判断同城配送是否开启
  133. // 判断同城配送是否开启
  134. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  135. $status = $this->request->param('status') == 1 ? 1 : 0;
  136. $this->repository->update($id, ['status' => $status]);
  137. return app('json')->success('修改成功');
  138. }
  139. /**
  140. * 获取配送站点表单信息
  141. * @param int $id 配送站点ID
  142. * @return \think\response\Json
  143. * @throws \app\common\exception\ValidateException
  144. */
  145. public function markForm($id)
  146. {
  147. // 判断同城配送是否开启
  148. // 判断同城配送是否开启
  149. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  150. return app('json')->success(formToData($this->repository->markForm($id, $this->request->merId())));
  151. }
  152. /**
  153. * 添加或修改配送站点备注信息
  154. * @param int $id 配送站点ID
  155. * @return \think\response\Json
  156. * @throws \app\common\exception\ValidateException
  157. */
  158. public function mark($id)
  159. {
  160. // 判断同城配送是否开启
  161. // 判断同城配送是否开启
  162. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  163. $data = $this->request->params(['mark']);
  164. $this->repository->update($id, $data);
  165. return app('json')->success('备注成功');
  166. }
  167. /**
  168. * 检查参数是否合法
  169. *
  170. * @return array 返回检查后的参数数组
  171. */
  172. public function checkParams()
  173. {
  174. // 从请求中获取参数
  175. $data = $this->request->params([
  176. 'station_name',
  177. 'business',
  178. 'station_address',
  179. 'lng',
  180. 'lat',
  181. 'contact_name',
  182. 'phone',
  183. 'username',
  184. 'password',
  185. ['status', 1],
  186. 'city_name',
  187. ]);
  188. // 创建验证器实例
  189. $make = app()->make(DeliveryStationValidate::class);
  190. // 设置配送类型
  191. $data['type'] = systemConfig('delivery_type');
  192. // 根据配送类型选择不同的验证场景
  193. if ($data['type'] == DeliverySevices::DELIVERY_TYPE_DADA) {
  194. $make->scene('dada')->check($data);
  195. } else {
  196. $make->check($data);
  197. // 将经纬度转换为百度坐标系
  198. [$data['lng'], $data['lat']] = gcj02ToBd09($data['lng'], $data['lat']);
  199. }
  200. // 返回检查后的参数数组
  201. return $data;
  202. }
  203. /**
  204. * 获取商家列表
  205. *
  206. * @return mixed 返回成功响应
  207. * @throws ValidateException 当同城配送未开启时抛出异常
  208. */
  209. public function getBusiness()
  210. {
  211. // 判断同城配送是否开启
  212. // 判断同城配送是否开启
  213. // 判断同城配送是否开启
  214. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  215. // 获取商家列表
  216. $data = $this->repository->getBusiness();
  217. // 返回成功响应
  218. // 返回成功响应
  219. return app('json')->success($data);
  220. }
  221. /**
  222. * 获取配送选项列表
  223. *
  224. * @return mixed 返回成功响应
  225. */
  226. public function options()
  227. {
  228. // 构造查询条件
  229. $where = [
  230. 'status' => 1,
  231. 'mer_id' => $this->request->merId(),
  232. 'type' => systemConfig('delivery_type'),
  233. ];
  234. // 获取配送选项列表
  235. return app('json')->success($this->repository->getOptions($where));
  236. }
  237. /**
  238. * 选择商品
  239. *
  240. * @return \think\response\Json
  241. */
  242. public function select()
  243. {
  244. // 构建查询条件
  245. $where = [
  246. 'mer_id' => $this->request->merId(),
  247. ];
  248. // 调用 repository 类的 getOptions 方法获取商品列表并返回 JSON 格式数据
  249. return app('json')->success($this->repository->getOptions($where));
  250. }
  251. /**
  252. * 获取城市列表
  253. *
  254. * @return \think\response\Json
  255. * @throws \app\common\exception\ValidateException
  256. */
  257. public function getCityLst()
  258. {
  259. // 判断同城配送是否开启
  260. // 判断同城配送是否开启
  261. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  262. // 调用 repository 类的 getCityLst 方法获取城市列表并返回 JSON 格式数据
  263. return app('json')->success($this->repository->getCityLst());
  264. }
  265. /**
  266. * 充值记录
  267. * @author Qinii
  268. * @day 2/18/22
  269. */
  270. public function payLst()
  271. {
  272. // 获取分页参数
  273. [$page, $limit] = $this->getPage();
  274. $where = [
  275. 'type' => 20,
  276. 'mer_id' => $this->request->merId(),
  277. 'date' => $this->request->param('date'),
  278. ];
  279. $data = app()->make(ServeOrderRepository::class)->getList($where, $page, $limit);
  280. $data['delivery_balance'] = $this->request->merchant()->delivery_balance;
  281. // 返回成功响应
  282. return app('json')->success($data);
  283. }
  284. /**
  285. * 获取二维码
  286. *
  287. * @return \Psr\Http\Message\ResponseInterface
  288. * @throws ValidateException 当同城配送未开启时抛出异常
  289. */
  290. public function getQrcode()
  291. {
  292. // 判断同城配送是否开启
  293. // 判断同城配送是否开启
  294. if (systemConfig('delivery_status') != 1) throw new ValidateException('未开启同城配送');
  295. // 获取支付类型和支付金额
  296. $data['pay_type'] = $this->request->param('pay_type', 1);
  297. $data['price'] = $this->request->param('price', 10);
  298. // 判断支付金额是否合法
  299. if (!is_numeric($data['price']) || $data['price'] <= 0)
  300. return app('json')->fail('支付金额不正确');
  301. // 调用 ServeOrderRepository 类的 QrCode 方法获取二维码
  302. $res = app()->make(ServeOrderRepository::class)->QrCode($this->request->merId(), 'delivery', $data);
  303. // 设置配送余额并返回结果
  304. $res['delivery_balance'] = $this->request->merchant()->delivery_balance;
  305. return app('json')->success($res);
  306. }
  307. }