Order.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\OrderComment;
  4. use app\common\model\ShoppingTrolley;
  5. use app\common\model\DeliveryAddress;
  6. use app\common\model\StoreGoods;
  7. use app\common\model\StoreGoodsItem;
  8. use app\common\model\GoodsOrder;
  9. use app\common\model\GoodsOrderItem;
  10. use app\common\model\User;
  11. use app\common\model\UserMessage;
  12. use app\common\service\ExpressService;
  13. use think\Db;
  14. use think\Exception;
  15. /**
  16. * @title 订单
  17. * @controller Order
  18. * @group base
  19. */
  20. class Order extends Base
  21. {
  22. public $int_rate = 100;
  23. public function initialize()
  24. {
  25. parent::initialize();
  26. parent::checkLogin();
  27. }
  28. /**
  29. * @title 订单确认页面(立即购买)
  30. * @desc 订单确认页面
  31. * @author qc
  32. * @url /api/Order/confirmOrderImmediately
  33. * @method GET
  34. * @header name:Authorization require:1 desc:Token
  35. * @param name:goods_id type:int require:1 default:0 desc:商品id
  36. * @param name:spec_id type:int require:1 default:0 desc:商品多规格的规格id
  37. * @param name:num type:int default:1 desc:数量
  38. * @return name:name type:string default:-- desc:商品名称
  39. * @return name:cover type:string default:-- desc:商品封面图
  40. * @return name:desc type:string default:-- desc:商品简述(副标题)
  41. * @return name:freight_type type:int default:-- desc:运费类型0固定金额,1运费模板
  42. * @return name:freight_id type:int default:-- desc:模板id(freight_type=1)
  43. * @return name:postage type:float default:-- desc:运费(freight_type=0)
  44. * @return name:total_price type:float default:-- desc:商品金额[不含运费]
  45. * @return name:freight type:float default:-- desc:运费
  46. * @return name:freight_code type:int default:1 desc:运费提示编号【1所选地址支持发货,0所选地址不支持发货】
  47. * @return name:freight_msg type:string default:-- desc:运费提示信息【freight_code=1忽略】
  48. * @return name:goods_no type:string default:-- desc:规格货号
  49. * @return name:goods_spec type:string default:-- desc:规格
  50. * @return name:sell_price type:float default:-- desc:售价
  51. * @return name:add_info type:array default:-- desc:默认收货地址(设置才有)
  52. * @return name:add_info.pro_id type:int default:-- desc:省id
  53. * @return name:add_info.city_id type:int default:-- desc:市id
  54. * @return name:add_info.county_id type:int default:-- desc:县区id
  55. * @return name:add_info.pro_name type:string default:-- desc:省名
  56. * @return name:add_info.city_name type:string default:-- desc:市名
  57. * @return name:add_info.county_name type:string default:-- desc:县区名
  58. * @return name:add_info.detail type:string default:-- desc:详细地址
  59. * @return name:add_info.phone type:string default:-- desc:联系电话
  60. * @return name:add_info.name type:string default:-- desc:联系人
  61. * @return name:freight type:float default:-- desc:运费
  62. * @return name:freight_code type:int default:1 desc:运费提示编号【1所选地址支持发货,0所选地址不支持发货】
  63. * @return name:freight_msg type:string default:-- desc:运费提示信息【freight_code=1忽略】
  64. */
  65. public function confirmOrderImmediately(){
  66. $goods_id = input('get.goods_id');
  67. $spec_id = input('get.spec_id');
  68. $num = input('get.num',1);
  69. $field = 'id,name,cover,desc,low_price,status,is_deleted,freight_type,postage,freight_id';
  70. $goods_info = StoreGoods::field($field)->where('id',$goods_id)->find()->toArray();
  71. $spec_info = StoreGoodsItem::where('id',$spec_id)->find()->toArray();
  72. $goods_info['total_price']= bcmul($spec_info['sell_price'],$num,2);
  73. $goods_info['goods_spec']= $spec_info['goods_spec'];
  74. $goods_info['goods_no']= $spec_info['goods_no'];
  75. $goods_info['sell_price']= $spec_info['sell_price'];
  76. $add_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
  77. $freight = 0;
  78. if($add_info){
  79. $goods_freight = ExpressService::getGoodsExpressPrice($goods_info,$add_info->id,$num);
  80. if($goods_freight['code'] !=200){
  81. $this->is_commit = false;
  82. $this->ret_msg = $goods_freight['msg'];
  83. }
  84. if($goods_freight['code'] == 200) $freight = $goods_freight['freight'];
  85. }
  86. $this->success('ok',['goods_info'=>$goods_info,
  87. 'add_info' =>$add_info ? $add_info->toArray():null,
  88. 'freight' =>$freight,
  89. 'freight_msg' => $this->ret_msg,
  90. 'freight_code' => $this->is_commit ? 1 :0
  91. ]);
  92. }
  93. /**
  94. * @title 生成订单(立即购买)
  95. * @desc 立即购买场景
  96. * @author qc
  97. * @url /api/Order/createOrderImmediately
  98. * @method POST
  99. * @header name:Authorization require:1 desc:Token
  100. * @param name:goods_id type:int require:1 default:0 desc:商品id
  101. * @param name:spec_id type:int require:1 default:0 desc:商品多规格的规格id
  102. * @param name:num type:int default:1 desc:数量
  103. * @param name:add_id type:int default:1 desc:收货地址id
  104. * @param name:remark type:string default:1 desc:订单备注
  105. * @return name:order_id type:int default:1 desc:订单id
  106. */
  107. public function createOrderImmediately()
  108. {
  109. $goods_id = input('post.goods_id');
  110. $spec_id = input('post.spec_id');
  111. $num = input('post.num');
  112. $add_id = input('post.add_id');
  113. $remark = input('post.remark');
  114. if(!$goods_id || !$spec_id) $this->error('请选择商品');
  115. if($num <=0 ) $this->error('数量有误');
  116. if(!$add_id ) $this->error('请选择收货地址');
  117. Db::startTrans();
  118. try {
  119. $add_info = DeliveryAddress::find($add_id)->toArray();
  120. $order_insert = [
  121. 'user_id' => $this->user_id,
  122. 'order_no' => get_order_sn(),
  123. 'goods_num' => $num,
  124. 'pro_name' => $add_info['pro_name'],
  125. 'city_name' => $add_info['city_name'],
  126. 'county_name' => $add_info['county_name'],
  127. 'street_name' => $add_info['street_name'],
  128. 'user_name' => $add_info['name'],
  129. 'add_detail' => $add_info['detail'],
  130. 'phone' => $add_info['phone'],
  131. 'remark' => $remark,
  132. ];
  133. $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
  134. return $query->where('id',$spec_id)->where('is_deleted',0);
  135. }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
  136. if(!$goods_info) throw new Exception('商品已下架');
  137. $goods_info = $goods_info->toArray();
  138. if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
  139. if($goods_info['item_list'][0]['stock'] < $num) throw new Exception('库存不足');
  140. $order_insert['original_price'] = bcmul($num,$goods_info['item_list'][0]['original_price'],2);// 原价
  141. $true_cost = $goods_info['item_list'][0]['sell_price'];//实际售价
  142. $spec_info = $goods_info['item_list'][0];
  143. $price_goods = bcmul($num,$true_cost,2); // 需要支付商品价格
  144. // 获取运费信息
  145. $exp_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$num);
  146. if($exp_data['code'] != 200 ) throw new Exception($exp_data['msg']);
  147. $price_express = $exp_data['freight']; // 商品运费
  148. //订单金额 = 商品金额 + 运费
  149. $order_money = bcadd($price_goods,$price_express ,2);
  150. if($order_money < 0) $this->exception('订单金额有误');
  151. $order_insert['price_total'] = $order_money;
  152. $order_insert['price_goods'] = $price_goods;
  153. $order_insert['price_express'] = $price_express;
  154. $order_info = GoodsOrder::create($order_insert);
  155. $order_item = [
  156. 'user_id' => $this->user_id,
  157. 'order_id' => $order_info->id,
  158. 'goods_id' => $goods_id,
  159. 'goods_no' => $spec_info['goods_no'],
  160. 'goods_spec' => $spec_info['goods_spec'],
  161. 'goods_name' => $goods_info['name'],
  162. 'spec_title' => $spec_info['spec_title'],
  163. 'spec_id' => $spec_info['id'],
  164. 'cover' => $goods_info['cover'],
  165. 'original_price' => $spec_info['original_price'],
  166. 'sell_price' => $true_cost,
  167. 'pay_price' => $order_money,
  168. 'num' => $num,
  169. ];
  170. GoodsOrderItem::create($order_item);
  171. StoreGoods::where('id',$goods_id)->setDec('stock',$num);
  172. StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
  173. Db::commit();
  174. }catch (\Exception $e){
  175. $this->is_commit = false;
  176. $this->ret_msg = $e->getMessage();
  177. Db::rollback();
  178. }
  179. $this->is_commit ? $this->success('添加成功',['order_id'=>$order_info->id]):$this->error($this->ret_msg);
  180. }
  181. /**
  182. * @title 生成订单(购物车)
  183. * @desc 购物车场景
  184. * @author qc
  185. * @url /api/Order/createOrderByTrolley
  186. * @method POST
  187. * @header name:Authorization require:1 desc:Token
  188. * @param name:ids type:int require:1 default:0 desc:购物车记录id
  189. * @param name:add_id type:int default:1 desc:收货地址id
  190. * @param name:remark type:string default:1 desc:订单备注
  191. * @return name:order_id type:int default:1 desc:订单id
  192. */
  193. public function createOrderByTrolley()
  194. {
  195. $ids = input('post.ids');
  196. $add_id = input('post.add_id');
  197. if(!$ids) $this->error('请选择商品');
  198. if(!$add_id ) $this->error('请选择收货地址');
  199. $ret_data = ['order_id'=>0];
  200. Db::startTrans();
  201. try{
  202. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  203. $trolley_list = ShoppingTrolley::with(['goodsSpec','goodsInfo'])->where('id','in',$ids)->select()->toArray();
  204. if(empty($trolley_list)) $this->exception('购物车记录有误');
  205. // 计算运费
  206. $price_express = 0;// 总运费
  207. $goods_list = ShoppingTrolley::where('id','in',$ids)->field('goods_id,sum(num) as total_num')->group('goods_id')->select()->toArray();
  208. foreach ($goods_list as $v){
  209. $goods_info = StoreGoods::field('name,freight_type,freight_id,postage')->where('id',$v['goods_id'])->find()->toArray();
  210. $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$v['total_num']);
  211. if($freight_data['code'] != 200){
  212. $this->exception($goods_info['name'].$freight_data['msg']);
  213. break;
  214. }
  215. $price_express += $freight_data['freight'];
  216. }
  217. // 计算运费 end
  218. $price_goods = 0; // 商品总金额
  219. $original_total = 0; // 商品总原价
  220. $total_num = 0;// 商品总数量
  221. $order_item = [];// 订单列表
  222. foreach ($trolley_list as $trolley)
  223. {
  224. $price_goods += $trolley['num'] * $trolley['goods_spec']['sell_price'];
  225. $original_total += $trolley['num'] * $trolley['goods_spec']['original_price'];
  226. $total_num +=$trolley['num'];
  227. $order_item[] = [
  228. 'user_id' => $this->user_id,
  229. 'goods_id' => $trolley['goods_id'],
  230. 'goods_no' => $trolley['goods_spec']['goods_no'],
  231. 'goods_name' => $trolley['goods_info']['name'],
  232. 'goods_spec' => $trolley['goods_spec']['goods_spec'],
  233. 'spec_title' => $trolley['goods_spec']['spec_title'],
  234. 'spec_id' => $trolley['spec_id'],
  235. 'cover' => $trolley['goods_info']['cover'],
  236. 'original_price' => $trolley['goods_spec']['original_price'],
  237. 'sell_price' => $trolley['goods_spec']['sell_price'],
  238. 'num' => $trolley['num'],
  239. 'trolley_id' => $trolley['id'],
  240. ];
  241. }
  242. $order_insert = [
  243. 'user_id' => $this->user_id,
  244. 'order_no' => get_order_sn(),
  245. 'goods_num' => $total_num,
  246. 'pro_name' => $add_info['pro_name'],
  247. 'city_name' => $add_info['city_name'],
  248. 'county_name' => $add_info['county_name'],
  249. 'user_name' => $add_info['name'],
  250. 'add_detail' => $add_info['detail'],
  251. 'phone' => $add_info['phone'],
  252. ];
  253. $price_total = bcadd($price_goods ,$price_express,2);
  254. $order_insert['price_total'] = $price_total;
  255. $order_insert['price_goods'] = $price_goods ;
  256. $order_insert['original_total'] = $original_total ;
  257. $order_insert['price_express'] = $price_express ;
  258. $order_info = GoodsOrder::create($order_insert);// 生成订单
  259. array_walk($order_item,function (&$v,$k)use ($order_info){
  260. $v['order_id'] = $order_info->id;
  261. });
  262. (new GoodsOrderItem())->insertAll($order_item);// 生成订单商品详情
  263. ShoppingTrolley::where('id','in',$ids)->delete();// 删除购物车
  264. $ret_data['order_id'] = $order_info->id;
  265. Db::commit();
  266. }catch (\Exception $e){
  267. $this->ret_msg = $e->getMessage();
  268. $this->is_commit = false;
  269. Db::rollback();
  270. }
  271. $this->transReturn($ret_data);
  272. }
  273. /**
  274. * @title 订单支付
  275. * @desc 订单支付
  276. * @author qc
  277. * @url /api/Order/payOrder
  278. * @method POST
  279. * @header name:Authorization require:1 desc:Token
  280. * @param name:order_id type:int require:1 default:0 desc:订单id
  281. * @param name:pay_type type:int require:1 default:0 desc:支付方式(1.h5微信,2.h5支付宝,3移动支付(微信),4移动支付(支付宝),5.h5微信内支付)
  282. * @return name:config type:array default:0 desc:小程序支付配置
  283. * @return name:config.appId type:string default:-- desc:公众账号ID(小程序支付)
  284. * @return name:config.signType type:string default:-- desc:签名类型(小程序支付)
  285. * @return name:config.paySign type:string default:-- desc:签名(小程序支付)
  286. * @return name:config.nonceStr type:string default:-- desc:随机字符串(小程序支付&&APP支付)
  287. * @return name:config.timestamp type:string default:-- desc:时间戳小程序支付&&APP支付)
  288. * @return name:config.partnerid type:string default:-- desc:商户号(App支付)
  289. * @return name:config.prepayid type:string default:-- desc:唯一支付号(App支付)
  290. * @return name:config.package type:string default:-- desc:package(App支付)
  291. * @return name:config.sign type:string default:-- desc:签名(App支付)
  292. * @return name:code_url type:string default:-- desc:付款码url
  293. */
  294. public function payOrder()
  295. {
  296. $order_id = input('post.order_id');
  297. $pay_type = input('post.pay_type');
  298. $order_info = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  299. if($order_info['status'] != 0) $this->error('订单状态错误');
  300. if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
  301. if($order_info['price_total'] <= 0) $this->error('订单金额错误');
  302. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();// 支付单号
  303. if(!$order_info['pay_no']) GoodsOrder::where('id',$order_id)->update(['pay_no'=>$pay_no]);
  304. $user_info = User::with('wallet')->where('id',$this->user_id)->find()->toArray();
  305. $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>''];
  306. Db::startTrans();
  307. GoodsOrder::where('id',$order_id)->update(['pay_type'=>$pay_type]);
  308. try {
  309. switch ($pay_type){
  310. case 1://微信
  311. $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
  312. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'MWEB');
  313. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  314. $pay_config['config']['mch_id'] = config('app.wx_pay')['mch_id'];
  315. $pay_config['config']['pay_no'] = $pay_no;
  316. $pay_config['config']['notify_url'] = $notify_url;
  317. $pay_config['config']['mweb_url'] = $pay_config['mweb_url'];
  318. $pay_config['config']['total_fee'] = $order_info['price_total'] * 100;
  319. $ret_data['config'] = $pay_config['config'];
  320. break;
  321. case 2:
  322. $notify_url = $this->request->root(true) . '/api/Alipay/goodsOrderNotify';
  323. $return_url ='https://'.$_SERVER['HTTP_HOST']."/dist/#/my";
  324. $config = Alipay::ali_pay_pc('订单支付',$pay_no,$order_info['price_total'],$notify_url,$return_url);
  325. $ret_data['config']['ali_url'] = $config;
  326. $ret_data['ali_url'] = $config;
  327. break;
  328. case 3: //移动支付(微信)
  329. $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
  330. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP');
  331. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  332. $ret_data['config'] = $pay_config['config'];
  333. break;
  334. case 4://移动支付(支付宝)
  335. $notify_url = $this->request->root(true) . '/api/Alipay/goodsOrderNotify';
  336. $config = Alipay::ali_pay('订单支付',$pay_no,$order_info['price_total'],$notify_url,);
  337. $ret_data['config']['ali_url'] = $config;
  338. $ret_data['ali_url'] = $config;
  339. break;
  340. case 5 :
  341. if(!$user_info['openid']) $this->exception('请绑定openid');
  342. $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
  343. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  344. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  345. $ret_data['config'] = $pay_config['config'];
  346. break;
  347. }
  348. Db::commit();
  349. }catch (\Exception $e){
  350. $this->is_commit = false;
  351. $this->ret_msg = $e->getMessage();
  352. Db::rollback();
  353. }
  354. $this->transReturn($ret_data);
  355. }
  356. /**
  357. * @title 获取订单列表
  358. * @desc 订单优惠:int_cash + coupon_cash
  359. * @author qc
  360. * @method GET
  361. * @url /api/Order/getOrderList
  362. * @header name:Authorization require:1 desc:Token
  363. * @param name:page type:int default:1 desc:页数
  364. * @param name:page_num type:int default:20 desc:每页数
  365. * @param name:status type:int default:-1 desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  366. * @param name:pay_state type:int default:-1 desc:支付状态(-1全部,0未支付,1已支付)
  367. * @param name:cancel_state type:int default:-1 desc:取消状态(-1全部,0未取消,1已取消)
  368. * @param name:apply_refund type:int default:-1 desc:申请退款状态(-1全部,0未申请||已取消,1已申请)
  369. * @return name:id type:int default:-- desc:订单id
  370. * @return name:order_no type:string default:-- desc:订单号
  371. * @return name:price_total type:float default:-- desc:待支付金额(所以优惠完价格+运费)
  372. * @return name:price_goods type:float default:-- desc:商品金额(用售价计算)
  373. * @return name:price_express type:float default:-- desc:运费金额
  374. * @return name:original_total type:float default:-- desc:总原价(下划线)
  375. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  376. * @return name:pay_type type:int default:-- desc:支付方式(1.h5微信,2.h5支付宝,3移动支付(微信),4移动支付(支付宝),5.h5微信内支付)
  377. * @return name:pay_no type:string default:-- desc:支付号
  378. * @return name:pay_at type:string default:-- desc:支付时间
  379. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  380. * @return name:cancel_at type:string default:-- desc:取消时间
  381. * @return name:cancel_desc type:string default:-- desc:取消原因
  382. * @return name:cancel_desc type:string default:-- desc:取消原因
  383. * @return name:refund_state type:int default:-- desc:退款状态(0未申请1待审核,2审核通过,3审核拒绝)
  384. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  385. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  386. * @return name:express_send_no type:string default:-- desc:物流单号
  387. * @return name:express_send_at type:string default:-- desc:发货时间
  388. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  389. * @return name:create_at type:string default:-- desc:下单时间
  390. * @return name:integral type:string default:-- desc:使用积分数量
  391. * @return name:int_cash type:float default:-- desc:使用积分抵扣金额
  392. * @return name:cl_ids type:string default:-- desc:使用优惠券id串
  393. * @return name:coupon_cash type:string default:-- desc:使用优惠券抵扣金额
  394. * @return name:blance type:string default:-- desc:使用余额支付金额
  395. * @return name:redu_money type:float default:-- desc:订单满减金额
  396. * @return name:remark type:string default:-- desc:订单备注
  397. * @return name:goods_num type:int default:-- desc:订单商品总数量
  398. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  399. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  400. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  401. * @return name:street_name type:string default:-- desc:街道名称(收货地址)
  402. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  403. * @return name:user_name type:string default:-- desc:收货人
  404. * @return name:phone type:string default:-- desc:收货人联系电话
  405. * @return name:act_type type:int default:-- desc:活动类型0无1秒杀2拼团
  406. * @return name:act_id type:int default:-- desc:活动id
  407. * @return name:share_user type:int default:-- desc:分享人id
  408. * @return name:order_item type:array default:-- desc:订单商品列表
  409. * @return name:order_item.name type:float default:-- desc:商品名
  410. * @return name:order_item.cover type:float default:-- desc:商品图
  411. * @return name:order_item.sell_price type:float default:-- desc:购买价格
  412. * @return name:order_item.goods_spec type:float default:-- desc:规格
  413. * @return name:order_item.num type:int default:-- desc:数量
  414. */
  415. public function getOrderList()
  416. {
  417. $status = input('get.status',-1);
  418. $pay_state = input('get.pay_state',-1);
  419. $cancel_state = input('get.cancel_state',-1);
  420. $refund_state = input('get.apply_refund',-1);
  421. $where = [];
  422. $where[] = ['user_id','=',$this->user_id];
  423. if($status > -1) $where[] = ['status','=',$status];
  424. if($pay_state > -1) $where[] = ['pay_state','=',$pay_state];
  425. if($cancel_state > -1) $where[] = ['cancel_state','=',$cancel_state];
  426. if($refund_state == 0) $where[] = ['refund_state','=',$refund_state];
  427. if($refund_state == 1) $where[] = ['refund_state','in','1,2,3,4'];
  428. $list = GoodsOrder::with(['orderItem'])->where($where)->order('id desc ')
  429. ->limit($this->off_set,$this->page_num)
  430. ->select()->toArray();
  431. $this->success('',['list'=>$list]);
  432. }
  433. /**
  434. * @title 获取订单详情
  435. * @desc 获取订单详情
  436. * @author qc
  437. * @method GET
  438. * @url /api/Order/getOrderDetail
  439. * @header name:Authorization require:1 desc:Token
  440. * @param name:order_id type:int default:-- desc:订单id
  441. * @return name:id type:int default:-- desc:订单id
  442. * @return name:order_no type:string default:-- desc:订单号
  443. * @return name:price_total type:float default:-- desc:待支付金额(商品价格+运费)
  444. * @return name:price_goods type:float default:-- desc:商品金额
  445. * @return name:price_express type:float default:-- desc:运费金额
  446. * @return name:original_total type:float default:-- desc:总原价(下划线)
  447. * @return name:original_total type:float default:-- desc:总原价(下划线)
  448. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  449. * @return name:pay_type type:int default:-- desc:支付方式(1.h5微信,2.h5支付宝,3移动支付(微信),4移动支付(支付宝),5.h5微信内支付)
  450. * @return name:pay_no type:string default:-- desc:支付号
  451. * @return name:pay_at type:string default:-- desc:支付时间
  452. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  453. * @return name:cancel_at type:string default:-- desc:取消时间
  454. * @return name:cancel_desc type:string default:-- desc:取消原因
  455. * @return name:refund_state type:int default:-- desc:退款状态(0未申请1待审核,2审核拒绝,3审核通过)
  456. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  457. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  458. * @return name:express_send_no type:string default:-- desc:物流单号
  459. * @return name:express_send_at type:string default:-- desc:发货时间
  460. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  461. * @return name:create_at type:string default:-- desc:下单时间
  462. * @return name:blance type:string default:-- desc:使用余额支付金额(余额抵扣)
  463. * @return name:remark type:string default:-- desc:订单备注
  464. * @return name:goods_num type:int default:-- desc:订单商品总数量
  465. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  466. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  467. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  468. * @return name:street_name type:string default:-- desc:街道名称(收货地址)
  469. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  470. * @return name:user_name type:string default:-- desc:收货人
  471. * @return name:phone type:string default:-- desc:收货人联系电话
  472. * @return name:order_item type:array default:-- desc:订单商品列表(按商品id分组)
  473. * @return name:order_item.name type:float default:-- desc:商品名
  474. * @return name:order_item.cover type:float default:-- desc:商品图
  475. * @return name:order_item.sell_price type:float default:-- desc:购买价格
  476. * @return name:order_item.goods_spec type:float default:-- desc:规格
  477. * @return name:order_item.num type:int default:-- desc:数量
  478. */
  479. public function getOrderDetail()
  480. {
  481. $order_id = input('get.order_id');
  482. $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
  483. $detail['order_item'] = (new GoodsOrderItem())->getOrderItem($order_id);
  484. $this->success('ok',['detail'=>$detail]);
  485. }
  486. /**
  487. * @title 计算商品运费(一个商品id)
  488. * @desc 计算商品运费(一个商品id)
  489. * @author qc
  490. * @url /api/Order/getGoodsFreight
  491. * @method GET
  492. * @header name:Authorization require:1 desc:Token
  493. * @param name:goods_id type:int require:1 default:0 desc:商品id
  494. * @param name:num type:int require:1 default:0 desc:商品数量
  495. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  496. * @return name:freight type:float require:0 default:0 desc运费金额
  497. */
  498. public function getGoodsFreight()
  499. {
  500. $goods_id = input('get.goods_id');
  501. $num = input('get.num');
  502. $add_id = input('get.add_id');
  503. $goods_info = StoreGoods::where('id',$goods_id)->find()->toArray();
  504. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  505. $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$num);
  506. if($freight_data['code'] != 200 ) $this->error($freight_data['msg']);
  507. $this->success('ok',['freight'=>$freight_data['freight']]);
  508. }
  509. /**
  510. * @title 计算购物车商品运费
  511. * @desc 计算购物车商品运费
  512. * @author qc
  513. * @url /api/Order/getTrolleyFreight
  514. * @method GET
  515. * @header name:Authorization require:1 desc:Token
  516. * @param name:ids type:int require:1 default:0 desc:购物车商品id串
  517. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  518. * @return name:freight type:float require:0 default:0 desc运费金额
  519. */
  520. public function getTrolleyFreight()
  521. {
  522. $trolley_ids = input('get.ids');
  523. $add_id = input('get.add_id');
  524. $goods_list = ShoppingTrolley::where('id','in',$trolley_ids)
  525. ->field('goods_id,sum(num) as total_num')
  526. ->group('goods_id')
  527. ->select()->toArray();
  528. $total_price = 0;
  529. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  530. foreach ($goods_list as $v){
  531. $goods_info = StoreGoods::field('name,freight_type,freight_id,postage')->where('id',$v['goods_id'])->find()->toArray();
  532. $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$v['total_num']);
  533. if($freight_data['code'] != 200){
  534. $this->error($goods_info['name'].$freight_data['msg']);
  535. break;
  536. }
  537. $total_price += $freight_data['freight'];
  538. }
  539. $this->success('ok',['freight'=>$total_price]);
  540. }
  541. /**
  542. * @title 取消订单
  543. * @desc 取消订单
  544. * @author qc
  545. * @url /api/Order/cancelOrder
  546. * @method POST
  547. * @header name:Authorization require:1 desc:Token
  548. * @param name:order_id type:int require:1 default:0 desc:订单id
  549. */
  550. public function cancelOrder()
  551. {
  552. $order_id = input('post.order_id');
  553. Db::startTrans();
  554. try {
  555. $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  556. if($detail['status'] != 0) $this->exception('订单状态有误');
  557. // 取消订单状态
  558. GoodsOrder::update(['status'=>9],['id'=>$order_id]);
  559. // 订单处理
  560. foreach ($detail['order_item'] as $item_info) {
  561. // 更改订单详情状态
  562. GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
  563. // 商品规格库存返回
  564. StoreGoodsItem::where('id',$item_info['spec_id'])->setInc('stock',$item_info['num']);
  565. // 商品总库存增加
  566. StoreGoods::where('id',$item_info['goods_id'])->setInc('stock',$item_info['num']);
  567. }
  568. Db::commit();
  569. }catch (\Exception $e){
  570. $this->ret_msg = $e->getMessage();
  571. $this->is_commit = false;
  572. Db::rollback();
  573. }
  574. $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
  575. }
  576. /**
  577. * @title 删除订单
  578. * @desc 删除订单
  579. * @author qc
  580. * @url /api/Order/deleteOrder
  581. * @method POST
  582. * @header name:Authorization require:1 desc:Token
  583. * @param name:order_id type:int require:1 default:0 desc:订单id
  584. */
  585. public function deleteOrder()
  586. {
  587. $order_id = input('post.order_id');
  588. $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  589. if($detail['status'] == 0) $this->error('请先取消订单');
  590. GoodsOrder::update(['is_deleted'=>1],['id'=>$order_id]);
  591. $this->success('删除成功');
  592. }
  593. /**
  594. * @title 更换订单收货地址
  595. * @desc 更换订单收货地址(仅待支付订单可以修改地址)
  596. * @author qc
  597. * @url /api/Order/changeOrderDelivery
  598. * @method POST
  599. * @header name:Authorization require:1 desc:Token
  600. * @param name:order_id type:int require:1 default:0 desc:订单id
  601. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  602. */
  603. public function changeOrderDelivery()
  604. {
  605. $order_id = input('post.order_id');
  606. $add_id = input('post.add_id');
  607. $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
  608. if($detail['status'] != 0) $this->error('操作错误');
  609. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  610. $order_update =[
  611. 'pro_name' => $add_info['pro_name'],
  612. 'city_name' => $add_info['city_name'],
  613. 'county_name' => $add_info['county_name'],
  614. 'street_name' => $add_info['street_name'],
  615. 'add_detail' => $add_info['detail'],
  616. 'user_name' => $add_info['name'],
  617. 'phone' => $add_info['phone'],
  618. ];
  619. GoodsOrder::update($order_update,['id'=>$order_id]);
  620. $this->success('修改成功');
  621. }
  622. /**
  623. * @title 确认收货
  624. * @desc 确认收货
  625. * @author qc
  626. * @url /api/Order/confirmReceipt
  627. * @method POST
  628. * @header name:Authorization require:1 desc:Token
  629. * @param name:order_id type:int require:1 default:0 desc:订单id
  630. */
  631. public function confirmReceipt()
  632. {
  633. $order_id = input('post.order_id');
  634. $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  635. if($detail['status'] == 0) $this->error('订单未支付');
  636. if($detail['status'] == 1) $this->error('订单未发货');
  637. if($detail['status'] == 3) $this->error('订单已收货');
  638. GoodsOrder::update(['status'=>3,'express_state'=>2],['id'=>$order_id]);
  639. GoodsOrderItem::update(['status'=>3,'express_state'=>2],['order_id'=>$order_id]);
  640. UserMessage::create(['user_id'=>$this->user_id,'type_id'=>3,'relation_id'=>$order_id,'content'=>'订单收货成功']);
  641. $this->success('已确认收货');
  642. }
  643. /**
  644. * @title 订单评价
  645. * @desc 订单评价
  646. * @author qc
  647. * @url /api/Order/orderComment
  648. * @method POST
  649. * @header name:Authorization require:1 desc:Token
  650. * @param name:item_id type:int require:1 default:0 desc:订单item表id
  651. * @param name:content type:string require:1 default:0 desc:评论内容
  652. * @param name:images type:string default:0 desc:图片
  653. * @param name:video type:string default:0 desc:视频
  654. * @param name:goods_core type:int default:0 desc:商品评分
  655. * @param name:wl_core type:int default:0 desc:物流评分
  656. * @param name:serve_core type:int default:0 desc:服务评分
  657. */
  658. public function orderComment()
  659. {
  660. $item_id = input('post.item_id','');
  661. $content = input('post.content','');
  662. $images = input('post.images','');
  663. $video = input('post.video','');
  664. $goods_core = input('post.goods_core','');
  665. $wl_core = input('post.wl_core','');
  666. $serve_core = input('post.serve_core','');
  667. $item = GoodsOrderItem::get($item_id);
  668. if(!$item) $this->error('订单不存在');
  669. $order_info = GoodsOrder::where('id',$item->order_id)->find();
  670. if($order_info->status < 3) $this->error('请先确认收货');
  671. GoodsOrderItem::where('id',$item->id)->update(['status'=>4]);
  672. GoodsOrder::where('id',$item->order_id)->update(['status'=>4]);
  673. $data = [
  674. 'user_id' => $this->user_id,
  675. 'order_id' =>$item->order_id,
  676. 'goods_id' =>$item->goods_id,
  677. 'spec_id' =>$item->spec_id,
  678. 'content' =>$content,
  679. 'images' =>$images,
  680. 'video' =>$video,
  681. 'goods_core' =>$goods_core,
  682. 'wl_core' =>$wl_core,
  683. 'serve_core' =>$serve_core,
  684. ];
  685. OrderComment::create($data);
  686. $this->success('评论成功');
  687. }
  688. /**
  689. * @title 获取物流详情
  690. * @desc 获取物流详情
  691. * @author qc
  692. * @url /api/Order/deliveryDetails
  693. * @method GET
  694. * @header name:Authorization require:1 desc:Token
  695. * @param name:order_id type:string require:1 default:-- desc:订单id
  696. * @return name:number type:string default:-- desc:单号
  697. * @return name:type type:string default:-- desc:快递类型
  698. * @return name:list type:array default:-- desc:数据列表
  699. * @return name:list.time type:string default:-- desc:时间
  700. * @return name:list.status type:string default:-- desc:内容
  701. * @return name:deliverystatus type:int default:-- desc:0:快递收件(揽件)1.在途中2.正在派件3.已签收4.派送失败5.疑难件6.退件签收
  702. * @return name:issign type:int default:-- desc:是否签收(1.是否签收)
  703. * @return name:expName type:string default:-- desc:快递公司名称
  704. * @return name:expSite type:string default:-- desc:快递公司官网
  705. * @return name:expPhone type:string default:-- desc:快递公司电话
  706. * @return name:courier type:string default:-- desc:快递员 或 快递站(没有则为空)
  707. * @return name:courierPhone type:string default:-- desc:快递员电话 (没有则为空)
  708. * @return name:updateTime type:string default:-- desc:快递轨迹信息最新时间
  709. * @return name:takeTime type:string default:-- desc:发货到收货消耗时长 (截止最新轨迹)
  710. * @return name:logo type:string default:-- desc:快递公司LOGO
  711. */
  712. public function deliveryDetails(){
  713. $send_no = GoodsOrder::where('id',input('get.order_id'))->value('express_send_no');
  714. if(!$send_no) $this->error('订单没有发货');
  715. $data = get_delivery($send_no);
  716. $this->success('ok',$data);
  717. }
  718. }