Active.php 13.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/20
 * Time: 15:25
 */

namespace app\api\controller;


use app\common\controller\Api;
use function fast\e;
use think\Db;
use think\Validate;


/**
 * 活动模块
 */
class Active extends Api
{
    /**
     * @ApiTitle    (添加活动)
     * @ApiSummary  (添加活动)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/active/index)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="theme", type="string", required=false, description="农场主题")
     * @ApiParams   (name="thumbnail", type="string", required=false, description="产品图片")
     * @ApiParams   (name="image", type="string", required=false, description="轮播图")
     * @ApiParams   (name="starttime", type="string", required=false, description="活动开始时间")
     * @ApiParams   (name="endtime", type="string", required=false, description="活动结束时间")
     * @ApiParams   (name="apply_starttime", type="string", required=false, description="报名开始时间")
     * @ApiParams   (name="apply_endtime", type="string", required=false, description="报名结束时间")
     * @ApiParams   (name="address", type="string", required=false, description="活动地点")
     * @ApiParams   (name="desc", type="string", required=false, description="活动简介")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        }
        })
     */
    public function index()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->getUserId();
        $identity = Db::name('user')
            ->where('id',$param['user_id'])
            ->field('id,identity')
            ->find();
        if($identity['identity'] == 1){
            $this->success('请先入驻农场再发起活动');
        }
        $farm_id = Db::name('farm')
            ->where('user_id',$param['user_id'])
            ->field('id,name,user_id')
            ->find();
        $param['farm_id'] = $farm_id['id'];
        $validate = new Validate([
            'theme' => 'require',
            'thumbnail' => 'require',
            'starttime'=>'require',
            'endtime'=>'require',
            'apply_starttime'=>'require',
            'apply_endtime' => 'require',
            'address' => 'require',
            'desc'=>'require'
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $http = 'http://q2ugvq3qf.bkt.clouddn.com';
        $param['thumbnail'] = str_replace($http,'',$param['thumbnail']);
        $param['starttime'] = strtotime($param['starttime']);
        $param['endtime'] = strtotime($param['endtime']);
        $param['apply_starttime'] = strtotime($param['apply_starttime']);
        $param['apply_endtime'] = strtotime($param['apply_endtime']);
        if($param['starttime']<time() && $param['endtime']>time()){
            $param['status'] = 1;
        }
        if($param['starttime']>time()){
            $param['status'] = 0;
        }
        $param['createtime'] = time();
        $data = Db::name('addactive')->insertGetId($param);
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (活动详情)
     * @ApiSummary  (活动详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/active/detail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="id", type="int", required=false, description="活动id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":活动ID
            "farm_id":农场ID
            "image"://活动banner
            "theme"://活动主题
            "user_num"://报名人数
            "status"://活动状态(0未开始1进行中2已结束)
            "address"://活动地点
            "activetime"://活动时间
            "starttime"://活动开始时间
            "endtime"://活动结束时间
            "apply_starttime"://报名开始时间
            "apply_endtime"://报名结束时间
            "applytime"://报名时间
            "desc"://活动简介
        }
        })
     */
    public function detail()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('addactive')
            ->where('id',$id)
            ->field('createtime,updatetime',true)
            ->find();
        $data['image'] = explode(',',$data['image']);
        foreach ($data['image'] as &$val){
            $val = 'http://q2ugvq3qf.bkt.clouddn.com'.$val;
        }

        //编辑返回的时间
        $data['kaishitime'] = date('Y-m-d',$data['starttime']);
        $data['jieshutime'] = date('Y-m-d',$data['endtime']);
        $data['huokaitime'] = date('Y-m-d',$data['apply_starttime']);
        $data['huojietime'] = date('Y-m-d',$data['apply_endtime']);



