Order.php 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\CouponConfig;
  4. use app\common\model\DiscountActivity;
  5. use app\common\model\GroupActivity;
  6. use app\common\model\GroupGoods;
  7. use app\common\model\GroupGoodsItem;
  8. use app\common\model\GroupLeader;
  9. use app\common\model\OrderBill;
  10. use app\common\model\OrderComment;
  11. use app\common\model\ReductionActivity;
  12. use app\common\model\SeckillActivity;
  13. use app\common\model\SeckillGoods;
  14. use app\common\model\SeckillGoodsItem;
  15. use app\common\model\ShoppingTrolley;
  16. use app\common\model\DeliveryAddress;
  17. use app\common\model\StoreGoods;
  18. use app\common\model\StoreGoodsItem;
  19. use app\common\model\GoodsOrder;
  20. use app\common\model\GoodsOrderItem;
  21. use app\common\model\StoreOrderRefund;
  22. use app\common\model\User;
  23. use app\common\model\UserCouponList;
  24. use app\common\model\UserMessage;
  25. use app\common\model\UserWallet;
  26. use app\common\service\Activity;
  27. use app\common\service\ExpressService;
  28. use app\common\service\OrderCallback;
  29. use EasyWeChat\Factory;
  30. use library\tools\Data;
  31. use think\cache\driver\Redis;
  32. use think\Db;
  33. use think\Exception;
  34. use function EasyWeChat\Kernel\data_get;
  35. /**
  36. * @title 订单
  37. * @controller Order
  38. * @group base
  39. */
  40. class Order extends Base
  41. {
  42. public $int_rate = 100;
  43. public function initialize()
  44. {
  45. parent::initialize();
  46. parent::checkLogin();
  47. }
  48. /**
  49. * @title 订单确认页面(立即购买)
  50. * @desc 订单确认页面
  51. * @author qc
  52. * @url /api/Order/confirmOrderImmediately
  53. * @method GET
  54. * @header name:Authorization require:1 desc:Token
  55. * @param name:goods_id type:int require:1 default:0 desc:商品id
  56. * @param name:spec_id type:int require:1 default:0 desc:商品多规格的规格id
  57. * @param name:num type:int default:1 desc:数量
  58. * @return name:name type:string default:-- desc:商品名称
  59. * @return name:cover type:string default:-- desc:商品封面图
  60. * @return name:desc type:string default:-- desc:商品简述(副标题)
  61. * @return name:freight_type type:int default:-- desc:运费类型0固定金额,1运费模板
  62. * @return name:freight_id type:int default:-- desc:模板id(freight_type=1)
  63. * @return name:postage type:float default:-- desc:运费(freight_type=0)
  64. * @return name:share_money type:float default:-- desc:分享返利金额
  65. * @return name:total_price type:float default:-- desc:订单金额
  66. * @return name:total_group type:float default:-- desc:拼团订单金额
  67. * @return name:total_seckill type:float default:-- desc:秒杀订单金额
  68. * @return name:total_discount type:float default:-- desc:折扣订单金额
  69. * @return name:item_list type:array default:-- desc:商品多规格列表
  70. * @return name:item_list.spec_info type:array default:-- desc:规格型号设置;
  71. * @return name:item_list.goods_no type:string default:-- desc:规格货号
  72. * @return name:item_list.goods_title:string default:-- desc:规格标题
  73. * @return name:item_list.cover type:string default:-- desc:规格封面
  74. * @return name:item_list.stock type:int default:-- desc:剩余库存
  75. * @return name:item_list.original_price type:float default:-- desc:原价
  76. * @return name:item_list.sell_price type:float default:-- desc:售价
  77. * @return name:item_list.seckill_price type:float default:-- desc:秒杀价(没有秒杀活动=售价)
  78. * @return name:item_list.group_price type:float default:-- desc:拼团价(没有拼团活动=售价)
  79. * @return name:item_list.discount_price type:float default:-- desc:折扣价(没有折扣活动=售价)
  80. * @return name:discount type:array default:-- desc:折扣活动详情(为空:商品没有正在进行的折扣活动)
  81. * @return name:seckill type:array default:-- desc:秒杀活动详情(为空:商品没有正在进行的秒杀活动)
  82. * @return name:seckill.start_time type:string default:-- desc:开始时间
  83. * @return name:seckill.end_time type:string default:-- desc:结束时间
  84. * @return name:group type:array default:-- desc:拼团活动
  85. * @return name:group.start_time type:string default:-- desc:开始时间
  86. * @return name:group.end_time type:string default:-- desc:结束时间
  87. * @return name:group.full_num type:int default:-- desc:成团人数
  88. * @return name:reduction type:array default:-- desc:满减活动
  89. * @return name:reduction.id type:int default:-- desc:满减活动id
  90. * @return name:reduction.title type:string default:-- desc:满减标题
  91. * @return name:reduction.start_time type:string default:-- desc:满减开始时间
  92. * @return name:reduction.end_time type:string default:-- desc:满减结束时间
  93. * @return name:reduction.act_set type:array default:-- desc:满减设置
  94. * @return name:reduction.act_set.full type:float default:-- desc:满多少
  95. * @return name:reduction.act_set.reduction type:float default:-- desc:减多少
  96. * @return name:goods_discount type:array default:-- desc:商品优惠
  97. * @return name:goods_discount.order_cash type:float default:-- desc:积分最高抵扣订单金额
  98. * @return name:goods_discount.coupon_list type:array default:-- desc:优惠券列表(单品可以只让用一张)
  99. * @return name:coupon_list.id type:int default:-- desc:优惠券列表id
  100. * @return name:coupon_list.low_amount type:float default:-- desc:订单最低金额
  101. * @return name:coupon_list.amount type:float default:-- desc:可抵扣金额
  102. * @return name:add_info type:array default:-- desc:默认收货地址(设置才有)
  103. * @return name:add_info.pro_id type:int default:-- desc:省id
  104. * @return name:add_info.city_id type:int default:-- desc:市id
  105. * @return name:add_info.county_id type:int default:-- desc:县区id
  106. * @return name:add_info.pro_name type:string default:-- desc:省名
  107. * @return name:add_info.city_name type:string default:-- desc:市名
  108. * @return name:add_info.county_name type:string default:-- desc:县区名
  109. * @return name:add_info.street_name type:string default:-- desc:街道名称
  110. * @return name:add_info.detail type:string default:-- desc:详细地址
  111. * @return name:add_info.phone type:string default:-- desc:联系电话
  112. * @return name:add_info.name type:string default:-- desc:联系人
  113. *
  114. */
  115. public function confirmOrderImmediately()
  116. {
  117. $user_id = $this->user_id;
  118. $goods_id = input('get.goods_id');
  119. $spec_id = input('get.spec_id');
  120. $num = input('get.num',1);
  121. $act_type = input('get.act_type',0);
  122. $act_id = input('get.act_id',0);
  123. $field = 'id,name,cover,desc,low_price,status,is_deleted,level_set,freight_type,postage';
  124. $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
  125. return $query->where('id',$spec_id);
  126. }])->field($field)->where('id',$goods_id)->find()->toArray();
  127. $act_data = Activity::goodsActivityList($goods_id);
  128. $goods_info['item_list'] = $goods_info['item_list'][0];
  129. // 秒杀价格
  130. $goods_info['item_list']['seckill_price'] = $goods_info['item_list']['sell_price'];
  131. if(!empty($act_data['seckill']['goods'])){
  132. foreach ($act_data['seckill']['goods']['item'] as $sv){
  133. if($sv['spec_id'] == $spec_id){
  134. $goods_info['item_list']['seckill_price'] = $sv['seckill_price'];
  135. break;
  136. }
  137. }
  138. }
  139. // 拼团价格
  140. $goods_info['item_list']['group_price'] = $goods_info['item_list']['sell_price'];
  141. if(!empty($act_data['group']['goods'])){
  142. foreach ($act_data['group']['goods']['item'] as $gv){
  143. if($gv['spec_id'] == $spec_id){
  144. $goods_info['item_list']['group_price'] = $gv['group_price'];
  145. break;
  146. }
  147. }
  148. }
  149. // 折扣活动
  150. $goods_info['item_list']['discount_price'] = $goods_info['item_list']['sell_price'];
  151. if(!empty($act_data['discount']['act'])){
  152. $goods_info['item_list']['discount_price'] = bcmul($goods_info['item_list']['sell_price'],$act_data['discount']['act']['discount']/100,2);
  153. }
  154. $level_set = $goods_info['level_set'] ? json_decode($goods_info['level_set'],true):[];
  155. // 商品优惠信息
  156. $goods_discount = [
  157. 'order_cash' => 0,
  158. 'express_cash' => 0,
  159. 'coupon_list'=> [],
  160. ];
  161. $user_info = User::field('u.id,u.level_id,w.integral,money')->alias('u')
  162. ->leftJoin('UserWallet w','u.id = w.user_id')
  163. ->where('u.id',$this->user_id)->find()->toArray();
  164. if($user_info['level_id'] > 0 && !empty($level_set))
  165. {
  166. $goods_discount['order_cash'] = isset($level_set[$user_info['level_id']]) ? $level_set[$user_info['level_id']]['integral_cash']:0;
  167. // $goods_discount['express_cash'] = isset($level_set[$user_info['level_id']]) ? $level_set[$user_info['level_id']]['integral_freight']:0;
  168. }
  169. // 优惠券 全场限时 || 全场不限时 || 商品券限时 || 商品券不限时
  170. $goods_discount['coupon_list'] = UserCouponList::where(function ($query)use($goods_id,$user_id){
  171. return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
  172. })->whereOr(function ($query)use($goods_id,$user_id){
  173. return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',0]]);
  174. })->whereOr(function ($query)use($goods_id,$user_id){
  175. return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_id]]);
  176. })->select()->toArray();
  177. $goods_info['total_price']= bcmul($goods_info['item_list']['sell_price'],$num,2);
  178. $goods_info['total_seckill']= bcmul($goods_info['item_list']['seckill_price'],$num,2);
  179. $goods_info['total_group']= bcmul($goods_info['item_list']['group_price'],$num,2);
  180. $goods_info['total_discount']= bcmul($goods_info['item_list']['discount_price'],$num,2);
  181. $add_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
  182. $this->success('ok',['goods_info'=>$goods_info,
  183. 'seckill'=>$act_data['seckill']['act'],
  184. 'group'=>$act_data['group']['act'],
  185. 'discount'=>$act_data['discount']['act'],
  186. 'goods_discount'=>$goods_discount,
  187. 'user_info'=>$user_info,
  188. 'reduction'=>ReductionActivity::getAct(),
  189. 'add_info' =>$add_info ? $add_info->toArray():'',
  190. ]);
  191. }
  192. /**
  193. * @title 生成订单(立即购买)
  194. * @desc 立即购买场景
  195. * @author qc
  196. * @url /api/Order/createOrderImmediately
  197. * @method POST
  198. * @header name:Authorization require:1 desc:Token
  199. * @param name:goods_id type:int require:1 default:0 desc:商品id
  200. * @param name:spec_id type:int require:1 default:0 desc:商品多规格的规格id
  201. * @param name:num type:int default:1 desc:数量
  202. * @param name:act_type type:int default:0 desc:活动类型(0正常购买,1限时秒杀(抢购),2拼团,3折扣)
  203. * @param name:act_id type:int default:0 desc:活动id(活动类型act_type>0必传)
  204. * @param name:leader_id type:int default:0 desc:参与团的id(为0是新开团)
  205. * @param name:share_user type:int default:0 desc:分享人(谁分享给你的)
  206. * @param name:integral_order type:int default:0 desc:积分(抵扣订单的)
  207. * @param name:coupon_id type:int default:0 desc:优惠券id
  208. * @param name:add_id type:int default:1 desc:收货地址id
  209. * @param name:pay_city type:int default:0 desc:本次登录的城市
  210. * @return name:order_id type:int default:1 desc:订单id
  211. */
  212. public function createOrderImmediately()
  213. {
  214. $goods_id = input('post.goods_id');
  215. $spec_id = input('post.spec_id');
  216. $num = input('post.num');
  217. $add_id = input('post.add_id');
  218. $act_type = input('post.act_type',0);
  219. $act_id = input('post.act_id',0);
  220. $share_user = input('post.share_user',0);
  221. $integral_order = input('post.integral_order',0);
  222. $integral_express = input('post.integral_express',0);
  223. $coupon_id = input('post.coupon_id',0);// 优惠券id
  224. $leader_id = input('post.leader_id',0);// 参与拼团的id
  225. if(!$goods_id || !$spec_id) $this->error('请选择商品');
  226. if($num <=0 ) $this->error('数量有误');
  227. if(!$add_id ) $this->error('请选择收货地址');
  228. Db::startTrans();
  229. try {
  230. $add_info = DeliveryAddress::find($add_id)->toArray();
  231. $order_insert = [
  232. 'user_id' => $this->user_id,
  233. 'order_no' => get_order_sn(),
  234. 'goods_num' => $num,
  235. 'pro_name' => $add_info['pro_name'],
  236. 'city_name' => $add_info['city_name'],
  237. 'county_name' => $add_info['county_name'],
  238. 'street_name' => $add_info['street_name'],
  239. 'user_name' => $add_info['name'],
  240. 'add_detail' => $add_info['detail'],
  241. 'phone' => $add_info['phone'],
  242. 'act_type' => $act_type,
  243. 'act_id' => $act_id,
  244. 'share_user' => $share_user,
  245. 'leader_id' => $leader_id,
  246. 'cl_ids' => $coupon_id,
  247. 'express_cash' => 0,
  248. 'coupon_cash' => 0,
  249. ];
  250. $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
  251. return $query->where('id',$spec_id)->where('is_deleted',0);
  252. }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
  253. if(!$goods_info) throw new Exception('商品已下架');
  254. $goods_info = $goods_info->toArray();
  255. if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
  256. //if($goods_info['item_list'][0]['depot_item_info']['stock'] < $num) throw new Exception('库存不足');
  257. $order_insert['original_price'] = bcmul($num,$goods_info['item_list'][0]['original_price'],2);// 原价
  258. $true_cost = $goods_info['item_list'][0]['sell_price'];//实际售价
  259. $spec_info = $goods_info['item_list'][0];
  260. // 秒杀购买
  261. if($act_type == 1){
  262. if(!$act_id) $this->exception('参数错误');
  263. $act_info = SeckillActivity::where('id',$act_id)->find()->toArray();
  264. if($act_info['status'] != 1 || $act_info['is_deleted'] == 1 || strtotime($act_info['end_time'])<time()) $this->exception('活动已结束');
  265. if(strtotime($act_info['start_time']) > time()) $this->exception('活动尚未开始请耐心等待');
  266. // 商品详情
  267. $seckill_goods_id = SeckillGoods::where(['goods_id'=>$goods_id,'act_id'=>$act_id])->value('id');
  268. if(!$seckill_goods_id) $this->exception('商品信息错误');
  269. $item = SeckillGoodsItem::where(['act_id'=>$act_id,'goods_id'=>$goods_id,'spec_id'=>$spec_id])->find()->toArray();
  270. if($item['status'] != 1) $this->exception('商品已下架');
  271. // redis 验证库存
  272. // $redis_prefix = 'SECKILL_'.$act_id.'_'.$item['id'];
  273. // $redis = new Redis();
  274. //$redis_stock = $redis->get($redis_prefix.'_stock',0);
  275. // if($redis_stock <= 0 || $redis_stock < $num || $item['stock'] < $num) $this->exception('库存不足');
  276. //$redis->dec($redis_prefix.'_stock',$num);// 扣减redis库存
  277. $true_cost = $item['seckill_price'];
  278. }
  279. //拼团购买
  280. if($act_type == 2){
  281. if(!$act_id) $this->exception('参数错误');
  282. $act_info = GroupActivity::where('id',$act_id)->find()->toArray();
  283. if($act_info['status'] != 1 || $act_info['is_deleted'] == 1 || strtotime($act_info['end_time'])<time()) $this->exception('活动已结束');
  284. if(strtotime($act_info['start_time']) > time()) $this->exception('活动尚未开始请耐心等待');
  285. $group_goods_id = GroupGoods::where(['goods_id'=>$goods_id,'act_id'=>$act_id])->value('id');
  286. if(!$group_goods_id) $this->exception('商品信息错误');
  287. $item = GroupGoodsItem::where(['act_id'=>$act_id,'goods_id'=>$goods_id,'spec_id'=>$spec_id])->find()->toArray();
  288. if($item['status'] != 1) $this->exception('商品已下架');
  289. // redis 验证库存
  290. /* $redis_prefix = 'GROUP_'.$act_id.'_';
  291. $redis = new Redis();
  292. $redis_stock = $redis->get($redis_prefix.$item['id'].'_stock',0);
  293. if($redis_stock <= 0 || $redis_stock < $num || $item['stock'] < $num) $this->exception('库存不足');
  294. */
  295. if($leader_id){
  296. // if($redis->get($redis_prefix.$leader_id,0)) $this->exception('该团已有团员参与');
  297. // $redis->set($redis_prefix.$leader_id,1);
  298. GroupLeader::where(['id'=>$leader_id])->update(['status'=>3]);
  299. }
  300. // $redis->dec($redis_prefix.'_stock',$num);// 扣减redis库存
  301. $true_cost = $item['group_price'];
  302. }
  303. // 折扣购买
  304. if($act_type == 3){
  305. if(!$act_id) $this->exception('参数错误');
  306. $act_info = DiscountActivity::where('id',$act_id)->find()->toArray();
  307. if($act_info['status'] != 1 || $act_info['is_deleted'] == 1 || strtotime($act_info['end_time'])<time()) $this->exception('活动已结束');
  308. if(strtotime($act_info['start_time']) > time()) $this->exception('活动尚未开始请耐心等待');
  309. $true_cost = bcmul($goods_info['item_list'][0]['sell_price'] , $act_info['discount']/100,2);
  310. }
  311. $price_goods = bcmul($num,$true_cost,2); // 需要支付商品价格
  312. // 积分抵扣金额
  313. $total_integral = $integral_order + $integral_express;// 积分使用数量
  314. if($total_integral > 0){
  315. $wallet_integral = UserWallet::where(['user_id'=>$this->user_id])->value('integral');
  316. if($total_integral > $wallet_integral) $this->exception('积分不足');
  317. }
  318. $integral_cash = bcdiv($total_integral,$this->int_rate,2);// 积分抵扣订单金额(全部)
  319. if($integral_express >0) $order_insert['express_cash'] = bcdiv($integral_express,$this->int_rate,2);// 积分抵扣运费金额
  320. // 优惠券
  321. if($coupon_id){
  322. $coupon_info = UserCouponList::where(function ($query)use($coupon_id){
  323. return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',0],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
  324. })->whereOr(function ($query)use($coupon_id){
  325. return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',0],['type','=',0]]);
  326. })->whereOr(function ($query)use($goods_id,$coupon_id){
  327. return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_id],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
  328. })->whereOr(function ($query)use($goods_id,$coupon_id){
  329. return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_id],['type','=',0]]);
  330. })->find();
  331. if(!$coupon_info) $this->exception('优惠券过期');
  332. if($coupon_info->low_amount < $price_goods) $this->exception('优惠券使用限制');
  333. UserCouponList::where('id',$coupon_id)->update(['status'=>1]);
  334. $order_insert['cl_ids'] = $coupon_id;
  335. $order_insert['coupon_cash'] = $coupon_info->amount; // 优惠券抵扣金额
  336. }
  337. // 获取运费信息
  338. $exp_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$num);
  339. if($exp_data['code'] != 200 ) throw new Exception($exp_data['msg']);
  340. $price_express = $exp_data['freight']; // 商品运费
  341. if($price_express < $order_insert['express_cash'] ) $this->exception('运费抵扣金额有误');
  342. $redu_money = ReductionActivity::getReduction($price_goods);// 商品满减
  343. $total_discount = $integral_cash + $order_insert['coupon_cash'] + $redu_money;
  344. //订单金额 = 商品金额 + 运费 - (积分抵扣 + 优惠券 + 满减)
  345. $order_money = bcsub($price_goods+$price_express,$total_discount ,2);
  346. if($order_money < 0) $this->exception('订单金额有误');
  347. $order_insert['price_total'] = $order_money;
  348. $order_insert['price_goods'] = $price_goods;
  349. $order_insert['price_express'] = $price_express;
  350. $order_insert['integral'] = $total_integral;
  351. $order_insert['total_discount'] = $total_discount ;
  352. $order_insert['redu_money'] = $redu_money;
  353. $order_info = GoodsOrder::create($order_insert);
  354. // 扣除积分
  355. if($total_integral) UserWallet::userIntegralChange($this->user_id,$total_integral,'订单抵扣',1,-1,$order_info->id);
  356. $order_item = [
  357. 'user_id' => $this->user_id,
  358. 'order_id' => $order_info->id,
  359. 'goods_id' => $goods_id,
  360. 'goods_no' => $spec_info['goods_no'],
  361. 'goods_spec' => $spec_info['goods_spec'],
  362. 'goods_name' => $goods_info['name'],
  363. 'spec_title' => $spec_info['spec_title'],
  364. 'spec_id' => $spec_info['id'],
  365. 'is_combo' => $goods_info['is_combo'],
  366. 'cover' => $goods_info['cover'],
  367. 'original_price' => $spec_info['original_price'],
  368. 'sell_price' => $true_cost,
  369. 'pay_price' => $order_money,
  370. 'num' => $num,
  371. 'depot_id' => $goods_info['depot_id'],
  372. 'belong' => StoreGoods::getGoodsBelong($goods_id),
  373. ];
  374. GoodsOrderItem::create($order_item);
  375. StoreGoods::where('id',$goods_id)->setDec('stock',$num);
  376. StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
  377. /* switch ($act_type){
  378. case 0: // 商城购买
  379. StoreGoods::where('id',$goods_id)->setDec('stock',$num);
  380. StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
  381. break;
  382. case 1: // 秒杀购买
  383. SeckillGoodsItem::where('id',$item['id'])->setDec('stock',$num);
  384. break;
  385. case 2:// 拼团购买
  386. GroupGoodsItem::where('id',$item['id'])->setDec('stock',$num);
  387. break;
  388. }*/
  389. Db::commit();
  390. }catch (\Exception $e){
  391. $this->is_commit = false;
  392. $this->ret_msg = $e->getMessage();
  393. Db::rollback();
  394. }
  395. $this->is_commit ? $this->success('添加成功',['order_id'=>$order_info->id]):$this->error($this->ret_msg);
  396. }
  397. /**
  398. * @title 订单支付
  399. * @desc 订单支付
  400. * @author qc
  401. * @url /api/Order/payOrder
  402. * @method POST
  403. * @header name:Authorization require:1 desc:Token
  404. * @param name:order_id type:int require:1 default:0 desc:订单id
  405. * @param name:pay_type type:int require:1 default:0 desc:支付方式(1微信小程序 ,2佣金余额 ,3移动支付(支付宝),4移动支付(微信),5pc(微信扫码),6pc(支付宝扫码))
  406. * @return name:config type:array default:0 desc:小程序支付配置
  407. * @return name:config.appId type:string default:-- desc:公众账号ID(小程序支付)
  408. * @return name:config.signType type:string default:-- desc:签名类型(小程序支付)
  409. * @return name:config.paySign type:string default:-- desc:签名(小程序支付)
  410. * @return name:config.nonceStr type:string default:-- desc:随机字符串(小程序支付&&APP支付)
  411. * @return name:config.timestamp type:string default:-- desc:时间戳小程序支付&&APP支付)
  412. * @return name:config.partnerid type:string default:-- desc:商户号(App支付)
  413. * @return name:config.prepayid type:string default:-- desc:唯一支付号(App支付)
  414. * @return name:config.package type:string default:-- desc:package(App支付)
  415. * @return name:config.sign type:string default:-- desc:签名(App支付)
  416. * @return name:code_url type:string default:-- desc:付款码url
  417. */
  418. public function payOrder()
  419. {
  420. $order_id = input('post.order_id');
  421. $pay_type = input('post.pay_type');
  422. $order_info = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  423. if($order_info['status'] != 0) $this->error('订单状态错误');
  424. if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
  425. if($order_info['price_total'] <= 0) $this->error('订单金额错误');
  426. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();// 支付单号
  427. if(!$order_info['pay_no']) GoodsOrder::where('id',$order_id)->update(['pay_no'=>$pay_no]);
  428. $user_info = User::with('wallet')->where('id',$this->user_id)->find()->toArray();
  429. $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>''];
  430. Db::startTrans();
  431. try {
  432. switch ($pay_type){
  433. case 1://微信小程序
  434. $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
  435. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  436. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  437. $ret_data['config'] = $pay_config['config'];
  438. break;
  439. case 2://佣金余额
  440. if($user_info['wallet']['money'] < $order_info['price_total'])$this->exception('余额不足');
  441. // 扣除余额
  442. UserWallet::userMoneyChange($this->user_id,$order_info['price_total'],'订单支付',1,-1,$order_id);
  443. // 支付完成后回调
  444. $back_res = OrderCallback::goodsOrderCallBack($order_info,$pay_type);
  445. if(!$back_res['ret_val']) $this->exception($back_res['msg']);
  446. $ret_data['pay_status'] = 1;
  447. break;
  448. case 3://移动支付(支付宝)
  449. break;
  450. case 4: //4移动支付(微信)
  451. $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
  452. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP');
  453. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  454. $ret_data['config'] = $pay_config['config'];
  455. break;
  456. case 5: //pc(微信扫码)
  457. $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
  458. $app = Factory::payment(config('app.wx_pay'));
  459. $result = $app->order->unify([
  460. 'body' => '微信扫码支付',
  461. 'out_trade_no' => $pay_no,
  462. 'total_fee' => $order_info['price_total'] * 100,
  463. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  464. 'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
  465. ]);
  466. if(!isset($result['code_url'])) $this->exception($result['err_code_des']?$result['err_code_des']:'获取付款码失败');
  467. $ret_data['code_url'] = $result['code_url'];
  468. break;
  469. case 6: //pc(支付宝扫码)
  470. break;
  471. }
  472. Db::commit();
  473. }catch (\Exception $e){
  474. $this->is_commit = false;
  475. $this->ret_msg = $e->getMessage();
  476. Db::rollback();
  477. }
  478. $this->transReturn($ret_data);
  479. }
  480. /**
  481. * @title 订单确认页面(购物车)
  482. * @desc 订单确认页面
  483. * @author qc
  484. * @url /api/Order/confirmOrderByTrolley
  485. * @method GET
  486. * @header name:Authorization require:1 desc:Token
  487. * @param name:ids type:int require:1 default:0 desc:购物车记录id
  488. * @return name:goods_id type:int default:-- desc:商品id
  489. * @return name:goods_name type:string default:-- desc:商品名称
  490. * @return name:cover type:string default:-- desc:商品封面
  491. * @return name:goods_status type:int default:-- desc:商品状态(1上架,0下架)
  492. * @return name:total_price type:float default:-- desc:该商品价格(这个商品下面所有规格商品)
  493. * @return name:integral_cash type:float default:-- desc:该商品积分可抵扣金额
  494. * @return name:deleted_status type:string default:-- desc:删除状态(1删除,0未删除)
  495. * @return name:item_list type:array default:-- desc:添加该商品的规格列表
  496. * @return name:item_list.id type:int default:-- desc:购物车记录id
  497. * @return name:item_list.num type:int default:-- desc:数量
  498. * @return name:item_list.goods_id type:int default:-- desc:商品id
  499. * @return name:item_list.spec_id type:int default:-- desc:规格id
  500. * @return name:item_list.goods_no type:string default:-- desc:货号
  501. * @return name:item_list.spec_title type:string default:-- desc:规格名称
  502. * @return name:item_list.sell_price type:flaot default:-- desc:售价
  503. * @return name:item_list.original_trolley type:flaot default:-- desc:规格总原价(下划线用[原价*数量])
  504. * @return name:item_list.sell_trolley type:flaot default:-- desc:规格总价(售价*数量)
  505. * @return name:item_list.original_price type:float default:-- desc:规格原价(下划线价格)
  506. * @return name:item_list.sell_price type:float default:-- desc:规格售价
  507. * @return name:coupon_list type:array default:-- desc:满减券
  508. * @return name:coupon_list.title type:string default:-- desc:满减券标题
  509. * @return name:coupon_list.low_amount type:float default:-- desc:最低消费金额
  510. * @return name:coupon_list.amount type:float default:-- desc:抵扣金额
  511. * @return name:order_money type:float default:-- desc:订单商品金额(不含运费)
  512. * @return name:total_integral_money type:float default:-- desc:积分可抵扣金额(订单最高)
  513. * @return name:reduction type:array default:-- desc:满减活动
  514. * @return name:reduction.id type:int default:-- desc:满减活动id
  515. * @return name:reduction.title type:string default:-- desc:满减标题
  516. * @return name:reduction.start_time type:string default:-- desc:满减开始时间
  517. * @return name:reduction.end_time type:string default:-- desc:满减结束时间
  518. * @return name:reduction.act_set type:array default:-- desc:满减设置
  519. * @return name:reduction.act_set.full type:float default:-- desc:满多少
  520. * @return name:reduction.act_set.reduction type:float default:-- desc:减多少
  521. * @return name:redu_money type:float default:-- desc:满减金额
  522. * @return name:add_info type:array default:-- desc:默认收货地址(设置才有)
  523. * @return name:add_info.pro_id type:int default:-- desc:省id
  524. * @return name:add_info.city_id type:int default:-- desc:市id
  525. * @return name:add_info.county_id type:int default:-- desc:县区id
  526. * @return name:add_info.pro_name type:string default:-- desc:省名
  527. * @return name:add_info.city_name type:string default:-- desc:市名
  528. * @return name:add_info.county_name type:string default:-- desc:县区名
  529. * @return name:add_info.street_name type:string default:-- desc:街道名称
  530. * @return name:add_info.detail type:string default:-- desc:详细地址
  531. * @return name:add_info.phone type:string default:-- desc:联系电话
  532. * @return name:add_info.name type:string default:-- desc:联系人
  533. */
  534. public function confirmOrderByTrolley()
  535. {
  536. $ids = input('get.ids');
  537. $user_id = $this->user_id;
  538. // 购物车数据分组
  539. $trolley_goods = ShoppingTrolley::field('goods_id')->where('user_id',$this->user_id)->where('id','in',$ids)->group('goods_id')->select();
  540. $goods_ids = array_column($trolley_goods->toArray(),'goods_id');
  541. // 用户等级&&积分
  542. $user_info = User::field('u.id,u.level_id,w.integral,money')->alias('u')
  543. ->leftJoin('UserWallet w','u.id = w.user_id')
  544. ->where('u.id',$this->user_id)->find()->toArray();
  545. $list = [];
  546. $field = 'id,name,cover,status,is_deleted,is_combo,level_set';
  547. $goods_list = StoreGoods::field($field)->where('id','in',$goods_ids)->select()->toArray();
  548. $total_integral_money = 0;
  549. $order_money = 0;// 订单商品金额
  550. foreach ($goods_list as $goods_info){
  551. $trolley_data = ['goods_id'=>$goods_info['id'],'cover'=>$goods_info['cover'],'goods_name'=>$goods_info['name'],'goods_status'=>$goods_info['status'],'deleted_status'=>$goods_info['is_deleted'],'is_combo'=>$goods_info['is_combo']];
  552. $level_set = $goods_info['level_set'] ? json_decode($goods_info['level_set'],true):[];
  553. $join_field = 't.id,t.num,t.spec_id,t.goods_id,i.goods_spec,i.goods_no,i.spec_title,i.stock,i.is_deleted,i.original_price,i.sell_price,i.weight';
  554. $item_list = ShoppingTrolley::field($join_field)->alias('t')
  555. ->where('t.goods_id',$goods_info['id'])->where('t.id','in',$ids)
  556. ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
  557. ->select()->toArray();
  558. foreach ($item_list as &$item) {
  559. $item['original_trolley'] = bcmul($item['num'],$item['original_price'],2);// 下划线价格
  560. $item['sell_trolley'] = bcmul($item['num'],$item['sell_price'],2);// 售价
  561. }
  562. $trolley_data['total_price'] =array_sum(array_column($item_list,'sell_trolley'));// 该商品总价格
  563. $trolley_data['item_list'] = $item_list;
  564. $order_money = $order_money + $trolley_data['total_price'];
  565. // 商品券
  566. /*$trolley_data['goods_coupon'] = UserCouponList::field('id,title,low_amount,type,coupon_type')->where(function ($query)use($goods_info,$user_id){
  567. return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_info['id']],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
  568. })->whereOr(function ($query)use($goods_info,$user_id){
  569. return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_info['id']],['type','=',0]]);
  570. })->select()->toArray();*/
  571. $goods_num = array_sum(array_column($item_list,'num'));
  572. if($user_info['level_id'] > 0 && !empty($level_set)) {
  573. $trolley_data['integral_cash'] = isset($level_set[$user_info['level_id']]) ? $level_set[$user_info['level_id']]['integral_cash']:0;
  574. $total_integral_money = bcadd($total_integral_money, $trolley_data['integral_cash'] ,2);
  575. }
  576. $list[] = $trolley_data;
  577. }
  578. $coupon_list = UserCouponList::field('id,title,low_amount,amount,type,coupon_type')->where(function ($query)use($user_id){
  579. return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
  580. })->whereOr(function ($query)use($user_id){
  581. return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',0]]);
  582. })->select()->toArray();
  583. $redu_money = ReductionActivity::getReduction($order_money);
  584. $add_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
  585. $this->success('ok',[
  586. 'list'=>$list,
  587. 'coupon_list'=>$coupon_list,
  588. 'order_money'=>$order_money,
  589. 'total_integral_money'=>$total_integral_money,
  590. 'reduction' => ReductionActivity::getAct(),
  591. 'redu_money' => $redu_money,
  592. 'add_info' => $add_info ? $add_info->toArray():'',
  593. ]);
  594. }
  595. /**
  596. * @title 生成订单(购物车[活动没有购物车])
  597. * @desc 购物车场景
  598. * @author qc
  599. * @url /api/Order/createOrderByTrolley
  600. * @method POST
  601. * @header name:Authorization require:1 desc:Token
  602. * @param name:ids type:int require:1 default:0 desc:购物车记录id
  603. * @param name:add_id type:int default:1 desc:收货地址id
  604. * @param name:coupon_ids type:int default:1 desc:使用优惠券id(多个用逗号隔开)
  605. * @param name:total_integral type:int default:0 desc:使用积分数量
  606. * @param name:pay_city type:int default:0 desc:本次登录的城市
  607. * @return name:order_id type:int default:1 desc:订单id
  608. */
  609. public function createOrderByTrolley()
  610. {
  611. $ids = input('post.ids');
  612. $add_id = input('post.add_id');
  613. $coupon_ids = input('post.coupon_ids');
  614. $total_integral = input('post.total_integral');
  615. if(!$ids) $this->error('请选择商品');
  616. if(!$add_id ) $this->error('请选择收货地址');
  617. $ret_data = ['order_id'=>0];
  618. Db::startTrans();
  619. try{
  620. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  621. $trolley_list = ShoppingTrolley::with(['goodsSpec','goodsInfo'])->where('id','in',$ids)->select()->toArray();
  622. if(empty($trolley_list)) $this->exception('购物车记录有误');
  623. // 计算运费
  624. $price_express = 0;// 总运费
  625. $goods_list = ShoppingTrolley::where('id','in',$ids)->field('goods_id,sum(num) as total_num')->group('goods_id')->select()->toArray();
  626. foreach ($goods_list as $v){
  627. $goods_info = StoreGoods::field('name,freight_type,freight_id,postage')->where('id',$v['goods_id'])->find()->toArray();
  628. $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$v['total_num']);
  629. if($freight_data['code'] != 200){
  630. $this->exception($goods_info['name'].$freight_data['msg']);
  631. break;
  632. }
  633. $price_express += $freight_data['freight'];
  634. }
  635. // 计算运费 end
  636. $price_goods = 0; // 商品总金额
  637. $original_total = 0; // 商品总原价
  638. $total_num = 0;// 商品总数量
  639. $order_item = [];// 订单列表
  640. foreach ($trolley_list as $trolley)
  641. {
  642. $price_goods += $trolley['num'] * $trolley['goods_spec']['sell_price'];
  643. $original_total += $trolley['num'] * $trolley['goods_spec']['original_price'];
  644. $total_num +=$trolley['num'];
  645. $order_item[] = [
  646. 'user_id' => $this->user_id,
  647. 'goods_id' => $trolley['goods_id'],
  648. 'goods_no' => $trolley['goods_spec']['goods_no'],
  649. 'goods_name' => $trolley['goods_info']['name'],
  650. 'goods_spec' => $trolley['goods_spec']['goods_spec'],
  651. 'spec_title' => $trolley['goods_spec']['spec_title'],
  652. 'spec_id' => $trolley['spec_id'],
  653. 'is_combo' => $trolley['goods_info']['is_combo'],
  654. 'cover' => $trolley['goods_info']['cover'],
  655. 'original_price' => $trolley['goods_spec']['original_price'],
  656. 'sell_price' => $trolley['goods_spec']['sell_price'],
  657. 'num' => $trolley['num'],
  658. 'depot_id' => $trolley['goods_info']['depot_id'],
  659. 'trolley_id' => $trolley['id'],
  660. 'belong' => StoreGoods::getGoodsBelong($trolley['goods_id']),
  661. ];
  662. }
  663. $order_insert = [
  664. 'user_id' => $this->user_id,
  665. 'order_no' => get_order_sn(),
  666. 'goods_num' => $total_num,
  667. 'pro_name' => $add_info['pro_name'],
  668. 'city_name' => $add_info['city_name'],
  669. 'county_name' => $add_info['county_name'],
  670. 'street_name' => $add_info['street_name'],
  671. 'user_name' => $add_info['name'],
  672. 'add_detail' => $add_info['detail'],
  673. 'phone' => $add_info['phone'],
  674. 'integral' => $total_integral,
  675. 'int_cash' => 0,
  676. 'cl_ids' => $coupon_ids,
  677. 'coupon_cash' => 0,
  678. ];
  679. // 积分抵扣金额
  680. if($total_integral > 0){
  681. $wallet_integral = UserWallet::where(['user_id'=>$this->user_id])->value('integral');
  682. if($total_integral > $wallet_integral) $this->exception('积分不足');
  683. $order_insert['int_cash'] = bcdiv($total_integral,$this->int_rate,2);// 积分抵扣订单金额(全部)
  684. }
  685. if($coupon_ids){
  686. $coupon_list = CouponConfig::field('id,low_amount,amount')->where('id','in',$coupon_ids)
  687. ->where('status','=','0')->order('low_amount desc')->select()->toArray();
  688. if($price_goods < $coupon_list[0]['low_amount']) $this->exception('订单金额最低:'. $coupon_list[0]['low_amount']);
  689. $order_insert['coupon_cash'] = array_sum(array_column($coupon_list,'amount'));
  690. }
  691. if($price_goods < $order_insert['int_cash']+$order_insert['coupon_cash']) $this->exception('订单金额有误');
  692. $redu_money = ReductionActivity::getReduction($price_goods);// 商品满减金额
  693. $total_discount = $order_insert['int_cash'] + $order_insert['coupon_cash']+$redu_money;
  694. $price_total = bcsub($price_goods + $price_express,$total_discount,2);
  695. $order_insert['price_total'] = $price_total;
  696. $order_insert['price_goods'] = $price_goods ;
  697. $order_insert['original_total'] = $original_total ;
  698. $order_insert['price_express'] = $price_express ;
  699. $order_insert['total_discount'] = $total_discount ;
  700. $order_insert['redu_money'] = $redu_money ;
  701. $order_info = GoodsOrder::create($order_insert);// 生成订单
  702. array_walk($order_item,function (&$v,$k)use($order_info,$total_discount){
  703. $v['order_id'] = $order_info->id;
  704. if($total_discount > 0){
  705. $v['pay_price'] = $v['sell_price']*$v['num'] - bcmul($total_discount , bcdiv($v['sell_price']*$v['num'],$order_info->price_goods,2),2) ;
  706. }
  707. });
  708. (new GoodsOrderItem())->saveAll($order_item);// 生成订单商品详情
  709. ShoppingTrolley::where('id','in',$ids)->delete();// 删除购物车
  710. $ret_data['order_id'] = $order_info->id;
  711. Db::commit();
  712. }catch (\Exception $e){
  713. $this->ret_msg = $e->getMessage();
  714. $this->is_commit = false;
  715. Db::rollback();
  716. }
  717. $this->transReturn($ret_data);
  718. }
  719. /**
  720. * @title 计算商品运费(一个商品id)
  721. * @desc 计算商品运费(一个商品id)
  722. * @author qc
  723. * @url /api/Order/getGoodsFreight
  724. * @method GET
  725. * @header name:Authorization require:1 desc:Token
  726. * @param name:goods_id type:int require:1 default:0 desc:商品id
  727. * @param name:num type:int require:1 default:0 desc:商品数量
  728. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  729. * @return name:freight type:float require:0 default:0 desc运费金额
  730. */
  731. public function getGoodsFreight()
  732. {
  733. $goods_id = input('get.goods_id');
  734. $num = input('get.num');
  735. $add_id = input('get.add_id');
  736. $goods_info = StoreGoods::where('id',$goods_id)->find()->toArray();
  737. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  738. $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$num);
  739. if($freight_data['code'] != 200 ) $this->error($freight_data['msg']);
  740. $this->success('ok',['freight'=>$freight_data['freight']]);
  741. }
  742. /**
  743. * @title 计算购物车商品运费
  744. * @desc 计算购物车商品运费
  745. * @author qc
  746. * @url /api/Order/getTrolleyFreight
  747. * @method GET
  748. * @header name:Authorization require:1 desc:Token
  749. * @param name:ids type:int require:1 default:0 desc:购物车商品id串
  750. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  751. * @return name:freight type:float require:0 default:0 desc运费金额
  752. */
  753. public function getTrolleyFreight()
  754. {
  755. $trolley_ids = input('get.ids');
  756. $add_id = input('get.add_id');
  757. $goods_list = ShoppingTrolley::where('id','in',$trolley_ids)
  758. ->field('goods_id,sum(num) as total_num')
  759. ->group('goods_id')
  760. ->select()->toArray();
  761. $total_price = 0;
  762. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  763. foreach ($goods_list as $v){
  764. $goods_info = StoreGoods::field('name,freight_type,freight_id,postage')->where('id',$v['goods_id'])->find()->toArray();
  765. $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$v['total_num']);
  766. if($freight_data['code'] != 200){
  767. $this->error($goods_info['name'].$freight_data['msg']);
  768. break;
  769. }
  770. $total_price += $freight_data['freight'];
  771. }
  772. $this->success('ok',['freight'=>$total_price]);
  773. }
  774. /**
  775. * @title 获取订单列表
  776. * @desc 订单优惠:int_cash + coupon_cash
  777. * @author qc
  778. * @method GET
  779. * @url /api/Order/getOrderList
  780. * @header name:Authorization require:1 desc:Token
  781. * @param name:page type:int default:1 desc:页数
  782. * @param name:page_num type:int default:20 desc:每页数
  783. * @param name:status type:int default:-1 desc:状态
  784. * @param name:apply_refund type:int default:-1 desc:申请退款状态(-1全部,0未申请||已取消,1已申请)
  785. * @return name:id type:int default:-- desc:订单id
  786. * @return name:order_no type:string default:-- desc:订单号
  787. * @return name:price_total type:float default:-- desc:待支付金额(所以优惠完价格+运费)
  788. * @return name:price_goods type:float default:-- desc:商品金额(用售价计算)
  789. * @return name:price_express type:float default:-- desc:运费金额
  790. * @return name:original_total type:float default:-- desc:总原价(下划线)
  791. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  792. * @return name:pay_type type:int default:-- desc:支付方式(1微信2余额3支付宝)
  793. * @return name:pay_no type:string default:-- desc:支付号
  794. * @return name:pay_at type:string default:-- desc:支付时间
  795. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  796. * @return name:cancel_at type:string default:-- desc:取消时间
  797. * @return name:cancel_desc type:string default:-- desc:取消原因
  798. * @return name:cancel_desc type:string default:-- desc:取消原因
  799. * @return name:refund_state type:int default:-- desc:退款状态(0未申请 1待审核,2审核通过,3审核拒绝)
  800. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  801. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  802. * @return name:express_send_no type:string default:-- desc:物流单号
  803. * @return name:express_send_at type:string default:-- desc:发货时间
  804. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  805. * @return name:create_at type:string default:-- desc:下单时间
  806. * @return name:integral type:string default:-- desc:使用积分数量
  807. * @return name:int_cash type:float default:-- desc:使用积分抵扣金额
  808. * @return name:cl_ids type:string default:-- desc:使用优惠券id串
  809. * @return name:coupon_cash type:string default:-- desc:使用优惠券抵扣金额
  810. * @return name:blance type:string default:-- desc:使用余额支付金额
  811. * @return name:redu_money type:float default:-- desc:订单满减金额
  812. * @return name:remark type:string default:-- desc:订单备注
  813. * @return name:goods_num type:int default:-- desc:订单商品总数量
  814. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  815. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  816. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  817. * @return name:street_name type:string default:-- desc:街道名称(收货地址)
  818. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  819. * @return name:user_name type:string default:-- desc:收货人
  820. * @return name:phone type:string default:-- desc:收货人联系电话
  821. * @return name:act_type type:int default:-- desc:活动类型0无1秒杀2拼团
  822. * @return name:act_id type:int default:-- desc:活动id
  823. * @return name:share_user type:int default:-- desc:分享人id
  824. * @return name:order_item type:array default:-- desc:订单商品列表
  825. * @return name:order_item.name type:float default:-- desc:商品名
  826. * @return name:order_item.cover type:float default:-- desc:商品图
  827. * @return name:order_item.sell_price type:float default:-- desc:购买价格
  828. * @return name:order_item.goods_spec type:float default:-- desc:规格
  829. * @return name:order_item.num type:int default:-- desc:数量
  830. * @return name:order_bill type:array default:-- desc:订单开票数据(为空则未申请)
  831. * @return name:order_bill.header type:string require:1 default:0 desc:抬头
  832. * @return name:order_bill.type type:string require:1 default:0 desc:类型(string)
  833. * @return name:order_bill.email type:string require:1 default:0 desc:邮箱(string)
  834. * @return name:order_bill.bank type:string require:1 default:0 desc:开户行(string)
  835. * @return name:order_bill.card_no type:string require:1 default:0 desc:账号(string)
  836. * @return name:order_bill.phone type:string require:1 default:0 desc:电话(string)
  837. * @return name:order_bill.status type:int require:1 default:0 desc:0待开票1已开票
  838. * @return name:order_refund type:array require:1 default:0 desc:退款申请信息
  839. * @return name:order_refund.status type:int default:0 desc:审核状态(0待审核1同意退款2审核拒绝3通过切退款4退款异常5取消申请)
  840. * @return name:order_refund.is_deleted type:int default:0 desc:是否已删除(0否1是)
  841. */
  842. public function getOrderList()
  843. {
  844. $status = input('get.status',-1);
  845. $refund_state = input('get.apply_refund',-1);
  846. $where = [];
  847. $where[] = ['user_id','=',$this->user_id];
  848. if($status > -1) $where[] = ['status','=',$status];
  849. if($refund_state == 0) $where[] = ['refund_state','=',$refund_state];
  850. if($refund_state == 1) $where[] = ['refund_state','in','1,2,3,4'];
  851. $list = GoodsOrder::with(['orderItem','orderBill','orderRefund'])->where($where)->order('id desc ')
  852. ->limit($this->off_set,$this->page_num)
  853. ->select()->toArray();
  854. $this->success('',['list'=>$list]);
  855. }
  856. /**
  857. * @title 获取订单详情
  858. * @desc 获取订单详情
  859. * @author qc
  860. * @method GET
  861. * @url /api/Order/getOrderDetail
  862. * @header name:Authorization require:1 desc:Token
  863. * @param name:order_id type:int default:-- desc:订单id
  864. * @return name:id type:int default:-- desc:订单id
  865. * @return name:order_no type:string default:-- desc:订单号
  866. * @return name:price_total type:float default:-- desc:待支付金额(所以优惠完价格+运费)
  867. * @return name:price_goods type:float default:-- desc:商品金额
  868. * @return name:price_express type:float default:-- desc:运费金额
  869. * @return name:original_total type:float default:-- desc:总原价(下划线)
  870. * @return name:original_total type:float default:-- desc:总原价(下划线)
  871. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  872. * @return name:pay_type type:int default:-- desc:支付方式(1微信2余额3支付宝)
  873. * @return name:pay_no type:string default:-- desc:支付号
  874. * @return name:pay_at type:string default:-- desc:支付时间
  875. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  876. * @return name:cancel_at type:string default:-- desc:取消时间
  877. * @return name:cancel_desc type:string default:-- desc:取消原因
  878. * @return name:cancel_desc type:string default:-- desc:取消原因
  879. * @return name:refund_state type:int default:-- desc:退款状态(0未申请1待审核,2审核拒绝,3审核通过)
  880. * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
  881. * @return name:express_company_title type:string default:-- desc:发货快递公司名称
  882. * @return name:express_send_no type:string default:-- desc:物流单号
  883. * @return name:express_send_at type:string default:-- desc:发货时间
  884. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
  885. * @return name:create_at type:string default:-- desc:下单时间
  886. * @return name:integral type:string default:-- desc:使用积分数量
  887. * @return name:cl_ids type:string default:-- desc:使用优惠券id串
  888. * @return name:coupon_cash type:string default:-- desc:使用优惠券抵扣金额
  889. * @return name:blance type:string default:-- desc:使用余额支付金额(余额抵扣)
  890. * @return name:redu_money type:float default:-- desc:订单满减金额
  891. * @return name:remark type:string default:-- desc:订单备注
  892. * @return name:goods_num type:int default:-- desc:订单商品总数量
  893. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  894. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  895. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  896. * @return name:street_name type:string default:-- desc:街道名称(收货地址)
  897. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  898. * @return name:user_name type:string default:-- desc:收货人
  899. * @return name:phone type:string default:-- desc:收货人联系电话
  900. * @return name:act_type type:int default:-- desc:活动类型0无1秒杀2拼团
  901. * @return name:act_id type:int default:-- desc:活动id
  902. * @return name:share_user type:int default:-- desc:分享人id
  903. * @return name:order_item type:array default:-- desc:订单商品列表(按商品id分组)
  904. * @return name:order_item.name type:float default:-- desc:商品名
  905. * @return name:order_item.cover type:float default:-- desc:商品图
  906. * @return name:order_item.sell_price type:float default:-- desc:购买价格
  907. * @return name:order_item.goods_spec type:float default:-- desc:规格
  908. * @return name:order_item.num type:int default:-- desc:数量
  909. */
  910. public function getOrderDetail()
  911. {
  912. $order_id = input('get.order_id');
  913. $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
  914. $detail['order_item'] = (new GoodsOrderItem())->getOrderItem($order_id);
  915. $this->success('',['detail'=>$detail]);
  916. }
  917. /**
  918. * @title 取消订单
  919. * @desc 取消订单
  920. * @author qc
  921. * @url /api/Order/cancelOrder
  922. * @method POST
  923. * @header name:Authorization require:1 desc:Token
  924. * @param name:order_id type:int require:1 default:0 desc:订单id
  925. */
  926. public function cancelOrder()
  927. {
  928. $order_id = input('post.order_id');
  929. Db::startTrans();
  930. try {
  931. $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  932. if($detail['status'] != 0) $this->exception('订单状态有误');
  933. // 取消订单状态
  934. GoodsOrder::update(['status'=>9],['id'=>$order_id]);
  935. // 优惠券退回
  936. if($detail['cl_ids']) UserCouponList::where(['id','in',$detail['cl_ids']])->update(['status'=>0]);
  937. // 积分退回
  938. if($detail['integral'] > 0) UserWallet::userIntegralChange($detail['user_id'],$detail['integral'],'订单取消退回',2,1,$detail['id']);
  939. // 余额退回
  940. if($detail['blance'] > 0) UserWallet::userMoneyChange($detail['user_id'],$detail['blance'],'订单取消退回',2, 1,$detail['id']);
  941. // 订单处理
  942. foreach ($detail['order_item'] as $item_info) {
  943. // 更改订单详情状态
  944. GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
  945. // 商品规格库存返回
  946. StoreGoodsItem::where('id',$item_info['spec_id'])->setInc('stock',$item_info['num']);
  947. // 商品总库存增加
  948. StoreGoods::where('id',$item_info['goods_id'])->setInc('stock',$item_info['num']);
  949. }
  950. if($detail['act_type'] == 2 && $detail['leader_id']) {
  951. GroupLeader::where(['id'=>$detail['leader_id']])->update(['status'=>0]);
  952. }
  953. /* switch ($detail['act_type']){
  954. case 1:
  955. // $redis = new Redis();
  956. // $redis_prefix = 'SECKILL_'.$detail['act_id'].'_';
  957. foreach ($detail['order_item'] as $item_info) {
  958. // 更改订单详情状态
  959. GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
  960. // 商品规格库存返回
  961. $item = SeckillGoodsItem::where(['act_id'=>$detail['act_id'],'goods_id'=>$item_info['goods_id'],'spec_id'=>$item_info['spec_id']])->find()->toArray();
  962. SeckillGoodsItem::where(['id'=>$item['id']])->setInc('stock',$item_info['num']);
  963. // $redis->inc($redis_prefix.$item['id'].'_stock',$item_info['num']);//增加redis
  964. }
  965. break;
  966. case 2:
  967. // $redis = new Redis();
  968. //$redis_prefix = 'GROUP_'.$detail['act_id'].'_';
  969. //拼团锁定的退款
  970. if($detail['leader_id']){
  971. GroupLeader::where(['id'=>$detail['leader_id']])->update(['status'=>0]);
  972. // $redis->set( $redis_prefix.$detail['leader_id'],0);
  973. }
  974. foreach ($detail['order_item'] as $item_info) {
  975. // 更改订单详情状态
  976. GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
  977. // 商品规格库存返回
  978. $item = GroupGoodsItem::where(['act_id'=>$detail['act_id'],'goods_id'=>$item_info['goods_id'],'spec_id'=>$item_info['spec_id']])->find()->toArray();
  979. GroupGoodsItem::where(['id'=>$item['id']])->setInc('stock',$item_info['num']);
  980. // $redis->inc($redis_prefix.$item['id'].'_stock',$item_info['num']);//增加redis
  981. }
  982. break;
  983. }*/
  984. Db::commit();
  985. }catch (\Exception $e){
  986. $this->ret_msg = $e->getMessage();
  987. $this->is_commit = false;
  988. Db::rollback();
  989. }
  990. $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
  991. }
  992. /**
  993. * @title 删除订单
  994. * @desc 删除订单
  995. * @author qc
  996. * @url /api/Order/deleteOrder
  997. * @method POST
  998. * @header name:Authorization require:1 desc:Token
  999. * @param name:order_id type:int require:1 default:0 desc:订单id
  1000. */
  1001. public function deleteOrder()
  1002. {
  1003. $order_id = input('post.order_id');
  1004. $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  1005. if($detail['status'] == 0) $this->error('请先取消订单');
  1006. GoodsOrder::update(['is_deleted'=>1],['id'=>$order_id]);
  1007. $this->success('删除成功');
  1008. }
  1009. /**
  1010. * @title 更换订单收货地址
  1011. * @desc 更换订单收货地址(仅待支付订单可以修改地址)
  1012. * @author qc
  1013. * @url /api/Order/changeOrderDelivery
  1014. * @method POST
  1015. * @header name:Authorization require:1 desc:Token
  1016. * @param name:order_id type:int require:1 default:0 desc:订单id
  1017. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  1018. */
  1019. public function changeOrderDelivery()
  1020. {
  1021. $order_id = input('post.order_id');
  1022. $add_id = input('post.add_id');
  1023. $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
  1024. if($detail['status'] != 0) $this->error('操作错误');
  1025. $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
  1026. $order_update =[
  1027. 'pro_name' => $add_info['pro_name'],
  1028. 'city_name' => $add_info['city_name'],
  1029. 'county_name' => $add_info['county_name'],
  1030. 'street_name' => $add_info['street_name'],
  1031. 'add_detail' => $add_info['detail'],
  1032. 'user_name' => $add_info['name'],
  1033. 'phone' => $add_info['phone'],
  1034. ];
  1035. GoodsOrder::update($order_update,['id'=>$order_id]);
  1036. $this->success('修改成功');
  1037. }
  1038. /**
  1039. * @title 确认收货
  1040. * @desc 确认收货
  1041. * @author qc
  1042. * @url /api/Order/confirmReceipt
  1043. * @method POST
  1044. * @header name:Authorization require:1 desc:Token
  1045. * @param name:order_id type:int require:1 default:0 desc:订单id
  1046. */
  1047. public function confirmReceipt()
  1048. {
  1049. $order_id = input('post.order_id');
  1050. $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  1051. if($detail['status'] == 0) $this->error('订单未支付');
  1052. if($detail['status'] == 1) $this->error('订单未发货');
  1053. if($detail['status'] == 3) $this->error('订单已收货');
  1054. GoodsOrder::update(['status'=>3],['id'=>$order_id]);
  1055. GoodsOrderItem::update(['status'=>3],['order_id'=>$order_id]);
  1056. UserMessage::create(['user_id'=>$this->user_id,'type_id'=>3,'relation_id'=>$order_id,'content'=>'订单收货成功']);
  1057. $this->success('已确认收货');
  1058. }
  1059. /**
  1060. * @title 订单评价
  1061. * @desc 订单评价
  1062. * @author qc
  1063. * @url /api/Order/orderComment
  1064. * @method POST
  1065. * @header name:Authorization require:1 desc:Token
  1066. * @param name:item_id type:int require:1 default:0 desc:订单item表id
  1067. * @param name:content type:string require:1 default:0 desc:评论内容
  1068. * @param name:images type:string default:0 desc:图片
  1069. * @param name:video type:string default:0 desc:视频
  1070. * @param name:goods_core type:int default:0 desc:商品评分
  1071. * @param name:wl_core type:int default:0 desc:物流评分
  1072. * @param name:serve_core type:int default:0 desc:服务评分
  1073. */
  1074. public function orderComment()
  1075. {
  1076. $item_id = input('post.item_id','');
  1077. $content = input('post.content','');
  1078. $images = input('post.images','');
  1079. $video = input('post.video','');
  1080. $goods_core = input('post.goods_core','');
  1081. $wl_core = input('post.wl_core','');
  1082. $serve_core = input('post.serve_core','');
  1083. $item = GoodsOrderItem::get($item_id);
  1084. if(!$item) $this->error('订单不存在');
  1085. $order_info = GoodsOrder::where('id',$item->order_id)->find();
  1086. if($order_info->status < 3) $this->error('请先确认收货');
  1087. $data = [
  1088. 'user_id' => $this->user_id,
  1089. 'order_id' =>$item->order_id,
  1090. 'goods_id' =>$item->goods_id,
  1091. 'spec_id' =>$item->spec_id,
  1092. 'content' =>$content,
  1093. 'images' =>$images,
  1094. 'video' =>$video,
  1095. 'goods_core' =>$goods_core,
  1096. 'wl_core' =>$wl_core,
  1097. 'serve_core' =>$serve_core,
  1098. ];
  1099. OrderComment::create($data);
  1100. $this->success('评论成功');
  1101. }
  1102. /**
  1103. * @title 获取退款理由
  1104. * @desc 获取退款理由
  1105. * @author qc
  1106. * @url /api/Order/getRefundCase
  1107. * @method GET
  1108. * @header name:Authorization require:1 desc:Token
  1109. * @param name:order_id type:int require:1 default:0 desc:订单id
  1110. * @param name:apply_case type:string default:0 desc:退款原因
  1111. * @param name:images type:string default:0 desc:图片
  1112. */
  1113. public function getRefundCase()
  1114. {
  1115. $list= Db::name('order_refund_case')->order('sort desc ,id desc')->select();
  1116. $this->success('ok',['list'=>$list]);
  1117. }
  1118. /**
  1119. * @title 订单申请退款(重新退款||退款编辑)
  1120. * @desc 订单申请退款
  1121. * @author qc
  1122. * @url /api/Order/orderRefund
  1123. * @method POST
  1124. * @header name:Authorization require:1 desc:Token
  1125. * @param name:order_id type:int require:1 default:0 desc:订单id
  1126. * @param name:apply_case type:string default:0 desc:退款原因
  1127. * @param name:images type:string default:0 desc:图片
  1128. */
  1129. public function orderRefund()
  1130. {
  1131. $order_id = input('post.order_id');
  1132. $apply_case = input('post.apply_case');
  1133. $images = input('post.images');
  1134. $order_info = GoodsOrder::where('id',$order_id)->find()->toArray();
  1135. if($order_info['status'] == 0) $this->error('订单未支付');
  1136. if($order_info['refund_state'] == 1) $this->error('订单已申请,等待审核');
  1137. if($order_info['refund_state'] == 2) $this->error('已通过审核');
  1138. $refund_info = [
  1139. 'user_id' => $this->user_id ,
  1140. 'order_id' => $order_id ,
  1141. 'num' => $order_info['goods_num'],
  1142. 'apply_case' => $apply_case,
  1143. 'images' => $images,
  1144. 'is_deleted'=> 0,
  1145. 'refund_type'=> $order_info['status'] == 1 ? :2,
  1146. 'create_at'=> date('Y-m-d H:i:s'),
  1147. 'refund_no'=> 'R'.$order_info['order_no'],
  1148. ];
  1149. Data::save('StoreOrderRefund',$refund_info,'order_id',['order_id'=>$order_id]);
  1150. GoodsOrder::where(['id'=>$order_id])->update(['refund_state'=>1]);
  1151. $this->success('申请成功等待审核');
  1152. }
  1153. /**
  1154. * @title 取消退款申请
  1155. * @desc 取消退款申请
  1156. * @author qc
  1157. * @url /api/Order/cancelApply
  1158. * @method POST
  1159. * @header name:Authorization require:1 desc:Token
  1160. * @param name:id type:int require:1 default:0 desc:退款记录id
  1161. */
  1162. public function cancelApply()
  1163. {
  1164. $id = input('post.id');
  1165. $refund_info = StoreOrderRefund::where('id',$id)->find();
  1166. if($refund_info['status'] != 0) $this->error('该记录已处理!');
  1167. StoreOrderRefund::where('id',$id)->update(['status'=>5]);
  1168. GoodsOrder::where('id',$refund_info['order_id'])->update(['refund_state'=>0]);
  1169. $this->success('取消成功!');
  1170. }
  1171. /**
  1172. * @title 订单退款详情
  1173. * @desc 订单退款详情
  1174. * @author qc
  1175. * @url /api/Order/getRefundInfo
  1176. * @method GET
  1177. * @header name:Authorization require:1 desc:Token
  1178. * @param name:order_id type:int require:1 default:0 desc:订单id
  1179. * @return name:apply_case type:string default:0 desc:退款原因
  1180. * @return name:images type:string default:0 desc:图片
  1181. * @return name:create_at type:string default:0 desc:申请时间
  1182. * @return name:status type:int default:0 desc:0待审核1同意退款2审核拒绝(失败)3通过切退款成功4退款异常5取消申请
  1183. * @return name:refund_type type:int default:0 desc:1退款2退款退货
  1184. * @return name:audit_desc type:string default:0 desc:审核描述
  1185. * @return name:refund_money type:float default:0 desc:审核退款金额
  1186. * @return name:refund_time type:float default:0 desc:退款到账时间
  1187. * @return name:audit_at type:float default:0 desc:审核时间
  1188. * @return name:refund_no type:float default:0 desc:退款编号
  1189. * @return name:express_info type:array default:0 desc:退货物流
  1190. * @return name:express_info.express_address type:string default:0 desc:0 desc:退货地址
  1191. * @return name:express_info.express_user type:string default:0 desc:0 desc:收件人
  1192. * @return name:express_info.express_tel type:string default:0 desc:0 desc:电话
  1193. * @return name:express_info.express_name type:string default:0 desc:0 desc:物流公司名称
  1194. * @return name:express_info.express_no type:string default:0 desc:快递号
  1195. * @return name:express_info.express_time type:string default:0 desc:时间
  1196. */
  1197. public function getRefundInfo()
  1198. {
  1199. $order_id = input('get.order_id');
  1200. $refund_info = StoreOrderRefund::where('order_id',$order_id)
  1201. ->field('id,apply_case,images,status,audit_desc,create_at,express_info,refund_type,refund_no,audit_at,refund_time')
  1202. ->find()->toArray();
  1203. if($refund_info['express_info'])$refund_info['express_info']= json_decode($refund_info['express_info'],true);
  1204. $this->success('ok',['detail'=>$refund_info]);
  1205. }
  1206. /**
  1207. * @title 完善退款物流
  1208. * @desc 完善退款物流
  1209. * @author qc
  1210. * @url /api/Order/refundExpress
  1211. * @method POST
  1212. * @header name:Authorization require:1 desc:Token
  1213. * @param name:id type:int require:1 default:0 desc:退款记录id
  1214. * @param name:express_address type:string default:0 desc:0 desc:退货地址
  1215. * @param name:express_user type:string default:0 desc:0 desc:收件人
  1216. * @param name:express_tel type:string default:0 desc:0 desc:电话
  1217. * @param name:express_name type:string default:0 desc:0 desc:物流公司名称(退货的物流公司是用户自己填写的,,,想用哪个用哪个)
  1218. * @param name:express_no type:string default:0 desc:快递号
  1219. */
  1220. public function refundExpress()
  1221. {
  1222. $id = input('post.id');
  1223. $express_address = input('post.express_address');
  1224. $express_user = input('post.express_user');
  1225. $express_tel= input('post.express_tel');
  1226. $express_name= input('post.express_name');
  1227. $express_no = input('post.express_no');
  1228. $exp_info = [
  1229. 'express_address'=>$express_address,
  1230. 'express_user'=>$express_user,
  1231. 'express_tel'=>$express_tel,
  1232. 'express_name'=>$express_name,
  1233. 'express_no'=>$express_no,
  1234. 'express_time'=>date("Y-m-d H:i:s"),
  1235. ];
  1236. StoreOrderRefund::where('id',$id)->update(['express_info'=>json_encode($exp_info)]);
  1237. $this->success('物流信息完善成功');
  1238. }
  1239. /**
  1240. * @title 申请(修改)订单发票
  1241. * @desc 申请(修改)订单发票
  1242. * @author qc
  1243. * @url /api/Order/applyOrderBill
  1244. * @method POST
  1245. * @header name:Authorization require:1 desc:Token
  1246. * @param name:order_id type:int require:1 default:0 desc:订单id
  1247. * @param name:header type:string require:1 default:0 desc:抬头
  1248. * @param name:type type:string require:1 default:0 desc:类型(string)
  1249. * @param name:email type:string require:1 default:0 desc:邮箱(string)
  1250. * @param name:bank type:string require:1 default:0 desc:开户行(string)
  1251. * @param name:card_no type:string require:1 default:0 desc:账号(string)
  1252. * @param name:phone type:string require:1 default:0 desc:电话(string)
  1253. */
  1254. public function applyOrderBill()
  1255. {
  1256. $order_id = input('post.order_id');
  1257. if(!$order_id) $this->error('请选择开票订单');
  1258. $bill_status = OrderBill::where(['order_id'=>$order_id])->value('status');
  1259. if($bill_status) $this->error('已开票');
  1260. $bill= [
  1261. 'user_id' => $this->user_id,
  1262. 'order_id' => $order_id,
  1263. 'header' => input('post.header'),
  1264. 'type' => input('post.type'),
  1265. 'email' => input('post.email'),
  1266. 'card_no' => input('post.card_no'),
  1267. 'bank' => input('post.bank'),
  1268. 'phone' => input('post.phone')
  1269. ];
  1270. $bill['user_id'] = $this->user_id;
  1271. Data::save('OrderBill',$bill,'order_id',['order_id'=>$order_id]);
  1272. $this->success('申请成功');
  1273. }
  1274. /**
  1275. * @title 获取订单发票详情
  1276. * @desc 获取订单发票详情
  1277. * @author qc
  1278. * @url /api/Order/getBillInfo
  1279. * @method GET
  1280. * @header name:Authorization require:1 desc:Token
  1281. * @param name:order_id type:int require:1 default:0 desc:订单id
  1282. * @return name:header type:string require:1 default:0 desc:抬头
  1283. * @return name:type type:string require:1 default:0 desc:类型(string)
  1284. * @return name:email type:string require:1 default:0 desc:邮箱(string)
  1285. * @return name:bank type:string require:1 default:0 desc:开户行(string)
  1286. * @return name:card_no type:string require:1 default:0 desc:账号(string)
  1287. * @return name:phone type:string require:1 default:0 desc:电话(string)
  1288. * @return name:remark type:string require:1 default:0 desc:审核备注
  1289. * @return name:bill_img type:string require:1 default:0 desc:电子发票图片
  1290. */
  1291. public function getBillInfo()
  1292. {
  1293. $detail = OrderBill::where('order_id',input('order_id'))->where('source',1)->find();
  1294. $this->success('ok',['detail'=>$detail ?$detail->toArray() :'']);
  1295. }
  1296. }