CustomOrder.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\DeliveryAddress;
  4. use app\common\model\DepotGoodsItem;
  5. use app\common\model\GiftGoods;
  6. use app\common\model\GiftOrder;
  7. use app\common\model\GoodsOrderItem;
  8. use app\common\model\OrderUrge;
  9. use app\common\model\StoreGoodsItem;
  10. use app\common\model\User;
  11. use app\common\model\UserWallet;
  12. use app\common\service\OrderCallback;
  13. use EasyWeChat\Factory;
  14. use think\Db;
  15. /**
  16. * @title 定制订单
  17. * @controller CustomOrder
  18. * @group base
  19. */
  20. class CustomOrder extends Base
  21. {
  22. public function initialize()
  23. {
  24. parent::initialize();
  25. parent::checkLogin();
  26. }
  27. /**
  28. * @title 生成订单(立即购买)
  29. * @desc 立即购买场景
  30. * @author qc
  31. * @url /api/Custom_order/creatOrder
  32. * @method POST
  33. * @header name:Authorization require:1 desc:Token
  34. * @param name:goods_id type:int require:1 default:0 desc:商品id
  35. * @param name:spec_id type:int require:1 default:0 desc:规格id(item_list下面id)
  36. * @param name:gift_cate type:int require:1 default:0 desc:定制分类
  37. * @param name:num type:int default:1 desc:数量
  38. * @param name:title type:string default:1 desc:包装名称
  39. * @param name:logo type:string default:1 desc:logo
  40. * @param name:custom_cover type:string default:1 desc:风格(图片路径)
  41. * @param name:custom_color type:string default:1 desc:颜色(图片路径)
  42. * @param name:self_cover type:string default:0 desc:自定义图片(多张逗号分开,最多三张)
  43. * @param name:remark type:string default:1 desc:订单备注
  44. * @param name:add_id type:int default:1 desc:收货地址id
  45. * @return name:order_id type:int default:1 desc:订单id
  46. */
  47. public function creatOrder()
  48. {
  49. $goods_id = input('post.goods_id',0);
  50. $spec_id = input('post.spec_id',0);
  51. $num = input('post.num',0);
  52. $gift_cate = input('post.gift_cate',0);
  53. $title = input('post.title');
  54. $logo = input('post.logo');
  55. $custom_cover = input('post.custom_cover');
  56. $custom_color = input('post.custom_color');
  57. $self_cover = input('post.self_cover');
  58. $remark = input('post.remark');
  59. $add_id = input('post.add_id');
  60. if(!$add_id) $this->error('请选择收货地址');
  61. $add_info = DeliveryAddress::where('id',$add_id)->find();
  62. $add_info = $add_info ? $add_info->toArray(): ['pro_name'=>'','city_name'=>'','county_name'=>'','street_name'=>'','name'=>'','detail'=>'','phone'=>''];
  63. $goods_info = GiftGoods::field('s.*,g.name,g.cover,g.low_price')
  64. ->alias('s')
  65. ->leftJoin('StoreGoods g','g.id = s.goods_id')
  66. ->where(['s.status'=>1,'s.is_custom'=>1,'s.is_deleted'=>0,'g.is_deleted'=>0,'g.status'=>1,'s.goods_id'=>$goods_id,'s.cate_id'=>$gift_cate])
  67. ->find();
  68. if(!$goods_info) $this->error('商品已下架');
  69. // 规格详情
  70. $item_info = StoreGoodsItem::where('id',$spec_id)->with('depotItemInfo')->find()->toArray();
  71. // $item_info['stock'] = $item_info['depot_item_info']['stock'];
  72. if($item_info['stock'] < $num ) $this->error('商品库存不足');
  73. if($item_info['status'] == 0) $this->error('改规格已下架');
  74. if($item_info['status'] == 0 || $item_info['is_deleted'] == 1) $this->error('改规格已下架');
  75. $order_insert = [
  76. 'user_id' => $this->user_id,
  77. 'order_no' => get_order_sn(),
  78. 'goods_id' => $goods_id,
  79. 'spec_id' => $spec_id,
  80. 'goods_num' => $num,
  81. 'price_total' => bcmul($num,$item_info['sell_price']),
  82. 'price_goods' => bcmul($num,$item_info['sell_price']),
  83. 'pro_name' => $add_info['pro_name'],
  84. 'city_name' => $add_info['city_name'],
  85. 'county_name' => $add_info['county_name'],
  86. 'street_name' => $add_info['street_name'],
  87. 'user_name' => $add_info['name'],
  88. 'add_detail' => $add_info['detail'],
  89. 'phone' => $add_info['phone'],
  90. 'custom_cover' => $custom_cover,
  91. 'logo' => $logo,
  92. 'remark' => $remark,
  93. 'custom_color' => $custom_color,
  94. 'self_cover' => $self_cover,
  95. 'title' => $title,
  96. 'gift_cate' => $gift_cate,
  97. ];
  98. $res = GiftOrder::create($order_insert);
  99. DepotGoodsItem::where('id',$item_info['depot_item_info']['id'])->setDec('stock',$num);
  100. $this->success('定制订单已提交',['order_id'=>$res->id]);
  101. }
  102. /**
  103. * @title 获取订单列表
  104. * @desc 待审核:sh_status=0;待付款:sh_status=1&&status=0;待发货:status=1;待收货:status=3;
  105. * @author qc
  106. * @method GET
  107. * @url /api/Custom_order/getCustomOrderList
  108. * @header name:Authorization require:1 desc:Token
  109. * @param name:page type:int default:1 desc:页数
  110. * @param name:page_num type:int default:20 desc:每页数
  111. * @param name:status type:int default:-1 desc:状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  112. * @param name:sh_status type:int default:-1 desc:审核状态(0待审核1审核通过2审核拒绝)
  113. * @return name:id type:int default:-- desc:订单id
  114. * @return name:order_no type:string default:-- desc:订单号
  115. * @return name:price_total type:float default:-- desc:待支付金额(商品价格+运费+包装费)
  116. * @return name:price_goods type:float default:-- desc:商品金额
  117. * @return name:price_express type:float default:-- desc:运费金额
  118. * @return name:price_pack type:float default:-- desc:包装费
  119. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  120. * @return name:pay_type type:int default:-- desc:支付方式(1微信2余额3支付宝)
  121. * @return name:pay_no type:string default:-- desc:支付号
  122. * @return name:pay_at type:string default:-- desc:支付时间
  123. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  124. * @return name:cancel_at type:string default:-- desc:取消时间
  125. * @return name:cancel_desc type:string default:-- desc:取消原因
  126. * @return name:cancel_desc type:string default:-- desc:取消原因
  127. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  128. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  129. * @return name:express_send_no type:string default:-- desc:物流单号
  130. * @return name:express_send_at type:string default:-- desc:发货时间
  131. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  132. * @return name:create_at type:string default:-- desc:下单时间
  133. * @return name:remark type:string default:-- desc:订单备注
  134. * @return name:goods_id type:int default:-- desc:订单商品id
  135. * @return name:goods_num type:int default:-- desc:订单商品总数量
  136. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  137. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  138. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  139. * @return name:street_name type:string default:-- desc:街道名称(收货地址)
  140. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  141. * @return name:user_name type:string default:-- desc:收货人
  142. * @return name:phone type:string default:-- desc:收货人联系电话
  143. * @return name:goods_spec type:string default:-- desc:商品规格
  144. * @return name:sell_price type:string default:-- desc:售价(单价【不含包装】)
  145. * @return name:spec_title type:string default:-- desc:规格标题
  146. * @return name:goods type:array default:-- desc:订单商品
  147. * @return name:goods.name type:string default:-- desc:商品名
  148. * @return name:goods.cover type:string default:-- desc:商品封面
  149. */
  150. public function getCustomOrderList()
  151. {
  152. $status = input('get.status',-1);
  153. $sh_status = input('get.sh_status',-1);
  154. $where = [];
  155. $where[] = ['o.user_id','=',$this->user_id];
  156. if($status > -1) $where[] = ['o.status','=',$status];
  157. if($sh_status > -1) $where[] = ['o.sh_status','=',$sh_status];
  158. $list = GiftOrder::with('goods')
  159. ->field('o.*,i.goods_spec,i.sell_price,i.spec_title')
  160. ->leftJoin('StoreGoodsItem i','i.id = o.spec_id')
  161. ->alias('o')
  162. ->where($where)->order('id desc')
  163. ->limit($this->off_set,$this->page_num)
  164. ->select()->toArray();
  165. $this->success('',['list'=>$list]);
  166. }
  167. /**
  168. * @title 获取订单详情
  169. * @desc 获取订单详情
  170. * @author qc
  171. * @method GET
  172. * @url /api/Custom_order/getCustomOrderDetail
  173. * @header name:Authorization require:1 desc:Token
  174. * @param name:order_id type:int default:1 desc:订单id
  175. * @return name:id type:int default:-- desc:订单id
  176. * @return name:order_no type:string default:-- desc:订单号
  177. * @return name:price_total type:float default:-- desc:待支付金额(商品价格+运费+包装费)
  178. * @return name:price_goods type:float default:-- desc:商品金额
  179. * @return name:price_express type:float default:-- desc:运费金额
  180. * @return name:price_pack type:float default:-- desc:包装费
  181. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  182. * @return name:pay_type type:int default:-- desc:支付方式(1微信2余额3支付宝)
  183. * @return name:pay_no type:string default:-- desc:支付号
  184. * @return name:pay_at type:string default:-- desc:支付时间
  185. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  186. * @return name:cancel_at type:string default:-- desc:取消时间
  187. * @return name:cancel_desc type:string default:-- desc:取消原因
  188. * @return name:cancel_desc type:string default:-- desc:取消原因
  189. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  190. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  191. * @return name:express_send_no type:string default:-- desc:物流单号
  192. * @return name:express_send_at type:string default:-- desc:发货时间
  193. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  194. * @return name:create_at type:string default:-- desc:下单时间
  195. * @return name:remark type:string default:-- desc:订单备注
  196. * @return name:goods_id type:int default:-- desc:订单商品id
  197. * @return name:goods_num type:int default:-- desc:订单商品总数量
  198. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  199. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  200. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  201. * @return name:street_name type:string default:-- desc:街道名称(收货地址)
  202. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  203. * @return name:user_name type:string default:-- desc:收货人
  204. * @return name:phone type:string default:-- desc:收货人联系电话
  205. * @return name:goods type:array default:-- desc:订单商品
  206. * @return name:goods.name type:string default:-- desc:商品名
  207. * @return name:goods.cover type:string default:-- desc:商品封面
  208. * @return name:goods_spec type:string default:-- desc:商品规格
  209. * @return name:sell_price type:string default:-- desc:售价(单价【不含包装】)
  210. * @return name:spec_title type:string default:-- desc:规格标题
  211. */
  212. public function getCustomOrderDetail()
  213. {
  214. $order_id =input('get.order_id');
  215. $detail = GiftOrder::with(['goods'])->where('id',$order_id)->find()->toArray();
  216. $item = StoreGoodsItem::field('goods_spec,sell_price,spec_title')->where('id',$detail['spec_id'])->find()->toArray();
  217. $detail['goods_spec'] = $item['goods_spec'];
  218. $detail['sell_price'] = $item['sell_price'];
  219. $detail['spec_title'] = $item['spec_title'];
  220. $this->success('',['detail'=>$detail]);
  221. }
  222. /**
  223. * @title 取消订单
  224. * @desc 取消订单
  225. * @author qc
  226. * @url /api/Custom_order/cancelOrder
  227. * @method POST
  228. * @header name:Authorization require:1 desc:Token
  229. * @param name:order_id type:int require:1 default:0 desc:订单id
  230. * @param name:cancel_desc type:string require:0 default:0 desc:取消描述
  231. */
  232. public function cancelOrder()
  233. {
  234. $order_id = input('post.order_id');
  235. $cancel_desc = input('post.cancel_desc');
  236. $order_info = GiftOrder::where('id',$order_id)->find();
  237. if($order_info['status'] != 0) $this->error('订单已支付');
  238. if($order_info['is_deleted'] != 0) $this->error('订单不存在');
  239. if($order_info['cancel_state'] != 0) $this->error('订单已取消');
  240. $order_info->cancel_state = 1;
  241. $order_info->status = 9;
  242. $order_info->cancel_at = date('Y-m-d H:i:s');
  243. $order_info->cancel_desc = $cancel_desc;
  244. $order_info->save();
  245. $this->success('订单已取消');
  246. }
  247. /**
  248. * @title 删除订单
  249. * @desc 删除订单
  250. * @author qc
  251. * @url /api/Custom_order/deleteOrder
  252. * @method POST
  253. * @header name:Authorization require:1 desc:Token
  254. * @param name:order_id type:int require:1 default:0 desc:订单id
  255. */
  256. public function deleteOrder()
  257. {
  258. $order_id = input('post.order_id');
  259. $detail = GiftOrder::where('id',$order_id)->find()->toArray();
  260. if(!in_array($detail['status'],[0,9])) $this->error('订单已支付');
  261. GiftOrder::update(['is_deleted'=>1],['id'=>$order_id]);
  262. $this->success('删除成功');
  263. }
  264. /**
  265. * @title 订单支付
  266. * @desc 订单支付
  267. * @author qc
  268. * @url /api/Custom_order/payOrder
  269. * @method POST
  270. * @header name:Authorization require:1 desc:Token
  271. * @param name:order_id type:int require:1 default:0 desc:订单id
  272. * @param name:pay_type type:int require:1 default:0 desc:支付方式
  273. */
  274. public function payOrder()
  275. {
  276. $order_id = input('post.order_id');
  277. $pay_type = input('post.pay_type');
  278. $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>''];
  279. Db::startTrans();
  280. try {
  281. $order_info = GiftOrder::where('id',$order_id)->find()->toArray();
  282. if($order_info['status'] != 0) $this->exception('订单状态错误');
  283. if($order_info['sh_status'] == 0) $this->exception('订单未审核');
  284. if($order_info['sh_status'] == 2) $this->exception('审核未通过');
  285. $pay_no = $order_info['pay_no'] ? $order_info['pay_no']:get_order_sn();// 支付单号
  286. if(!$order_info['pay_no']) GiftOrder::where('id',$order_id)->update(['pay_no'=>$pay_no]);
  287. $user_info = User::with('wallet')->where('id',$this->user_id)->find()->toArray();
  288. switch ($pay_type){
  289. case 1://微信小程序
  290. $notify_url = $this->request->root(true) . '/api/we_chat_pay/customOrderNotify';
  291. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  292. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  293. $ret_data['config'] = $pay_config['config'];
  294. break;
  295. case 2://佣金余额
  296. if($user_info['wallet']['money'] < $order_info['price_total'])$this->exception('余额不足');
  297. // 扣除余额
  298. UserWallet::userMoneyChange($this->user_id,$order_info['price_total'],'订单支付',1,-1,$order_id);
  299. // 支付完成后回调
  300. $back_res = OrderCallback::goodsOrderCallBack($order_info,$pay_type);
  301. if(!$back_res['ret_val']) $this->exception($back_res['msg']);
  302. $ret_data['pay_status'] = 1;
  303. break;
  304. case 3://移动支付(支付宝)
  305. break;
  306. case 4: //4移动支付(微信)
  307. $notify_url = $this->request->root(true) . '/api/we_chat_pay/customOrderNotify';
  308. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP',$user_info['openid']);
  309. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  310. $ret_data['config'] = $pay_config['config'];
  311. break;
  312. case 5: //pc(微信扫码)
  313. $notify_url = $this->request->root(true) . '/api/we_chat_pay/customOrderNotify';
  314. $app = Factory::payment(config('app.wx_pay'));
  315. $result = $app->order->unify([
  316. 'body' => '微信扫码支付',
  317. 'out_trade_no' => $pay_no,
  318. 'total_fee' => $order_info['price_total'] * 100,
  319. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  320. 'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
  321. ]);
  322. if(!isset($result['code_url']))$this->exception('获取微信付款码失败');
  323. $ret_data['code_url'] = $result['code_url'];
  324. break;
  325. case 6: //pc(支付宝扫码)
  326. break;
  327. }
  328. Db::commit();
  329. }catch (\Exception $e){
  330. $this->is_commit = false;
  331. $this->ret_msg = $e->getMessage();
  332. Db::rollback();
  333. }
  334. $this->transReturn($ret_data);
  335. }
  336. /**
  337. * @title 确认收货
  338. * @desc 确认收货
  339. * @author qc
  340. * @url /api/Custom_order/confirmReceipt
  341. * @method POST
  342. * @header name:Authorization require:1 desc:Token
  343. * @param name:order_id type:int require:1 default:0 desc:订单id
  344. */
  345. public function confirmReceipt()
  346. {
  347. $order_id = input('post.order_id');
  348. $detail = GiftOrder::where('id',$order_id)->find()->toArray();
  349. if($detail['status'] == 0) $this->error('订单未支付');
  350. if($detail['status'] == 1) $this->error('订单未发货');
  351. if($detail['status'] == 3) $this->error('订单已收货');
  352. GiftOrder::update(['status'=>3],['id'=>$order_id]);
  353. $this->success('已确认收货');
  354. }
  355. /**
  356. * @title 催促发货
  357. * @desc 催促发货
  358. * @author qc
  359. * @url /api/Custom_order/urgeExpress
  360. * @method POST
  361. * @header name:Authorization require:1 desc:Token
  362. * @param name:order_id type:int require:1 default:0 desc:订单id
  363. */
  364. public function urgeExpress()
  365. {
  366. $order_id = input('post.order_id');
  367. $detail = GiftOrder::where('id',$order_id)->find()->toArray();
  368. if($detail['status'] == 1) $this->error('订单状态有误');
  369. $check_urge = OrderUrge::where(['order_table'=>'gift_order','order_id'=>$order_id,'day'=>date('Y-m-d')])->count();
  370. if($check_urge) $this->error('今天已经催发过了...');
  371. OrderUrge::create(['order_id'=>$order_id,'user_id'=>$this->user_id,'order_table'=>'gift_order','day'=>date('Y-m-d')]);
  372. $this->success('催发成功');
  373. }
  374. }