123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use EasyWeChat\Factory;
- /**
- * @title 兑换订单
- * @controller ExchangeOrder
- * @group base
- */
- class ExchangeOrder extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::check_login();
- }
- /**
- * @title 订单确认
- * @desc 订单确认
- * @author qc
- * @url /api/Exchange_order/orderConfirm
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:0 desc:商品id
- * @param name:num type:int require:1 default:1 desc:商品数量
- * @return name:add_info type:array require:0 default:-- desc:默认地址
- * @return name:add_info.id type:int default:1 desc:收货地址记录id
- * @return name:add_info.pro_id type:int default:-- desc:省id
- * @return name:add_info.city_id type:int default:-- desc:市id
- * @return name:add_info.county_id type:int default:-- desc:县区id
- * @return name:add_info.pro_name type:string default:-- desc:省名
- * @return name:add_info.city_name type:string default:-- desc:市名
- * @return name:add_info.county_name type:string default:-- desc:县区名
- * @return name:add_info.detail type:string default:-- desc:详细地址
- * @return name:add_info.phone type:string default:-- desc:联系电话
- * @return name:add_info.name type:string default:-- desc:联系人
- * @return name:goods_data type:array require:0 default:-- desc:商品详情
- * @return name:goods_data.id type:array require:0 default:-- desc:商品id
- * @return name:goods_data.stock type:string require:0 default:-- desc:剩余库存
- * @return name:goods_data.name type:string require:0 default:-- desc:名称
- * @return name:goods_data.cover type:string require:0 default:-- desc:图片
- * @return name:goods_data.crystal type:string require:0 default:-- desc:兑换价格
- * @return name:freight_data type:array default:-- desc:运费详情
- * @return name:freight_data.all_freight type:int default:-- desc:总运费
- * @return name:freight_data.first_freight type:int default:-- desc:首件运费
- * @return name:freight_data.later_freight type:int default:-- desc:续件运费
- * @return name:coupon_list type:array default:-- desc:可用运费券
- * @return name:coupon_list.id type:int default:-- desc:券id
- * @return name:coupon_list.past_at type:int default:-- desc:过期时间
- */
- public function orderConfirm(){
- $goods_id = input('get.goods_id',0);
- $num = input('get.num',1);
- $goods_info = Db::table('exchange_goods')->field('id,name,cover,status,stock,crystal')
- ->where(['id'=>$goods_id,'is_deleted'=>0])->find();
- // if($goods_info['status'] != 1) $this->error('商品已下架');
- // if($goods_info['stock'] < $num) $this->error('商品库存不足');
- // 默认收货地址
- $add_info = Db::table('delivery_address')->where(['user_id'=>$this->uid,'is_mr'=>1])->find();
- $freight_data = get_freight($num);// 计算运费
- // 运费券
- $coupon_list = Db::table('user_coupon_list')
- ->field('id,past_at')
- ->where('user_id','=',$this->uid)
- ->where('status','=',1)
- ->where('past_at','> time',date('Y-m-d H:i:s'))
- ->order('id desc')
- ->select();
- $this->success('ok',['add_info'=> $add_info ,'goods_data'=>$goods_info,'freight_data'=>$freight_data,'coupon_list'=>$coupon_list]);
- }
- /**
- * @title 生成订单(立即购买 )
- * @desc 生成订单(立即购买 )
- * @author qc
- * @url /api/Exchange_order/createOrderByImmediately
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:-- desc:商品id
- * @param name:num type:int require:1 default:1 desc:商品数量
- * @param name:add_id type:int require:1 default:0 desc:收货地址id
- * @param name:coupon_id type:int require:0 default:0 desc:券id
- * @param name:exchange_type type:int require:1 default:1 desc:1明信片2余额
- * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
- */
- public function createOrderByImmediately()
- {
- $goods_id = input("post.goods_id",0);
- $num = input("post.num",1);
- $add_id = input("post.add_id",0);
- $coupon_id = input("post.coupon_id",0);
- $exchange_type = input("post.exchange_type",1);
- if(!$add_id) $this->error('请选择收货地址');
- $goods_info = Db::table('exchange_goods')->field('id,name,cover,status,stock,crystal')
- ->where(['id'=>$goods_id,'is_deleted'=>0])->find();
- $send_info = Db::table('delivery_address')->field('pro_name,city_name,county_name,mer_name,detail,phone,name')->find($add_id);
- if(empty($goods_info)) $this->error('商品已下架');
- if($goods_info['status'] != 1) $this->error('商品已下架');
- if($goods_info['stock'] < $num) $this->error('商品库存不足');
- if($coupon_id) {
- $coupon_info = Db::table('user_coupon_list')
- ->where('id','=',$coupon_id)
- ->where('past_at','>=',date('Y-m-d H:i:s'))
- ->find();
- if(!$coupon_info || $coupon_info['status'] != 1 ) $this->error('运费券信息有误');
- }
- $pro_info =[
- 'goods_id' => $goods_id,
- 'name' => $goods_info['name'],
- 'cover' => $goods_info['cover'],
- 'num' => $num,
- 'crystal' => $goods_info['crystal'],
- ];
- $freight = get_freight($num); // 运费
- $price_total = $coupon_id ? $freight['all_freight'] - $freight['first_freight']:$freight['all_freight'] ;
- $order_int =[
- 'uid'=>$this->uid,
- 'order_no' => get_order_sn(),
- 'price_total' => $price_total,
- 'goods_num' => $num,
- 'pro_info' => json_encode($pro_info),
- 'create_at' =>date('Y-m-d H:i:s'),
- 'freight' =>$freight['all_freight'],
- 'cl_id' =>$coupon_id,
- 'address_id' =>$add_id,
- 'goods_id' => $goods_id,
- 'goods_name' => $goods_info['name'],
- 'goods_cover' => $goods_info['cover'],
- 'crystal' => bcmul($goods_info['crystal'],$num,2),
- 'exchange_type' =>$exchange_type,
- 'send_info' =>json_encode($send_info),
- ];
- $user_info =Db::table('store_member')->find($this->uid);
- if($exchange_type == 1 && $user_info['crystal']< $order_int['crystal']) $this->error('明信片不足!');
- if($exchange_type == 2 && $user_info['crystal_cash']< $order_int['crystal']) $this->error('余额不足!');
- Db::startTrans();
- $msg= '';$com=true;
- try{
- //扣减库存
- Db::table('exchange_goods')->where('id',$goods_id)->setDec('stock',$num);
- // 扣除货币余额
- if($exchange_type == 1 ) {
- Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal'=>bcsub($user_info['crystal'],$order_int['crystal'],2)]);
- }else{
- Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcsub($user_info['crystal_cash'],$order_int['crystal'],2)]);
- }
- if($coupon_id) Db::table('user_coupon_list')->where('id',$coupon_id)->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
- // 生成订单
- Db::table('exchange_order')->insert($order_int);
- $order_id = Db::getLastInsID();
- if($exchange_type == 1) {
- crystal_log($this->uid,$order_int['crystal']*-1,'明信片兑换',5,$order_id);
- }else{
- balance_log($this->uid,$order_int['crystal']*-1,'余额兑换',5,$order_id);
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $com=false;
- $msg = $e->getMessage();
- }
- if($com){
- $this->success('ok',['order_id'=>$order_id]);
- }else{
- $this->error($msg);
- }
- }
- /**
- * @title 获取订单详情
- * @desc 获取订单详情
- * @author qc
- * @url /api/Exchange_order/getOrderDetail
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
- * @return name:price_total type:float default:-- desc:订单金额(实际支付金额)
- * @return name:freight type:float default:-- desc:运费
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:pay_time type:string default:-- desc:支付时间
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_name type:string default:-- desc:商品名
- * @return name:goods_cover type:string default:-- desc:商品图片
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:exchange_type type:int default:-- desc:兑换方式(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:express_company_id type:string default:-- desc:物流公司id
- * @return name:send_info type:array default:-- desc:收货详情
- */
- public function getOrderDetail()
- {
- $order_id = input('id',0);
- $detail = Db::table('exchange_order')->find($order_id);
- $this->success('获取成功',$detail);
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Exchange_order/getMyOrderList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int : default:1 desc:页数
- * @param name:page_num type:int : default:20 desc:每页数
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
- * @return name:price_total type:float default:-- desc:订单金额(实际支付金额)
- * @return name:freight type:float default:-- desc:运费
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:pay_time type:string default:-- desc:支付时间
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_name type:string default:-- desc:商品名
- * @return name:goods_cover type:string default:-- desc:商品图片
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:exchange_type type:int default:-- desc:兑换方式(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:express_company_id type:string default:-- desc:物流公司id
- */
- public function getMyOrderList()
- {
- $status = input('status',-1);
- $where=[];
- if($status > -1) $where['status'] = $status;
- $where['uid'] = $this->uid;
- $where['is_deleted'] = 0;
- $list = Db::table('exchange_order')
- ->where($where)
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('获取成功',$list);
- }
- /**
- * @title 去支付
- * @desc 去支付
- * @author qc
- * @url /api/Exchange_order/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:o_id type:int require:1 default:-- desc:订单id
- * @return name:pay_type type:int require:1 default:-- desc:1纯兑换,不支付,2微信支付
- * @return name:config type:array default:0 desc:支付配置pay_type=1时忽略
- * @return name:config.appId type:string default:-- desc:公众账号ID
- * @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()
- {
- $o_id = input('post.o_id');
- $user_info = Db::table('store_member')->where('id',$this->uid)->find();
- if(!$user_info['openid']) $this->error('请先完善用户信息');
- $order_info = Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
- if(empty($order_info)) $this->error('订单不存在');
- if($order_info['cancel_state'] != 0) $this->error('订单已取消');
- if($order_info['status'] != 0) $this->error('订单已支付');
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- // 纯积分兑换
- $msg ='';
- if($order_info['price_total'] == 0 ) {
- Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])
- ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1,'pay_no'=>$pay_no]);
- $this->success('兑换成功',['pay_type'=>2,'config' => '']);
- }else{
- $notify_url = $this->request->root(true) . '/api/Pay/exchangeOrderNotify';
- Db::startTrans();
- try{
- // 更新订单
- $price_total = $order_info['price_total'];
- Db::table('exchange_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
- $config = Pay::wxPay('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
- //var_dump('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
- if($config){
- Db::commit();
- }else{
- Db::rollback();
- }
- }catch (\Exception $e){
- Db::rollback();
- $msg = $e->getMessage();
- }
- if($config){
- $this->success('ok',['pay_type'=>1,'config'=>$config]);
- }else{
- $this->error($msg);
- }
- }
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Exchange_order/cancelOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- */
- public function cancelOrder()
- {
- $order_id = input('post.id');
- $order_detail = Db::table('exchange_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
- if(empty($order_detail)) $this->error('订单不存在');
- if($order_detail['pay_state'] != 0) $this->error('订单已支付');
- if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
- $user_info =Db::table('store_member')->find($this->uid);
- Db::startTrans();
- try {
- if($order_detail['crystal'] > 0) {
- if($order_detail['exchange_type'] == 1) {
- Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcadd($user_info['crystal'],$order_detail['crystal'],2)]);
- crystal_log($this->uid,$order_detail['crystal'],'订单取消',9,$order_id);
- }else{
- Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcadd($user_info['crystal_cash'],$order_detail['crystal'],2)]);
- balance_log($this->uid,$order_detail['crystal'],'订单取消',9,$order_id);
- }
- }
- //恢复库存
- Db::table('exchange_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
- if($order_detail['cl_id']) Db::table('user_coupon_list')->where('id',$order_detail['cl_id'])->update(['status'=>1,'used_time'=>'']);
- // 取消订单
- Db::table('exchange_order')
- ->where(['id'=>$order_id])
- ->update(['cancel_state'=>1,'status'=>9]);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- }
- $this->success('取消成功');
- }
- /**
- * @title 删除订单
- * @desc 删除订单
- * @author qc
- * @url /api/Exchange_order/delOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:订单id
- */
- public function delOrder()
- {
- $order_id = input('post.id');
- $order_info = Db::table('exchange_order')
- ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
- ->find();
- if(empty($order_info)) $this->error('订单不存在');
- Db::table('exchange_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
- $this->success('删除成功');
- }
- /**
- * @title 确认收货
- * @desc 确认收货
- * @author qc
- * @url /api/Exchange_order/confirmReceipt
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:订单id
- */
- public function confirmReceipt(){
- $order_info = Db::table('exchange_order')
- ->where(['uid'=>$this->uid,'id'=>input('post.id')])
- ->find();
- if(empty($order_info) || $order_info['status'] != 2) $this->error('商品未发货');
- Db::table('exchange_order')->where(['id'=>$order_info['id']])->update(['status'=>3,'receipt_at'=>date('Y-m-d H:i:s')]);
- $this->success('收货成功');
- }
- }
|