ExchangeOrder.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. /**
  6. * @title 兑换订单
  7. * @controller ExchangeOrder
  8. * @group base
  9. */
  10. class ExchangeOrder extends Base
  11. {
  12. public function initialize()
  13. {
  14. parent::initialize();
  15. parent::check_login();
  16. }
  17. /**
  18. * @title 订单确认
  19. * @desc 订单确认
  20. * @author qc
  21. * @url /api/Exchange_order/orderConfirm
  22. * @method GET
  23. * @header name:Authorization require:1 desc:Token
  24. * @param name:goods_id type:int require:1 default:0 desc:商品id
  25. * @param name:num type:int require:1 default:1 desc:商品数量
  26. * @return name:add_info type:array require:0 default:-- desc:默认地址
  27. * @return name:add_info.id type:int default:1 desc:收货地址记录id
  28. * @return name:add_info.pro_id type:int default:-- desc:省id
  29. * @return name:add_info.city_id type:int default:-- desc:市id
  30. * @return name:add_info.county_id type:int default:-- desc:县区id
  31. * @return name:add_info.pro_name type:string default:-- desc:省名
  32. * @return name:add_info.city_name type:string default:-- desc:市名
  33. * @return name:add_info.county_name type:string default:-- desc:县区名
  34. * @return name:add_info.detail type:string default:-- desc:详细地址
  35. * @return name:add_info.phone type:string default:-- desc:联系电话
  36. * @return name:add_info.name type:string default:-- desc:联系人
  37. * @return name:goods_data type:array require:0 default:-- desc:商品详情
  38. * @return name:goods_data.id type:array require:0 default:-- desc:商品id
  39. * @return name:goods_data.stock type:string require:0 default:-- desc:剩余库存
  40. * @return name:goods_data.name type:string require:0 default:-- desc:名称
  41. * @return name:goods_data.cover type:string require:0 default:-- desc:图片
  42. * @return name:goods_data.crystal type:string require:0 default:-- desc:水晶
  43. * @return name:freight_data type:array default:-- desc:运费详情
  44. * @return name:freight_data.all_freight type:int default:-- desc:总运费
  45. * @return name:freight_data.first_freight type:int default:-- desc:首件运费
  46. * @return name:freight_data.later_freight type:int default:-- desc:续件运费
  47. * @return name:coupon_list type:array default:-- desc:可用运费券
  48. * @return name:coupon_list.id type:int default:-- desc:券id
  49. * @return name:coupon_list.past_at type:int default:-- desc:过期时间
  50. */
  51. public function orderConfirm(){
  52. $goods_id = input('get.goods_id',0);
  53. $num = input('get.num',1);
  54. $goods_info = Db::table('exchange_goods')->field('id,name,cover,status,stock,crystal')
  55. ->where(['id'=>$goods_id,'is_deleted'=>0])->find();
  56. // if($goods_info['status'] != 1) $this->error('商品已下架');
  57. // if($goods_info['stock'] < $num) $this->error('商品库存不足');
  58. // 默认收货地址
  59. $add_info = Db::table('delivery_address')->where(['user_id'=>$this->uid,'is_mr'=>1])->find();
  60. $freight_data = get_freight($num);// 计算运费
  61. // 运费券
  62. $coupon_list = Db::table('user_coupon_list')
  63. ->field('id,past_at')
  64. ->where('user_id','=',$this->uid)
  65. ->where('status','=',1)
  66. ->where('past_at','> time',date('Y-m-d H:i:s'))
  67. ->order('id desc')
  68. ->select();
  69. $this->success('ok',['add_info'=> $add_info ,'goods_data'=>$goods_info,'freight_data'=>$freight_data,'coupon_list'=>$coupon_list]);
  70. }
  71. /**
  72. * @title 生成订单(立即购买 )
  73. * @desc 生成订单(立即购买 )
  74. * @author qc
  75. * @url /api/Exchange_order/createOrderByImmediately
  76. * @method POST
  77. * @header name:Authorization require:1 desc:Token
  78. * @param name:goods_id type:int require:1 default:-- desc:商品id
  79. * @param name:num type:int require:1 default:1 desc:商品数量
  80. * @param name:price_total type:float require:1 default:-- desc:支付金额
  81. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  82. * @param name:coupon_id type:int require:0 default:0 desc:券id
  83. * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
  84. */
  85. public function createOrderByImmediately()
  86. {
  87. $goods_id = input("post.goods_id",0);
  88. $num = input("post.num",1);
  89. $add_id = input("post.add_id",0);
  90. $coupon_id = input("post.coupon_id",0);
  91. if(!$add_id) $this->error('请选择收货地址');
  92. $goods_info = Db::table('exchange_goods')->field('id,name,cover,status,stock,crystal')
  93. ->where(['id'=>$goods_id,'is_deleted'=>0])->find();
  94. if(empty($goods_info)) $this->error('商品已下架');
  95. if($goods_info['status'] != 1) $this->error('商品已下架');
  96. if($goods_info['stock'] < $num) $this->error('商品库存不足');
  97. if($coupon_id) {
  98. $coupon_info = Db::table('user_coupon_list')
  99. ->where('id','=',$coupon_id)
  100. ->where('past_at','> = ',date('Y-m-d H:i:s'))
  101. ->find();
  102. if(!$coupon_info || $coupon_info['status'] != 1 ) $this->error('运费券信息有误');
  103. }
  104. $pro_info =[
  105. 'goods_id' => $goods_id,
  106. 'name' => $goods_info['name'],
  107. 'cover' => $goods_info['cover'],
  108. 'num' => $num,
  109. 'crystal' => $goods_info['crystal'],
  110. ];
  111. $freight = get_freight($num); // 运费
  112. $price_total = $coupon_id ? $freight['all_freight'] - $freight['first_freight']:$freight['all_freight'] ;
  113. $order_int =[
  114. 'uid'=>$this->uid,
  115. 'order_no' => get_order_sn(),
  116. 'price_total' => $price_total,
  117. 'goods_num' => $num,
  118. 'pro_info' => json_encode($pro_info),
  119. 'create_at' =>date('Y-m-d H:i:s'),
  120. 'freight' =>$freight['all_freight'],
  121. 'cl_id' =>$coupon_id,
  122. 'address_id' =>$add_id,
  123. 'goods_id' => $goods_id,
  124. 'goods_name' => $goods_info['name'],
  125. 'goods_cover' => $goods_info['cover'],
  126. 'crystal' => bcmul($goods_info['crystal'],$num,2),
  127. ];
  128. $user_info =Db::table('store_member')->find($this->uid);
  129. if(bcadd($user_info['crystal'],$user_info['crystal_cash'],2) < $order_int['crystal'])$this->error('水晶余额不足!');
  130. Db::startTrans();
  131. $msg= '';$com=true;
  132. try{
  133. //扣减库存
  134. Db::table('exchange_goods')->where('id',$goods_id)->setDec('stock',$num);
  135. // 扣除余额
  136. $user_up = [];
  137. if($user_info['crystal'] >= $order_int['crystal'] ) {
  138. $user_up['crystal'] = bcsub($user_info['crystal'],$order_int['crystal'],2);
  139. }else{
  140. $user_up['crystal'] = 0;
  141. $user_up['crystal_cash'] = bcsub( $user_info['crystal_cash'] + $user_info['crystal'],$order_int['crystal'],2);
  142. }
  143. Db::table('store_member')->where(['id'=>$this->uid])->update($user_up);
  144. if($coupon_id) Db::table('user_coupon_list')->where('id',$coupon_id)->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
  145. // 生成订单
  146. Db::table('exchange_order')->insert($order_int);
  147. $order_id = Db::getLastInsID();
  148. crystal_log($this->uid,$order_int['crystal']*-1,'水晶兑换',5,$order_id);
  149. Db::commit();
  150. }catch (\Exception $e){
  151. Db::rollback();
  152. $com=false;
  153. $msg = $e->getMessage();
  154. }
  155. if($com){
  156. $this->success('ok',['order_id'=>$order_id]);
  157. }else{
  158. $this->error($msg);
  159. }
  160. }
  161. /**
  162. * @title 获取订单详情
  163. * @desc 获取订单详情
  164. * @author qc
  165. * @url /api/Exchange_order/getOrderDetail
  166. * @method GET
  167. * @header name:Authorization require:1 desc:Token
  168. * @param name:id type:int require:1 default:-- desc:订单id
  169. * @return name:order_no type:string default:-- desc:订单号
  170. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  171. * @return name:price_total type:float default:-- desc:订单金额(实际支付金额)
  172. * @return name:freight type:float default:-- desc:运费
  173. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  174. * @return name:pay_no type:string default:-- desc:支付单号
  175. * @return name:pay_time type:string default:-- desc:支付时间
  176. * @return name:create_at type:string default:-- desc:订单创建时间
  177. * @return name:cancel_state type:int default:-- desc:取消状态
  178. * @return name:goods_id type:int default:-- desc:商品id
  179. * @return name:goods_name type:string default:-- desc:商品名
  180. * @return name:goods_cover type:string default:-- desc:商品图片
  181. * @return name:goods_num type:int default:-- desc:商品数量
  182. * @return name:express_company_title type:string default:-- desc:物流公司
  183. * @return name:express_send_no type:string default:-- desc:物流单号
  184. * @return name:express_send_at type:string default:-- desc:发货时间
  185. * @return name:express_company_id type:string default:-- desc:物流公司id
  186. * @return name:address_area type:string default:0 desc:收货地区(精确到县区)
  187. * @return name:address_detail type:string default:0 desc:详细地址
  188. * @return name:address_user type:string default:0 desc:收货人姓名
  189. * @return name:address_tel type:string default:0 desc:收货人电话
  190. */
  191. public function getOrderDetail()
  192. {
  193. $order_id = input('id',0);
  194. $detail = Db::table('exchange_order')->find($order_id);
  195. $add_info = Db::table('delivery_address')->find($detail['address_id']);
  196. $detail['address_area'] = $add_info['mer_name'];
  197. $detail['address_detail'] = $add_info['detail'];
  198. $detail['address_user'] = $add_info['name'];
  199. $detail['address_tel'] = $add_info['phone'];
  200. $this->success('获取成功',$detail);
  201. }
  202. /**
  203. * @title 去支付
  204. * @desc 去支付
  205. * @author qc
  206. * @url /api/Exchange_order/payOrder
  207. * @method POST
  208. * @header name:Authorization require:1 desc:Token
  209. * @param name:o_id type:int require:1 default:-- desc:订单id
  210. * @return name:pay_type type:int require:1 default:-- desc:1纯兑换,不支付,2微信支付
  211. * @return name:config type:array default:0 desc:支付配置pay_type=1时忽略
  212. * @return name:config.appId type:string default:-- desc:公众账号ID
  213. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  214. * @return name:config.signType type:string default:-- desc:签名类型
  215. * @return name:config.paySign type:string default:-- desc:签名
  216. * @return name:config.timestamp type:string default:-- desc:时间戳
  217. */
  218. public function payOrder()
  219. {
  220. $o_id = input('post.o_id');
  221. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  222. if(!$user_info['openid']) $this->error('请先完善用户信息');
  223. $order_info = Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
  224. if(empty($order_info)) $this->error('订单不存在');
  225. if($order_info['cancel_state'] != 0) $this->error('订单已取消');
  226. if($order_info['status'] != 0) $this->error('订单已支付');
  227. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  228. // 纯积分兑换
  229. $msg ='';
  230. if($order_info['price_total'] == 0 ) {
  231. Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])
  232. ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1,'pay_no'=>$pay_no]);
  233. $this->success('兑换成功',['pay_type'=>2,'config' => '']);
  234. }else{
  235. $notify_url = $this->request->root(true) . '/api/Pay/exchangeOrderNotify';
  236. Db::startTrans();
  237. try{
  238. // 更新订单
  239. $price_total = $order_info['price_total'];
  240. Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
  241. $config = Pay::wxPay('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
  242. //var_dump('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
  243. if($config){
  244. Db::commit();
  245. }else{
  246. Db::rollback();
  247. }
  248. }catch (\Exception $e){
  249. Db::rollback();
  250. $msg = $e->getMessage();
  251. }
  252. if($config){
  253. $this->success('ok',['pay_type'=>1,'config'=>$config]);
  254. }else{
  255. $this->error($msg);
  256. }
  257. }
  258. }
  259. /**
  260. * @title 取消订单
  261. * @desc 取消订单
  262. * @author qc
  263. * @url /api/Exchange_order/cancelOrder
  264. * @method POST
  265. * @header name:Authorization require:1 desc:Token
  266. * @param name:id type:int require:1 default:-- desc:订单id
  267. */
  268. public function cancelOrder()
  269. {
  270. $order_id = input('post.id');
  271. $order_detail = Db::table('exchange_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
  272. if(empty($order_detail)) $this->error('订单不存在');
  273. if($order_detail['pay_state'] != 0) $this->error('订单已支付');
  274. if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
  275. $user_info =Db::table('store_member')->find($this->uid);
  276. Db::startTrans();
  277. try {
  278. if($order_detail['crystal'] > 0) {
  279. Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal'=>bcadd($user_info['crystal'],$order_detail['crystal'],2)]);
  280. crystal_log($this->uid,$order_detail['crystal'],'订单取消',9,$order_id);
  281. }
  282. //恢复库存
  283. Db::table('exchange_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
  284. if($order_detail['cl_id']) Db::table('user_coupon_list')->where('id',$order_detail['cl_id'])->update(['status'=>1,'used_time'=>'']);
  285. // 取消订单
  286. Db::table('exchange_order')
  287. ->where(['id'=>$order_id])
  288. ->update(['cancel_state'=>1,'status'=>9]);
  289. Db::commit();
  290. }catch (\Exception $e){
  291. Db::rollback();
  292. }
  293. $this->success('取消成功');
  294. }
  295. /**
  296. * @title 获取订单列表
  297. * @desc 获取订单列表
  298. * @author qc
  299. * @url /api/Exchange_order/getMyOrderList
  300. * @method GET
  301. * @header name:Authorization require:1 desc:Token
  302. * @param name:page type:int : default:1 desc:页数
  303. * @param name:page_num type:int : default:20 desc:每页数
  304. * @return name:order_no type:string default:-- desc:订单号
  305. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  306. * @return name:price_total type:float default:-- desc:订单金额(实际支付金额)
  307. * @return name:freight type:float default:-- desc:运费
  308. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  309. * @return name:pay_no type:string default:-- desc:支付单号
  310. * @return name:pay_time type:string default:-- desc:支付时间
  311. * @return name:create_at type:string default:-- desc:订单创建时间
  312. * @return name:cancel_state type:int default:-- desc:取消状态
  313. * @return name:goods_id type:int default:-- desc:商品id
  314. * @return name:goods_name type:string default:-- desc:商品名
  315. * @return name:goods_cover type:string default:-- desc:商品图片
  316. * @return name:goods_num type:int default:-- desc:商品数量
  317. * @return name:express_company_title type:string default:-- desc:物流公司
  318. * @return name:express_send_no type:string default:-- desc:物流单号
  319. * @return name:express_send_at type:string default:-- desc:发货时间
  320. * @return name:express_company_id type:string default:-- desc:物流公司id
  321. */
  322. public function getMyOrderList()
  323. {
  324. $status = input('status',-1);
  325. $where=[];
  326. if($status > -1) $where['status'] = $status;
  327. $where['uid'] = $this->uid;
  328. $where['is_deleted'] = 0;
  329. $list = Db::table('exchange_order')
  330. ->where($where)
  331. ->order('id desc')
  332. ->limit($this->off_set,$this->page_num)
  333. ->select();
  334. $this->success('获取成功',$list);
  335. }
  336. /**
  337. * @title 删除订单
  338. * @desc 删除订单
  339. * @author qc
  340. * @url /api/Exchange_order/delOrder
  341. * @method POST
  342. * @header name:Authorization require:1 desc:Token
  343. * @param name:id type:int require:1 default:1 desc:订单id
  344. */
  345. public function delOrder()
  346. {
  347. $order_id = input('post.id');
  348. $order_info = Db::table('exchange_order')
  349. ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
  350. ->find();
  351. if(empty($order_info)) $this->error('订单不存在');
  352. Db::table('exchange_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
  353. $this->success('删除成功');
  354. }
  355. /**
  356. * @title 确认收货
  357. * @desc 确认收货
  358. * @author qc
  359. * @url /api/Exchange_order/confirmReceipt
  360. * @method POST
  361. * @header name:Authorization require:1 desc:Token
  362. * @param name:id type:int require:1 default:1 desc:订单id
  363. */
  364. public function confirmReceipt(){
  365. $order_info = Db::table('exchange_order')
  366. ->where(['uid'=>$this->uid,'id'=>input('post.id')])
  367. ->find();
  368. if(empty($order_info) || $order_info['status'] != 2) $this->error('商品未发货');
  369. Db::table('exchange_order')->where(['id'=>$order_info['id']])->update(['status'=>3,'receipt_at'=>date('Y-m-d H:i:s')]);
  370. $this->success('收货成功');
  371. }
  372. }