ExchangeOrder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\ExchangeGoods;
  4. use app\common\service\OrderCallback;
  5. use app\common\model\ExchangeOrder as EOM;
  6. use app\common\model\DeliveryAddress;
  7. use app\common\model\GoodsOrder;
  8. use app\common\model\User;
  9. use app\common\model\UserWallet;
  10. use think\Db;
  11. use think\Exception;
  12. /**
  13. * @title 兑换订单
  14. * @controller ExchangeOrder
  15. * @group base
  16. */
  17. class ExchangeOrder extends Base
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize();
  22. parent::checkLogin();
  23. }
  24. /**
  25. * @title 生成订单(立即购买)
  26. * @desc 立即购买场景
  27. * @author qc
  28. * @url /api/Exchange_order/createOrderImmediately
  29. * @method POST
  30. * @header name:Authorization require:1 desc:Token
  31. * @param name:goods_id type:int require:1 default:0 desc:商品id
  32. * @param name:num type:int default:1 desc:数量
  33. * @param name:add_id type:int default:1 desc:收货地址id
  34. * @return name:order_id type:int default:1 desc:订单id
  35. */
  36. public function createOrderImmediately()
  37. {
  38. $goods_id = input('post.goods_id');
  39. $num = input('post.num');
  40. $add_id = input('post.add_id');
  41. if(!$goods_id ) $this->error('请选择商品');
  42. if($num <=0 ) $this->error('数量有误');
  43. if(!$add_id ) $this->error('请选择收货地址');
  44. Db::startTrans();
  45. try {
  46. // 商品详情
  47. $goods_info = ExchangeGoods::where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
  48. if(!$goods_info) throw new Exception('商品已下架');
  49. $goods_info = $goods_info->toArray();
  50. if($goods_info['stock'] < $num) throw new Exception('库存不足');
  51. $integral = UserWallet::where('user_id',$this->user_id)->value('integral');
  52. $need_integral = $goods_info['integral'] * $num;
  53. if($integral < $need_integral) throw new Exception('积分余额不足');
  54. $add_info = DeliveryAddress::find($add_id)->toArray();
  55. $price_goods = bcmul($num,$goods_info['sell_price'],2); // 需要支付价格
  56. $price_express = 0; // 快递费用
  57. $order_insert = [
  58. 'user_id' => $this->user_id,
  59. 'goods_id' => $goods_id,
  60. 'goods_name' => $goods_info['name'],
  61. 'goods_cover' => $goods_info['cover'],
  62. 'order_no' => get_order_sn(),
  63. 'price_total' => $price_goods + $price_express,
  64. 'price_goods' => $price_goods,
  65. 'price_express' => $price_express,
  66. 'goods_num' => $num,
  67. 'integral' => $need_integral,
  68. 'pro_name' => $add_info['pro_name'],
  69. 'city_name' => $add_info['city_name'],
  70. 'county_name' => $add_info['county_name'],
  71. 'street_name' => $add_info['street_name'],
  72. 'user_name' => $add_info['name'],
  73. 'add_detail' => $add_info['detail'],
  74. 'phone' => $add_info['phone'],
  75. ];
  76. $order_info = EOM::create($order_insert);
  77. ExchangeGoods::where('id',$goods_id)->setDec('stock',$num);
  78. if($need_integral) UserWallet::userIntegralChange($this->user_id,$need_integral,'兑换商品',1,-1,$order_info['id']);
  79. Db::commit();
  80. }catch (\Exception $e){
  81. $this->is_commit = false;
  82. $this->ret_msg = $e->getMessage();
  83. Db::rollback();
  84. }
  85. $this->is_commit ? $this->success('添加成功',['order_id'=>$order_info->id]):$this->error($this->ret_msg);
  86. }
  87. /**
  88. * @title 兑换订单支付
  89. * @desc 兑换订单支付
  90. * @author qc
  91. * @url /api/Exchange_order/orderPay
  92. * @method POST
  93. * @header name:Authorization require:1 desc:Token
  94. * @param name:order_id type:int require:1 default:-- desc:订单id
  95. * @param name:pay_type type:int require:1 default:1 desc:支付方式1微信2余额3支付宝
  96. * @return name:pay_status type:array default:0 desc:支付状态(0待支付,1支付成功,【余额支付或金额为0不需要吊起支付】直接完成订单状态)
  97. * @return name:config type:array default:0 desc:支付配置
  98. * @return name:config.appId type:string default:-- desc:公众账号ID(pay_type=1)
  99. * @return name:config.nonceStr type:string default:-- desc:随机字符串(pay_type=1)
  100. * @return name:config.signType type:string default:-- desc:签名类型(pay_type=1)
  101. * @return name:config.paySign type:string default:-- desc:签名(pay_type=1)
  102. * @return name:config.timestamp type:string default:-- desc:时间戳(pay_type=1)
  103. */
  104. public function orderPay()
  105. {
  106. $order_id = input('post.order_id');
  107. $pay_type = input('post.pay_type',1);
  108. Db::startTrans();
  109. $ret_data = ['pay_status'=>0,'config'=>[]];
  110. try{
  111. $order_info = EOM::where('id',$order_id)->find()->toArray();
  112. if($order_info['status'] !=0 ) $this->exception('订单状态有误');
  113. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  114. EOM::where('id',$order_id)->update(['pay_no'=>$pay_no]);
  115. if($order_info['price_total'] > 0) {
  116. switch ($pay_type){
  117. case 1:
  118. $notify_url = $this->request->root(true) . '/api/WeChatPay/exchangeOrderNotify';
  119. $user_info = User::where('id',$this->user_id)->find()->toArray();
  120. $config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  121. if(!$config) $this->exception('获取微信支付配置失败');
  122. break;
  123. case 2:
  124. $money = UserWallet::where('user_id',$this->user_id)->value('money');
  125. if($money < $order_info['price_total']) $this->exception('佣金余额不足!');
  126. UserWallet::userMoneyChange($this->user_id,$order_info['price_total'],'佣金支付',1,-1,$order_id);
  127. $pay_res = OrderCallback::exchangeOrderCallBack($order_info,$pay_type);
  128. Db::commit();
  129. $ret_data=['pay_status'=>1,'config'=>[]];
  130. $this->ret_msg = '支付成功';
  131. break;
  132. case 3:
  133. // 支付宝支付
  134. break;
  135. }
  136. }else{
  137. OrderCallback::exchangeOrderCallBack($order_info,$pay_type);
  138. Db::commit();
  139. $ret_data=['pay_status'=>1,'config'=>[]];
  140. $this->ret_msg = '支付成功';
  141. }
  142. }catch (\Exception $e){
  143. $this->ret_msg = $e->getMessage();
  144. $this->is_commit = false;
  145. Db::rollback();
  146. }
  147. $this->transReturn($ret_data);
  148. }
  149. /**
  150. * @title 获取订单列表
  151. * @desc 获取订单列表
  152. * @author qc
  153. * @method GET
  154. * @url /api/Exchange_order/getOrderList
  155. * @header name:Authorization require:1 desc:Token
  156. * @param name:page type:int default:1 desc:页数
  157. * @param name:page_num type:int default:20 desc:每页数
  158. * @param name:status type:int default:-1 desc:状态
  159. * @return name:goods_id type:int default:-- desc:商品id
  160. * @return name:goods_name type:string default:-- desc:商品名称
  161. * @return name:goods_cover type:string default:--desc:商品封面
  162. * @return name:order_no type:string default:-- desc:订单号
  163. * @return name:price_total type:float default:-- desc:待支付金额(含运费)
  164. * @return name:price_goods type:float default:-- desc:商品金额(不含运费)
  165. * @return name:price_express type:float default:-- desc:运费
  166. * @return name:pay_state type:int default:0 desc:支付状态(0未支付,1已支付)
  167. * @return name:pay_type type:int default:0 desc:支付方式(1微信2余额3支付宝)
  168. * @return name:status type:int default:0 desc:订单状态--0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,,9取消
  169. * @return name:pay_no type:string default:-- desc:支付单号
  170. * @return name:pay_at type:string default:-- desc:支付时间
  171. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消,1已取消)
  172. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  173. * @return name:express_company_id type:int default:-- desc:快递公司id
  174. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  175. * @return name:express_send_no type:string default:-- desc:发货单号
  176. * @return name:express_send_at type:string default:-- desc:发货时间
  177. * @return name:remark type:string default:-- desc:订单备注
  178. * @return name:pro_name type:string default:-- desc:收货省
  179. * @return name:city_name type:string default:-- desc:收货市
  180. * @return name:county_name type:string default:-- desc:收货区
  181. * @return name:street_name type:string default:-- desc:收货街道(乡镇)
  182. * @return name:add_detail type:string default:-- desc:收货详细地址
  183. * @return name:user_name type:string default:-- desc:收货人
  184. * @return name:phone type:string default:-- desc:收货人电话
  185. */
  186. public function getOrderList()
  187. {
  188. $status = input('get.status',-1);
  189. $where = [];
  190. $where[] = ['user_id','=',$this->user_id];
  191. if($status > -1) $where[] = ['status','=',$status];
  192. $list = EOM::where($where)->order('id desc ')->limit($this->off_set,$this->page_num)->select()->toArray();
  193. $this->success('',['list'=>$list]);
  194. }
  195. /**
  196. * @title 获取订单详情
  197. * @desc 获取订单详情
  198. * @author qc
  199. * @method GET
  200. * @url /api/Exchange_order/getOrderDetail
  201. * @header name:Authorization require:1 desc:Token
  202. * @param name:order_id type:int default:-- desc:订单id
  203. * @return name:goods_id type:int default:-- desc:商品id
  204. * @return name:goods_name type:string default:-- desc:商品名称
  205. * @return name:goods_cover type:string default:--desc:商品封面
  206. * @return name:order_no type:string default:-- desc:订单号
  207. * @return name:price_total type:float default:-- desc:待支付金额(含运费)
  208. * @return name:price_goods type:float default:-- desc:商品金额(不含运费)
  209. * @return name:price_express type:float default:-- desc:运费
  210. * @return name:pay_state type:int default:0 desc:支付状态(0未支付,1已支付)
  211. * @return name:pay_type type:int default:0 desc:支付方式(1微信2余额3支付宝)
  212. * @return name:status type:int default:0 desc:订单状态--0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,,9取消
  213. * @return name:pay_no type:string default:-- desc:支付单号
  214. * @return name:pay_at type:string default:-- desc:支付时间
  215. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消,1已取消)
  216. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  217. * @return name:express_company_id type:int default:-- desc:快递公司id
  218. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  219. * @return name:express_send_no type:string default:-- desc:发货单号
  220. * @return name:express_send_at type:string default:-- desc:发货时间
  221. * @return name:remark type:string default:-- desc:订单备注
  222. * @return name:pro_name type:string default:-- desc:收货省
  223. * @return name:city_name type:string default:-- desc:收货市
  224. * @return name:county_name type:string default:-- desc:收货区
  225. * @return name:street_name type:string default:-- desc:收货街道(乡镇)
  226. * @return name:add_detail type:string default:-- desc:收货详细地址
  227. * @return name:user_name type:string default:-- desc:收货人
  228. * @return name:phone type:string default:-- desc:收货人电话
  229. */
  230. public function getOrderDetail()
  231. {
  232. $order_id = input('get.order_id');
  233. $detail = EOM::where('id',$order_id)->find()->toArray();
  234. $this->success('ok',['detail'=>$detail]);
  235. }
  236. /**
  237. * @title 取消订单
  238. * @desc 取消订单
  239. * @author qc
  240. * @url /api/Exchange_order/cancelOrder
  241. * @method POST
  242. * @header name:Authorization require:1 desc:Token
  243. * @param name:order_id type:int require:1 default:0 desc:订单id
  244. */
  245. public function cancelOrder()
  246. {
  247. $order_id = input('post.order_id');
  248. Db::startTrans();
  249. try {
  250. $detail = EOM::where('id',$order_id)->find()->toArray();
  251. if($detail['status'] != 0) $this->exception('订单状态有误');
  252. // 商品库存
  253. ExchangeGoods::where('id',$detail['goods_id'])->setInc('stock',$detail['goods_num']);
  254. // 取消订单状态
  255. GoodsOrder::update(['status'=>9],['id'=>$order_id]);
  256. // 积分退回
  257. if($detail['integral']) UserWallet::userIntegralChange($detail['user_id'],$detail['integral'],'订单取消退回',2,1,$order_id);
  258. Db::commit();
  259. }catch (\Exception $e){
  260. $this->ret_msg = $e->getMessage();
  261. $this->is_commit = false;
  262. Db::rollback();
  263. }
  264. $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
  265. }
  266. /**
  267. * @title 删除订单
  268. * @desc 删除订单
  269. * @author qc
  270. * @url /api/Exchange_order/deleteOrder
  271. * @method POST
  272. * @header name:Authorization require:1 desc:Token
  273. * @param name:order_id type:int require:1 default:0 desc:订单id
  274. */
  275. public function deleteOrder()
  276. {
  277. $order_id = input('post.order_id');
  278. $detail = EOM::where('id',$order_id)->find()->toArray();
  279. if($detail['status'] == 0) $this->error('请先取消订单');
  280. GoodsOrder::update(['is_deleted'=>1],['id'=>$order_id]);
  281. $this->success('删除成功');
  282. }
  283. /**
  284. * @title 更换订单收货地址
  285. * @desc 更换订单收货地址(仅待支付订单可以修改地址)
  286. * @author qc
  287. * @url /api/Exchange_order/changeOrderDelivery
  288. * @method POST
  289. * @header name:Authorization require:1 desc:Token
  290. * @param name:order_id type:int require:1 default:0 desc:订单id
  291. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  292. */
  293. public function changeOrderDelivery()
  294. {
  295. $order_id = input('post.order_id');
  296. $add_id = input('post.add_id');
  297. $detail = EOM::where('id',$order_id)->find()->toArray();
  298. if($detail['status'] != 0) $this->error('操作错误');
  299. $add_info = DeliveryAddress::find($add_id)->toArray();
  300. $order_update =[
  301. 'pro_name' => $add_info['pro_name'],
  302. 'city_name' => $add_info['city_name'],
  303. 'county_name' => $add_info['county_name'],
  304. 'street_name' => $add_info['street_name'],
  305. 'add_detail' => $add_info['detail'],
  306. 'user_name' => $add_info['name'],
  307. 'phone' => $add_info['phone'],
  308. ];
  309. EOM::update($order_update,['id'=>$order_id]);
  310. $this->success('修改成功');
  311. }
  312. /**
  313. * @title 确认收货
  314. * @desc 确认收货
  315. * @author qc
  316. * @url /api/Exchange_order/confirmReceipt
  317. * @method POST
  318. * @header name:Authorization require:1 desc:Token
  319. * @param name:order_id type:int require:1 default:0 desc:订单id
  320. */
  321. public function confirmReceipt()
  322. {
  323. $order_id = input('post.order_id');
  324. $detail =EOM::where('id',$order_id)->find()->toArray();
  325. if($detail['status'] == 0) $this->error('订单未支付');
  326. if($detail['status'] == 1) $this->error('订单未发货');
  327. if($detail['status'] == 3) $this->error('订单已收货');
  328. EOM::update(['status'=>3],['id'=>$order_id]);
  329. $this->success('已确认收货');
  330. }
  331. }