ExchangeOrder.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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:add_id type:int require:1 default:0 desc:收货地址id
  81. * @param name:coupon_id type:int require:0 default:0 desc:券id
  82. * @param name:exchange_type type:int require:1 default:1 desc:1明信片2余额
  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. $exchange_type = input("post.exchange_type",1);
  92. if(!$add_id) $this->error('请选择收货地址');
  93. $goods_info = Db::table('exchange_goods')->field('id,name,cover,status,stock,crystal')
  94. ->where(['id'=>$goods_id,'is_deleted'=>0])->find();
  95. $send_info = Db::table('delivery_address')->field('pro_name,city_name,county_name,mer_name,detail,phone,name')->find($add_id);
  96. if(empty($goods_info)) $this->error('商品已下架');
  97. if($goods_info['status'] != 1) $this->error('商品已下架');
  98. if($goods_info['stock'] < $num) $this->error('商品库存不足');
  99. if($coupon_id) {
  100. $coupon_info = Db::table('user_coupon_list')
  101. ->where('id','=',$coupon_id)
  102. ->where('past_at','>=',date('Y-m-d H:i:s'))
  103. ->find();
  104. if(!$coupon_info || $coupon_info['status'] != 1 ) $this->error('运费券信息有误');
  105. }
  106. $pro_info =[
  107. 'goods_id' => $goods_id,
  108. 'name' => $goods_info['name'],
  109. 'cover' => $goods_info['cover'],
  110. 'num' => $num,
  111. 'crystal' => $goods_info['crystal'],
  112. ];
  113. $freight = get_freight($num); // 运费
  114. $price_total = $coupon_id ? $freight['all_freight'] - $freight['first_freight']:$freight['all_freight'] ;
  115. $order_int =[
  116. 'uid'=>$this->uid,
  117. 'order_no' => get_order_sn(),
  118. 'price_total' => $price_total,
  119. 'goods_num' => $num,
  120. 'pro_info' => json_encode($pro_info),
  121. 'create_at' =>date('Y-m-d H:i:s'),
  122. 'freight' =>$freight['all_freight'],
  123. 'cl_id' =>$coupon_id,
  124. 'address_id' =>$add_id,
  125. 'goods_id' => $goods_id,
  126. 'goods_name' => $goods_info['name'],
  127. 'goods_cover' => $goods_info['cover'],
  128. 'crystal' => bcmul($goods_info['crystal'],$num,2),
  129. 'exchange_type' =>$exchange_type,
  130. 'send_info' =>json_encode($send_info),
  131. ];
  132. $user_info =Db::table('store_member')->find($this->uid);
  133. if($exchange_type == 1 && $user_info['crystal']< $order_int['crystal']) $this->error('明信片不足!');
  134. if($exchange_type == 2 && $user_info['crystal_cash']< $order_int['crystal']) $this->error('余额不足!');
  135. Db::startTrans();
  136. $msg= '';$com=true;
  137. try{
  138. //扣减库存
  139. Db::table('exchange_goods')->where('id',$goods_id)->setDec('stock',$num);
  140. // 扣除货币余额
  141. if($exchange_type == 1 ) {
  142. Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal'=>bcsub($user_info['crystal'],$order_int['crystal'],2)]);
  143. }else{
  144. Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcsub($user_info['crystal_cash'],$order_int['crystal'],2)]);
  145. }
  146. if($coupon_id) Db::table('user_coupon_list')->where('id',$coupon_id)->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
  147. // 生成订单
  148. Db::table('exchange_order')->insert($order_int);
  149. $order_id = Db::getLastInsID();
  150. if($exchange_type == 1) {
  151. crystal_log($this->uid,$order_int['crystal']*-1,'明信片兑换',5,$order_id);
  152. }else{
  153. balance_log($this->uid,$order_int['crystal']*-1,'余额兑换',5,$order_id);
  154. }
  155. Db::commit();
  156. }catch (\Exception $e){
  157. Db::rollback();
  158. $com=false;
  159. $msg = $e->getMessage();
  160. }
  161. if($com){
  162. $this->success('ok',['order_id'=>$order_id]);
  163. }else{
  164. $this->error($msg);
  165. }
  166. }
  167. /**
  168. * @title 获取订单详情
  169. * @desc 获取订单详情
  170. * @author qc
  171. * @url /api/Exchange_order/getOrderDetail
  172. * @method GET
  173. * @header name:Authorization require:1 desc:Token
  174. * @param name:id type:int require:1 default:-- desc:订单id
  175. * @return name:order_no type:string default:-- desc:订单号
  176. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  177. * @return name:price_total type:float default:-- desc:订单金额(实际支付金额)
  178. * @return name:freight type:float default:-- desc:运费
  179. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  180. * @return name:pay_no type:string default:-- desc:支付单号
  181. * @return name:pay_time type:string default:-- desc:支付时间
  182. * @return name:create_at type:string default:-- desc:订单创建时间
  183. * @return name:cancel_state type:int default:-- desc:取消状态
  184. * @return name:goods_id type:int default:-- desc:商品id
  185. * @return name:goods_name type:string default:-- desc:商品名
  186. * @return name:goods_cover type:string default:-- desc:商品图片
  187. * @return name:goods_num type:int default:-- desc:商品数量
  188. * @return name:exchange_type type:int default:-- desc:兑换方式(1明信片2余额)
  189. * @return name:express_company_title type:string default:-- desc:物流公司
  190. * @return name:express_send_no type:string default:-- desc:物流单号
  191. * @return name:express_send_at type:string default:-- desc:发货时间
  192. * @return name:express_company_id type:string default:-- desc:物流公司id
  193. * @return name:send_info type:array default:-- desc:收货详情
  194. */
  195. public function getOrderDetail()
  196. {
  197. $order_id = input('id',0);
  198. $detail = Db::table('exchange_order')->find($order_id);
  199. $this->success('获取成功',$detail);
  200. }
  201. /**
  202. * @title 获取订单列表
  203. * @desc 获取订单列表
  204. * @author qc
  205. * @url /api/Exchange_order/getMyOrderList
  206. * @method GET
  207. * @header name:Authorization require:1 desc:Token
  208. * @param name:page type:int : default:1 desc:页数
  209. * @param name:page_num type:int : default:20 desc:每页数
  210. * @return name:order_no type:string default:-- desc:订单号
  211. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  212. * @return name:price_total type:float default:-- desc:订单金额(实际支付金额)
  213. * @return name:freight type:float default:-- desc:运费
  214. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  215. * @return name:pay_no type:string default:-- desc:支付单号
  216. * @return name:pay_time type:string default:-- desc:支付时间
  217. * @return name:create_at type:string default:-- desc:订单创建时间
  218. * @return name:cancel_state type:int default:-- desc:取消状态
  219. * @return name:goods_id type:int default:-- desc:商品id
  220. * @return name:goods_name type:string default:-- desc:商品名
  221. * @return name:goods_cover type:string default:-- desc:商品图片
  222. * @return name:goods_num type:int default:-- desc:商品数量
  223. * @return name:exchange_type type:int default:-- desc:兑换方式(1明信片2余额)
  224. * @return name:express_company_title type:string default:-- desc:物流公司
  225. * @return name:express_send_no type:string default:-- desc:物流单号
  226. * @return name:express_send_at type:string default:-- desc:发货时间
  227. * @return name:express_company_id type:string default:-- desc:物流公司id
  228. */
  229. public function getMyOrderList()
  230. {
  231. $status = input('status',-1);
  232. $where=[];
  233. if($status > -1) $where['status'] = $status;
  234. $where['uid'] = $this->uid;
  235. $where['is_deleted'] = 0;
  236. $list = Db::table('exchange_order')
  237. ->where($where)
  238. ->order('id desc')
  239. ->limit($this->off_set,$this->page_num)
  240. ->select();
  241. $this->success('获取成功',$list);
  242. }
  243. /**
  244. * @title 去支付
  245. * @desc 去支付
  246. * @author qc
  247. * @url /api/Exchange_order/payOrder
  248. * @method POST
  249. * @header name:Authorization require:1 desc:Token
  250. * @param name:o_id type:int require:1 default:-- desc:订单id
  251. * @return name:pay_type type:int require:1 default:-- desc:1纯兑换,不支付,2微信支付
  252. * @return name:config type:array default:0 desc:支付配置pay_type=1时忽略
  253. * @return name:config.appId type:string default:-- desc:公众账号ID
  254. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  255. * @return name:config.signType type:string default:-- desc:签名类型
  256. * @return name:config.paySign type:string default:-- desc:签名
  257. * @return name:config.timestamp type:string default:-- desc:时间戳
  258. */
  259. public function payOrder()
  260. {
  261. $o_id = input('post.o_id');
  262. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  263. if(!$user_info['openid']) $this->error('请先完善用户信息');
  264. $order_info = Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
  265. if(empty($order_info)) $this->error('订单不存在');
  266. if($order_info['cancel_state'] != 0) $this->error('订单已取消');
  267. if($order_info['status'] != 0) $this->error('订单已支付');
  268. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  269. // 纯积分兑换
  270. $msg ='';
  271. if($order_info['price_total'] == 0 ) {
  272. Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])
  273. ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1,'pay_no'=>$pay_no]);
  274. $this->success('兑换成功',['pay_type'=>2,'config' => '']);
  275. }else{
  276. $notify_url = $this->request->root(true) . '/api/Pay/exchangeOrderNotify';
  277. Db::startTrans();
  278. try{
  279. // 更新订单
  280. $price_total = $order_info['price_total'];
  281. Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
  282. $config = Pay::wxPay('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
  283. //var_dump('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
  284. if($config){
  285. Db::commit();
  286. }else{
  287. Db::rollback();
  288. }
  289. }catch (\Exception $e){
  290. Db::rollback();
  291. $msg = $e->getMessage();
  292. }
  293. if($config){
  294. $this->success('ok',['pay_type'=>1,'config'=>$config]);
  295. }else{
  296. $this->error($msg);
  297. }
  298. }
  299. }
  300. /**
  301. * @title 取消订单
  302. * @desc 取消订单
  303. * @author qc
  304. * @url /api/Exchange_order/cancelOrder
  305. * @method POST
  306. * @header name:Authorization require:1 desc:Token
  307. * @param name:id type:int require:1 default:-- desc:订单id
  308. */
  309. public function cancelOrder()
  310. {
  311. $order_id = input('post.id');
  312. $order_detail = Db::table('exchange_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
  313. if(empty($order_detail)) $this->error('订单不存在');
  314. if($order_detail['pay_state'] != 0) $this->error('订单已支付');
  315. if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
  316. $user_info =Db::table('store_member')->find($this->uid);
  317. Db::startTrans();
  318. try {
  319. if($order_detail['crystal'] > 0) {
  320. if($order_detail['exchange_type'] == 1) {
  321. Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcadd($user_info['crystal'],$order_detail['crystal'],2)]);
  322. crystal_log($this->uid,$order_detail['crystal'],'订单取消',9,$order_id);
  323. }else{
  324. Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcadd($user_info['crystal_cash'],$order_detail['crystal'],2)]);
  325. balance_log($this->uid,$order_detail['crystal'],'订单取消',9,$order_id);
  326. }
  327. }
  328. //恢复库存
  329. Db::table('exchange_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
  330. if($order_detail['cl_id']) Db::table('user_coupon_list')->where('id',$order_detail['cl_id'])->update(['status'=>1,'used_time'=>'']);
  331. // 取消订单
  332. Db::table('exchange_order')
  333. ->where(['id'=>$order_id])
  334. ->update(['cancel_state'=>1,'status'=>9]);
  335. Db::commit();
  336. }catch (\Exception $e){
  337. Db::rollback();
  338. }
  339. $this->success('取消成功');
  340. }
  341. /**
  342. * @title 删除订单
  343. * @desc 删除订单
  344. * @author qc
  345. * @url /api/Exchange_order/delOrder
  346. * @method POST
  347. * @header name:Authorization require:1 desc:Token
  348. * @param name:id type:int require:1 default:1 desc:订单id
  349. */
  350. public function delOrder()
  351. {
  352. $order_id = input('post.id');
  353. $order_info = Db::table('exchange_order')
  354. ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
  355. ->find();
  356. if(empty($order_info)) $this->error('订单不存在');
  357. Db::table('exchange_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
  358. $this->success('删除成功');
  359. }
  360. /**
  361. * @title 确认收货
  362. * @desc 确认收货
  363. * @author qc
  364. * @url /api/Exchange_order/confirmReceipt
  365. * @method POST
  366. * @header name:Authorization require:1 desc:Token
  367. * @param name:id type:int require:1 default:1 desc:订单id
  368. */
  369. public function confirmReceipt(){
  370. $order_info = Db::table('exchange_order')
  371. ->where(['uid'=>$this->uid,'id'=>input('post.id')])
  372. ->find();
  373. if(empty($order_info) || $order_info['status'] != 2) $this->error('商品未发货');
  374. Db::table('exchange_order')->where(['id'=>$order_info['id']])->update(['status'=>3,'receipt_at'=>date('Y-m-d H:i:s')]);
  375. $this->success('收货成功');
  376. }
  377. }