WashManage.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 创建订单
  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. $coupon_info = [];
  222. if($coupon_id) {
  223. $module = $this->module + 1;
  224. $coupon_info = UserCouponList::where(['l.user_id'=>$this->user_id,'l.status'=>1,'l.id'=>$coupon_id])
  225. ->alias('l.*,c.title,c.module,c.coupon_type,c.is_new')
  226. ->when($module,function ($query)use ($module){
  227. if($module > 1 ) $query->where('module','in','1,'. $module);
  228. })->leftJoin('CouponConfig c','c.id = l.coupon_id')
  229. ->find();
  230. if(empty($coupon_info)) $coupon_info = $this->exception('所选优惠券有误');
  231. $coupon_info = $coupon_info->toArray();
  232. }
  233. $address = DeliveryAddress::field('id,pro_name,city_name,county_name,mer_name,detail,phone,name,sex')
  234. ->where('user_id',$this->user_id)->where('id',$address_id)->find()->toArray();
  235. $price_total = 0;
  236. $order_info = [
  237. 'user_id' => $this->user_id,
  238. 'order_no' => get_order_sn(),
  239. 'pay_no' => get_order_sn(),
  240. 'goods_num' => array_sum(array_column($goods_data,'num')),
  241. 'pro_name' => $address['pro_name'],
  242. 'city_name' =>$address['city_name'],
  243. 'county_name' => $address['county_name'],
  244. 'add_detail' => $address['detail'],
  245. 'phone' => $address['phone'],
  246. 'sex' => $address['sex'],
  247. 'user_name' => $address['name'],
  248. 'remark' => $remark,
  249. 'images' => $images,
  250. 'coupon_id' => $coupon_id,
  251. 'module' => $this->module,
  252. ];
  253. $order_item = [];
  254. array_walk($list,function (&$v)use (&$price_total,$goods_column,&$order_item){
  255. $item_price = bcmul($v['price'],$goods_column[$v['id']]['num'],2);
  256. $price_total = bcadd($item_price,$price_total,2);
  257. $order_item[] = [
  258. 'user_id' => $this->user_id,
  259. 'goods_id' => $v['id'],
  260. 'goods_name' => $v['name'],
  261. 'cover' => $v['cover'],
  262. 'sell_price' => $v['price'],
  263. 'pay_price' => $item_price,
  264. 'wash_type' => $v['wash_type'],
  265. 'num' => $goods_column[$v['id']]['num'],
  266. ];
  267. });
  268. array_multisort(array_column($order_item,'sell_price'),SORT_DESC,$order_item);
  269. $order_info['price_total'] = bcadd($postage,$price_total,2);
  270. $order_info['postage'] = $postage;
  271. $order_info = WashOrder::create($order_info)->toArray();
  272. array_walk($order_item,function (&$v,$k)use ($order_info){
  273. $v['order_id'] = $order_info['id'];
  274. });
  275. (new WashOrderItem())->insertAll($order_item);// 生成订单商品详情
  276. $ret_data['order_id'] = $order_info['id'];
  277. if($price_total == 0) OrderCallback::washOrderCallBack($order_info,1);
  278. Db::commit();
  279. }catch (\Exception $e) {
  280. Db::rollback();
  281. $this->error($e->getMessage());
  282. }
  283. $this->success('ok',$ret_data);
  284. }
  285. /**
  286. * @title 获取订单详情
  287. * @desc 获取订单详情
  288. * @author qc
  289. * @url /api/Wash_manage/getOrderDetail
  290. * @method GET
  291. * @header name:Authorization require:1 desc:Token
  292. * @param name:id type:int default:1 desc:订单id
  293. * @return name:id type:int default:-- desc:订单id
  294. * @return name:module type:int default:-- desc:1洗鞋2洗衣
  295. * @return name:order_no type:string default:-- desc:订单号
  296. * @return name:price_total type:float default:-- desc:待支付金额
  297. * @return name:price_express type:0 default:-- desc:运费金额
  298. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  299. * @return name:pay_type type:int default:-- desc:支付方式(1微信)
  300. * @return name:pay_no type:string default:-- desc:支付号
  301. * @return name:pay_at type:string default:-- desc:支付时间
  302. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  303. * @return name:cancel_at type:string default:-- desc:取消时间
  304. * @return name:cancel_desc type:string default:-- desc:取消原因
  305. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付,2待取送,3订单完成,8退款,9取消)
  306. * @return name:create_at type:string default:-- desc:下单时间
  307. * @return name:pay_at type:string default:-- desc:支付时间
  308. * @return name:remark type:string default:-- desc:订单备注
  309. * @return name:finish_time type:string default:-- desc:完成时间
  310. * @return name:finish_int type:string default:-- desc:完成时间【时间戳】
  311. * @return name:images type:string default:-- desc:图片(多张|隔开)
  312. * @return name:images_arr type:array default:-- desc:图片
  313. * @return name:goods_num type:int default:-- desc:订单商品总数量
  314. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  315. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  316. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  317. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  318. * @return name:user_name type:string default:-- desc:收货人
  319. * @return name:phone type:string default:-- desc:收货人联系电话
  320. * @return name:sex type:int require:1 default:-- desc:性别(1男2女)
  321. * @return name:second type:int default:-- desc:倒计时(单位秒)
  322. * @return name:afresh_order type:int default:-- desc:重洗订单id,没申请是0
  323. * @return name:order_item type:array default:-- desc:订单商品列表
  324. * @return name:order_item.name type:string default:-- desc:商品名
  325. * @return name:order_item.cover type:string default:-- desc:商品图
  326. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格
  327. * @return name:order_item.num type:int default:-- desc:数量
  328. * @return name:order_item.wash_type_name type:int default:-- desc:洗涤类型
  329. */
  330. public function getOrderDetail()
  331. {
  332. $order_id = input('get.id');
  333. $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';
  334. $detail = WashOrder::field($field)
  335. ->with('orderItem')->where('id',$order_id)->find()->toArray();
  336. if($detail['is_deleted'] == 1) $this->error('订单已删除');
  337. $detail['images_arr'] = $detail['images'] ? explode('|',$detail['images']) : null;
  338. $afresh_order = AfreshOrder::where('wash_id',$detail['id'])->where('cancel_state',0)->value('id');
  339. $detail['afresh_order'] =intval($afresh_order);
  340. $detail['second'] = 0;
  341. if($detail['status'] == 0) {
  342. $detail['second'] = time() - strtotime($detail['create_at']) + 900;
  343. }
  344. $this->success('ok',['detail'=>$detail]);
  345. }
  346. /**
  347. * @title 获取订单列表
  348. * @desc 获取订单列表
  349. * @author qc
  350. * @method GET
  351. * @url /api/Wash_manage/getOrderList
  352. * @header name:Authorization require:1 desc:Token
  353. * @param name:page type:int default:1 desc:页数
  354. * @param name:page_num type:int default:20 desc:每页数
  355. * @param name:sel_status type:int default:1 desc:订单类型(0全部1待支付2已支付3待取送4已完成)
  356. * @return name:order_no type:string default:-- desc:订单号
  357. * @return name:price_total type:float default:-- desc:待支付金额
  358. * @return name:price_express type:0 default:-- desc:运费金额
  359. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  360. * @return name:pay_type type:int default:-- desc:支付方式(1微信)
  361. * @return name:pay_no type:string default:-- desc:支付号
  362. * @return name:pay_at type:string default:-- desc:支付时间
  363. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  364. * @return name:cancel_at type:string default:-- desc:取消时间
  365. * @return name:cancel_desc type:string default:-- desc:取消原因
  366. * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付,2待取送,3订单完成,8退款,9取消)
  367. * @return name:create_at type:string default:-- desc:下单时间
  368. * @return name:finish_time type:string default:-- desc:完成时间
  369. * @return name:finish_int type:string default:-- desc:完成时间【时间戳】
  370. * @return name:pay_at type:string default:-- desc:支付时间
  371. * @return name:remark type:string default:-- desc:订单备注
  372. * @return name:goods_num type:int default:-- desc:订单商品总数量
  373. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  374. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  375. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  376. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  377. * @return name:user_name type:string default:-- desc:收货人
  378. * @return name:sex type:int default:-- desc:性别(1男2女)
  379. * @return name:phone type:string default:-- desc:收货人联系电话
  380. * @return name:order_item type:array default:-- desc:订单商品列表
  381. * @return name:order_item.name type:float default:-- desc:商品名
  382. * @return name:order_item.cover type:float default:-- desc:商品图
  383. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格(单件)
  384. * @return name:order_item.num type:int default:-- desc:数量
  385. * @return name:order_item.wash_type_name type:int default:-- desc:洗涤类型
  386. */
  387. public function getOrderList()
  388. {
  389. $sel_status = input('get.sel_status',0);
  390. $where = [];
  391. $where[] = ['user_id','=',$this->user_id];
  392. $where[] = ['is_deleted','=',0];
  393. $where[] = ['cancel_state','=',0];
  394. $where[] = ['refund_state','=',0];
  395. switch ($sel_status)
  396. {
  397. case 1:
  398. $where[] = ['pay_state','=',0];
  399. break;
  400. case 2:
  401. $where[] = ['pay_state','=',1];
  402. break;
  403. case 3:
  404. $where[] = ['status','=','2'];
  405. break;
  406. case 4:
  407. $where[] = ['status','=','3'];
  408. break;
  409. }
  410. $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';
  411. $list = WashOrder::field($field)->with(['orderItem'])->where($where)->order('id desc ')
  412. ->limit($this->off_set,$this->page_num)
  413. ->select()->toArray();
  414. $this->success('ok',['list'=>$list]);
  415. }
  416. /**
  417. * @title 订单支付
  418. * @desc 订单支付
  419. * @author qc
  420. * @url /api/Wash_manage/payOrder
  421. * @method POST
  422. * @header name:Authorization require:1 desc:Token
  423. * @param name:id type:int require:1 default:0 desc:订单id
  424. * @return name:config type:array default:0 desc:小程序支付配置
  425. * @return name:config.appId type:string default:-- desc:appid
  426. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  427. * @return name:config.signType type:string default:-- desc:签名类型
  428. * @return name:config.paySign type:string default:-- desc:签名
  429. * @return name:config.timestamp type:string default:-- desc:时间戳
  430. */
  431. public function payOrder()
  432. {
  433. $order_id = input('post.id');
  434. $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';
  435. $order_info = WashOrder::field($field)
  436. ->with('orderItem')->where('id',$order_id)->find()->toArray();
  437. if($order_info['is_deleted'] == 1) $this->error('订单已删除');
  438. if($order_info['cancel_state'] == 1) $this->error('订单已取消');
  439. if($order_info['status'] != 0) $this->error('订单已支付');
  440. $user_info = $this->userInfo();
  441. $ret_data = ['pay_status'=>0,'config'=>[]];
  442. $notify_url = $this->request->root(true) . '/api/we_chat_pay/washOrderNotify';
  443. $config = WeChatPay::wxPay('订单支付',$order_info['pay_no'],$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  444. if(!$config) $this->error('支付配置错误');
  445. $ret_data['config']=$config['config'];
  446. $this->success('ok',$ret_data);
  447. }
  448. /**
  449. * @title 取消订单
  450. * @desc 取消订单
  451. * @author qc
  452. * @url /api/Wash_manage/cancelOrder
  453. * @method POST
  454. * @header name:Authorization require:1 desc:Token
  455. * @param name:id type:int require:1 default:0 desc:订单id
  456. */
  457. public function cancelOrder()
  458. {
  459. $order_id = input('post.id');
  460. $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';
  461. $order_info = WashOrder::field($field)
  462. ->with('orderItem')->where('id',$order_id)->find()->toArray();
  463. if($order_info['is_deleted'] == 1) $this->error('订单已删除');
  464. if($order_info['cancel_state'] == 1) $this->error('订单已取消');
  465. if($order_info['status'] == 8) $this->error('订单已取消');
  466. if($order_info['status'] == 0){
  467. WashOrder::where('id',$order_id)->update(['status'=>9,'cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s')]);
  468. }else{
  469. $refund_info = [
  470. 'user_id' => $this->user_id ,
  471. 'order_id' => $order_id ,
  472. 'num' => $order_info['goods_num'],
  473. 'apply_case' => '订单取消',
  474. 'is_deleted'=> 0,
  475. 'refund_type'=> 1,
  476. 'create_at'=> date('Y-m-d H:i:s'),
  477. 'refund_no'=> 'W'.$order_info['order_no'],
  478. 'source'=> 2,
  479. 'refund_money'=>$order_info['price_total'],
  480. ];
  481. $res = \library\tools\Data::save('StoreOrderRefund',$refund_info,'order_id',['order_id'=>$order_id,'source'=>2]);
  482. $refund_info =StoreOrderRefund::where(['order_id'=>$order_id,'source'=>2,'user_id'=>$this->user_id])->find()->toArray();
  483. WashOrder::where(['id'=>$order_id])->update(['refund_state'=>1]);
  484. OrderCallback::refundMoney($refund_info ,$order_info,$order_id);
  485. }
  486. $this->success('订单取消成功');
  487. }
  488. /**
  489. * @title 以下接口为重洗订单接口
  490. * @desc 以下接口为重洗订单接口
  491. * @author qc
  492. * @url /api/Wash_manage/classIntro
  493. * @method GET
  494. * @return name:1 type:string default:-- desc:重洗申请页面的商品可以调用订单详情接口获取商品列表
  495. */
  496. public function classIntro(){}
  497. /**
  498. * @title 申请重洗
  499. * @desc 申请重洗
  500. * @author qc
  501. * @url /api/Wash_manage/applyAfreshOrder
  502. * @method POST
  503. * @param name:order_id type:int require:1 default:0 desc:洗涤订单id
  504. * @param name:goods_json type:string require:1 default:0 desc:商品json信息[{'item_id':'订单的商品列表id','num':'数量'}]
  505. * @param name:images type:string require:0 default:0 desc:图片(多张|隔开)
  506. */
  507. public function applyAfreshOrder()
  508. {
  509. $order_id = input('post.order_id','');
  510. $goods_json = input('post.goods_json','');
  511. $remark = input('post.remark','');
  512. $images = input('post.images','');
  513. $goods_data = json_decode($goods_json,true);
  514. $wash_order = WashOrder::where('id',$order_id)->find()->toArray();
  515. $check = AfreshOrder::where('wash_id',$order_id)
  516. ->where('is_deleted',0)->where('cancel_state',0)
  517. ->value('id');
  518. if($check) $this->error('已申请重洗');
  519. if($wash_order['status'] !=3) $this->error('订单未完成');
  520. if($wash_order['finish_int'] < time() - 86400 * 2) $this->error('订单已完成48小时,无法申请重洗');
  521. Db::startTrans();
  522. try {
  523. $order_info = [
  524. 'user_id' => $this->user_id,
  525. 'wash_id' => $order_id,
  526. 'order_no' => get_order_sn(),
  527. 'goods_num' => array_sum(array_column($goods_data,'num')),
  528. 'remark' => $remark,
  529. 'images' => $images,
  530. 'module' => $wash_order['module'],
  531. ];
  532. $order_info = AfreshOrder::create($order_info);
  533. $item= [] ;
  534. foreach ($goods_data as $iv) {
  535. $check_item = WashOrderItem::where('id',$iv['item_id'])->value('num');
  536. if($iv['num'] > $check_item) $this->exception('重洗数量错误');
  537. $item[]= [
  538. 'user_id' => $this->user_id,
  539. 'afresh_id' => $order_info['id'],
  540. 'item_id' => $iv['item_id'],
  541. 'num' => $iv['num'],
  542. ];
  543. }
  544. (new AfreshOrderItem())->insertAll($item);
  545. Db::commit();
  546. }catch (\Exception $e) {
  547. Db::rollback();
  548. $this->error($e->getMessage());
  549. }
  550. $this->success('申请成功等待审核');
  551. }
  552. /**
  553. * @title 申请重洗订单列表
  554. * @desc 申请重洗订单列表
  555. * @author qc
  556. * @url /api/Wash_manage/getAfreshOrderList
  557. * @method GET
  558. * @param name:page type:int default:1 desc:页数
  559. * @param name:page_num type:int default:20 desc:每页数
  560. * @return name:id type:int require:1 default:0 desc:重洗订单id
  561. * @return name:status type:int require:1 default:0 desc:订单状态(0默认状态【未处理】1待取送2订单完成)
  562. * @return name:sh_status type:int require:1 default:0 desc:审核状态(0待审核1审核通过2审核拒绝)
  563. * @return name:sh_remark type:string require:1 default:0 desc:审核备注
  564. * @return name:wash_id type:int require:1 default:0 desc:洗涤订单id
  565. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  566. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  567. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  568. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  569. * @return name:add_detail type:int default:-- desc:清洗数量
  570. * @return name:user_name type:string default:-- desc:收货人
  571. * @return name:phone type:string default:-- desc:收货人联系电话
  572. * @return name:sex type:int require:1 default:-- desc:性别(1男2女)
  573. * @return name:order_item type:array default:-- desc:订单商品列表
  574. * @return name:order_item.name type:string default:-- desc:商品名
  575. * @return name:order_item.cover type:string default:-- desc:商品图
  576. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格
  577. * @return name:order_item.num type:int default:-- desc:清洗数量
  578. */
  579. public function getAfreshOrderList()
  580. {
  581. $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')
  582. ->alias('a')
  583. ->with('orderItem')
  584. ->leftJoin('WashOrder w','w.id = a.wash_id')
  585. ->where('a.user_id',$this->user_id)
  586. ->where('a.cancel_state',0)
  587. ->order('a.id desc')
  588. ->limit($this->off_set,$this->page_num)
  589. ->select()->toArray();
  590. array_walk($list,function (&$v){
  591. $v['images_arr'] = $v['images'] ? explode("|",$v['images']) : null;
  592. });
  593. $this->success('ok',['detail'=>$list]);
  594. }
  595. /**
  596. * @title 获取申请重洗订单详情
  597. * @desc 获取申请重洗订单详情
  598. * @author qc
  599. * @url /api/Wash_manage/applyAfreshOrderDetail
  600. * @method GET
  601. * @param name:order_id type:int require:1 default:0 desc:洗涤订单id
  602. * @return name:id type:int require:1 default:0 desc:重洗订单id
  603. * @return name:status type:int require:1 default:0 desc:订单状态(0默认状态【未处理】1待取送2订单完成)
  604. * @return name:sh_status type:int require:1 default:0 desc:审核状态(0待审核1审核通过2审核拒绝)
  605. * @return name:sh_remark type:string require:1 default:0 desc:审核备注
  606. * @return name:wash_id type:int require:1 default:0 desc:洗涤订单id
  607. * @return name:pro_name type:string default:-- desc:省名称(收货地址)
  608. * @return name:city_name type:string default:-- desc:市名称(收货地址)
  609. * @return name:county_name type:string default:-- desc:县区名称(收货地址)
  610. * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
  611. * @return name:add_detail type:int default:-- desc:清洗数量
  612. * @return name:user_name type:string default:-- desc:收货人
  613. * @return name:phone type:string default:-- desc:收货人联系电话
  614. * @return name:sex type:int default:-- desc:性别(1男2女)
  615. * @return name:order_item type:array default:-- desc:订单商品列表
  616. * @return name:order_item.name type:string default:-- desc:商品名
  617. * @return name:order_item.cover type:string default:-- desc:商品图
  618. * @return name:order_item.sell_price type:float default:-- desc:洗涤价格
  619. * @return name:order_item.num type:int default:-- desc:清洗数量
  620. */
  621. public function applyAfreshOrderDetail()
  622. {
  623. $order_id = input('get.order_id');
  624. $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')
  625. ->alias('a')
  626. ->with('orderItem')
  627. ->leftJoin('WashOrder w','w.id = a.wash_id')
  628. ->where('w.id',$order_id)
  629. ->where('a.cancel_state',0)
  630. ->find();
  631. if(!$detail) $this->error('订单暂未申请重洗');
  632. $detail= $detail->toArray();
  633. $detail['images_arr'] = $detail['images'] ? explode("|",$detail['images']) : null;
  634. $this->success('ok',['detail'=>$detail]);
  635. }
  636. /**
  637. * @title 取消重洗
  638. * @desc 取消重洗
  639. * @author qc
  640. * @url /api/Wash_manage/cancelAfreshOrder
  641. * @method POST
  642. * @param name:id type:int require:1 default:0 desc:重洗订单id
  643. */
  644. public function cancelAfreshOrder()
  645. {
  646. AfreshOrder::where('id',input('post.id'))
  647. ->where('user_id',$this->user_id)
  648. ->update(['cancel_state'=>1]);
  649. $this->success('取消成功');
  650. }
  651. }