        //详情需要用到的时间
        $data['thumbnail'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$data['thumbnail'];
        $data['activetime'] = date('Y年m月d日',$data['starttime']).'~'.date('Y年m月d日',$data['endtime']);
        $data['applytime'] = date('Y年m月d日',$data['apply_starttime']).'~'.date('Y年m月d日',$data['apply_endtime']);
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (活动编辑)
     * @ApiSummary  (活动编辑)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/active/upd)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="id", type="int", required=false, description="活动id")
     * @ApiParams   (name="theme", type="string", required=false, description="农场主题")
     * @ApiParams   (name="thumbnail", type="string", required=false, description="产品图片")
     * @ApiParams   (name="image", type="string", required=false, description="轮播图")
     * @ApiParams   (name="starttime", type="string", required=false, description="活动开始时间")
     * @ApiParams   (name="endtime", type="string", required=false, description="活动结束时间")
     * @ApiParams   (name="apply_starttime", type="string", required=false, description="报名开始时间")
     * @ApiParams   (name="apply_endtime", type="string", required=false, description="报名结束时间")
     * @ApiParams   (name="address", type="string", required=false, description="活动地点")
     * @ApiParams   (name="desc", type="string", required=false, description="活动简介")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function upd()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->getUserId();

        if(empty($param['id'])){
            $this->error('缺少必要参数');
        }
        $identity = Db::name('user')
            ->where('id',$param['user_id'])
            ->field('id,identity')
            ->find();
        if($identity['identity'] == 1){
            $this->success('请先入驻农场再发起活动');
        }
        $farm_id = Db::name('farm')
            ->where('user_id',$param['user_id'])
            ->field('id,name,user_id')
            ->find();
        $param['farm_id'] = $farm_id['id'];
        $validate = new Validate([
            'theme' => 'require',
            'thumbnail' => 'require',
            'starttime'=>'require',
            'endtime'=>'require',
            'apply_starttime'=>'require',
            'apply_endtime' => 'require',
            'address' => 'require',
            'desc'=>'require'
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $http = 'http://q2ugvq3qf.bkt.clouddn.com';
        $param['thumbnail'] = str_replace($http,'',$param['thumbnail']);
        $param['starttime'] = strtotime($param['starttime']);
        $param['endtime'] = strtotime($param['endtime']);
        $param['apply_starttime'] = strtotime($param['apply_starttime']);
        $param['apply_endtime'] = strtotime($param['apply_endtime']);
        if($param['starttime']<time() && $param['endtime']>time()){
            $param['status'] = 1;
        }
        if($param['starttime']>time()){
            $param['status'] = 0;
        }
        $param['updatetime'] = time();
        $data = Db::name('addactive')->where('id',$param['id'])->update($param);
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (活动报名)
     * @ApiSummary  (活动报名)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/active/apply)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="active_id", type="int", required=false, description="活动ID")
     * @ApiParams   (name="name", type="string", required=false, description="姓名")
     * @ApiParams   (name="phone", type="string", required=false, description="电话")
     * @ApiParams   (name="address", type="string", required=false, description="地址")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function apply()
    {
        $param['user_id'] = $this->getUserId();
        $param['addactive_id'] = $this->request->param('active_id');
        $param['name'] = $this->request->param('name');
        $param['phone'] = $this->request->param('phone');
        $param['address'] = $this->request->param('address');

        if(empty($param['addactive_id'])){
            $this->error('缺少必要参数');
        }
        //查询用户是否报过名
        $apply = Db::name('apply')
            ->where('user_id',$param['user_id'])
            ->where('addactive_id',$param['addactive_id'])
            ->find();
        if(!empty($apply)){
            $this->error('您已经报过名了');
        }

        $data = Db::name('addactive')
            ->where('id',$param['addactive_id'])
            ->find();
        $param['createtime'] = time();
        if($param['createtime'] > $data['apply_endtime']){
            $this->error('报名时间已过');
        }
        $user_num = $data['user_num']+1;
        Db::startTrans();
        $upd = Db::name('addactive')
            ->where('id',$param['addactive_id'])
            ->update(['user_num'=>$user_num]);
        $res = Db::name('apply')
            ->insertGetId($param);
        if($res && $upd){
            Db::commit();
            $this->success('报名成功');
        }else{
           Db::rollback();
           $this->error('报名失败');
        }
    }

    /**
     * @ApiTitle    (热门活动)
     * @ApiSummary  (热门活动)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/active/hotactive)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":活动ID
            "farm_id":农场ID
            "thumbnail":活动图片
            "theme"://活动主题
            "user_num"://报名人数
            "status"://活动状态(0未开始1进行中2已结束)
            "address"://活动地点
            "activetime"://活动时间
            "applytime"://报名时间
            "desc"://活动简介
            "is_bao"://是否报过名(1没有报过2报过了)
        }
    })
     */
    public function hotactive()
    {
        $user_id = $this->getUserId();
        $data = Db::name('addactive')
            ->where('is_have',1)
            ->where('shen',1)
            ->order('status,createtime desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['thumbnail'];

            $apply = Db::name('apply')
                ->where('user_id',$user_id)
                ->where('addactive_id',$v['id'])
                ->find();
            if(empty($apply)){
                $v['is_bao'] = 1;
            }else{
                $v['is_bao'] = 2;
            }
            $v['activetime'] = date('Y年m月d日',$v['starttime']).'~'.date('m月d日',$v['endtime']);
            $v['applytime'] = date('Y年m月d日',$v['apply_starttime']).'~'.date('m月d日',$v['apply_endtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (发起历史)
     * @ApiSummary  (发起历史)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/active/hisactive)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        "id":活动ID
        "farm_id":农场ID
        "thumbnail":活动图片
        "theme"://活动主题
        "user_num"://报名人数
        "status"://活动状态(0未开始1进行中2已结束)
        "address"://活动地点
        "activetime"://活动时间
        "applytime"://报名时间
        "desc"://活动简介
        }
        })
     */
    public function hisactive()
    {
        $user_id = $this->getUserId();
        $data = Db::name('addactive')
            ->where('user_id',$user_id)
            ->order('status,createtime desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['thumbnail'];
            $v['activetime'] = date('Y年m月d日',$v['starttime']).'~'.date('m月d日',$v['endtime']);
            $v['applytime'] = date('Y年m月d日',$v['apply_starttime']).'~'.date('m月d日',$v['apply_endtime']);
        }
        $this->success('success',$data);
    }
}