Coupon.php 3.1 KB
<?php

namespace app\api\controller;

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

/**
 * 首页接口
 */
class Coupon extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $postParam;
    protected $store_id;
    protected $store;

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

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

    /**
     * 1、获取活动
     */
    public function getactivity(){
        //根据门店查询活动
        $time=time();
        $where["closetime"] = [">", $time];
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')->where($where)->find();
        $this->success('活动获取成功', $activity);
    }
    /**
     * 2、创建订单
     */
    public function crateorder(){
        $time=time();
        $verification_activity_id=$this->postParam['verification_activity_id'];
        //查询到活动
        $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,
            '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){
            $this->success('添加订单成功');
        }else{
            $this->error("添加订单失败!");
        }
    }

    /**
     *3、获取订单
     */
    public function getorder(){
        $type=$this->postParam['type'];

    }
}