Choose.php 11.8 KB
<?php

namespace app\api\controller;

use app\api\model\ChoosePage;
use app\api\model\Exhibition;
use app\api\model\Menu;
use app\api\model\MenuLikeLog;
use app\api\model\Video;
use app\api\model\VideoLikeLog;
use app\common\controller\Api;
use think\Db;
use think\Validate;
use think\Config;

/**
 * 选购助手接口
 */
class Choose extends Api
{

    protected function _initialize()
    {
        parent::_initialize();
    }


    /**
     * @ApiTitle    (获取选购助手列表)
     * @ApiSummary  (获取选购助手列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/choose/getChooseList)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587518501",
    "data": [
    {
    "id": 1,
    "title": "公司介绍", 标题
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 图片
    },
    {
    "id": 2,
    "title": "展会活动",
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png",
    }}
    }
    })
     */
    public function getChooseList()
    {
        $model = new ChoosePage();
        $data = $model->selectData([], $this->lang);
        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (获取选购助手图文详情)
     * @ApiSummary  (获取选购助手图文详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/choose/getChooseInfo)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=choose_id, type=string, required=true, description="选购助手id")
     * @ApiReturn({


    })
     */
    public function getChooseInfo()
    {
        $model = new ChoosePage();
        $choose_id = $this->request->param('choose_id');
        if (!$choose_id) $this->error('缺少参数 choose_id!');

        if ($this->lang == 'ch') $field = 'id,ch_title title,ch_content content,image,phone,wechat,activitytime';
        else $field = 'id,en_title title,en_content content,image,phone,wechat,activitytime';

        $data = $model->where(['id' => $choose_id])->field($field)->find();
        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (获取选购助手短视频列表)
     * @ApiSummary  (获取选购助手短视频列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/choose/getChooseVideo)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=page, type=string, required=true, description="页数")
     * @ApiReturn({


    })
     */
    public function getChooseVideo()
    {
        $model = new Video();
        $videoLogModel = new VideoLikeLog();
        $page = $this->request->param('page');
        $limit = Config::get('paginate.index_rows');

        $data = $model->selectPageData([], $page, $limit, $this->lang);
        foreach ($data['list'] as $k => $v) {
            if ($this->user['id']) {
                $data['list'][$k]['like_status'] = $videoLogModel->where(['user_id' => $this->user['id'], 'video_id' => $v['id']])->count();
            } else {
                $data['list'][$k]['like_status'] = 0;
            }
        }
        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (获取选购助手短视频点赞/取消点赞)
     * @ApiSummary  (获取选购助手短视频点赞/取消点赞)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=video_id, type=string, required=true, description="视频id")
     * @ApiReturn({

    })
     */
    public
    function addVideoLikeLog()
    {
        $model = new Video();
        $videoLogModel = new VideoLikeLog();
        $userId = $this->getUserId();
        $videoId = $this->request->param('video_id');
        if (!$videoId) $this->error('缺少参数 video_id!');

        $data = [
            'user_id' => $userId,
            'video_id' => $videoId
        ];

        $last = $videoLogModel->where($data)->count();
        if ($last) {
            $videoLogModel->where($data)->delete();
            $model->where('id', $videoId)->setDec('like_num', 1);
            $this->success('取消点赞');
        } else {
            $videoLogModel->save($data);
            $model->where('id', $videoId)->setInc('like_num', 1);
            $this->success('点赞成功');
        }
    }

    /**
     * @ApiTitle    (获取选购助手食谱列表)
     * @ApiSummary  (获取选购助手食谱列表)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=page, type=string, required=true, description="页数")
     * @ApiReturn({


    })
     */
    public function getChooseMenu()
    {
        $model = new Menu();
        $menuLogModel = new MenuLikeLog();
        $page = $this->request->param('page');
        $limit = Config::get('paginate.index_rows');

        $data = $model->selectPageData([], $page, $limit, $this->lang);
        foreach ($data['list'] as $k => $v) {
            if ($this->user['id']) {
                $data['list'][$k]['like_status'] = $menuLogModel->where(['user_id' => $this->user['id'], 'menu_id' => $v['id']])->count();
            } else {
                $data['list'][$k]['like_status'] = 0;
            }
        }
        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (获取选购助手食谱点赞/取消点赞)
     * @ApiSummary  (获取选购助手食谱点赞/取消点赞)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=menu_id, type=string, required=true, description="食谱id")
     * @ApiReturn({

    })
     */
    public
    function addMenuLikeLog()
    {
        $model = new Menu();
        $menuLogModel = new MenuLikeLog();
        $userId = $this->getUserId();
        $menuId = $this->request->param('menu_id');
        if (!$menuId) $this->error('缺少参数 menu_id!');

        $data = [
            'user_id' => $userId,
            'menu_id' => $menuId
        ];

        $last = $menuLogModel->where($data)->count();
        if ($last) {
            $menuLogModel->where($data)->delete();
            $model->where('id', $menuId)->setDec('like_num', 1);
            $this->success('取消点赞');
        } else {
            $menuLogModel->save($data);
            $model->where('id', $menuId)->setInc('like_num', 1);
            $this->success('点赞成功');
        }
    }

