Client.php 13.0 KB
<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 * 用户端接口
 */
class Client extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $postParam;
    protected $store_id;
    protected $store;

    public function _initialize()
    {
        $postParam = $this->request->param();
        $this->postParam = $postParam;

        $res=$this->request->header();

        parent::_initialize();
        //$this->auth->id ;//测试用
        $postParam = $this->request->param();
        $this->postParam = $postParam;
        if (empty($postParam['store_id'])) {
            $this->error("门店store_id异常");
        }
        $this->store_id = $postParam['store_id'];
        $store = Db::name("verification_store")->where("id", $this->store_id)->find();
        if (empty($store)) {
            $this->error("门店不存在,请退出登录后重试!");
        }
        $this->store = $store;


    }
    /**
     * 1、获取活动
     */
    public function getactivity(){

        //根据门店查询活动
        $time=time();
        $where["closetime"] = [">", $time];
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')
            ->alias('a')
            ->join('verification_store b','a.verification_store_id=b.id')
            ->field('a.*,b.address,b.name,b.phone')
            ->where($where)->find();
        if(!empty($activity)){
            $activity['image'] = !empty($activity['poster'])?request()->domain().$activity['image']:'';
            $activity['poster'] = !empty($activity['poster'])?request()->domain().$activity['poster']:'';
        }
        $this->success('活动获取成功', $activity);
    }
    /**
     * 1.1参加活动用户信息
     */
    public function participate_activitie(){
        $activity_id=$this->postParam['id'];
        $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10);
        $where['type']=1;
        $where['verification_activity_id']=$activity_id;

        $user=Db::name("verification_order")
            ->where($where)
            ->field('name,createtime')
            ->paginate($total, false, ['page' => $page])
            ->toArray();
        if(!empty($user['data'])){
        for ($i=0;$i<count($user['data']);$i++){
            $sort=$i+1;
            $user['data'][$i]['sort']=$sort;
            $user['data'][$i]['createtime']=date("Y-m-d",$user['data'][$i]['createtime']);
            $user['data'][$i]['name']=$this->substr_cut($user['data'][$i]['name']);
        }
        }

        $this->success('查询成功',$user);
    }

    /**
     * 1.1.1名字加*
     * @param $user_name
     * @return string
     */
    function substr_cut($user_name){
        $strlen     = mb_strlen($user_name, 'utf-8'); //获取字符长度
        $firstStr     = mb_substr($user_name, 0, 1, 'utf-8');  //查找字符第一个
        $str=$firstStr . str_repeat('*', $strlen - 1);  //拼接第一个+把字符串 "* " 重复 $strlen - 1 次:
        return $str;
    }

    /**
     * 2、举办活动
     */
    public function holdactivity(){
        $verification_store_id=$this->store_id;
        $name=$this->postParam['name'];
        $phone=$this->postParam['phone'];
        $industry=$this->postParam['industry'];
        $address=$this->postParam['address'];
        $verification_activity_id=$this->postParam['id'];
        $user_id=$this->auth->id;
        $data=[
            'verification_store_id'=>$verification_store_id,
            'name'=>$name,
            'phone'=>$phone,
            'industry'=>$industry,
            'address'=>$address,
            'verification_activity_id'=>$verification_activity_id,
            'user_id'=>$user_id
        ];
        $res=Db::name('verification_hold_activity')->insertGetId($data);
        if($res){
            $this->success("提交成功");
        }else{
            $this->error("提交失败");
        }

    }



    /**
     * 3、查询个人订单
     */
    public function myorder(){
        $id=$this->auth->id;
        $type=$this->postParam['type'];
        $verification_store_id=$this->store_id;
        $where['a.verification_store_id']=$verification_store_id;
        $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10);

        if($type){
            $where['a.type']=$type;
        }
        $row=Db::name('verification_order')
            ->alias('a')
            ->join('verification_store b','a.verification_store_id=b.id')
            ->join('verification_activity c','a.verification_activity_id=c.id')
            ->field('a.id,a.type,a.status,c.image,a.order_no,c.title,b.address,b.lng,b.lat,a.price,a.payMode,DATE_FORMAT(FROM_UNIXTIME(a.paytime), "%Y-%m-%d %H:%i:%S") AS paytime')
            ->where($where)
            ->paginate($total, false, ['page' => $page])
            ->toArray();
            foreach ($row['data'] as $key => $value) {
                if ($row['data'][$key]['image']) {
                    $row['data'][$key]['image'] = request()->domain() . $row['data'][$key]['image'];
                }
            }

        return $this->success("查询成功",$row);
    }

    /**
     * 4、订单详情
     */
    public function orderinfo(){
        $order_id=$this->postParam['id'];
        
        $where['a.id']=$order_id;
        $where['a.user_id']=$this->auth->id;
        $verification_store_id=$this->store_id;
        $where['a.verification_store_id']=$verification_store_id;
        $order=Db::name('verification_order')
            ->alias('a')
            ->where($where)
            ->join('verification_store b','a.verification_store_id=b.id')
            ->join('verification_activity c','a.verification_activity_id=c.id')
            ->field('a.id,a.type,a.status,c.image,a.order_no,c.title,b.address,b.lng,b.lat,a.price,a.payMode,DATE_FORMAT(FROM_UNIXTIME(a.paytime), "%Y-%m-%d %H:%i:%S") AS paytime')
            ->find();
       
        if($order){
            $order['image'] = !empty($order['image'])?request()->domain().$order['image']:'';
            $receivewhere['verification_order_id']=$order_id;
            $receive=Db::name('verification_receive')
                ->alias('a')
                ->join('verification_coupon b','a.verification_coupon_id=b.id')
                ->field("a.id,b.name,b.type,b.voucher_amount,b.gift,b.consumption,b.reduction,b.consumption_name,b.image,DATE_FORMAT(FROM_UNIXTIME(b.closetime), '%Y-%m-%d') AS closetime")
                ->where($receivewhere)->select();
            $order['receive']=$receive;
            foreach ($order['receive'] as $key => $value) {
                if($order['receive'][$key]['image']){
                    $order['receive'][$key]['image']= request()->domain().$order['receive'][$key]['image'];
                }
            }
            $this->success("查询成功",$order);
        }else{
            $this->error("查询失败");
        }
    }

	/**
     * 5、创建订单
     */
    public function crateorder(){
        $time=time();
        $verification_activity_id=$this->postParam['verification_activity_id'];
        if(empty($verification_activity_id)){return $this->error('活动信息错误');}
        //查询到活动
        $where["id"] = ["=", $verification_activity_id];
        $activity=Db::name('verification_activity')->where($where)->find();
        //判断活动是否到期
        if(!empty($activity)){
            if($activity['closetime']<=$time){
                $this->error("活动已过期!");
            }
        }else{
            $this->error("活动不存在!");

        }
        $phone=$this->postParam['phone'];
        $name=$this->postParam['name'];
        $verification_coupon_ids=$activity['verification_coupon_ids'];
        $order_no=$time.rand(10000,99999);
        $verification_store_id=$this->store_id;
        $user_id=$this->auth->id;
        //测试 
		
        $createtime=$time;
        $type=0;
        //整理数组
        $data=[
            'order_no'=>$order_no,
            'verification_store_id'=>$verification_store_id,
            'user_id'=>$user_id,
            'createtime'=>$createtime,
            'type'=>$type,
            'price'=>$activity['price'],
            'verification_coupon_ids'=>$verification_coupon_ids,
            'verification_activity_id'=>$verification_activity_id,
            'name'=>$name,
            'phone'=>$phone
        ];
        //插入订单表
        $res=Db::name('verification_order')->insertGetId($data);
        if($res){
            $res = (new \app\api\controller\v1\Wechat())->prepare_order($order_no);
            $this->success('添加订单成功',$res);
        }else{
            $this->error("添加订单失败!");
        }
    }


    /**
     * 6、卡卷详情
     */
    public function receiveinfo(){
        $receive_id=$this->postParam['id'];
        $where['a.id']=$receive_id;
        $where['a.user_id']=$this->auth->id;
        $field="a.*,b.image,b.name as store_name,c.name as coupon_name";
        $receive=Db::name('verification_receive')
            ->alias('a')
            ->join('verification_store b','a.verification_store_id=b.id')
            ->join('verification_coupon c','a.verification_coupon_id = c.id','left')
            ->where($where)
            ->field($field)
            ->find();
        if(!empty($receive)){
            $receive['qr_code'] = !empty($receive['qr_code'])?request()->domain().$receive['qr_code']:'';
            $receive['image'] = !empty($receive['image'])?request()->domain().$receive['image']:'';
            $receive['closetime'] = date("Y-m-d",$receive['closetime']);
        }
        $this->success("查询成功",$receive);
    }


     /*
        7.生成二维码 及 规则 https://coupon.xp.yn.cn?store_id=1&activity_id=3&share_id=2
        二维码 没有则创新
        参数 用户id 门店id 活动id
            share_id store_id activiy_id
        请求需要加 header token
        由前端自行合成分销图片
    */
    public function synthesis(){
        //参数
        $param  = request()->param();
        $user_id = $this->auth->id;
        //验证
        if(empty($user_id)){return $this->error('用户信息错误');}
        if(empty($param['store_id'])){return $this->error('请确认门店');}
        if(empty($param['activity_id'])){return $this->error('请确认活动');}
        $w['user_id'] = $user_id;
        $w['store_id'] = $param['store_id'];
        $w['activity_id'] = $param['activity_id'];
        $r = Db::name('verification_user_synthesis')->where($w)->find();
        //有 则 返回 无 则新增
        if(empty($r)){
            $param['share_id']=$user_id;
            $url = (new \app\api\controller\v1\Base())->url_h5.'?'.http_build_query($param);
            $res = (new \app\api\controller\v1\Index())->build($url);
            $w['create_at'] = time();
            $w['url'] = request()->domain().$res;
            Db::name('verification_user_synthesis')->insert($w);
            $res = $w['url'];
        }else{
            $res = $r['url'];
        }
        //获取 当前活动当前门店的背景图
        $st_a['verification_store_id'] = $param['store_id'];
        $st_a['id'] = $param['activity_id'];
        $storeActivity = Db::name('verification_activity')->where($st_a)->find();
        $data['ercode_url'] = $res;//二维码地址
        $data['bg_url'] = !empty($storeActivity['poster'])?request()->domain().$storeActivity['poster']:'';//背景图
        return $this->success('',$data);
    }


    /*
        我的礼品券
        store_id 
        分页参数page 
        coupon_type  券类型 卡卷类型:0=代金卷,1=礼品卷,2=消费卷,3=满减卷
        type 卡卷状态:0=未使用,1=已使用,2=已过期
    */
    public function my_receive_coupons_recode(){
        //参数
        $param  = request()->param();
        $user_id = $this->auth->id;
        //验证
        if(empty($user_id)){return $this->error('用户信息错误');}
        if(empty($param['store_id'])){return $this->error('请确认门店');}
        if(!is_numeric($param['type'])){return $this->error('请确认卡券类型');}
        $w['user_id'] = $user_id;
        $w['verification_store_id'] = $param['store_id'];
        $w['coupon_type'] = 1;
        $w['type'] = $param['type'];
        $data = Db::name('verification_receive')->where($w)->paginate(10)->each(function($item){
            $item['qr_code'] = !empty($item['qr_code'])?request()->domain().$item['qr_code']:'';
            $item['closetime'] = date("Y-m-d",$item['closetime']);
            //门店名称
            $store = Db::name('verification_store')->where(['id'=>$item['verification_store_id']])->field('name')->find();
            $item['store_name'] = $store['name'];
            //卡券名称
            $coupon = Db::name('verification_coupon')->where(['id'=>$item['verification_coupon_id']])->field('name')->find();
            $item['coupon_name'] = $coupon['name'];
            return $item;
        });
      
        return $this->success('',$data);
    }

}