WashManage.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\SDK\Vod\V20170321\Models\GetAIMediaAuditJobResponseBody\mediaAuditJob\data;
  4. use app\common\model\AfreshOrder;
  5. use app\common\model\AfreshOrderItem;
  6. use app\common\model\DeliveryAddress;
  7. use app\common\model\GoodsOrder;
  8. use app\common\model\StoreOrderRefund;
  9. use app\common\model\User;
  10. use app\common\model\UserCouponList;
  11. use app\common\model\WashOrder;
  12. use app\common\model\WashCate;
  13. use app\common\model\WashClothes;
  14. use app\common\model\WashOrderItem;
  15. use app\common\model\WashType;
  16. use app\common\service\OrderCallback;
  17. use think\Db;
  18. use think\Exception;
  19. /**
  20. * @title 洗涤管理
  21. * @controller WashManage
  22. * @group base
  23. */
  24. class WashManage extends Base
  25. {
  26. protected $module = 1;
  27. protected $need_login = [
  28. 'orderConfirm',
  29. 'createOrder',
  30. 'getOrderDetail',
  31. 'getOrderList',
  32. 'payOrder',
  33. 'cancelOrder',
  34. 'applyAfreshOrder',
  35. 'applyAfreshOrderDetail',
  36. 'cancelAfreshOrder',
  37. 'getAfreshOrderList',
  38. ];
  39. public function initialize()
  40. {
  41. parent::initialize();
  42. $this->module = input('module',1);
  43. if($this->user_id) {
  44. $cancel_list = WashOrder::where('user_id','=',$this->user_id)
  45. ->where('pay_state',0)
  46. ->where('cancel_state',0)
  47. ->where('create_at','<=',date("Y-m-d H:i:s",time() - 900))
  48. ->column('id');
  49. if(!empty($cancel_list)) {
  50. WashOrder::where('id','in',$cancel_list)->update(['cancel_state'=>1,'status'=>9,'cancel_desc'=>'系统自动取消']);
  51. }
  52. }
  53. }
  54. /**
  55. * @title 获取洗涤分类
  56. * @desc 获取洗涤分类
  57. * @author qc
  58. * @url /api/Wash_manage/getWashCate
  59. * @method GET
  60. * @header name:Authorization require:1 desc:Token
  61. * @param name:module type:int default:1 desc:1洗鞋分类2洗衣分类
  62. * @return name:id type:int default:-- desc:分类id
  63. * @return name:title type:string default:-- desc:标题
  64. */
  65. public function getWashCate()
  66. {
  67. $all_cate = WashCate::getAllCateByModule($this->module);
  68. $this->success('ok',['list'=>$all_cate]);
  69. }
  70. /**
  71. * @title 获取洗涤商品【列表】
  72. * @desc 获取洗涤商品【列表】
  73. * @author qc
  74. * @url /api/Wash_manage/getGoodsList
  75. * @method GET
  76. * @header name:Authorization require:1 desc:Token
  77. * @param name:module type:int default:1 desc:1洗鞋2洗衣
  78. * @param name:cate_id type:int default:1 desc:分类id(不传查洗鞋或洗衣全部的)
  79. * @return name:id type:int default:-- desc:id
  80. * @return name:name type:string default:-- desc:商品名
  81. * @return name:cover type:string default:-- desc:商品封面
  82. * @return name:price type:float default:-- desc:价格
  83. * @return name:wash_type_name type:string default:-- desc:洗涤类型名称
  84. */
  85. public function getGoodsList()
  86. {
  87. $cate_id = input('get.cate_id');
  88. $list = WashClothes::where(['c.module'=>$this->module,'c.status'=>1,'c.is_deleted'=>0])
  89. ->alias('c')
  90. ->field('c.*,t.title wash_type_name')
  91. ->leftJoin('WashType t','c.wash_type = t.id')
  92. ->when($cate_id,function ($query)use ($cate_id){
  93. if($cate_id) $query->where('c.cate_id',$cate_id);
  94. })->limit($this->off_set,$this->page_num)
  95. ->order('sort desc ,id desc')
  96. ->select()->toArray();
  97. $this->success('ok',['list'=>$list]);
  98. }
  99. /**
  100. * @title 获取商品详情
  101. * @desc 获取商品详情
  102. * @author qc
  103. * @url /api/Wash_manage/getGoodsDetail
  104. * @method GET
  105. * @header name:Authorization require:1 desc:Token
  106. * @param name:module type:int default:1 desc:1洗鞋2洗衣
  107. * @return name:id type:int default:-- desc:id
  108. * @return name:name type:string default:-- desc:商品名
  109. * @return name:cover type:string default:-- desc:商品封面
  110. * @return name:price type:float default:-- desc:价格
  111. * @return name:wash_type_name type:string default:-- desc:洗涤类型名称
  112. */
  113. public function getGoodsDetail()
  114. {
  115. $goods_id = input('get.goods_id');
  116. $detail = WashClothes::where('id',$goods_id)->find()->toArray();
  117. if($detail['status'] == 0 || $detail['is_deleted'] == 1) $this->error('商品已下架');
  118. $detail['wash_type_name'] = WashType::where('id',$detail['wash_type'])->value('title');
  119. $this->success('获取成功',['detail'=>$detail]);
  120. }
  121. /**
  122. * @title 订单确认页数据
  123. * @desc 订单确认页数据
  124. * @author qc
  125. * @url /api/Wash_manage/orderConfirm
  126. * @method GET
  127. * @header name:Authorization require:1 desc:Token
  128. * @param name:module type:int default:1 desc:1洗鞋2洗衣
  129. * @param name:goods_json type:string require:1 default:0 desc:商品json信息[{'goods_id':'商品id','num':'数量'}]
  130. * @return name:list type:array default:-- desc:商品列表
  131. * @return name:list.id type:int default:-- desc:商品id
  132. * @return name:list.name type:float default:-- desc:商品名
  133. * @return name:list.price type:float default:-- desc:商品价格
  134. * @return name:list.cover type:float default:-- desc:商品封面
  135. * @return name:list.cate_name type:float default:-- desc:所属分类名
  136. * @return name:list.wash_type_name type:float default:-- desc:洗涤类型名
  137. * @return name:list.buy_num type:float default:-- desc:选择数量
  138. * @return name:list.order_price type:float default:-- desc:该商品金额
  139. * @return name:price_total type:float default:-- desc:订单金额(包含取件费)
  140. * @return name:total_num type:int default:-- desc:总件数
  141. * @return name:postage type:int default:-- desc:取件费用
  142. * @return name:goods_price type:int default:-- desc:商品总费用
  143. * @return name:address type:array default:-- desc:默认售货地址
  144. * @return name:address.pro_name type:string require:1 default:-- desc:省名
  145. * @return name:address.city_name type:string require:1 default:-- desc:市名
  146. * @return name:address.county_name type:string require:1 default:-- desc:县区名
  147. * @return name:address.detail type:string require:1 default:-- desc:详细地址
  148. * @return name:address.phone type:string require:1 default:-- desc:联系电话
  149. * @return name:address.name type:string require:1 default:-- desc:联系人
  150. * @return name:address.sex type:int require:1 default:-- desc:性别(1男2女)
  151. */
  152. public function orderConfirm()
  153. {
  154. $goods_json = input('get.goods_json','');
  155. $goods_data = json_decode($goods_json,true);
  156. $goods_column = array_column($goods_data,null,'goods_id');
  157. $goods_ids = array_keys($goods_column);
  158. $list = WashClothes::field('a.id,a.price,a.name,a.cover,c.title cate_name,t.title wash_type_name,wash_type')
  159. ->alias('a')
  160. ->leftJoin('WashCate c','c.id = a.cate_id')
  161. ->leftJoin('WashType t','t.id = a.wash_type')
  162. ->where('a.id','in',$goods_ids)
  163. ->where('a.module','=',$this->module)
  164. ->select()->toArray();
  165. if(count($list) != count($goods_ids)) $this->error('商品信息有误');
  166. $price_total = 0;
  167. array_walk($list,function (&$v)use (&$price_total,$goods_column){
  168. $v['order_price'] = bcmul($v['price'],$goods_column[$v['id']]['num'],2);
  169. $v['buy_num'] = $goods_column[$v['id']]['num'];
  170. $price_total = bcadd($v['order_price'],$price_total,2);
  171. });
  172. $postage = sysconf('postage');
  173. $address = DeliveryAddress::field('id,pro_name,city_name,county_name,mer_name,detail,phone,name,sex')->where('user_id',$this->user_id)->where('is_mr',1)->find();
  174. $this->success('ok',[
  175. 'list'=>$list,
  176. 'goods_price'=>$price_total,
  177. 'price_total'=> bcadd($price_total,$postage,2),
  178. 'postage'=>$postage,
  179. 'total_num'=>array_sum(array_column($goods_data,'num')),
  180. 'address' => $address ? $address->toArray() : null,
  181. ]);
  182. }
  183. /**
  184. * @title 创建订单【订单金额(price_total)为0时,不需要调支付,订单直接完成】
  185. * @desc 创建订单【优惠券抵扣金额最高的一件】
  186. * @author qc
  187. * @url /api/Wash_manage/createOrder
  188. * @method POST
  189. * @header name:Authorization require:1 desc:Token
  190. * @param name:module type:int default:1 desc:1洗鞋2洗衣
  191. * @param name:goods_json type:string require:1 default:0 desc:商品json信息[{'goods_id':'商品id','num':'数量'}]
  192. * @param name:remark type:string default:-- desc:订单备注
  193. * @param name:images type:string default:-- desc:图片(多张|隔开)
  194. * @param name:address_id type:int default:-- desc:地址id
  195. * @param name:coupon_id type:int default:-- desc:优惠券id
  196. */
  197. public function createOrder()
  198. {
  199. $goods_json = input('post.goods_json','');
  200. $address_id = input('post.address_id','');
  201. $remark = input('post.remark','');
  202. $images = input('post.images','');
  203. $coupon_id = input('post.coupon_id','');
  204. $goods_data = json_decode($goods_json,true);
  205. $goods_column = array_column($goods_data,null,'goods_id');
  206. $goods_ids = array_keys($goods_column);
  207. if(!$address_id) $this->error('请选择售货地址');
  208. $list = WashClothes::field('a.id,a.price,a.cover,a.name,c.title cate_name,t.title wash_type_name,wash_type')
  209. ->alias('a')
  210. ->leftJoin('WashCate c','c.id = a.cate_id')
  211. ->leftJoin('WashType t','t.id = a.wash_type')
  212. ->where('a.id','in',$goods_ids)
  213. ->where('a.module','=',$this->module)
  214. ->select()->toArray();
  215. // $this->success('ok',['user_id'=>$this->user_id,'address_id'=>$address_id]);
  216. if(count($list) != count($goods_ids)) $this->error('商品信息有误');
  217. $postage = sysconf('postage');
  218. $ret_data = ['pay_status'=>0,'config'=>[],'order_id'=>0];
  219. Db::startTrans();
  220. try {
  221. $address = DeliveryAddress::field('id,pro_name,city_name,county_name,mer_name,detail,phone,name,sex')
  222. ->where('user_id',$this->user_id)->where('id',$address_id)->find()->toArray();
  223. $price_total = 0;
  224. $order_info = [
  225. 'user_id' => $this->user_id,
  226. 'order_no' => get_order_sn(),
  227. 'pay_no' => get_order_sn(),
  228. 'goods_num' => array_sum(array_column($goods_data,'num')),
  229. 'pro_name' => $address['pro_name'],
  230. 'city_name' =>$address['city_name'],
  231. 'county_name' => $address['county_name'],
  232. 'add_detail' => $address['detail'],
  233. 'phone' => $address['phone'],
  234. 'sex' => $address['sex'],
  235. 'user_name' => $address['name'],
  236. 'remark' => $remark,
  237. 'images' => $images,
  238. 'coupon_id' => $coupon_id,
  239. 'module' => $this->module,
  240. ];
  241. $order_item = [];
  242. array_walk($list,function (&$v)use (&$price_total,$goods_column,&$order_item){
  243. $item_price = bcmul($v['price'],$goods_column[$v['id']]['num'],2);
  244. $price_total = bcadd($item_price,$price_total,2);
  245. $order_item[] = [
  246. 'user_id' => $this->user_id,
  247. 'goods_id' => $v['id'],
  248. 'goods_name' => $v['name'],
  249. 'cover' => $v['cover'],
  250. 'sell_price' => $v['price'],
  251. 'pay_price' => $item_price,
  252. 'wash_type' => $v['wash_type'],
  253. 'num' => $goods_column[$v['id']]['num'],
  254. ];
  255. });
  256. // 数组降序
  257. array_multisort(array_column($order_item,'sell_price'),SORT_DESC,$order_item);
  258. // 计算优惠券抵扣金额
  259. if($coupon_id) {
  260. $module = $this->module + 1;
  261. $coupon_info = UserCouponList::where(['l.user_id'=>$this->user_id,'l.status'=>1,'l.id'=>$coupon_id])
  262. ->alias('l')
  263. ->field('l.*,c.title,c.module,c.coupon_type,c.is_new')
  264. ->when($module,function ($query)use ($module){
  265. if($module > 1 ) $query->where('c.module','in','1,'. $module);
  266. })->leftJoin('CouponConfig c','c.id = l.coupon_id')
  267. ->find();
  268. if(empty($coupon_info)) $coupon_info = $this->exception('所选优惠券有误');
  269. $coupon_info = $coupon_info->toArray();
  270. if($coupon_info['coupon_type'] == 1 && $coupon_info['low_amount'] > $price_total) $this->exception('优惠券使用金额限制,最低:'.$coupon_info['low_amount'].'元');
  271. $coupon_cash = $coupon_info['coupon_type'] == 1 ? $coupon_info['amount'] : $order_item[0]['sell_price'];
  272. $price_total = bcsub($price_total , $coupon_cash,2);
  273. $order_info['coupon_cash'] = $coupon_cash;
  274. UserCouponList::where('id',$coupon_id)->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
  275. }
  276. $order_info['price_total'] = bcadd($postage,$price_total,2);
  277. $order_info['postage'] = $postage;
  278. $order_info = WashOrder::create($order_info)->toArray();
  279. array_walk($order_item,function (&$v,$k)use ($order_info){
  280. $v['order_id'] = $order_info['id'];
  281. });
  282. (new WashOrderItem())->insertAll($order_item);// 生成订单商品详情
  283. $ret_data['order_id'] = $order_info['id'];
  284. if($price_total == 0) OrderCallback::washOrderCallBack($order_info,1);
  285. Db::commit();
  286. }catch (\Exception $e) {
  287. Db::rollback();
  288. $this->error($e->getMessage());
  289. }
  290. $this->success('ok',$ret_data);
  291. }
  292. /**
  293. * @title 获取订单详情
  294. * @desc 获取订单详情
  295. * @author qc
  296. * @url /api/Wash_manage/getOrderDetail
  297. * @method GET
  298. * @header name:Authorization require:1 desc:Token
  299. * @param name:id type:int default:1 desc:订单id
  300. * @return name:id type:int default:-- desc:订单id
  301. * @return name:module type:int default:-- desc:1洗鞋2洗衣
  302. * @return name:order_no type:string default:-- desc:订单号
  303. * @return name:price_total type:float default:-- desc:待支付金额
  304. * @return name:price_express type:0 default:-- desc:运费金额
  305. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  306. * @return name:pay_type type:int default:-- desc:支付方式(1微信)
  307. * @return name:pay_no type:string default:-- desc:支付号
  308. * @return name:pay_at type:string default:-- desc:支付时间
  309. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  310. * @return name:cancel_at type:string default:-- desc:取消时间
  311. * @return name:cancel_desc type:string default:-- desc:取消原因
  312. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付,2待取送,3订单完成,8退款,9取消)
  313. * @return name:create_at type:string default:-- desc:下单时间
  314. * @return name:pay_at type:string default:-- desc:支付时间
  315. * @return name:remark type:string default:-- desc:订单备注
  316. * @return name:finish_time type:string default:-- desc:完成时间
  317. * @return name:finish_int type:string default:-- desc:完成时间【时间戳】
  318. * @return name:images type:string default:-- desc:图片(多张|隔开)
  319. * @return name:images_arr type:array default:-- desc:图片
  320. * @return name:goods_num type:int default:-- desc:订单商品总数量
  321. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  322. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  323. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  324. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  325. * @return name:user_name type:string default:-- desc:收货人
  326. * @return name:phone type:string default:-- desc:收货人联系电话
  327. * @return name:sex type:int require:1 default:-- desc:性别(1男2女)
  328. * @return name:second type:int default:-- desc:倒计时(单位秒)
  329. * @return name:coupon_cash type:float default:-- desc:优惠券抵扣金额
  330. * @return name:afresh_order type:int default:-- desc:重洗订单id,没申请是0
  331. * @return name:order_item type:array default:-- desc:订单商品列表
  332. * @return name:order_item.name type:string default:-- desc:商品名
  333. * @return name:order_item.cover type:string default:-- desc:商品图
  334. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格
  335. * @return name:order_item.num type:int default:-- desc:数量
  336. * @return name:order_item.wash_type_name type:int default:-- desc:洗涤类型
  337. */
  338. public function getOrderDetail()
  339. {
  340. $order_id = input('get.id');
  341. $field = 'id,module,order_no,status,price_total,pay_state,images,pay_no,is_deleted,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage,create_at,finish_int,finish_time,sex,coupon_id,coupon_cash';
  342. $detail = WashOrder::field($field)
  343. ->with('orderItem')->where('id',$order_id)->find()->toArray();
  344. if($detail['is_deleted'] == 1) $this->error('订单已删除');
  345. $detail['images_arr'] = $detail['images'] ? explode('|',$detail['images']) : null;
  346. $afresh_order = AfreshOrder::where('wash_id',$detail['id'])->where('cancel_state',0)->value('id');
  347. $detail['afresh_order'] =intval($afresh_order);
  348. $detail['second'] = 0;
  349. if($detail['status'] == 0) {
  350. $detail['second'] = time() - strtotime($detail['create_at']) + 900;
  351. }
  352. $this->success('ok',['detail'=>$detail]);
  353. }
  354. /**
  355. * @title 获取订单列表
  356. * @desc 获取订单列表
  357. * @author qc
  358. * @method GET
  359. * @url /api/Wash_manage/getOrderList
  360. * @header name:Authorization require:1 desc:Token
  361. * @param name:page type:int default:1 desc:页数
  362. * @param name:page_num type:int default:20 desc:每页数
  363. * @param name:sel_status type:int default:1 desc:订单类型(0全部1待支付2已支付3待取送4已完成)
  364. * @return name:order_no type:string default:-- desc:订单号
  365. * @return name:price_total type:float default:-- desc:待支付金额
  366. * @return name:price_express type:0 default:-- desc:运费金额
  367. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  368. * @return name:pay_type type:int default:-- desc:支付方式(1微信)
  369. * @return name:pay_no type:string default:-- desc:支付号
  370. * @return name:pay_at type:string default:-- desc:支付时间
  371. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  372. * @return name:cancel_at type:string default:-- desc:取消时间
  373. * @return name:cancel_desc type:string default:-- desc:取消原因
  374. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付,2待取送,3订单完成,8退款,9取消)
  375. * @return name:create_at type:string default:-- desc:下单时间
  376. * @return name:finish_time type:string default:-- desc:完成时间
  377. * @return name:finish_int type:string default:-- desc:完成时间【时间戳】
  378. * @return name:pay_at type:string default:-- desc:支付时间
  379. * @return name:remark type:string default:-- desc:订单备注
  380. * @return name:goods_num type:int default:-- desc:订单商品总数量
  381. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  382. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  383. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  384. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  385. * @return name:user_name type:string default:-- desc:收货人
  386. * @return name:sex type:int default:-- desc:性别(1男2女)
  387. * @return name:coupon_cash type:float default:-- desc:优惠券抵扣金额
  388. * @return name:phone type:string default:-- desc:收货人联系电话
  389. * @return name:order_item type:array default:-- desc:订单商品列表
  390. * @return name:order_item.name type:float default:-- desc:商品名
  391. * @return name:order_item.cover type:float default:-- desc:商品图
  392. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格(单件)
  393. * @return name:order_item.num type:int default:-- desc:数量
  394. * @return name:order_item.wash_type_name type:int default:-- desc:洗涤类型
  395. */
  396. public function getOrderList()
  397. {
  398. $sel_status = input('get.sel_status',0);
  399. $where = [];
  400. $where[] = ['user_id','=',$this->user_id];
  401. $where[] = ['is_deleted','=',0];
  402. $where[] = ['cancel_state','=',0];
  403. $where[] = ['refund_state','=',0];
  404. switch ($sel_status)
  405. {
  406. case 1:
  407. $where[] = ['pay_state','=',0];
  408. break;
  409. case 2:
  410. $where[] = ['pay_state','=',1];
  411. break;
  412. case 3:
  413. $where[] = ['status','=','2'];
  414. break;
  415. case 4:
  416. $where[] = ['status','=','3'];
  417. break;
  418. }
  419. $field = 'id,module,order_no,status,price_total,pay_state,pay_no,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage,create_at,finish_int,finish_time,sex,coupon_id,coupon_cash';
  420. $list = WashOrder::field($field)->with(['orderItem'])->where($where)->order('id desc ')
  421. ->limit($this->off_set,$this->page_num)
  422. ->select()->toArray();
  423. $this->success('ok',['list'=>$list]);
  424. }
  425. /**
  426. * @title 订单支付
  427. * @desc 订单支付
  428. * @author qc
  429. * @url /api/Wash_manage/payOrder
  430. * @method POST
  431. * @header name:Authorization require:1 desc:Token
  432. * @param name:id type:int require:1 default:0 desc:订单id
  433. * @return name:config type:array default:0 desc:小程序支付配置
  434. * @return name:config.appId type:string default:-- desc:appid
  435. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  436. * @return name:config.signType type:string default:-- desc:签名类型
  437. * @return name:config.paySign type:string default:-- desc:签名
  438. * @return name:config.timestamp type:string default:-- desc:时间戳
  439. */
  440. public function payOrder()
  441. {
  442. $order_id = input('post.id');
  443. $field = 'id,module,status,order_no,price_total,pay_state,pay_no,is_deleted,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage';
  444. $order_info = WashOrder::field($field)
  445. ->with('orderItem')->where('id',$order_id)->find()->toArray();
  446. if($order_info['is_deleted'] == 1) $this->error('订单已删除');
  447. if($order_info['cancel_state'] == 1) $this->error('订单已取消');
  448. if($order_info['status'] != 0) $this->error('订单已支付');
  449. $user_info = $this->userInfo();
  450. $ret_data = ['pay_status'=>0,'config'=>[]];
  451. $notify_url = $this->request->root(true) . '/api/we_chat_pay/washOrderNotify';
  452. $config = WeChatPay::wxPay('订单支付',$order_info['pay_no'],$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  453. if(!$config) $this->error('支付配置错误');
  454. $ret_data['config']=$config['config'];
  455. $this->success('ok',$ret_data);
  456. }
  457. /**
  458. * @title 取消订单
  459. * @desc 取消订单
  460. * @author qc
  461. * @url /api/Wash_manage/cancelOrder
  462. * @method POST
  463. * @header name:Authorization require:1 desc:Token
  464. * @param name:id type:int require:1 default:0 desc:订单id
  465. */
  466. public function cancelOrder()
  467. {
  468. $order_id = input('post.id');
  469. $field = 'id,module,status,order_no,price_total,pay_state,pay_no,is_deleted,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage,coupon_id';
  470. $order_info = WashOrder::field($field)
  471. ->with('orderItem')->where('id',$order_id)->find()->toArray();
  472. if($order_info['is_deleted'] == 1) $this->error('订单已删除');
  473. if($order_info['cancel_state'] == 1) $this->error('订单已取消');
  474. if($order_info['status'] == 8) $this->error('订单已取消');
  475. if($order_info['coupon_id']) UserCouponList::where('id',$order_info['coupon_id'])->update(['status'=>1,'used_time'=>null]);
  476. if($order_info['status'] == 0){
  477. WashOrder::where('id',$order_id)->update(['status'=>9,'cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s')]);
  478. }else{
  479. $refund_info = [
  480. 'user_id' => $this->user_id ,
  481. 'order_id' => $order_id ,
  482. 'num' => $order_info['goods_num'],
  483. 'apply_case' => '订单取消',
  484. 'is_deleted'=> 0,
  485. 'refund_type'=> 1,
  486. 'create_at'=> date('Y-m-d H:i:s'),
  487. 'refund_no'=> 'W'.$order_info['order_no'],
  488. 'source'=> 2,
  489. 'refund_money'=>$order_info['price_total'],
  490. ];
  491. $res = \library\tools\Data::save('StoreOrderRefund',$refund_info,'order_id',['order_id'=>$order_id,'source'=>2]);
  492. $refund_info =StoreOrderRefund::where(['order_id'=>$order_id,'source'=>2,'user_id'=>$this->user_id])->find()->toArray();
  493. WashOrder::where(['id'=>$order_id])->update(['refund_state'=>1]);
  494. OrderCallback::refundMoney($refund_info ,$order_info,$order_id);
  495. }
  496. $this->success('订单取消成功');
  497. }
  498. /**
  499. * @title 以下接口为重洗订单接口
  500. * @desc 以下接口为重洗订单接口
  501. * @author qc
  502. * @url /api/Wash_manage/classIntro
  503. * @method GET
  504. * @return name:1 type:string default:-- desc:重洗申请页面的商品可以调用订单详情接口获取商品列表
  505. */
  506. public function classIntro(){}
  507. /**
  508. * @title 申请重洗
  509. * @desc 申请重洗
  510. * @author qc
  511. * @url /api/Wash_manage/applyAfreshOrder
  512. * @method POST
  513. * @param name:order_id type:int require:1 default:0 desc:洗涤订单id
  514. * @param name:goods_json type:string require:1 default:0 desc:商品json信息[{'item_id':'订单的商品列表id','num':'数量'}]
  515. * @param name:images type:string require:0 default:0 desc:图片(多张|隔开)
  516. */
  517. public function applyAfreshOrder()
  518. {
  519. $order_id = input('post.order_id','');
  520. $goods_json = input('post.goods_json','');
  521. $remark = input('post.remark','');
  522. $images = input('post.images','');
  523. $goods_data = json_decode($goods_json,true);
  524. $wash_order = WashOrder::where('id',$order_id)->find()->toArray();
  525. $check = AfreshOrder::where('wash_id',$order_id)
  526. ->where('is_deleted',0)->where('cancel_state',0)
  527. ->value('id');
  528. if($check) $this->error('已申请重洗');
  529. if($wash_order['status'] !=3) $this->error('订单未完成');
  530. if($wash_order['finish_int'] < time() - 86400 * 2) $this->error('订单已完成48小时,无法申请重洗');
  531. Db::startTrans();
  532. try {
  533. $order_info = [
  534. 'user_id' => $this->user_id,
  535. 'wash_id' => $order_id,
  536. 'order_no' => get_order_sn(),
  537. 'goods_num' => array_sum(array_column($goods_data,'num')),
  538. 'remark' => $remark,
  539. 'images' => $images,
  540. 'module' => $wash_order['module'],
  541. ];
  542. $order_info = AfreshOrder::create($order_info);
  543. $item= [] ;
  544. foreach ($goods_data as $iv) {
  545. $check_item = WashOrderItem::where('id',$iv['item_id'])->value('num');
  546. if($iv['num'] > $check_item) $this->exception('重洗数量错误');
  547. $item[]= [
  548. 'user_id' => $this->user_id,
  549. 'afresh_id' => $order_info['id'],
  550. 'item_id' => $iv['item_id'],
  551. 'num' => $iv['num'],
  552. ];
  553. }
  554. (new AfreshOrderItem())->insertAll($item);
  555. Db::commit();
  556. }catch (\Exception $e) {
  557. Db::rollback();
  558. $this->error($e->getMessage());
  559. }
  560. $this->success('申请成功等待审核');
  561. }
  562. /**
  563. * @title 申请重洗订单列表
  564. * @desc 申请重洗订单列表
  565. * @author qc
  566. * @url /api/Wash_manage/getAfreshOrderList
  567. * @method GET
  568. * @param name:page type:int default:1 desc:页数
  569. * @param name:page_num type:int default:20 desc:每页数
  570. * @return name:id type:int require:1 default:0 desc:重洗订单id
  571. * @return name:status type:int require:1 default:0 desc:订单状态(0默认状态【未处理】1待取送2订单完成)
  572. * @return name:sh_status type:int require:1 default:0 desc:审核状态(0待审核1审核通过2审核拒绝)
  573. * @return name:sh_remark type:string require:1 default:0 desc:审核备注
  574. * @return name:wash_id type:int require:1 default:0 desc:洗涤订单id
  575. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  576. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  577. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  578. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  579. * @return name:add_detail type:int default:-- desc:清洗数量
  580. * @return name:user_name type:string default:-- desc:收货人
  581. * @return name:phone type:string default:-- desc:收货人联系电话
  582. * @return name:sex type:int require:1 default:-- desc:性别(1男2女)
  583. * @return name:order_item type:array default:-- desc:订单商品列表
  584. * @return name:order_item.name type:string default:-- desc:商品名
  585. * @return name:order_item.cover type:string default:-- desc:商品图
  586. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格
  587. * @return name:order_item.num type:int default:-- desc:清洗数量
  588. */
  589. public function getAfreshOrderList()
  590. {
  591. $list = AfreshOrder::field('a.id,a.wash_id,a.status,a.images,pro_name,city_name,county_name,add_detail,user_name,phone,a.module,sh_status,w.sex')
  592. ->alias('a')
  593. ->with('orderItem')
  594. ->leftJoin('WashOrder w','w.id = a.wash_id')
  595. ->where('a.user_id',$this->user_id)
  596. ->where('a.cancel_state',0)
  597. ->order('a.id desc')
  598. ->limit($this->off_set,$this->page_num)
  599. ->select()->toArray();
  600. array_walk($list,function (&$v){
  601. $v['images_arr'] = $v['images'] ? explode("|",$v['images']) : null;
  602. });
  603. $this->success('ok',['detail'=>$list]);
  604. }
  605. /**
  606. * @title 获取申请重洗订单详情
  607. * @desc 获取申请重洗订单详情
  608. * @author qc
  609. * @url /api/Wash_manage/applyAfreshOrderDetail
  610. * @method GET
  611. * @param name:order_id type:int require:1 default:0 desc:洗涤订单id
  612. * @return name:id type:int require:1 default:0 desc:重洗订单id
  613. * @return name:status type:int require:1 default:0 desc:订单状态(0默认状态【未处理】1待取送2订单完成)
  614. * @return name:sh_status type:int require:1 default:0 desc:审核状态(0待审核1审核通过2审核拒绝)
  615. * @return name:sh_remark type:string require:1 default:0 desc:审核备注
  616. * @return name:wash_id type:int require:1 default:0 desc:洗涤订单id
  617. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  618. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  619. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  620. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  621. * @return name:add_detail type:int default:-- desc:清洗数量
  622. * @return name:user_name type:string default:-- desc:收货人
  623. * @return name:phone type:string default:-- desc:收货人联系电话
  624. * @return name:sex type:int default:-- desc:性别(1男2女)
  625. * @return name:order_item type:array default:-- desc:订单商品列表
  626. * @return name:order_item.name type:string default:-- desc:商品名
  627. * @return name:order_item.cover type:string default:-- desc:商品图
  628. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格
  629. * @return name:order_item.num type:int default:-- desc:清洗数量
  630. */
  631. public function applyAfreshOrderDetail()
  632. {
  633. $order_id = input('get.order_id');
  634. $detail = AfreshOrder::field('a.id,a.wash_id,a.status,a.sh_remark,a.images,pro_name,city_name,county_name,add_detail,user_name,phone,a.module,sh_status,w.sex')
  635. ->alias('a')
  636. ->with('orderItem')
  637. ->leftJoin('WashOrder w','w.id = a.wash_id')
  638. ->where('w.id',$order_id)
  639. ->where('a.cancel_state',0)
  640. ->find();
  641. if(!$detail) $this->error('订单暂未申请重洗');
  642. $detail= $detail->toArray();
  643. $detail['images_arr'] = $detail['images'] ? explode("|",$detail['images']) : null;
  644. $this->success('ok',['detail'=>$detail]);
  645. }
  646. /**
  647. * @title 取消重洗
  648. * @desc 取消重洗
  649. * @author qc
  650. * @url /api/Wash_manage/cancelAfreshOrder
  651. * @method POST
  652. * @param name:id type:int require:1 default:0 desc:重洗订单id
  653. */
  654. public function cancelAfreshOrder()
  655. {
  656. AfreshOrder::where('id',input('post.id'))
  657. ->where('user_id',$this->user_id)
  658. ->update(['cancel_state'=>1]);
  659. $this->success('取消成功');
  660. }
  661. }