    /**
     * @ApiTitle    (获取选购助手食谱详情)
     * @ApiSummary  (获取选购助手食谱详情)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=menu_id, type=string, required=true, description="食谱id")
     * @ApiReturn({


    })
     */
    public function getChooseMenuInfo()
    {
        $model = new Menu();
        $menuLogModel = new MenuLikeLog();
        $menuId = $this->request->param('menu_id');
        if (!$menuId) $this->error('缺少参数 menu_id!');

        $data = $model->findData(['id'=>$menuId],$this->lang);

        if ($this->user['id']) {
            $data['like_status'] = $menuLogModel->where(['user_id' => $this->user['id'], 'menu_id' => $menuId])->count();
        } else {
            $data['like_status'] = 0;
        }
        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (添加展会预约(个人预约))
     * @ApiSummary  (添加展会预约(个人预约))
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/choose/addExhibitionType1)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=name, type=string, required=true, description="姓名")
     * @ApiParams  (name=phone, type=string, required=true, description="手机号")
     * @ApiParams  (name=code, type=string, required=true, description="验证码")
     * @ApiParams  (name=sex, type=string, required=true, description="性别:1=男,2=女")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587472510",
    "data":
    ]
    }
    })
     */
    public
    function addExhibitionType1()
    {
        $model = new Exhibition();
        $userId = $this->getUserId();
        $data = $this->request->param();
        $data['user_id'] = $userId;
        $data['status'] = 1; //个人预约
        $validate = new Validate([
            'name' => 'require',
            'phone' => ['^1\d{10}$', 'require'],
            'sex' => 'require',
            'code' => 'require',
        ]);

        $validate->message([
            'name' => '缺少参数 name!',
            'phone.require' => '缺少参数 phone!',
            'sex' => '缺少参数 sex!',
            'phone' => '手机号格式错误',
            'code' => '缺少参数 code!',
        ]);

        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $smsModel = new \app\common\model\Sms();
        $last = $smsModel->where(['mobile' => $data['phone'], 'code' => $data['code']])->find();
        if ($last) {
            if ($last['createtime'] + 600 < time()) $this->error('验证码已失效');
            unset($data['code']);
            $data = $model->save($data);
            $this->success('SUCCESS', $data);
        } else {
            $this->error('验证码不正确');
        }
    }

    /**
     * @ApiTitle    (添加展会预约(商家预约))
     * @ApiSummary  (添加展会预约(商家预约))
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/choose/addExhibitionType2)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=name, type=string, required=true, description="姓名")
     * @ApiParams  (name=phone, type=string, required=true, description="手机号")
     * @ApiParams  (name=code, type=string, required=true, description="验证码")
     * @ApiParams  (name=sex, type=string, required=true, description="性别:1=男,2=女")
     * @ApiParams  (name=store_type, type=string, required=true, description="商家类型:1=公司,2=餐厅")
     * @ApiParams  (name=num, type=string, required=true, description="参展人数")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587472510",
    "data":
    })
     */
    public
    function addExhibitionType2()
    {
        $model = new Exhibition();
        $userId = $this->getUserId();
        $data = $this->request->param();
        $data['user_id'] = $userId;
        $data['status'] = 2; //商家预约
        $validate = new Validate([
            'name' => 'require',
            'phone' => ['^1\d{10}$', 'require'],
            'sex' => 'require',
            'store_type' => 'require',
            'num' => 'require',
            'code' => 'require',
        ]);

        $validate->message([
            'name' => '缺少参数 name!',
            'phone.require' => '缺少参数 phone!',
            'phone' => '手机号格式错误',
            'sex' => '缺少参数 sex!',
            'store_type' => '缺少参数 store_type!',
            'num' => '缺少参数 num!',
            'code' => '缺少参数 code!',
        ]);

        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $smsModel = new \app\common\model\Sms();
        $last = $smsModel->where(['mobile' => $data['phone'], 'code' => $data['code']])->find();
        if ($last) {
            if ($last['createtime'] + 600 < time()) $this->error('验证码已失效');
            unset($data['code']);
            $data = $model->save($data);
            $this->success('SUCCESS', $data);
        } else {
            $this->error('验证码不正确');
        }
    }
}