|
@@ -1,333 +0,0 @@
|
|
|
-<?php
|
|
|
-namespace app\api\controller;
|
|
|
-use app\common\model\GoodsOrder;
|
|
|
-use app\common\model\GoodsOrderItem;
|
|
|
-use app\common\model\StoreGoods;
|
|
|
-use app\common\model\StoreGoodsItem;
|
|
|
-use app\common\model\User;
|
|
|
-use think\Db;
|
|
|
-use think\Exception;
|
|
|
-
|
|
|
-/**
|
|
|
- * @title 商城订单
|
|
|
- * @controller Order
|
|
|
- * @group base
|
|
|
- */
|
|
|
-class Order extends Base
|
|
|
-{
|
|
|
- public function initialize()
|
|
|
- {
|
|
|
- parent::initialize();
|
|
|
- parent::checkLogin();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @title 创建订单
|
|
|
- * @desc 创建订单
|
|
|
- * @author qc
|
|
|
- * @url /api/Order/createOrder
|
|
|
- * @method POST
|
|
|
- * @header name:Authorization require:1 desc:Token
|
|
|
- * @param name:goods_json type:string require:1 default:0 desc:商品json信息[{'goods_id':'商品id',spec_id:'规格id';'num':'数量'}]
|
|
|
- * @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()
|
|
|
- {
|
|
|
- $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','');
|
|
|
- $remark = input('post.remark','');
|
|
|
- if(!$goods_json) $this->error('请选择商品');
|
|
|
- if(!$pro_name || !$city_name || !$county_name || !$add_detail) $this->error('请完善收货地址');
|
|
|
- if(!$user_name || !$phone) $this->error('请完善收货人信息');
|
|
|
- $goods_data = json_decode($goods_json,true);
|
|
|
- $goods_ids = array_column($goods_data,'goods_id');
|
|
|
- $goods_list = StoreGoods::field('specs,lists',true)->where('id','in',implode(',',$goods_ids))->where('status',1)->where('is_deleted',0)->with('itemList')->select()->toArray();
|
|
|
- if(count($goods_list) != count($goods_ids)) $this->error('所选商品已下架');
|
|
|
- $goods_column = array_column($goods_list,null,'id');
|
|
|
- $ret_data = ['pay_status'=>0,'config'=>[],'order_id'=>0];
|
|
|
- Db::startTrans();
|
|
|
- try{
|
|
|
- // 验证商品库存start
|
|
|
- $check_goods = 0;
|
|
|
- $error_goods = '';
|
|
|
- $price_goods = 0; // 商品总金额
|
|
|
- $original_total = 0; // 商品总原价
|
|
|
- $order_item = [];// 订单列表
|
|
|
- foreach ($goods_data as $gv) {
|
|
|
- $goods_spec = $goods_column[$gv['goods_id']]['item_list'];
|
|
|
- $spec_column = array_column($goods_spec,null,'id');
|
|
|
- if(!isset($spec_column[$gv['spec_id']])){
|
|
|
- $check_goods = 1;
|
|
|
- $error_goods = $goods_column[$gv['goods_id']]['name'];
|
|
|
- break;
|
|
|
- }else if($spec_column[$gv['spec_id']]['stock'] < $gv['num']){
|
|
|
- $check_goods = 2;
|
|
|
- $error_goods = $goods_column[$gv['goods_id']]['name'];
|
|
|
- break;
|
|
|
- }
|
|
|
- $price_goods += $gv['num'] * $spec_column[$gv['spec_id']]['sell_price'];
|
|
|
- $original_total += $gv['num'] * $spec_column[$gv['spec_id']]['original_price'];
|
|
|
- $order_item[] = [
|
|
|
- 'user_id' => $this->user_id,
|
|
|
- 'goods_id' => $gv['goods_id'],
|
|
|
- 'goods_no' => $spec_column[$gv['spec_id']]['goods_no'],
|
|
|
- 'goods_name' => $goods_column[$gv['goods_id']]['name'],
|
|
|
- 'goods_spec' => $spec_column[$gv['spec_id']]['goods_spec'],
|
|
|
- 'spec_title' => $spec_column[$gv['spec_id']]['spec_title'],
|
|
|
- 'spec_id' => $gv['spec_id'],
|
|
|
- 'cover' => $spec_column[$gv['spec_id']]['cover'],
|
|
|
- 'original_price' => $spec_column[$gv['spec_id']]['original_price'],
|
|
|
- 'sell_price' => $spec_column[$gv['spec_id']]['sell_price'],
|
|
|
- 'num' => $gv['num'],
|
|
|
- ];
|
|
|
- StoreGoods::where('id',$gv['goods_id'])->setDec('stock',$gv['num']);
|
|
|
- StoreGoodsItem::where('id',$gv['spec_id'])->setDec('stock',$gv['num']);
|
|
|
- }
|
|
|
- if($check_goods == 1) $this->exception('所选商品'.$error_goods.'已下架');
|
|
|
- if($check_goods == 2) $this->exception('所选商品'.$error_goods.'规格库存不足');
|
|
|
- // 验证商品库存end
|
|
|
- $order_insert = [
|
|
|
- 'user_id' => $this->user_id,
|
|
|
- 'order_no' => get_order_sn(),
|
|
|
- 'goods_num' => array_sum(array_column($goods_data,'num')),
|
|
|
- 'pro_name' => $pro_name,
|
|
|
- 'city_name' =>$city_name,
|
|
|
- 'county_name' => $county_name,
|
|
|
- 'add_detail' => $add_detail,
|
|
|
- 'user_name' => $user_name,
|
|
|
- 'phone' => $phone,
|
|
|
- 'remark' => $remark,
|
|
|
- ];
|
|
|
- $order_insert['price_total'] = $price_goods;
|
|
|
- $order_insert['price_goods'] = $price_goods ;
|
|
|
- $order_insert['original_total'] = $original_total ;
|
|
|
- $order_insert['price_express'] = 0 ;
|
|
|
- $order_info = GoodsOrder::create($order_insert);// 生成订单
|
|
|
- array_walk($order_item,function (&$v,$k)use ($order_info){
|
|
|
- $v['order_id'] = $order_info->id;
|
|
|
- });
|
|
|
- (new GoodsOrderItem())->insertAll($order_item);// 生成订单商品详情
|
|
|
- $ret_data['order_id'] = $order_info->id;
|
|
|
- Db::commit();
|
|
|
- }catch (\Exception $e){
|
|
|
- Db::rollback();
|
|
|
- $this->error($e->getMessage());
|
|
|
- }
|
|
|
- $this->success($ret_data);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * @title 订单支付
|
|
|
- * @desc 订单支付
|
|
|
- * @author qc
|
|
|
- * @url /api/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 = GoodsOrder::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/goodsOrderNotify';
|
|
|
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
|
|
|
- Db::name('store_order')->where(['uid'=>$this->user_id,'id'=>$order_id])->update(['pay_no'=>$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/Order/getOrderList
|
|
|
- * @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已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
|
|
|
- * @param name:pay_state type:int default:-1 desc:支付状态(-1全部,0未支付,1已支付)
|
|
|
- * @return name:id type:int default:-- desc:订单id
|
|
|
- * @return name:order_no type:string default:-- desc:订单号
|
|
|
- * @return name:price_total type:float default:-- desc:订单金额
|
|
|
- * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
|
|
|
- * @return name:create_at type:string default:-- desc:下单时间
|
|
|
- * @return name:remark type:string default:-- desc:订单备注
|
|
|
- * @return name:order_item type:array default:-- desc:订单商品列表
|
|
|
- * @return name:order_item.name type:float default:-- desc:商品名
|
|
|
- * @return name:order_item.cover type:float default:-- desc:商品图
|
|
|
- * @return name:order_item.sell_price type:float default:-- desc:购买价格
|
|
|
- * @return name:order_item.goods_spec type:float default:-- desc:规格
|
|
|
- * @return name:order_item.num type:int default:-- desc:数量
|
|
|
- */
|
|
|
- public function getOrderList()
|
|
|
- {
|
|
|
- $status = input('get.status',-1);
|
|
|
- $pay_state = input('get.pay_state',-1);
|
|
|
- $cancel_state = input('get.cancel_state',-1);
|
|
|
- $refund_state = input('get.apply_refund',-1);
|
|
|
- $where = [];
|
|
|
- $where[] = ['user_id','=',$this->user_id];
|
|
|
- if($status > -1) $where[] = ['status','=',$status];
|
|
|
- if($pay_state > -1) $where[] = ['pay_state','=',$pay_state];
|
|
|
- if($cancel_state > -1) $where[] = ['cancel_state','=',$cancel_state];
|
|
|
- if($refund_state == 0) $where[] = ['refund_state','=',$refund_state];
|
|
|
- if($refund_state == 1) $where[] = ['refund_state','in','1,2,3,4'];
|
|
|
- $list = GoodsOrder::field('id,order_no,status,create_at,remark')->with(['orderItem'])->where($where)->order('id desc ')
|
|
|
- ->limit($this->off_set,$this->page_num)
|
|
|
- ->select()->toArray();
|
|
|
- $this->success('ok',['list'=>$list]);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * @title 获取订单详情
|
|
|
- * @desc 获取订单详情
|
|
|
- * @author qc
|
|
|
- * @method GET
|
|
|
- * @url /api/Order/getOrderDetail
|
|
|
- * @header name:Authorization require:1 desc:Token
|
|
|
- * @param name:order_id type:int default:-- desc:订单id
|
|
|
- * @return name:id type:int default:-- desc:订单id
|
|
|
- * @return name:order_no type:string default:-- desc:订单号
|
|
|
- * @return name:price_total type:float default:-- desc:待支付金额(
|
|
|
- * @return name:price_goods type:float default:-- desc:商品金额
|
|
|
- * @return name:price_express type:0 default:-- desc:运费金额
|
|
|
- * @return name:original_total type:float default:-- desc:总原价(下划线)
|
|
|
- * @return name:original_total type:float default:-- desc:总原价(下划线)
|
|
|
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
|
|
|
- * @return name:pay_type type:int default:-- desc:支付方式(1微信)
|
|
|
- * @return name:pay_no type:string default:-- desc:支付号
|
|
|
- * @return name:pay_at type:string default:-- desc:支付时间
|
|
|
- * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
|
|
|
- * @return name:cancel_at type:string default:-- desc:取消时间
|
|
|
- * @return name:cancel_desc type:string default:-- desc:取消原因
|
|
|
- * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
|
|
|
- * @return name:express_company_title type:string default:-- desc:发货快递公司名称
|
|
|
- * @return name:express_send_no type:string default:-- desc:物流单号
|
|
|
- * @return name:express_send_at type:string default:-- desc:发货时间
|
|
|
- * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
|
|
|
- * @return name:create_at type:string default:-- desc:下单时间
|
|
|
- * @return name:pay_at type:string default:-- desc:支付时间
|
|
|
- * @return name:remark type:string default:-- desc:订单备注
|
|
|
- * @return name:goods_num type:int 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:street_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:订单商品列表(按商品id分组)
|
|
|
- * @return name:order_item.name type:float default:-- desc:商品名
|
|
|
- * @return name:order_item.cover type:float default:-- desc:商品图
|
|
|
- * @return name:order_item.sell_price type:float default:-- desc:购买价格
|
|
|
- * @return name:order_item.goods_spec type:float default:-- desc:规格
|
|
|
- * @return name:order_item.num type:int default:-- desc:数量
|
|
|
- */
|
|
|
- public function getOrderDetail()
|
|
|
- {
|
|
|
- $order_id = input('get.order_id');
|
|
|
- $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
|
|
|
- $detail['order_item'] = (new GoodsOrderItem())->getOrderItem($order_id);
|
|
|
- $this->success('ok',['detail'=>$detail]);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * @title 取消订单
|
|
|
- * @desc 取消订单
|
|
|
- * @author qc
|
|
|
- * @url /api/Order/cancelOrder
|
|
|
- * @method POST
|
|
|
- * @header name:Authorization require:1 desc:Token
|
|
|
- * @param name:order_id type:int require:1 default:0 desc:订单id
|
|
|
- */
|
|
|
- public function cancelOrder()
|
|
|
- {
|
|
|
- $order_id = input('post.order_id');
|
|
|
- Db::startTrans();
|
|
|
- try {
|
|
|
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
|
|
|
- if($detail['status'] != 0) $this->exception('订单状态有误');
|
|
|
- // 取消订单状态
|
|
|
- GoodsOrder::update(['status'=>9],['id'=>$order_id]);
|
|
|
- // 订单处理
|
|
|
- foreach ($detail['order_item'] as $item_info) {
|
|
|
- // 更改订单详情状态
|
|
|
- GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
|
|
|
- // 商品规格库存返回
|
|
|
- StoreGoodsItem::where('id',$item_info['spec_id'])->setInc('stock',$item_info['num']);
|
|
|
- // 商品总库存增加
|
|
|
- StoreGoods::where('id',$item_info['goods_id'])->setInc('stock',$item_info['num']);
|
|
|
- }
|
|
|
- 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/Order/confirmReceipt
|
|
|
- * @method POST
|
|
|
- * @header name:Authorization require:1 desc:Token
|
|
|
- * @param name:order_id type:int require:1 default:0 desc:订单id
|
|
|
- */
|
|
|
- public function confirmReceipt()
|
|
|
- {
|
|
|
- $order_id = input('post.order_id');
|
|
|
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
|
|
|
- if($detail['status'] == 0) $this->error('订单未支付');
|
|
|
- if($detail['status'] == 1) $this->error('订单未发货');
|
|
|
- if($detail['status'] == 3) $this->error('订单已收货');
|
|
|
- GoodsOrder::update(['status'=>3,'express_state'=>2],['id'=>$order_id]);
|
|
|
- GoodsOrderItem::update(['status'=>3,'express_state'=>2],['order_id'=>$order_id]);
|
|
|
- $this->success('已确认收货');
|
|
|
- }
|
|
|
-
|
|
|
-}
|