123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace app\api\controller;
- use app\common\model\GoodsOrder;
- use app\common\model\User;
- use app\common\model\WashCate;
- use app\common\model\WashOrderItem;
- use think\Db;
- use think\Exception;
- /**
- * @title 商城订单
- * @controller WashOrder
- * @group base
- */
- class WashOrder extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 创建订单【创建洗鞋订】
- * @desc 创建订单
- * @author qc
- * @url /api/Wash_order/createOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_json type:string require:1 default:0 desc:洗鞋信息[{'cate_id':'洗鞋分类id',ladder_key:'洗鞋分类ladder_key',images:'图片(多张|隔开)',remark:'备注'}
- * @param name:pro_name type:string default:-- desc:收货地址--省
- * @param name:city_name type:string default:-- desc:收货地址--市
- * @param name:county_name type:string default:-- desc:收货地址--区
- * @param name:add_detail type:string default:-- desc:收货地址--详情
- * @param name:user_name type:string default:-- desc:收货人
- * @param name:phone type:string default:-- desc:收货人联系方式
- * @param name:remark type:string default:1 desc:订单备注
- * @return name:order_id type:int default:1 desc:订单id
- */
- public function createOrder()
- {
- /* $post_json = [
- [
- 'cate_id'=>1,
- 'ladder_key'=>0,
- 'images'=>'https://gaoyixia2.oss-cn-shanghai.aliyuncs.com/5e48b5b47f8f0770/55fdfd184a2b4c55.jpg',
- 'remark'=>'AAAAA',
- ]
- ];
- var_dump(json_encode($post_json));*/
- $goods_json = input('post.goods_json','');
- $pro_name = input('post.pro_name','');
- $city_name = input('post.city_name','');
- $county_name = input('post.county_name','');
- $add_detail = input('post.add_detail','');
- $user_name = input('post.user_name','');
- $phone = input('post.phone','');
- if(!$pro_name || !$city_name || !$county_name || !$add_detail) $this->error('请完善地址信心');
- if(!$user_name || !$phone) $this->error('请完善收货人信息');
- $goods_data = json_decode($goods_json,true);
- Db::startTrans();
- try {
- $user_info = $this->userInfo();
- $wash_cate = WashCate::column('ladder_set','id');
- $item_data = [];
- $user_id = $this->user_id;
- $price_total = 0;
- array_walk($goods_data,function (&$v,$k)use (&$item_data,$user_id,$wash_cate,&$price_total){
- $item_info = [
- 'user_id' => $user_id,
- 'num' => 1,
- 'cate_id' => $v['cate_id'],
- 'ladder_key' => $v['ladder_key'],
- 'images' => $v['images'],
- 'remark' => $v['remark'],
- 'sell_price' => json_decode($wash_cate[$v['cate_id']],true)[$v['ladder_key']]['price'],
- ];
- $price_total = bcadd($price_total,$item_info['sell_price'],2);
- $item_data[] = $item_info;
- });
- $order_insert = [
- 'user_id' => $this->user_id,
- 'order_no' => get_order_sn(),
- 'pay_no' => get_order_sn(),
- 'pro_name' => $pro_name,
- 'city_name' =>$city_name,
- 'county_name' => $county_name,
- 'add_detail' => $add_detail,
- 'user_name' => $user_name,
- 'phone' => $phone,
- 'price_total' => $price_total,
- 'goods_num' => array_sum(array_column($item_data,'num')),
- ];
- $order_info = \app\common\model\WashOrder::create($order_insert);
- foreach ($item_data as &$gv) {
- $gv['order_id'] = $order_info->id;
- }
- (new WashOrderItem())->insertAll($item_data);
- Db::commit();
- }catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('订单创建成功',['order_id'=>$order_info->id]);
- }
- /**
- * @title 订单支付
- * @desc 订单支付
- * @author qc
- * @url /api/Wash_order/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @return name:config type:array default:0 desc:小程序支付配置
- * @return name:config.appId type:string default:-- desc:appid
- * @return name:config.nonceStr type:string default:-- desc:随机字符串
- * @return name:config.signType type:string default:-- desc:签名类型
- * @return name:config.paySign type:string default:-- desc:签名
- * @return name:config.timestamp type:string default:-- desc:时间戳
- */
- public function payOrder()
- {
- $order_id = input('post.order_id');
- $order_info = \app\common\model\WashOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($order_info['status'] != 0) $this->error('订单状态错误');
- if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
- if($order_info['price_total'] <= 0) $this->error('订单金额错误');
- $user_info = User::where('id',$this->user_id)->find()->toArray();
- $ret_data = ['pay_status'=>0,'config'=>[]];
- Db::startTrans();
- try {
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/washOrderNotify';
- $pay_no = $order_info['pay_no'] ;
- $config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if(!$config) throw new Exception('支付配置错误');
- $ret_data['config'] = $config;
- Db::commit();
- }catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('ok',$ret_data);
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @method GET
- * @url /api/Wash_order/getWashOrderList
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:status type:int default:-1 desc:订单状态(-1全部,0未支付,1已支付,2已完成,9取消)
- * @return name:id type:int default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:pay_no type:string default:-- desc:支付号
- * @return name:status type:int default:-- desc:订单状态(-1全部,0未支付,1已支付,2已完成,9取消)
- * @return name:create_at type:string default:-- desc:下单时间
- * @return name:pro_name type:string default:-- desc:省名称(订单地址)
- * @return name:city_name type:string default:-- desc:市名称(订单地址)
- * @return name:county_name type:string default:-- desc:县区名称(订单地址)
- * @return name:add_detail type:string default:-- desc:详细地址(订单地址)
- * @return name:user_name type:string default:-- desc:订单人
- * @return name:phone type:string default:-- desc:人联系
- * @return name:order_item type:array default:-- desc:订单明细
- * @return name:order_item.id type:int default:-- desc:订单明细id
- * @return name:order_item.cate_id type:int default:-- desc:洗鞋分类id
- * @return name:order_item.sell_price type:int default:-- desc:价格
- * @return name:order_item.images type:int default:-- desc:图片(多张)
- * @return name:order_item.images_arr type:array default:-- desc:图片(多张)
- * @return name:order_item.remark type:int default:-- desc:备注
- * @return name:order_item.cate_name type:string default:-- desc:分类名
- * @return name:order_item.cate_cover type:string default:-- desc:分类logo
- * @return name:order_item.cate_title type:string default:-- desc:价格区间标题
- * @return name:order_item.cate_price type:string default:-- desc:价格[订单单价]
- */
- public function getWashOrderList()
- {
- $status = input('get.status',-1);
- $where = [];
- $where[] = ['user_id','=',$this->user_id];
- $where[] = ['is_deleted','=',0];
- if($status > -1) $where[] = ['status','=',$status];
- $list = \app\common\model\WashOrder::with('orderItem')
- ->where($where)
- ->limit($this->off_set,$this->page_num)
- ->order('id desc')
- ->select()->toArray();
- array_walk($list,function (&$v,$k){
- foreach ($v['order_item'] as &$iv){
- $iv['images_arr'] = $iv['images'] ? explode('|',$iv['images']) : null;
- $cate_info = WashCate::where('id',$iv['cate_id'])->find()->toArray();
- $iv['cate_name'] = $cate_info['title'];
- $iv['cate_cover'] = $cate_info['logo'];
- $ladder_set = json_decode($cate_info['ladder_set'],true);
- $iv['cate_title'] = $ladder_set[$iv['ladder_key']]['title'];
- $iv['cate_price'] = $ladder_set[$iv['ladder_key']]['price'];
- }
- });
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表【订单类型不要了】
- * @author qc
- * @method GET
- * @url /api/Wash_order/getWashOrderDetail
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int default:1 desc:订单id
- * @return name:id type:int default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:pay_no type:string default:-- desc:支付号
- * @return name:status type:int default:-- desc:订单状态(0未支付,1已支付,2已完成,9取消)
- * @return name:create_at type:string default:-- desc:下单时间
- * @return name:pay_at type:string default:-- desc:支付时间
- * @return name:pro_name type:string default:-- desc:省名称(订单地址)
- * @return name:city_name type:string default:-- desc:市名称(订单地址)
- * @return name:county_name type:string default:-- desc:县区名称(订单地址)
- * @return name:add_detail type:string default:-- desc:详细地址(订单地址)
- * @return name:user_name type:string default:-- desc:订单人
- * @return name:phone type:string default:-- desc:人联系
- * @return name:order_item type:array default:-- desc:订单明细
- * @return name:order_item.id type:int default:-- desc:订单明细id
- * @return name:order_item.cate_id type:int default:-- desc:洗鞋分类id
- * @return name:order_item.sell_price type:int default:-- desc:价格
- * @return name:order_item.images type:int default:-- desc:图片(多张)
- * @return name:order_item.images_arr type:array default:-- desc:图片(多张)
- * @return name:order_item.remark type:string default:-- desc:备注
- * @return name:order_item.cate_name type:string default:-- desc:分类名
- * @return name:order_item.cate_cover type:string default:-- desc:分类logo
- * @return name:order_item.cate_title type:string default:-- desc:价格区间标题
- * @return name:order_item.cate_price type:string default:-- desc:价格[订单单价]
- *
- */
- public function getWashOrderDetail()
- {
- $order_id = input('get.id');
- $detail = \app\common\model\WashOrder::with('orderItem')
- ->where('id',$order_id)
- ->find()->toArray();
- array_walk($detail['order_item'],function (&$v){
- $v['images_arr'] = $v['images'] ? explode('|',$v['images']) : null;
- $cate_info = WashCate::where('id',$v['cate_id'])->find()->toArray();
- $v['cate_name'] = $cate_info['title'];
- $v['cate_cover'] = $cate_info['logo'];
- $ladder_set = json_decode($cate_info['ladder_set'],true);
- $v['cate_title'] = $ladder_set[$v['ladder_key']]['title'];
- $v['cate_price'] = $ladder_set[$v['ladder_key']]['title'];
- });
- $this->success('ok',['detail'=>$detail]);
- }
- /**
- * @title 取消预约
- * @desc 取消预约
- * @author qc
- * @url /api/Wash_order/cancelOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:0 desc:订单id
- * @param name:cancel_desc type:string default:0 desc:取消原因
- */
- public function cancelOrder()
- {
- $order_id = input('post.id');
- $cancel_desc = input('post.cancel_desc');
- Db::startTrans();
- try {
- $detail = \app\common\model\WashOrder::where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->exception('订单已完成或是已取消');
- \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]);
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
- }
- /**
- * @title 预约完成
- * @desc 预约完成
- * @author qc
- * @url /api/Wash_order/completeOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:0 desc:订单记录id
- */
- public function completeOrder()
- {
- $order_id = input('post.id');
- $detail = \app\common\model\WashOrder::where('id',$order_id)->find()->toArray();
- if($detail['status'] != 1) $this->error('订单状态错误');
- \app\common\model\WashOrder::update(['status'=>2],['id'=>$order_id]);
- $this->success('订单已完成');
- }
- }
|