WashOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\GoodsOrder;
  4. use app\common\model\User;
  5. use app\common\model\WashCate;
  6. use app\common\model\WashOrderItem;
  7. use think\Db;
  8. use think\Exception;
  9. /**
  10. * @title 商城订单
  11. * @controller WashOrder
  12. * @group base
  13. */
  14. class WashOrder extends Base
  15. {
  16. public function initialize()
  17. {
  18. parent::initialize();
  19. parent::checkLogin();
  20. }
  21. /**
  22. * @title 创建订单【创建洗鞋订】
  23. * @desc 创建订单
  24. * @author qc
  25. * @url /api/Wash_order/createOrder
  26. * @method POST
  27. * @header name:Authorization require:1 desc:Token
  28. * @param name:goods_json type:string require:1 default:0 desc:洗鞋信息[{'cate_id':'洗鞋分类id',ladder_key:'洗鞋分类ladder_key',images:'图片(多张|隔开)',remark:'备注'}
  29. * @param name:pro_name type:string default:-- desc:收货地址--省
  30. * @param name:city_name type:string default:-- desc:收货地址--市
  31. * @param name:county_name type:string default:-- desc:收货地址--区
  32. * @param name:add_detail type:string default:-- desc:收货地址--详情
  33. * @param name:user_name type:string default:-- desc:收货人
  34. * @param name:phone type:string default:-- desc:收货人联系方式
  35. * @param name:remark type:string default:1 desc:订单备注
  36. * @return name:order_id type:int default:1 desc:订单id
  37. */
  38. public function createOrder()
  39. {
  40. /* $post_json = [
  41. [
  42. 'cate_id'=>1,
  43. 'ladder_key'=>0,
  44. 'images'=>'https://gaoyixia2.oss-cn-shanghai.aliyuncs.com/5e48b5b47f8f0770/55fdfd184a2b4c55.jpg',
  45. 'remark'=>'AAAAA',
  46. ]
  47. ];
  48. var_dump(json_encode($post_json));*/
  49. $goods_json = input('post.goods_json','');
  50. $pro_name = input('post.pro_name','');
  51. $city_name = input('post.city_name','');
  52. $county_name = input('post.county_name','');
  53. $add_detail = input('post.add_detail','');
  54. $user_name = input('post.user_name','');
  55. $phone = input('post.phone','');
  56. if(!$pro_name || !$city_name || !$county_name || !$add_detail) $this->error('请完善地址信心');
  57. if(!$user_name || !$phone) $this->error('请完善收货人信息');
  58. $goods_data = json_decode($goods_json,true);
  59. Db::startTrans();
  60. try {
  61. $user_info = $this->userInfo();
  62. $wash_cate = WashCate::column('ladder_set','id');
  63. $item_data = [];
  64. $user_id = $this->user_id;
  65. $price_total = 0;
  66. array_walk($goods_data,function (&$v,$k)use (&$item_data,$user_id,$wash_cate,&$price_total){
  67. $item_info = [
  68. 'user_id' => $user_id,
  69. 'num' => 1,
  70. 'cate_id' => $v['cate_id'],
  71. 'ladder_key' => $v['ladder_key'],
  72. 'images' => $v['images'],
  73. 'remark' => $v['remark'],
  74. 'sell_price' => json_decode($wash_cate[$v['cate_id']],true)[$v['ladder_key']]['price'],
  75. ];
  76. $price_total = bcadd($price_total,$item_info['sell_price'],2);
  77. $item_data[] = $item_info;
  78. });
  79. $order_insert = [
  80. 'user_id' => $this->user_id,
  81. 'order_no' => get_order_sn(),
  82. 'pay_no' => get_order_sn(),
  83. 'pro_name' => $pro_name,
  84. 'city_name' =>$city_name,
  85. 'county_name' => $county_name,
  86. 'add_detail' => $add_detail,
  87. 'user_name' => $user_name,
  88. 'phone' => $phone,
  89. 'price_total' => $price_total,
  90. 'goods_num' => array_sum(array_column($item_data,'num')),
  91. ];
  92. $order_info = \app\common\model\WashOrder::create($order_insert);
  93. foreach ($item_data as &$gv) {
  94. $gv['order_id'] = $order_info->id;
  95. }
  96. (new WashOrderItem())->insertAll($item_data);
  97. Db::commit();
  98. }catch (\Exception $e) {
  99. Db::rollback();
  100. $this->error($e->getMessage());
  101. }
  102. $this->success('订单创建成功',['order_id'=>$order_info->id]);
  103. }
  104. /**
  105. * @title 订单支付
  106. * @desc 订单支付
  107. * @author qc
  108. * @url /api/Wash_order/payOrder
  109. * @method POST
  110. * @header name:Authorization require:1 desc:Token
  111. * @param name:order_id type:int require:1 default:0 desc:订单id
  112. * @return name:config type:array default:0 desc:小程序支付配置
  113. * @return name:config.appId type:string default:-- desc:appid
  114. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  115. * @return name:config.signType type:string default:-- desc:签名类型
  116. * @return name:config.paySign type:string default:-- desc:签名
  117. * @return name:config.timestamp type:string default:-- desc:时间戳
  118. */
  119. public function payOrder()
  120. {
  121. $order_id = input('post.order_id');
  122. $order_info = \app\common\model\WashOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  123. if($order_info['status'] != 0) $this->error('订单状态错误');
  124. if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
  125. if($order_info['price_total'] <= 0) $this->error('订单金额错误');
  126. $user_info = User::where('id',$this->user_id)->find()->toArray();
  127. $ret_data = ['pay_status'=>0,'config'=>[]];
  128. Db::startTrans();
  129. try {
  130. $notify_url = $this->request->root(true) . '/api/we_chat_pay/washOrderNotify';
  131. $pay_no = $order_info['pay_no'] ;
  132. $config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  133. if(!$config) throw new Exception('支付配置错误');
  134. $ret_data['config'] = $config;
  135. Db::commit();
  136. }catch (\Exception $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. }
  140. $this->success('ok',$ret_data);
  141. }
  142. /**
  143. * @title 获取订单列表
  144. * @desc 获取订单列表
  145. * @author qc
  146. * @method GET
  147. * @url /api/Wash_order/getWashOrderList
  148. * @header name:Authorization require:1 desc:Token
  149. * @param name:page type:int default:1 desc:页数
  150. * @param name:page_num type:int default:20 desc:每页数
  151. * @param name:status type:int default:-1 desc:订单状态(-1全部,0未支付,1已支付,2已完成,9取消)
  152. * @return name:id type:int default:-- desc:订单id
  153. * @return name:order_no type:string default:-- desc:订单号
  154. * @return name:pay_no type:string default:-- desc:支付号
  155. * @return name:status type:int default:-- desc:订单状态(-1全部,0未支付,1已支付,2已完成,9取消)
  156. * @return name:create_at type:string default:-- desc:下单时间
  157. * @return name:pro_name type:string default:-- desc:省名称(订单地址)
  158. * @return name:city_name type:string default:-- desc:市名称(订单地址)
  159. * @return name:county_name type:string default:-- desc:县区名称(订单地址)
  160. * @return name:add_detail type:string default:-- desc:详细地址(订单地址)
  161. * @return name:user_name type:string default:-- desc:订单人
  162. * @return name:phone type:string default:-- desc:人联系
  163. * @return name:order_item type:array default:-- desc:订单明细
  164. * @return name:order_item.id type:int default:-- desc:订单明细id
  165. * @return name:order_item.cate_id type:int default:-- desc:洗鞋分类id
  166. * @return name:order_item.sell_price type:int default:-- desc:价格
  167. * @return name:order_item.images type:int default:-- desc:图片(多张)
  168. * @return name:order_item.images_arr type:array default:-- desc:图片(多张)
  169. * @return name:order_item.remark type:int default:-- desc:备注
  170. * @return name:order_item.cate_name type:string default:-- desc:分类名
  171. * @return name:order_item.cate_cover type:string default:-- desc:分类logo
  172. * @return name:order_item.cate_title type:string default:-- desc:价格区间标题
  173. * @return name:order_item.cate_price type:string default:-- desc:价格[订单单价]
  174. */
  175. public function getWashOrderList()
  176. {
  177. $status = input('get.status',-1);
  178. $where = [];
  179. $where[] = ['user_id','=',$this->user_id];
  180. $where[] = ['is_deleted','=',0];
  181. if($status > -1) $where[] = ['status','=',$status];
  182. $list = \app\common\model\WashOrder::with('orderItem')
  183. ->where($where)
  184. ->limit($this->off_set,$this->page_num)
  185. ->order('id desc')
  186. ->select()->toArray();
  187. array_walk($list,function (&$v,$k){
  188. foreach ($v['order_item'] as &$iv){
  189. $iv['images_arr'] = $iv['images'] ? explode('|',$iv['images']) : null;
  190. $cate_info = WashCate::where('id',$iv['cate_id'])->find()->toArray();
  191. $iv['cate_name'] = $cate_info['title'];
  192. $iv['cate_cover'] = $cate_info['logo'];
  193. $ladder_set = json_decode($cate_info['ladder_set'],true);
  194. $iv['cate_title'] = $ladder_set[$iv['ladder_key']]['title'];
  195. $iv['cate_price'] = $ladder_set[$iv['ladder_key']]['price'];
  196. }
  197. });
  198. $this->success('ok',['list'=>$list]);
  199. }
  200. /**
  201. * @title 获取订单列表
  202. * @desc 获取订单列表【订单类型不要了】
  203. * @author qc
  204. * @method GET
  205. * @url /api/Wash_order/getWashOrderDetail
  206. * @header name:Authorization require:1 desc:Token
  207. * @param name:id type:int default:1 desc:订单id
  208. * @return name:id type:int default:-- desc:订单id
  209. * @return name:order_no type:string default:-- desc:订单号
  210. * @return name:pay_no type:string default:-- desc:支付号
  211. * @return name:status type:int default:-- desc:订单状态(0未支付,1已支付,2已完成,9取消)
  212. * @return name:create_at type:string default:-- desc:下单时间
  213. * @return name:pay_at type:string default:-- desc:支付时间
  214. * @return name:pro_name type:string default:-- desc:省名称(订单地址)
  215. * @return name:city_name type:string default:-- desc:市名称(订单地址)
  216. * @return name:county_name type:string default:-- desc:县区名称(订单地址)
  217. * @return name:add_detail type:string default:-- desc:详细地址(订单地址)
  218. * @return name:user_name type:string default:-- desc:订单人
  219. * @return name:phone type:string default:-- desc:人联系
  220. * @return name:order_item type:array default:-- desc:订单明细
  221. * @return name:order_item.id type:int default:-- desc:订单明细id
  222. * @return name:order_item.cate_id type:int default:-- desc:洗鞋分类id
  223. * @return name:order_item.sell_price type:int default:-- desc:价格
  224. * @return name:order_item.images type:int default:-- desc:图片(多张)
  225. * @return name:order_item.images_arr type:array default:-- desc:图片(多张)
  226. * @return name:order_item.remark type:string default:-- desc:备注
  227. * @return name:order_item.cate_name type:string default:-- desc:分类名
  228. * @return name:order_item.cate_cover type:string default:-- desc:分类logo
  229. * @return name:order_item.cate_title type:string default:-- desc:价格区间标题
  230. * @return name:order_item.cate_price type:string default:-- desc:价格[订单单价]
  231. *
  232. */
  233. public function getWashOrderDetail()
  234. {
  235. $order_id = input('get.id');
  236. $detail = \app\common\model\WashOrder::with('orderItem')
  237. ->where('id',$order_id)
  238. ->find()->toArray();
  239. array_walk($detail['order_item'],function (&$v){
  240. $v['images_arr'] = $v['images'] ? explode('|',$v['images']) : null;
  241. $cate_info = WashCate::where('id',$v['cate_id'])->find()->toArray();
  242. $v['cate_name'] = $cate_info['title'];
  243. $v['cate_cover'] = $cate_info['logo'];
  244. $ladder_set = json_decode($cate_info['ladder_set'],true);
  245. $v['cate_title'] = $ladder_set[$v['ladder_key']]['title'];
  246. $v['cate_price'] = $ladder_set[$v['ladder_key']]['title'];
  247. });
  248. $this->success('ok',['detail'=>$detail]);
  249. }
  250. /**
  251. * @title 取消预约
  252. * @desc 取消预约
  253. * @author qc
  254. * @url /api/Wash_order/cancelOrder
  255. * @method POST
  256. * @header name:Authorization require:1 desc:Token
  257. * @param name:id type:int require:1 default:0 desc:订单id
  258. * @param name:cancel_desc type:string default:0 desc:取消原因
  259. */
  260. public function cancelOrder()
  261. {
  262. $order_id = input('post.id');
  263. $cancel_desc = input('post.cancel_desc');
  264. Db::startTrans();
  265. try {
  266. $detail = \app\common\model\WashOrder::where('id',$order_id)->find()->toArray();
  267. if($detail['status'] != 0) $this->exception('订单已完成或是已取消');
  268. \app\common\model\WashOrder::where('id',$order_id)->update(['status'=>9,'cancel_at'=>date("Y-m-d H:i:s"),'cancel_state'=>1,'cancel_desc'=>$cancel_desc]);
  269. Db::commit();
  270. }catch (\Exception $e){
  271. $this->ret_msg = $e->getMessage();
  272. $this->is_commit = false;
  273. Db::rollback();
  274. }
  275. $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
  276. }
  277. /**
  278. * @title 预约完成
  279. * @desc 预约完成
  280. * @author qc
  281. * @url /api/Wash_order/completeOrder
  282. * @method POST
  283. * @header name:Authorization require:1 desc:Token
  284. * @param name:id type:int require:1 default:0 desc:订单记录id
  285. */
  286. public function completeOrder()
  287. {
  288. $order_id = input('post.id');
  289. $detail = \app\common\model\WashOrder::where('id',$order_id)->find()->toArray();
  290. if($detail['status'] != 1) $this->error('订单状态错误');
  291. \app\common\model\WashOrder::update(['status'=>2],['id'=>$order_id]);
  292. $this->success('订单已完成');
  293. }
  294. }