User.php 11.0 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Db;
use think\Validate;
use fast\Http;

/**
 * 个人中心
 */
class User extends Api
{
    protected $noNeedLogin = ['login'];
    protected $noNeedRight = '*';
    protected $uid = '';

    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (小程序登录)
     * @ApiSummary  (小程序登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/login)
     * @ApiParams   (name="code", type="string", required=true, description="小程序code")
     * @ApiParams   (name="nickname", type="string", required=true, description="小程序昵称")
     * @ApiParams   (name="avatar", type="string", required=true, description="小程序头像")
     * @ApiReturn({
    "code": 1,
    "msg": "登录成功",
    "time": "1553839125",
    "data": {
    "token": "677afb39-1a4f-4492-84d3-0bcf32016b8a",//token
    "user_id": 27,//用户id
    "createtime": 1553839125,//登录时间
    "expiretime": 1556431125,//token失效时间
    "expires_in": 2592000//token失效剩余时间(单位s)
    "openid": 1485212522522//openid
    })
     */
    public function login(){
        if($this->request->isPost()){
            //小程序配置
            $config =  config('verify.raw');
            //小程序传递数据,包含昵称,头像,code
            $raw_data = $this->request->post();
            //验证表数据
            $rule = config('verify.user');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check($raw_data)) {
                $this->error($validate->getError());
            }
            $params = [
                'appid'      => $config['app_id'],
                'secret'     => $config['secret'],
                'js_code'    => $raw_data['code'],
                'grant_type' => 'authorization_code'
            ];
            $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
            if ($result['ret']) {
                $json = (array)json_decode($result['msg'], true);
                if (isset($json['openid'])) {
                    $result = [
                        'openid' => $json['openid'],
                        'nickname' => $raw_data['nickname'],
                        'avatar' => $raw_data['avatar']
                    ];
                    $ret = $this->auth->login($result);
                    if ($ret) {
                        $data = $this->auth->getUserinfo();
                        $this->success('登录成功', $data);
                    }else {
                        $this->error($this->auth->getError());
                    }
                } else {
                    $this->error("登录失败",$json);
                }
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (首页视频、图片、精美大片入口)
     * @ApiSummary  (首页视频、图片、精美大片入口)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/index)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "time": "1571492001",
        "data": {
        "video_thumbnail": http://mapwww.baidu.com 视频封面图地址
        "pic_thumbnail": http://mapwww.baidu.com 图片地址
        "wonderful_thumbnail": http://mapwww.baidu.com 精美大片地址
        }
    })
     */
    public function index()
    {
        $rule = config('verify.path');
        $data = Db::name('video')
            ->field('id,thumbnail')
            ->order('id desc')
            ->limit('1')
            ->find();
        $pic = Db::name('pic')
            ->field('id,thumbnail')
            ->order('id desc')
            ->limit('1')
            ->find();
        $wonderful = Db::name('wonderful')
            ->field('id,thumbnail')
            ->order('id desc')
            ->limit('1')
            ->find();
        $this->auth;
        $res['video_thumbnail'] = $rule.$data['thumbnail'];
        $res['pic_thumbnail'] = $rule.$pic['thumbnail'];
        $res['wonderful_thumbnail'] = $rule.$wonderful['thumbnail'];
        $this->success('SUCCESS',$res);
    }

    /**
     * @ApiTitle    (地区分类)
     * @ApiSummary  (地区分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/type)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "id": 4,//分类id
        "area_name": //分类名称
        "thumbnail": //图片
        }
    })
     */
    public function type()
    {
        $rule = config('verify.path');
        $data = Db::name('type')
            ->field('id,area_name,thumbnail')
            ->order('id desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] = $rule.$v['thumbnail'];
        }
        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (搜索页)
     * @ApiSummary  (搜索页)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/search)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type_id", type="inter", required=false, description="视频/图片  (视频为1 图片为2 精美大片为3)")
     * @ApiParams   (name="keyword", type="string", required=true, description="关键字")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//视频id/图片id,
            "thumbnail": //视频封面图地址/图片地址,
            "video": //视频路径/精美大片视频路径,
        }
    })
     */
    public function search()
    {
        $rule = config('verify.path');
        $type_id = $this->request->post('type_id');
        $keyword = $this->request->post('keyword');
        if(!empty($keyword)){
            if($type_id == 1){
                $where['keyword'] = ['like',"%$keyword%"];
                $where['title'] = ['like',"%$keyword%"];
                $data = Db::name('video')
                    ->where($where)
                    ->order('id desc')
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
                $this->success('SUCCESS',$data);
        }elseif($type_id == 2){
                $where['keyword'] = ['like',"%$keyword%"];
                $where['title'] = ['like',"%$keyword%"];
                $data = Db::name('pic')
                    ->where($where)
                    ->order('id desc')
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                }
                $this->success('SUCCESS',$data);
            }else{
                $where['keyword'] = ['like',"%$keyword%"];
                $data = Db::name('wonderful')
                    ->where($where)
                    ->order('id desc')
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
                $this->success('SUCCESS',$data);
            }
        }else{
            $this->error(['code'=>2,'msg'=>'请输入关键字']);
        }

    }

    /**
     * @ApiTitle    (图片列表)
     * @ApiSummary  (图片列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/pic)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type_id", type="inter", required=false, description="地区ID")
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "id": 4,//图片id,
    "thumbnail": //缩略图,
    }
    })
     */
    public function pic()
    {
        $rule = config('verify.path');
        $type_id = $this->request->post('type_id');
        if(empty($type_id)){
            $data = Db::name('pic')
                ->field('id,thumbnail')
                ->order('id desc')
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
            }
            $this->success('SUCCESS',$data);
        }else{
            $type = ','.$type_id.',';
            $where['type_ids'] = ['like',"%$type%"];
            $data = Db::name('pic')
                ->where($where)
                ->field('id,thumbnail')
                ->order('id desc')
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
            }
            $this->success('SUCCESS',$data);
        }
    }

    /**
     * @ApiTitle    (图片详情)
     * @ApiSummary  (图片详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/picdetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="pic_id", type="inter", required=true, description="图片ID")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//图片id,
            "thumbnail": //缩略图,
            "water_url": //小样带水印的图片地址,
            "url": //高清地址,
           "price"://正常价格,
           "vip_price"://vip价格,
           "content"://图片介绍,
           "is_vip"://是否为会员(0否 1是)

        }
    })
     */
    public function picdetail()
    {
        $uid = $this->uid;
        $rule = config('verify.path');
        $res = Db::name('user')
            ->where('id',$uid)
            ->field('id,is_vip')
            ->find();
        if($res['is_vip'] == 0){
            $pic_id = $this->request->post('pic_id');
            if(empty($pic_id)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $data = Db::name('pic')
                ->where('id',$pic_id)
                ->find();
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['water_url'] = $rule.$data['water_url'];
            $data['is_vip'] = 0;
            $this->success('SUCCESS',$data);
        }elseif($res['is_vip'] == 1){
            $pic_id = $this->request->post('pic_id');
            if(empty($pic_id)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $data = Db::name('pic')
                ->where('id',$pic_id)
                ->find();
            $data['is_vip'] = 1;
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['water_url'] = $rule.$data['water_url'];
            $this->success('SUCCESS',$data);
        }
    }

}