123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- namespace app\api\controller;
- use think\cache\driver\Redis;
- use think\Db;
- use think\Exception;
- use EasyWeChat\Factory;
- use think\Session;
- /**
- * @title 商品订单
- * @controller GoodsOrder
- * @group base
- */
- class GoodsOrder extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::check_login();
- cancel_goods_order($this->uid);
- }
- /**
- * @title 生成订单(立即购买 )
- * @desc 生成订单(立即购买 )
- * @author qc
- * @url /api/Goods_order/createOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:-- desc:商品id
- * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
- */
- public function createOrder()
- {
- $goods_id = input("post.goods_id");
- $num = input("post.num",1);
- $msg= ''; $com=true; $order_id = 0;
- Db::startTrans();
- try {
- $user_info = Db::name('store_member')->where('id',$this->uid)->find();
- if(!$user_info['is_auth']) throw new Exception('请实名认证后购买!');
- $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'type'=>1,'status'=>1,'is_deleted'=>0])->find();
- if(empty($goods_info)) throw new Exception('商品已下架,嘤嘤嘤~~~');
- if(strtotime($goods_info['sell_time']) > time()) throw new Exception('商品还未开始售卖!请耐心等待');
- if($goods_info['stock'] <= 0) throw new Exception('商品已售罄~~');
- if($goods_info['goods_price'] <= 0) throw new Exception('商品价格有误');
- $pro_info = [
- 'type'=>$goods_info['type'],
- 'is_gift'=>$goods_info['is_gift'],
- 'crystal'=>$goods_info['crystal'],
- 'gift_day'=>$goods_info['gift_day'],
- 'goods_price'=>$goods_info['goods_price'],
- ];
- $order_int =[
- 'uid' => $this->uid,
- 'order_no' => get_order_sn(),
- 'price_total' => $goods_info['goods_price'] * $num,
- 'goods_num' => $num,
- 'goods_id' => $goods_id,
- 'goods_cover' => $goods_info['cover'],
- 'goods_name' => $goods_info['name'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'pro_info' => json_encode($pro_info),
- ];
- //扣减库存
- Db::table('store_goods')->where('id',$goods_id)->setDec('stock',$num);
- // 生成订单
- Db::table('goods_order')->insert($order_int);
- $order_id = Db::getLastInsID();
- 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/Goods_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已支付(待发货),9取消
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1微信)
- * @return name:pay_time type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:goods_cover type:string default:-- desc:商品封面
- * @return name:goods_auth type:string default:-- desc:创作者
- * @return name:auth_img type:string default:-- desc:创作者头像
- * @return name:issuer type:string default:-- desc:发行方
- * @return name:detail type:string default:-- desc:详情
- * @return name:buy_notice type:string default:-- desc:购买须知
- * @return name:pay_content type:string default:-- desc:支付弹窗内容
- * @return name:pay_pic type:string default:-- desc:支付弹窗图片
- * @return name:adv_type type:int default:-- desc:广告类型(1图片,2mp3,3视频)
- * @return name:adv_cover type:string default:-- desc:广告图片
- * @return name:mp3_url type:string default:-- desc:广告mp3
- * @return name:audio_url type:string default:-- desc:广告视频
- */
- public function getOrderDetail()
- {
- $order_id = input('id',0);
- $detail = Db::table('goods_order')->alias('o')
- ->field('o.*,g.goods_auth,g.auth_img,g.goods_auth,g.issuer,g.detail,g.buy_notice,g.pay_content,g.pay_pic,g.type as goods_type,g.adv_type,g.adv_cover,g.mp3_url,g.audio_line,g.audio_url,g.vir_num')
- ->join('store_goods g','g.id = o.goods_id','LEFT')
- ->find($order_id);
- $this->success('获取成功',['detail'=>$detail]);
- }
- /**
- * @title 订单支付
- * @desc 订单支付
- * @author qc
- * @url /api/Goods_order/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:0 default:0 desc:订单id
- * @return name:config type:array default:0 desc:支付配置
- * @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:时间戳
- * @return name:goods_info type:array default:-- desc:商品
- * @return name:goods_info.pay_pic type:string default:-- desc:支付弹窗图片
- * @return name:goods_info.pay_content type:string default:-- desc:支付弹窗内容
- */
- public function payOrder(){
- Db::startTrans();
- $msg= 'ok';$config=false;
- try{
- $order_id = input('post.id',0);
- $order_info = Db::table('goods_order')->find($order_id);
- if(empty($order_info)) throw new Exception('订单不存在');
- if($order_info['status'] != 0 ) throw new Exception('订单状态有误!');
- $user_info = Db::table('store_member')->find($this->uid);
- if(!$user_info['openid']) throw new Exception('用户openid为空!');
- $goods_info = Db::table('store_goods')->field('pay_pic,pay_content')->find($order_info['goods_id']);
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- if(!$order_info['pay_no'])Db::table('goods_order')->where(['id'=>$order_id])->update(['pay_no'=>$pay_no]);
- $notify_url = $this->request->root(true) . '/api/Pay/goodsOrderNotify';
- $config = Pay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- $config ? Db::commit() :Db::rollback();
- }catch (\Exception $e){
- Db::rollback();
- $msg = $e->getMessage();
- }
- $config ? $this->success($msg,['config'=>$config,'goods_info'=>$goods_info]) : $this->error($msg);
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Goods_order/cancelOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- */
- public function cancelOrder()
- {
- $is_commit = true;
- Db::startTrans();
- try {
- $order_id = input('post.id');
- $order_detail = Db::table('goods_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
- if(empty($order_detail)) throw new Exception('订单不存在');
- if($order_detail['pay_state'] != 0) throw new Exception('订单已支付');
- if($order_detail['cancel_state'] != 0) throw new Exception('订单已取消,不能重复取消');
- //恢复库存
- Db::table('store_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
- // 取消订单
- Db::table('goods_order')
- ->where(['id'=>$order_id])
- ->update(['cancel_state'=>1,'status'=>9]);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $is_commit = false;
- $msg = $e->getMessage();
- }
- $is_commit ? $this->success('取消成功') : $this->error($msg);
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Goods_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:每页数
- * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付(待发货),9取消)
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),9取消
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1微信)
- * @return name:pay_time type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:goods_cover type:int default:-- desc:商品封面
- * @return name:past_int type:int default:-- desc:订单多少秒后过期
- * @return name:goods_name type:int default:-- desc:商品名称
- * @return name:goods_auth type:int default:-- desc:作者
- * @return name:pay_content type:string default:-- desc:支付弹窗内容
- * @return name:pay_pic type:string default:-- desc:支付弹窗图片
- */
- public function getMyOrderList()
- {
- $status = input('status',-1);
- $where=[];
- if($status > -1) $where['o.status'] = $status;
- $where['o.uid'] = $this->uid;
- $where['o.is_deleted'] = 0;
- $list = Db::table('goods_order')->alias('o')
- ->field('o.*,g.goods_auth,g.pay_content,g.pay_pic,g.vir_num')
- ->join('store_goods g','g.id = o.goods_id','LEFT')
- ->where($where)
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $set_time = intval(sysconf('cancel_time')) ? : 10;// 没设置默认5分钟未支付的自动取消
- array_walk($list,function (&$val) use ($set_time){
- $val['past_int'] = 0;
- if($val['status'] == 0 && strtotime($val['create_at']) + $set_time * 60 > time()){
- $val['past_int'] = strtotime($val['create_at']) + $set_time * 60 - time();
- }
- });
- $this->success('获取成功',['list'=>$list]);
- }
- /**
- * @title 删除订单
- * @desc 删除订单
- * @author qc
- * @url /api/Goods_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('goods_order')
- ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
- ->find();
- if(empty($order_info)) $this->error('订单不存在');
- if($order_info['status'] == 0) $this->error('未取消订单不能删除');
- Db::table('goods_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
- $this->success('删除成功');
- }
- }
|