Order.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. /**
  6. * @title 会员订单管理
  7. * @controller Order
  8. * @group base
  9. */
  10. class Order extends Base
  11. {
  12. public function initialize()
  13. {
  14. parent::check_login();
  15. }
  16. /**
  17. * @title 订单确认(直接购买&&直接兑换)
  18. * @desc 生成订单
  19. * @author qc
  20. * @url /api/Order/orderConfirm
  21. * @method GET
  22. * @header name:Authorization require:1 desc:Token
  23. * @param name:goods_id type:int require:1 default:-- desc:商品id
  24. * @param name:goods_no type:string require:1 default:-- desc:商品货号
  25. * @param name:num type:int require:0 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.goods_id type:array require:0 default:-- desc:商品id
  39. * @return name:goods_data.name type:string require:0 default:-- desc:商品名称
  40. * @return name:goods_data.cover type:string require:0 default:-- desc:商品图片
  41. * @return name:goods_data.freight type:float require:0 default:-- desc:商品运费
  42. * @return name:goods_data.integral type:int require:0 default:-- desc:商品积分数(单个商品积分乘以个数)
  43. */
  44. public function orderConfirm()
  45. {
  46. $goods_id = input('get.goods_id');
  47. $goods_no = input('get.goods_no');
  48. $num = input('get.num',1);
  49. $add_info = Db::table('delivery_address')
  50. ->where(['user_id'=>$this->uid,'is_mr'=>1])->find();
  51. $goods_info = Db::table('store_goods')->find($goods_id);
  52. $spec = json_decode($goods_info['spec'],true);
  53. $cover = $goods_info['cover'];
  54. foreach ($spec as $sk=>$sv)
  55. {
  56. if($goods_no == $sv['goods_no']) $cover = $sv['spec_img'];
  57. }
  58. $freight = $goods_info['freight_type'] == 0 ? $goods_info['postage'] :0 ;
  59. // 运费模板计算运费
  60. if(!empty($add_info) && $goods_info['freight_type']){
  61. $cal_freight = $this->claFreight($add_info['id'],$goods_info['freight_id'],$num);
  62. if($cal_freight['code'] != 200){
  63. $add_info = null ;
  64. }else{
  65. $freight = $cal_freight['freight'];
  66. }
  67. }
  68. $goods_data = [
  69. 'goods_id' => $goods_info['id'],
  70. 'goods_no' => $goods_no,
  71. 'name' => $goods_info['name'],
  72. 'cover' => $cover,
  73. 'integral'=>$goods_info['integral'] * $num,
  74. 'freight' => $freight
  75. ];
  76. $this->success('ok',['add_info'=> $add_info ,'goods_data'=>$goods_data]);
  77. }
  78. /**
  79. * 计算运费
  80. * @param $add_id 地址id
  81. * @param $freight_id 模板id
  82. * @param $num 商品数量
  83. * @return array
  84. */
  85. public function claFreight($add_id,$freight_id,$num)
  86. {
  87. $data = ['code'=>200,'freight'=>0 ,'msg'=>''];
  88. $address_info = Db::table('delivery_address')->find($add_id);
  89. $template_info = Db::table('freight_template')->find($freight_id);
  90. if(empty($address_info)){
  91. $data['code'] = 201;
  92. $data['msg'] = '运费模板不存在';
  93. return $data;
  94. }
  95. $city_id = $address_info['city_id'];
  96. $no_express = explode(',',$template_info['no_express']);
  97. if(in_array($city_id,$no_express)) return ['code'=>202,'msg'=>'该收货地址不配送物流'];
  98. // 模板自定义分组
  99. $custom = json_decode($template_info['custom'],true);
  100. $base_num = $template_info['base_num'];// 基件数量
  101. $base_price = $template_info['base_price']; // 基件费用
  102. $base_keep_price = $template_info['base_keep_price']; // 续件费用
  103. foreach($custom as $cus_info) {
  104. $express =explode(',',$cus_info['express']);
  105. if(in_array($city_id,$express)) {
  106. $base_num = $cus_info['first_num'];
  107. $base_price = $cus_info['first_price'];
  108. $base_keep_price = $cus_info['keep_price'];
  109. break;
  110. }
  111. }
  112. if($base_num >= $num ) return $data = ['code'=>200,'freight'=>$base_price ,'msg'=>'ok'];
  113. $sub_keep = bcmul(bcsub($num,$base_num),$base_keep_price,2);
  114. return $data = ['code'=>200,'freight'=>bcadd($base_price,$sub_keep) ,'msg'=>'ok'];
  115. }
  116. /**
  117. * @title 获取运费
  118. * @desc 获取运费
  119. * @author qc
  120. * @url /api/Order/getFreight
  121. * @method GET
  122. * @header name:Authorization require:1 desc:Token
  123. * @param name:goods_id type:int require:1 default:-- desc:商品id
  124. * @param name:add_id type:int require:1 default:-- desc:地址id
  125. * @param name:num type:int require:0 default:1 desc:商品数量
  126. * @return name:freight type:float require:0 default:-- desc:运费金额
  127. */
  128. public function getFreight()
  129. {
  130. $goods_id = input('goods_id',0);
  131. $add_id = input('add_id',0);
  132. $num = input('num',0);
  133. $goods_info = Db::table('store_goods')->find($goods_id);
  134. $freight = $goods_info['freight_type'] == 0 ? $goods_info['postage'] :0 ;
  135. if($goods_info['freight_type']){
  136. $cal_freight = $this->claFreight($add_id,$goods_info['freight_id'],$num);
  137. if($cal_freight['code'] != 200){
  138. $this->error($cal_freight['msg']);
  139. }else{
  140. $freight = $cal_freight['freight'];
  141. }
  142. }
  143. $this->success('ok',['freight'=>$freight]);
  144. }
  145. /**
  146. * @title 生成订单(立即购买 [商城 && 积分商城])
  147. * @desc 生成订单(立即购买 [商城 && 积分商城])
  148. * @author qc
  149. * @url /api/Order/createOrderByImmediately
  150. * @method POST
  151. * @header name:Authorization require:1 desc:Token
  152. * @param name:goods_id type:int require:1 default:-- desc:商品id
  153. * @param name:goods_no type:int require:1 default:-- desc:商品货号
  154. * @param name:price_total type:float require:1 default:-- desc:支付金额(优惠后的商品金额+运费)
  155. * @param name:num type:int require:1 default:1 desc:商品数量
  156. * @param name:price_goods type:float require:0 default:0 desc:商品总金额(没优惠的)
  157. * @param name:price_express type:float require:0 default:0 desc:运费
  158. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  159. * @param name:remark type:string require:0 default:0 desc:订单备注
  160. * @param name:source type:int require:0 default:0 desc:商城的为0(默认),积分商城的值为1
  161. * @param name:integral type:int require:0 default:0 desc:使用多少积分,source为1时必传
  162. * @param name:int_cash type:float require:0 default:0 desc:积分抵扣多少钱,source为1时必传
  163. * @param name:cl_id type:int require:0 default:0 desc:会员优惠券id,使用优惠券必传
  164. * @param name:coupon_cash type:float require:0 default:0 desc:会员优惠券抵扣金额
  165. * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
  166. */
  167. public function createOrderByImmediately()
  168. {
  169. $goods_id = input("post.goods_id");
  170. $goods_no = input("post.goods_no");
  171. $price_total = input("post.price_total",0);
  172. $price_goods = input("post.price_goods",0);
  173. $price_express = input("post.price_express",0);
  174. $num = input("post.num",1);
  175. $add_id = input("post.add_id",0);
  176. $source = input("post.source",0);
  177. $integral = input("post.integral",0);
  178. $int_cash = input("post.int_cash",0);
  179. $cl_id = input("post.cl_id",0);
  180. $coupon_cash = input("post.coupon_cash",0);
  181. $remark = input("post.remark",'');
  182. //商品验证start
  183. $goods_info = Db::table('store_goods')->field('id,name,spec,stock,is_integral')
  184. ->where(['id'=>$goods_id,'status'=>1,'is_deleted'=>0])
  185. ->find();
  186. if(empty($goods_info)) $this->error('商品已下架');
  187. $spec_info = [];
  188. $goods_spec = json_decode($goods_info['spec'],true);
  189. foreach ($goods_spec as &$c){
  190. if($c['goods_no'] == $goods_no) {
  191. $spec_info =$c;
  192. $c['store_num'] -=$num;
  193. break;
  194. }
  195. }
  196. if(!isset($spec_info['integral'])) $spec_info['integral'] = 0;
  197. if($spec_info['store_num'] < $num || $goods_info['stock'] < $num) $this->error('商品库存不足');
  198. // 券验证
  199. if($cl_id) {
  200. $cl_info = Db::table('user_coupon_list')
  201. ->where(['user_id'=>$this->uid,'id'=>$cl_id,'status'=>1])
  202. ->find();
  203. if(empty($cl_info)) $this->error('优惠券不存在');
  204. if($cl_info['past_at'] < date('Y-m-d H:i:s')) $this->error('优惠券已过期');
  205. if($cl_info['low_amount'] > bcsub($price_total,$price_express,2) ) $this->error('订单金额不够,再看看吧');
  206. if($cl_info['amount'] != $coupon_cash ) $this->error('优惠金额错误');
  207. }
  208. // 积分验证
  209. $user = Db::table('store_member')->find($this->uid);
  210. if($integral && $user['integral'] < $integral) $this->error('积分余额不足');
  211. $pro_info[]=[
  212. 'goods_id' =>$goods_id,
  213. 'goods_no' =>$goods_no,
  214. 'num' => $num,
  215. 'integral' =>$spec_info['integral'] * $num,
  216. 'int_cash' =>$int_cash,
  217. 'goods_name' =>$goods_info['name'],
  218. 'spec_img' =>$spec_info['spec_img'],
  219. 'sell_money'=>$spec_info['sell_money'],
  220. 'spec_exp' =>$spec_info['spec_exp'],
  221. 'spec_key' =>$spec_info['spec_key'],
  222. 'is_evaluate' => 0,
  223. ];
  224. $order_int =[
  225. 'uid'=>$this->uid,
  226. 'order_no' => get_order_sn(),
  227. 'price_total' => $price_total,
  228. 'price_goods' => $price_goods,
  229. 'price_express' => $price_express,
  230. 'create_at' => date("Y-m-d H:i:s"),
  231. 'address_id' =>$add_id,
  232. 'source' =>$source,
  233. 'integral' =>$integral,
  234. 'int_cash' =>$int_cash,
  235. 'cl_id' =>$cl_id,
  236. 'coupon_cash' =>$coupon_cash,
  237. 'remark' =>$remark,
  238. 'goods_num' =>$num,
  239. 'pro_info' =>json_encode($pro_info)
  240. ];
  241. Db::startTrans();
  242. $msg= '';$com=true;
  243. try{
  244. // 生成订单
  245. Db::table('store_order')->insert($order_int);
  246. $order_id = Db::getLastInsID();
  247. //扣减库存
  248. update_goods_stock($goods_id,$goods_no,$num);
  249. //扣减积分
  250. if($integral){
  251. update_user_integral($this->uid,$integral * -1,5,'积分抵扣',$order_id);
  252. }
  253. // 扣除券
  254. if($cl_id) {
  255. Db::table('user_coupon_list')->where(['id'=>$cl_id])->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
  256. }
  257. Db::commit();
  258. }catch (\Exception $e){
  259. Db::rollback();
  260. $com=false;
  261. $msg = $e->getMessage();
  262. }
  263. if($com){
  264. $this->success('ok',['order_id'=>$order_id]);
  265. }else{
  266. $this->error($msg);
  267. }
  268. }
  269. /**
  270. * @title 取消订单
  271. * @desc 取消订单
  272. * @author qc
  273. * @url /api/Order/cancelOrder
  274. * @method POST
  275. * @header name:Authorization require:1 desc:Token
  276. * @param name:id type:int require:1 default:-- desc:订单id
  277. * @param name:cancel_desc type:string require:1 default:-- desc:取消描述
  278. */
  279. public function cancelOrder()
  280. {
  281. $order_id = input('post.id');
  282. $desc = input('post.desc');
  283. $order_detail = Db::table('store_order')->where(['id'=>$order_id])->find();
  284. if(empty($order_detail)) $this->error('订单不存在');
  285. if($order_detail['pay_state'] != 0) $this->error('订单已支付');
  286. if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
  287. Db::startTrans();
  288. try {
  289. // 积分退回
  290. if($order_detail['integral'] > 0) {
  291. update_user_integral($order_detail['uid'],$order_detail['integral'],8,'订单取消退回',$order_id);
  292. }
  293. // 优惠券回档
  294. if($order_detail['cl_id'] > 0) {
  295. $user_coupon = Db::table('user_coupon_list')->find($order_detail['cl_id']);// 会员优惠券记录
  296. $coupon_set = Db::table('store_coupon_config')->find($user_coupon['coupon_id']);//优惠券设置
  297. if($coupon_set['status'] != 1 || $coupon_set['is_deleted'] == 1){
  298. $coupon_status = 4;
  299. }else{
  300. $coupon_status = $user_coupon['past_at'] > date("Y-m-d H:i:s") ? 1:3;
  301. }
  302. Db::table('user_coupon_list')->where(['id'=>$order_detail['cl_id']])->update(['status'=>$coupon_status]);
  303. }
  304. // 商品库存恢复
  305. $pro_info = json_decode($order_detail['pro_info'],true);
  306. foreach ($pro_info as $pv) {
  307. update_goods_stock($pv['goods_id'],$pv['goods_no'],$pv['num']*-1);
  308. }
  309. // 取消订单
  310. Db::table('store_order')
  311. ->where(['id'=>$order_id])
  312. ->update(['cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s'),'cancel_desc'=>$desc,'status'=>9]);
  313. Db::commit();
  314. }catch (\Exception $e){
  315. Db::rollback();
  316. }
  317. $this->success('取消成功');
  318. }
  319. /**
  320. * @title 生成订单(购物车)
  321. * @desc 生成订单(购物车)
  322. * @author qc
  323. * @url /api/Order/createOrderByTrolley
  324. * @method POST
  325. * @header name:Authorization require:1 desc:Token
  326. * @param name:ids type:string : default:1 desc:购物车记录id串(逗号分开)
  327. * @param name:price_total type:float require:1 default:-- desc:支付金额(优惠后的商品金额+运费)
  328. * @param name:price_goods type:float require:0 default:0 desc:商品总金额(没优惠的)
  329. * @param name:price_express type:float require:0 default:0 desc:运费
  330. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  331. * @param name:remark type:string require:0 default:0 desc:订单备注
  332. * @param name:source type:int require:0 default:0 desc:商城的为0(默认),积分商城的值为1
  333. * @param name:integral type:int require:0 default:0 desc:使用多少积分,source为1时必传
  334. * @param name:int_cash type:float require:0 default:0 desc:积分抵扣多少钱,source为1时必传
  335. * @param name:cl_id type:int require:0 default:0 desc:会员优惠券id,使用优惠券必传
  336. * @param name:coupon_cash type:float require:0 default:0 desc:会员优惠券抵扣金额
  337. * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
  338. */
  339. public function createOrderByTrolley()
  340. {
  341. $ids = input("post.ids");
  342. $price_total = input("post.price_total",0);
  343. $price_goods = input("post.price_goods",0);
  344. $price_express = input("post.price_express",0);
  345. $add_id = input("post.add_id",0);
  346. $source = input("post.source",0);
  347. $integral = input("post.integral",0);
  348. $int_cash = input("post.int_cash",0);
  349. $cl_id = input("post.cl_id",0);
  350. $coupon_cash = input("post.coupon_cash",0);
  351. $remark = input("post.remark",'');
  352. if(!$ids || !$price_total) $this->error('请选择要结算的商品');
  353. $trolley_arr = Db::table('shopping_trolley')
  354. ->where(['user_id'=>$this->uid,'id'=>explode(',',$ids)])
  355. ->select();
  356. $pro_info = [];$total_num = 0;$check_goods = true;
  357. // 验证商品是否下架 && 获取订单商品详情
  358. foreach ($trolley_arr as $tv) {
  359. $goods_info = Db::table('store_goods')->field('id,spec,name')
  360. ->where(['id'=>$tv['goods_id'],'is_deleted'=>0,'status'=>1])
  361. ->find();
  362. if(empty($goods_info)){
  363. $check_goods =false;
  364. break;
  365. }
  366. foreach (json_decode($goods_info['spec'],true) as $sv)
  367. {
  368. if($sv['goods_no'] == $tv['goods_no']) {
  369. $pro_info[]=[
  370. 'goods_id' =>$tv['goods_id'],
  371. 'goods_no' =>$tv['goods_no'],
  372. 'num' =>$tv['num'],
  373. 'integral' => isset($sv['integral']) ? $sv['integral'] * $tv['num'] : 0,
  374. 'int_cash' =>'--',
  375. 'goods_name' =>$goods_info['name'],
  376. 'spec_img' =>$sv['spec_img'],
  377. 'sell_money'=>$sv['sell_money'],
  378. 'spec_exp' =>$sv['spec_exp'],
  379. 'spec_key' =>$sv['spec_key'],
  380. 'is_evaluate' => 0,
  381. ];
  382. $total_num +=$tv['num'];
  383. }
  384. }
  385. }
  386. if(!$check_goods) $this->error('所选商品已下架');
  387. // 券验证
  388. if($cl_id) {
  389. $cl_info = Db::table('user_coupon_list')
  390. ->where(['user_id'=>$this->uid,'id'=>$cl_id,'status'=>1])
  391. ->find();
  392. if(empty($cl_info)) $this->error('优惠券不存在');
  393. if($cl_info['past_at'] < date('Y-m-d H:i:s')) $this->error('优惠券已过期');
  394. if($cl_info['low_amount'] > bcsub($price_total,$price_express,2) ) $this->error('订单金额不够,再看看吧');
  395. if($cl_info['amount'] != $coupon_cash ) $this->error('优惠金额错误');
  396. }
  397. // 积分验证
  398. $user = Db::table('store_member')->find($this->uid);
  399. if($integral && $user['integral'] < $integral) $this->error('积分余额不足');
  400. $order_int =[
  401. 'uid'=>$this->uid,
  402. 'order_no' => get_order_sn(),
  403. 'price_total' => $price_total,
  404. 'price_goods' => $price_goods,
  405. 'price_express' => $price_express,
  406. 'create_at' => date("Y-m-d H:i:s"),
  407. 'address_id' =>$add_id,
  408. 'source' =>$source,
  409. 'integral' =>$integral,
  410. 'int_cash' =>$int_cash,
  411. 'cl_id' =>$cl_id,
  412. 'coupon_cash' =>$coupon_cash,
  413. 'remark' =>$remark,
  414. 'goods_num' =>$total_num,
  415. 'pro_info' =>json_encode($pro_info)
  416. ];
  417. Db::startTrans();
  418. $msg= '';$com=true;
  419. try{
  420. // 生成订单
  421. Db::table('store_order')->insert($order_int);
  422. $order_id = Db::getLastInsID();
  423. // 删除购物车记录
  424. Db::table('shopping_trolley')->where(['id'=>explode(',',$ids)])->delete();
  425. //扣减积分
  426. if($integral){
  427. Db::table('store_member')->where(['id'=>$this->uid])->setDec('integral',$integral);
  428. //积分扣减记录
  429. $integral_info=[
  430. 'user_id'=> $this->uid,
  431. 'create_at'=> date("Y-m-d H:i:s"),
  432. 'integral'=> $integral * -1,
  433. 'before'=> $user['integral'],
  434. 'after'=> $user['integral'] - $integral,
  435. 'desc'=> '积分抵现',
  436. 'rel_id'=> $order_id,
  437. ];
  438. Db::table('integral_info')->insert($integral_info);
  439. }
  440. // 扣除券
  441. if($cl_id) {
  442. Db::table('user_coupon_list')->where(['id'=>$cl_id])->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
  443. }
  444. Db::commit();
  445. }catch (\Exception $e){
  446. Db::rollback();
  447. $com=false;
  448. $msg = $e->getMessage();
  449. }
  450. if($com){
  451. $this->success('ok',['order_id'=>$order_id]);
  452. }else{
  453. $this->error($msg);
  454. }
  455. }
  456. /**
  457. * @title 获取订单详情
  458. * @desc 获取订单详情
  459. * @author qc
  460. * @url /api/Order/getOrderDetail
  461. * @method GET
  462. * @header name:Authorization require:1 desc:Token
  463. * @param name:id type:int require:1 default:-- desc:订单id
  464. * @return name:order_no type:string default:-- desc:订单号
  465. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  466. * @return name:price_total type:float default:-- desc:订单金额(需要支付金额【包含运费,扣除所有优惠】)
  467. * @return name:pro_info type:string default:-- desc:订单商品
  468. * @return name:pro_info.goods_id type:int default:-- desc:商品id
  469. * @return name:pro_info.goods_no type:string default:-- desc:商品货号
  470. * @return name:pro_info.sell_money type:string default:-- desc:售价
  471. * @return name:pro_info.spec_img type:string default:-- desc:封面
  472. * @return name:pro_info.is_evaluate type:string default:-- desc:商品是否评价
  473. * @return name:price_goods type:float default:-- desc:商品金额
  474. * @return name:price_express type:float default:-- desc:运费
  475. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  476. * @return name:order_no type:string default:-- desc:订单号
  477. * @return name:pay_no type:string default:-- desc:支付单号
  478. * @return name:create_at type:string default:-- desc:订单创建时间
  479. * @return name:pay_at type:string default:-- desc:支付时间
  480. * @return name:cancel_at type:string default:-- desc:取消时间(status=9)
  481. * @return name:cancel_desc type:string default:-- desc:取消描述(status=9)
  482. * @return name:refund_type type:int default:-- desc:订单申请退款、退换货状态0未申请,1部分申请,2全部申请
  483. * @return name:source type:int default:0 desc:订单来源:0商城,1积分商城
  484. * @return name:integral type:int default:0 desc:使用多少积分
  485. * @return name:int_cash type:int default:0 desc:积分抵扣多少金额
  486. * @return name:cl_id type:int default:0 desc:使用优惠券的id
  487. * @return name:coupon_cash type:float default:0 desc:券抵扣金额
  488. */
  489. public function getOrderDetail()
  490. {
  491. $order_id = input('id',0);
  492. $detail = Db::table('store_order')->find($order_id);
  493. $this->success('获取成功',$detail);
  494. }
  495. /**
  496. * @title 去支付
  497. * @desc 去支付
  498. * @author qc
  499. * @url /api/Order/payOrder
  500. * @method POST
  501. * @header name:Authorization require:1 desc:Token
  502. * @param name:o_id type:int require:1 default:-- desc:订单id
  503. * @return name:type type:int default:0 desc:类型(1.type=1,调支付,2纯积分兑换成功)
  504. * @return name:config type:array default:0 desc:支付配置(type=2时忽略)
  505. * @return name:config.appId type:string default:-- desc:公众账号ID
  506. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  507. * @return name:config.signType type:string default:-- desc:签名类型
  508. * @return name:config.paySign type:string default:-- desc:签名
  509. * @return name:config.timestamp type:string default:-- desc:时间戳
  510. */
  511. public function payOrder()
  512. {
  513. $o_id = input('post.o_id');
  514. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  515. if(!$user_info['phone'] || !$user_info['openid']) $this->error('请先完善用户信息');
  516. $order_info = Db::table('store_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
  517. if(empty($order_info)) $this->error('订单不存在');
  518. if($order_info['status'] != 0) $this->error('订单已支付');
  519. if($order_info['cancel_state'] != 0) $this->error('订单已取消');
  520. if($order_info['price_total'] <= 0 && $order_info['source'] == 0) $this->error('订单金额错误');
  521. // 纯积分兑换
  522. if($order_info['price_total'] <= 0 && $order_info['source'] == 1 )
  523. {
  524. // 销量
  525. $sell_data = [];
  526. foreach (json_decode($order_info['pro_info'],true) as $pv){
  527. $sell_data[] =[
  528. 'goods_id' => $pv['goods_id'],
  529. 'goods_no' => $pv['goods_no'],
  530. 'num' => $pv['num'],
  531. 'user_id' => $order_info['uid'],
  532. 'order_id' => $order_info['id'],
  533. 'create_at' => date('Y-m-d H:i:s'),
  534. 'year' => date('Y'),
  535. 'month' => date('m'),
  536. 'day' => date('d'),
  537. ];
  538. }
  539. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  540. Db::table('goods_sell_info')->insertAll($sell_data);
  541. // 更新订单状态
  542. Db::table('store_order')->where(['uid'=>$this->uid,'id'=>$o_id])
  543. ->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1,'pay_no'=>$pay_no]);
  544. $this->success('兑换成功',['type'=>2]);
  545. }else{
  546. Db::startTrans();
  547. $config = false;$msg= '';
  548. try{
  549. $notify_url = $this->request->root(true) . '/api/Pay/payNotify';
  550. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  551. Db::table('store_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
  552. $config = Pay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  553. if($config){
  554. Db::commit();
  555. }else{
  556. Db::rollback();
  557. }
  558. }catch (\Exception $e){
  559. Db::rollback();
  560. $msg = $e->getMessage();
  561. }
  562. if($config){
  563. $this->success('ok',['type'=>1,'jump'=>1,'config'=>$config]);
  564. }else{
  565. $this->error($msg);
  566. }
  567. }
  568. }
  569. /**
  570. * @title 获取订单列表
  571. * @desc 获取订单列表
  572. * @author qc
  573. * @url /api/Order/getMyOrderList
  574. * @method GET
  575. * @header name:Authorization require:1 desc:Token
  576. * @param name:page type:int : default:1 desc:页数
  577. * @param name:page_num type:int : default:20 desc:每页数
  578. * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消)
  579. * @return name:order_no type:string default:-- desc:订单号
  580. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  581. * @return name:price_total type:float default:-- desc:订单金额
  582. * @return name:pro_info type:float default:-- desc:订单详情
  583. * @return name:pro_info.goods_id type:int default:-- desc:商品id
  584. * @return name:pro_info.goods_name type:int default:-- desc:商品名
  585. * @return name:pro_info.goods_no type:string default:-- desc:商品货号
  586. * @return name:pro_info.sell_money type:string default:-- desc:售价
  587. * @return name:pro_info.spec_img type:string default:-- desc:封面
  588. * @return name:pro_info.is_evaluate type:string default:-- desc:商品是否评价
  589. * @return name:pay_at type:string default:-- desc:支付时间
  590. * @return name:create_at type:string default:-- desc:订单创建时间
  591. * @return name:remark type:string default:-- desc:备注
  592. * @return name:cancel_at type:string default:-- desc:取消时间(status=9)
  593. * @return name:cancel_desc type:string default:-- desc:取消描述(status=9)
  594. * @return name:refund_type type:int default:-- desc:订单申请退款、退换货状态0未申请,1申请
  595. * @return name:source type:int default:0 desc:订单来源:0商城,1积分商城
  596. * @return name:integral type:int default:0 desc:使用多少积分
  597. * @return name:int_cash type:int default:0 desc:积分抵扣多少金额
  598. * @return name:cl_id type:int default:0 desc:使用优惠券的id
  599. * @return name:coupon_cash type:float default:0 desc:券抵扣金额
  600. */
  601. public function getMyOrderList()
  602. {
  603. $status = input('status',-1);
  604. $where=[];
  605. if($status > -1) $where['o.status'] = $status;
  606. $where['o.uid'] = $this->uid;
  607. $where['o.is_deleted'] = 0;
  608. $list = Db::table('store_order o')
  609. ->field('o.id,o.order_no,o.pay_state,o.create_at,pay_at,pro_info,o.price_total,o.remark,source,integral')
  610. ->where($where)
  611. ->order('o.id desc')
  612. ->limit($this->off_set,$this->page_num)
  613. ->select();
  614. /* foreach ($list as &$v)
  615. {
  616. $v['pro_info'] = json_decode($v['pro_info'],true);
  617. }*/
  618. $this->success('获取成功',$list);
  619. }
  620. /**
  621. * @title 删除订单
  622. * @desc 删除订单
  623. * @author qc
  624. * @url /api/Order/delOrder
  625. * @method POST
  626. * @header name:Authorization require:1 desc:Token
  627. * @param name:id type:int require:1 default:1 desc:订单id
  628. */
  629. public function delOrder()
  630. {
  631. $order_id = input('post.id');
  632. $order_info = Db::table('store_order')
  633. ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
  634. ->find();
  635. if(empty($order_info)) $this->error('订单不存在');
  636. Db::table('store_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
  637. $this->success('删除成功');
  638. }
  639. /**
  640. * @title 确认收货
  641. * @desc 确认收货
  642. * @author qc
  643. * @url /api/Order/confirmReceipt
  644. * @method POST
  645. * @header name:Authorization require:1 desc:Token
  646. * @param name:id type:int require:1 default:1 desc:订单id
  647. */
  648. public function confirmReceipt(){
  649. $order_info = Db::table('store_order')
  650. ->where(['uid'=>$this->uid,'id'=>input('post.id')])
  651. ->find();
  652. if(empty($order_info) || !$order_info['status'] != 2) $this->error('商品未发货');
  653. Db::table('store_order')->where(['id'=>$order_info['id']])->update(['status'=>3]);
  654. $this->success('收货成功');
  655. }
  656. /**
  657. * @title 订单商品评论
  658. * @desc 订单商品评论
  659. * @author qc
  660. * @url /api/Order/orderEvaluate
  661. * @method POST
  662. * @header name:Authorization require:1 desc:Token
  663. * @param name:order_id type:int require:1 default:-- desc:订单order_id
  664. * @param name:goods_id type:int require:1 default:-- desc:商品id
  665. * @param name:goods_no type:int require:1 default:-- desc:商品货号
  666. * @param name:goods_score type:int require:1 default:-- desc:商品评分
  667. * @param name:content type:string require:1 default:-- desc:评论内容
  668. * @param name:images type:int require:1 default:-- desc:图片(多张逗号隔开)
  669. */
  670. public function orderEvaluate()
  671. {
  672. $order_id = input('post.order_id');
  673. $goods_id = input('post.goods_id');
  674. $goods_no = input('post.goods_no');
  675. $goods_score = input('post.goods_score');
  676. $images = input('post.images');
  677. $content = input('post.content');
  678. $eva_data =[
  679. 'order_id' => $order_id,
  680. 'goods_id' => $goods_id,
  681. 'goods_no' => $goods_no,
  682. 'goods_score' => $goods_score,
  683. 'images' => $images,
  684. 'content' => $content,
  685. ];
  686. Db::table('goods_evaluate')->insert($eva_data);
  687. $order_info = Db::table('order_goods')->where(['id'=>$order_id])->find();
  688. $pro_info = json_decode($order_info['pro_info'],true);
  689. foreach ($pro_info as $pk=>$pv) {
  690. if($pv['goods_id'] == $goods_id && $pv['goods_no'] == $goods_no) {
  691. $pro_info[$pk]['is_evaluate'] = 1;
  692. }
  693. }
  694. Db::table('order_goods')->where(['id'=>$order_id])->update(['status'=>4,'pro_info'=>json_encode($pro_info)]);
  695. }
  696. /**
  697. * @title 退款部分
  698. * @desc 退款部分
  699. */
  700. public function refundPart(){}
  701. /**
  702. * @title 获取平台退款原因
  703. * @desc 获取平台退款原因
  704. * @author qc
  705. * @url /api/Order/getRefundCause
  706. * @method GET
  707. * @header name:Authorization require:1 desc:Token
  708. * @return name:id type:int default:-- desc:记录id
  709. * @return name:title type:string default:-- desc:退款原因描述
  710. */
  711. public function getRefundCause(){
  712. $list = get_refund_cause();
  713. $this->success('ok',['list'=>$list]);
  714. }
  715. /**
  716. * @title 提交订单退款
  717. * @desc 提交订单退款
  718. * @author qc
  719. * @url /api/Order/refundOrder
  720. * @method POST
  721. * @header name:Authorization require:1 desc:Token
  722. * @param name:order_id type:int require:1 default:1 desc:订单id
  723. * @param name:cause_id type:int require:1 default:1 desc:退款原因id
  724. * @param name:apply_type type:int require:1 default:1 desc:申请类型 1退款,2退货退款
  725. * @param name:apply_money type:float require:1 default:1 desc:申请退回金额
  726. * @param name:re_desc type:string require:0 default:-- desc:描述
  727. * @param name:re_images type:string require:0 default:-- desc:退款图片最多三张(多张用逗号隔开)
  728. * @return name:re_id type:int require:0 default:0 desc:成功时返回退款记录id
  729. */
  730. public function refundOrder()
  731. {
  732. $order_id = input('post.order_id');
  733. $cause_id = input('post.cause_id');
  734. $apply_type = input('post.apply_type',1);
  735. $apply_money = input('post.apply_money');
  736. $re_desc = input('post.re_desc');
  737. $re_images = input('post.re_images');
  738. $ck_refund = Db::table('order_refund')
  739. ->where(['order_id'=>$order_id,'ck_status'=>0])
  740. ->value('id');
  741. if($ck_refund) $this->error('审核中,请勿重新申请');
  742. $ref_data = [
  743. 'uid' => $this->uid,
  744. 'order_id' => $order_id,
  745. 'apply_type' => $apply_type,
  746. 'apply_money' => $apply_money,
  747. 'create_at' => date("Y-m-d H:i:s"),
  748. 'cause_id' => $cause_id,
  749. 're_desc' => $re_desc,
  750. 're_images' => $re_images,
  751. ];
  752. Db::table('order_refund')->insert($ref_data);
  753. $re_id = Db::getLastInsID();
  754. // 更新订单表退款申请状态
  755. Db::table('store_order')->where('id',$order_id)->update(['refund_state'=>1]);
  756. $this->success('ok',['re_id'=>$re_id]);
  757. }
  758. /**
  759. * @title 获取提交订单退款详情
  760. * @desc 获取提交订单退款详情
  761. * @author qc
  762. * @url /api/Order/getRefundInfo
  763. * @method GET
  764. * @header name:Authorization require:1 desc:Token
  765. * @param name:id type:int require:1 default:1 desc:退款记录id
  766. * @return name:cause_id type:int default:1 desc:退款原因id
  767. * @return name:apply_type type:int default:1 desc:申请类型 1退款,2退货退款
  768. * @return name:apply_money type:float default:1 desc:申请退回金额
  769. * @return name:create_at type:string default:1 desc:申请时间
  770. * @return name:re_desc type:string default:-- desc:描述
  771. * @return name:re_images type:string default:-- desc:退款图片
  772. * @return name:pro_info type:string default:-- desc:申请订单的商品详情
  773. * @return name:pro_info.goods_id type:int default:-- desc:商品id
  774. * @return name:pro_info.goods_no type:string default:-- desc:商品货号
  775. * @return name:pro_info.sell_money type:string default:-- desc:售价
  776. * @return name:pro_info.spec_img type:string default:-- desc:封面
  777. * @return name:pro_info.is_evaluate type:string default:-- desc:商品是否评价
  778. * @return name:goods_num type:string default:-- desc:购买的商品数量
  779. * @return name:ck_status type:int default:0 desc:审核状态(0待审核 ,1 审核通过 , 2 审核不通过),
  780. * @return name:refund_money type:float default:0 desc:审核退款金额,
  781. * @return name:refund_state type:int default:0 desc:同意退款后的退款状态(0未退款,1待退款,2已退款,3退款失败)
  782. * @return name:refund_desc type:int default:0 desc:退款描述
  783. * @return name:wl_no type:string default:0 desc:退货物流单号 ( apply_type = 2)
  784. * @return name:wl_company type:string default:0 desc:退货物流公司( apply_type = 2)
  785. */
  786. public function getRefundInfo()
  787. {
  788. $id = input('post.id');
  789. $detail = Db::table('order_refund')
  790. ->field('r.*,o.pro_info,o.goods_num')
  791. ->alias('r')
  792. ->join('store_order o','r.order_id = o.id','LEFT')
  793. ->where(['r.order_id'=>$id])
  794. ->find();
  795. $re_cause = get_refund_cause();
  796. $detail['refund_desc'] = array_column($re_cause,null,'id')[$detail['cause_id']]['title'];
  797. $this->success('ok',['refund_detail'=>$detail]);
  798. }
  799. /**
  800. * @title 完善退货物流信息
  801. * @desc 完善退货物流信息
  802. * @author qc
  803. * @url /api/Order/completeLogistics
  804. * @method POST
  805. * @header name:Authorization require:1 desc:Token
  806. * @param name:id type:int require:1 default:-- desc:退款记录id
  807. * @param name:wl_no type:string require:1 default:-- desc:物流单号
  808. * @param name:wl_company type:string require:1 default:1 desc:物流公司
  809. */
  810. public function completeLogistics()
  811. {
  812. $id = input('post.id');
  813. $wl_no = input('post.wl_no');
  814. $wl_company = input('post.wl_company');
  815. $info = $detail = Db::table('order_refund')->find($id);
  816. if($info['apply_type'] == 1) $this->error('申请类型不符合,无需物流信息');
  817. // 更新物流信息
  818. Db::table('order_refund')->where(['id'=>$id])->update(['wl_no'=>$wl_no,'wl_company'=>$wl_company]);
  819. $this->success('ok');
  820. }
  821. /**
  822. * @title 修改订单退款(退货)申请信息
  823. * @desc 修改订单退款(退货)申请信息
  824. * @author qc
  825. * @url /api/Order/modifyRefundInfo
  826. * @method POST
  827. * @header name:Authorization require:1 desc:Token
  828. * @param name:id type:int require:1 default:1 desc:需要修改的退款申请id
  829. * @param name:cause_id type:int require:1 default:1 desc:退款原因id
  830. * @param name:apply_type type:int require:1 default:1 desc:申请类型 1退款,2退货退款
  831. * @param name:apply_money type:float require:1 default:1 desc:申请退回金额
  832. * @param name:re_desc type:string default:-- desc:描述
  833. * @param name:re_images type:string default:-- desc:退款图片最多三张(多张用逗号隔开)
  834. * @param name:wl_no type:string default:-- desc:物流单号
  835. * @param name:wl_company type:string default:1 desc:物流公司
  836. */
  837. public function modifyRefundInfo()
  838. {
  839. $id = input('post.id');
  840. $detail = Db::table('order_refund')->find($id);
  841. if($detail['ck_status'] != 0) $this->error('已审核无法修改,请重新提交');
  842. $up_data = [];
  843. if(input('post.cause_id')) $up_data['cause_id'] = input('post.cause_id');
  844. if(input('post.apply_type')) $up_data['apply_type'] = input('post.apply_type');
  845. if(input('post.apply_money')) $up_data['apply_money'] = input('post.apply_money');
  846. if(input('post.re_desc')) $up_data['re_desc'] = input('post.re_desc');
  847. if(input('post.re_images')) $up_data['re_images'] = input('post.re_images');
  848. if(input('post.wl_no')) $up_data['wl_no'] = input('post.wl_no');
  849. if(input('post.wl_company')) $up_data['wl_company'] = input('post.wl_company');
  850. if(!empty($up_data)) {
  851. Db::table('order_refund')->where(['id'=>$id])->update($up_data);
  852. }
  853. $this->success('修改成功');
  854. }
  855. /**
  856. * @title 取消申请退款(退货)
  857. * @desc 取消申请退款(退货)
  858. * @author qc
  859. * @url /api/Order/cancelRefund
  860. * @method POST
  861. * @header name:Authorization require:1 desc:Token
  862. * @param name:order_id type:int require:1 default:1 desc:订单id
  863. */
  864. public function cancelRefund(){
  865. $order_id = input('post.order_id');
  866. $re_info = Db::table('order_refund')->where(['uid'=>$this->uid,'order_id'=>$order_id,'ck_status'=>0])->count();
  867. if(!$re_info) $this->error('退款记录不存在');
  868. // 删除记录
  869. Db::table('order_refund')->where(['order_id'=>$order_id,'ck_status'=>0])->delete();
  870. $ck = Db::table('order_refund')->where(['order_id'=>$order_id])->find();
  871. $stat = empty($ck) ? 0 : 2 ;
  872. Db::table('store_order')->where(['id'=>$order_id])->update(['refund_state'=>$stat]);
  873. }
  874. /**
  875. * @title 获取提交订单退款列表 ???
  876. * @desc 获取提交订单退款列表 ???
  877. */
  878. public function getRefundList()
  879. {
  880. }
  881. }