UserOrder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\UserApi;
  4. use app\common\model\Payment;
  5. use app\common\model\UserCoupon;
  6. use app\service\Pet;
  7. use app\service\UserOrderService;
  8. use app\UserException;
  9. use app\validate\UserOrderValidate;
  10. use think\Db;
  11. use think\Exception;
  12. use app\common\model\UserOrder as UO;
  13. /**
  14. * 用户端订单接口
  15. */
  16. class UserOrder extends UserApi
  17. {
  18. protected $noNeedLogin = ['category','spec','protect'];
  19. protected $noNeedRight = '*';
  20. /**
  21. * 常用地址
  22. * @ApiReturnParams (name=id,description=id)
  23. * @ApiReturnParams (name=name,description=姓名)
  24. * @ApiReturnParams (name=mobile,description=手机号)
  25. * @ApiReturnParams (name=address,description=地址)
  26. * @ApiReturnParams (name=city,description=城市)
  27. */
  28. public function address(){
  29. $user=$this->auth->getUser();
  30. $this->success('',$user->address()->order('id','desc')->select());
  31. }
  32. /**
  33. * 宠物类别
  34. * @ApiReturnParams (name=id,description=id)
  35. * @ApiReturnParams (name=name,description=名称)
  36. */
  37. public function category(Pet $pet){
  38. $this->success('',$pet::category());
  39. }
  40. /**
  41. * 宠物规格
  42. * @ApiReturnParams (name=id,description=id)
  43. * @ApiReturnParams (name=name,description=名称)
  44. * @ApiReturnParams (name=weight,description=重量范围)
  45. */
  46. public function spec(Pet $pet){
  47. $this->success('',$pet::spec());
  48. }
  49. /**
  50. * 宠物保
  51. * @ApiReturnParams (name=id,description=id)
  52. * @ApiReturnParams (name=name,description=名称)
  53. * @ApiReturnParams (name=max,description=最高保多少)
  54. * @ApiReturnParams (name=price,description=价格)
  55. */
  56. public function protect(Pet $pet){
  57. $this->success('',$pet::protect());
  58. }
  59. /**
  60. * 预下单
  61. * @ApiMethod (POST)
  62. * @param array from_addr 必须,取宠地址信息
  63. * @param string from_addr[longitude] 必须,取宠地址经度
  64. * @param string from_addr[latitude] 必须,取宠地址纬度
  65. * @param string from_addr[name] 必须,取宠人
  66. * @param string from_addr[mobile] 必须,取宠手机号
  67. * @param string from_addr[city] 必须,取宠城市
  68. * @param string from_addr[address] 必须,取宠详细地址
  69. * @param array to_addr 必须,收宠地址信息
  70. * @param string to_addr[longitude] 必须,收宠地址经度
  71. * @param string to_addr[latitude] 必须,收宠地址纬度
  72. * @param string to_addr[name] 必须,收宠人
  73. * @param string to_addr[mobile] 必须,收宠手机号
  74. * @param string to_addr[city] 必须,收宠城市
  75. * @param string to_addr[address] 必须,收宠详细地址
  76. * @param string pick_up 0无1上门接2上门送4上门接送
  77. * @param string agree_time 取宠时间
  78. * @param string pet_category 宠物分类ID
  79. * @param string num 宠物数量
  80. * @param string weight 宠物重量
  81. * @param string spec 宠物规格
  82. * @param string piece 宠物件数
  83. * @param string has_cage 有无宠具0无1有
  84. * @param string remark 备注信息
  85. * @param string images 照片
  86. * @param string protect_id 安心保ID
  87. * @param string coupon_id 优惠券ID
  88. * @param string freight fast快车air空运special专车
  89. *
  90. * @ApiReturnParams (name=total_amount,type=string,description=总价)
  91. * @ApiReturnParams (name=coupon_amount,type=string,description=优惠券优惠金额)
  92. * @ApiReturnParams (name=real_amount,type=string,description=实付总价)
  93. * @ApiReturnParams (name=freights[0][price],type=string,description=实际价格)
  94. * @ApiReturnParams (name=freights[0][total_price],type=string,description=总价)
  95. * @ApiReturnParams (name=freights[0][type],type=string,description=类型:fast快车air空运special专车)
  96. * @ApiReturnParams (name=freights[0][coupon][0][amount],type=string,description=优惠券优惠额度)
  97. * @ApiReturnParams (name=freights[0][coupon][0][end_time],type=string,description=优惠券过期时间)
  98. * @ApiReturnParams (name=freights[0][coupon][0][id],type=string,description=优惠券id)
  99. * @ApiReturnParams (name=freights[0][coupon][0][is_used],type=string,description=优惠券实付已使用)
  100. * @ApiReturnParams (name=freights[0][coupon][0][max],type=string,description=优惠券满多少可以减)
  101. * @ApiReturnParams (name=freights[0][coupon][0][selected],type=string,description=实付是选中的优惠券)
  102. * @ApiReturnParams (name=freights[0][coupon][0][start_time],type=string,description=优惠券开始时间)
  103. * @ApiReturnParams (name=freights[0][coupon][0][type],type=string,description="优惠券类型,1代金券2满减券3折扣券4新用户下单券")
  104. * @ApiReturnParams (name=freights[0][coupon][0][type_desc],type=string,description=优惠券类型)
  105. */
  106. public function prepare(UserOrderService $userOrder){
  107. $data=input();
  108. $this->validate($data,UserOrderValidate::class);
  109. $user=$this->auth->getUser();
  110. if(!empty($data['freight'])){
  111. $this->validate($data,UserOrderValidate::$freight);
  112. }
  113. try {
  114. $data=$userOrder->setUser($user)
  115. ->import($data)
  116. ->setCoupon($data['coupon_id']??null)
  117. ->prepare();
  118. $this->success('',$data);
  119. }catch (UserException $e){
  120. $this->error($e->getMessage());
  121. }
  122. }
  123. /**
  124. * 提交订单
  125. * @param array from_addr 必须,取宠地址信息
  126. * @param string from_addr[longitude] 必须,取宠地址经度
  127. * @param string from_addr[latitude] 必须,取宠地址纬度
  128. * @param string from_addr[name] 必须,取宠人
  129. * @param string from_addr[mobile] 必须,取宠手机号
  130. * @param string from_addr[city] 必须,取宠城市
  131. * @param string from_addr[address] 必须,取宠详细地址
  132. * @param array to_addr 必须,收宠地址信息
  133. * @param string to_addr[longitude] 必须,收宠地址经度
  134. * @param string to_addr[latitude] 必须,收宠地址纬度
  135. * @param string to_addr[name] 必须,收宠人
  136. * @param string to_addr[mobile] 必须,收宠手机号
  137. * @param string to_addr[city] 必须,收宠城市
  138. * @param string to_addr[address] 必须,收宠详细地址
  139. * @param string pick_up 0无1上门接2上门送4上门接送
  140. * @param string agree_time 取宠时间
  141. * @param string pet_category 宠物分类ID
  142. * @param string num 宠物数量
  143. * @param string weight 宠物重量
  144. * @param string spec 宠物规格
  145. * @param string piece 宠物件数
  146. * @param string has_cage 有无宠具0无1有
  147. * @param string remark 备注信息
  148. * @param string images 照片
  149. * @param string protect_id 安心保ID
  150. * @param string coupon_id 优惠券ID
  151. * @param string freight fast快车air空运special专车
  152. * @param string pay_type 1余额2微信
  153. *
  154. * @ApiReturnParams (name=total_amount,type=string,description=总价)
  155. */
  156. public function submit(UserOrderService $userOrder){
  157. $data=input();
  158. $this->validate($data,UserOrderValidate::class);
  159. $user=$this->auth->getUser();
  160. $this->validate($data,UserOrderValidate::$freight);
  161. $this->validate($data,[
  162. 'pay_type'=>['require','in:1,2'],
  163. ]);
  164. Db::startTrans();
  165. $data=$userOrder->setUser($user)
  166. ->import($data)
  167. ->setCoupon($data['coupon_id']??null)
  168. ->prepare();
  169. $data['user_id']=$user['id'];
  170. $order=new \app\common\model\UserOrder();
  171. $order->allowField(true)->save($data);
  172. if($order['coupon_id']){
  173. UserCoupon::where('id',$order['coupon_id'])->save(['is_used'=>1]);
  174. }
  175. $params=$order->pay();
  176. Db::commit();
  177. $this->success('',$params);
  178. }
  179. /**
  180. * 订单列表
  181. * @ApiParams(name=status,required=false,description="1待支付2进行中3已完成4已取消")
  182. * @ApiParams(name=page,required=false,description="页码,默认1")
  183. * @ApiParams(name=limit,required=false,description="每页数量,默认10")
  184. * @ApiReturnParams(name=id,type=string,description=订单id)
  185. * @ApiReturnParams(name=from_addr[name],type=string,description=取件人)
  186. * @ApiReturnParams(name=from_addr[mobile],type=string,description=取件人手机号)
  187. * @ApiReturnParams(name=from_addr[address],type=string,description=取件人地址)
  188. * @ApiReturnParams(name=to_addr[name],type=string,description=送件人)
  189. * @ApiReturnParams(name=to_addr[mobile],type=string,description=送件人手机号)
  190. * @ApiReturnParams(name=to_addr[address],type=string,description=送件人地址)
  191. * @ApiReturnParams(name=pet_category,type=string,description=宠物类别)
  192. * @ApiReturnParams(name=spec,type=string,description=规格)
  193. * @ApiReturnParams(name=real_amount,type=string,description=金额)
  194. */
  195. public function list(){
  196. $data=input();
  197. $user=$this->auth->getUser();
  198. $query=$user->orders()->order('id','desc');
  199. if(!is_null($data['status'])){
  200. switch ($data['status']) {
  201. case 1:
  202. $query->where('status', UO::STATUS_WAIT_PAY);
  203. break;
  204. case 2:
  205. $query->whereBetween('status',[UO::STATUS_WAIT_GET,UO::STATUS_GIVED]);
  206. break;
  207. case 3:
  208. $query->whereBetween('status',UO::STATUS_COMPLETE);
  209. break;
  210. case 4:
  211. $query->whereBetween('status',UO::STATUS_CANCEL);
  212. break;
  213. }
  214. }
  215. $orders=$query->paginate($data['limit']?:10);
  216. $this->success('',$orders);
  217. }
  218. /**
  219. * 订单详情
  220. * @ApiParams (name=id,decription=订单ID)
  221. * @ApiReturnParams(name=id,type=string,description=订单id)
  222. * @ApiReturnParams(name=from_addr[name],type=string,description=取件人)
  223. * @ApiReturnParams(name=from_addr[mobile],type=string,description=取件人手机号)
  224. * @ApiReturnParams(name=from_addr[address],type=string,description=取件人地址)
  225. * @ApiReturnParams(name=to_addr[name],type=string,description=送件人)
  226. * @ApiReturnParams(name=to_addr[mobile],type=string,description=送件人手机号)
  227. * @ApiReturnParams(name=to_addr[address],type=string,description=送件人地址)
  228. * @ApiReturnParams(name=pet_category,type=string,description=宠物类别)
  229. * @ApiReturnParams(name=spec,type=string,description=规格)
  230. * @ApiReturnParams(name=real_amount,type=string,description=金额)
  231. * @ApiReturnParams(name=num,type=string,description=宠物数量)
  232. * @ApiReturnParams(name=pick_up,type=string,description=0不接不送1接宠2送宠4接送宠)
  233. * @ApiReturnParams(name=has_cage,type=string,description=有无宠具)
  234. * @ApiReturnParams(name=weight,type=string,description=重量)
  235. * @ApiReturnParams(name=piece,type=string,description=件数)
  236. * @ApiReturnParams(name=remark,type=string,description=备注)
  237. * @ApiReturnParams(name=images,type=string,description=照片)
  238. * @ApiReturnParams(name=coupon_amount,type=string,description=优惠金额)
  239. * @ApiReturnParams(name=no,type=string,description=订单编号)
  240. * @ApiReturnParams(name=created_at,type=string,description=下单时间)
  241. * @ApiReturnParams(name=pay_type,type=string,description=支付方式1余额2微信)
  242. * @ApiReturnParams(name=protect_amount,type=string,description=安心保金额)
  243. * @ApiReturnParams(name=protect_max,type=string,description=安心保保多少)
  244. * @ApiReturnParams (name=status,description="0待支付1待接单10取宠中15运输中20已完成30已结算40已取消")
  245. * @ApiReturnParams (name=cancel_at,description="用户取消时间")
  246. * @ApiReturnParams (name=refund_at,description="退款时间")
  247. * @ApiReturnParams (name=agree_date,description="取宠时间日期")
  248. * @ApiReturnParams (name=agree_time,description="取宠具体时间")
  249. * @ApiReturnParams (name=freight,description="fast快车air空运special专车")
  250. * @ApiReturnParams (name=refund_status,description="-1未申请0审核中1审核通过2驳回")
  251. * @ApiReturnParams (name=refund_amount,description="退款金额")
  252. * @ApiReturnParams (name=refund_images,description="退款图片")
  253. * @ApiReturnParams (name=refund_reason,description="退款原因")
  254. * @ApiReturnParams (name=refund_at,description="退款时间")
  255. */
  256. public function detail(){
  257. $data=input();
  258. $this->validate($data,[
  259. 'id'=>['require','integer','gt:0'],
  260. ]);
  261. $user=$this->auth->getUser();
  262. $order=$user->orders()->findOrFail($data['id']);
  263. return $order;
  264. }
  265. /**
  266. * 托运详情
  267. * @ApiParams (name=id,decription=订单ID)
  268. * @ApiReturnParams (name=addr,description="取宠地址")
  269. * @ApiReturnParams (name=created_at,description="时间")
  270. * @ApiReturnParams (name=status,description="状态")
  271. * @ApiReturnParams (name=txt,description="描述")
  272. * @ApiReturnParams (name=media,description="视频")
  273. */
  274. public function send_detail(){
  275. $data=input();
  276. $this->validate($data,[
  277. 'id'=>['require']
  278. ]);
  279. $user=$this->auth->getUser();
  280. $order=$user->orders()->findOrFail($data['id']);
  281. $this->success('',$order->send_detail);
  282. }
  283. /**
  284. * 取消订单
  285. * @ApiParams (name=id,decription=订单ID)
  286. */
  287. public function cancel(){
  288. $data=input();
  289. $this->validate($data,[
  290. 'id'=>['require']
  291. ]);
  292. $user=$this->auth->getUser();
  293. Db::startTrans();
  294. $order=$user->orders()->lock(true)->findOrFail($data['id']);
  295. $order->cancel();
  296. Db::commit();
  297. $this->success();
  298. }
  299. /**
  300. * 继续支付
  301. * @ApiParams (name=id,decription=订单ID)
  302. */
  303. public function pay(){
  304. $data=input();
  305. $this->validate($data,[
  306. 'id'=>['require']
  307. ]);
  308. $user=$this->auth->getUser();
  309. Db::startTrans();
  310. $order=$user->orders()->lock(true)->findOrFail($data['id']);
  311. $params=$order->pay();
  312. Db::commit();
  313. $this->success('',$params);
  314. }
  315. /**
  316. * 确认完成
  317. * @ApiParams (name=id,decription=订单ID)
  318. */
  319. public function complete(){
  320. $data=input();
  321. $this->validate($data,[
  322. 'id'=>['require']
  323. ]);
  324. $user=$this->auth->getUser();
  325. Db::startTrans();
  326. $order=$user->orders()->lock(true)->findOrFail($data['id']);
  327. $params=$order->complete();
  328. Db::commit();
  329. $this->success('',$params);
  330. }
  331. /**
  332. * 获取申请退款信息
  333. * @ApiParams (name=id,decription=订单ID)
  334. */
  335. public function refund_info(){
  336. $data=input();
  337. $this->validate($data,[
  338. 'id'=>['require'],
  339. ]);
  340. $user=$this->auth->getUser();
  341. $order=$user->orders()->findOrFail($data['id']);
  342. $res=[];
  343. $res['fee']=[
  344. 'land'=>config('site.order_refund_land'),
  345. 'air'=>config('site.order_refund_air'),
  346. ];
  347. $res['refund_amount']=$order->refund_money;
  348. $this->success('',$res);
  349. }
  350. /**
  351. * 申请退款
  352. * @ApiParams (name=id,description=订单ID)
  353. * @ApiParams (name=reason,description=原因必须)
  354. * @ApiParams (name=images,description=图片可选)
  355. */
  356. public function refund(){
  357. $data=input();
  358. $this->validate($data,[
  359. 'id'=>['require'],
  360. 'reason'=>['require','max:200'],
  361. 'images'=>['array'],
  362. ]);
  363. $user=$this->auth->getUser();
  364. Db::startTrans();
  365. $order=$user->orders()->lock(true)->findOrFail($data['id']);
  366. $order->refund($data['reason'],$data['images']);
  367. Db::commit();
  368. $this->success();
  369. }
  370. /**
  371. * 删除订单
  372. * @ApiParams (name=id,decription=订单ID)
  373. */
  374. public function delete(){
  375. $data=input();
  376. $this->validate($data,[
  377. 'id'=>['require'],
  378. ]);
  379. $user=$this->auth->getUser();
  380. $order=$user->orders()->findOrFail($data['id']);
  381. $order->remove();
  382. $this->success();
  383. }
  384. }