User.php 13.5 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use EasyWeChat\Foundation\Application;
use fast\Random;
use think\Db;
use think\Request;
use think\Validate;
use function fast\e;

/*小程序Config*/
define('appid', 'wx7334e2c232b4ccee');
define('secret', '608b8b235d8f31c81f8df29518a7554c');

/**
 * 用户接口
 */
class User extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    /**
     * @ApiTitle    (小程序授权)
     * @ApiSummary  (小程序授权)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/MiniAppLogin)
     * @ApiParams   (name="nickName", type="string", required=true, description="微信名")
     * @ApiParams   (name="avatarUrl", type="string", required=true, description="头像")
     * @ApiParams   (name="code", type="string", required=true, description="code")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "token":登录令牌,
    "status":身份:0=用户,1=教练,2=超级教练员,
    }
    })
     */
    public function MiniAppLogin()
    {
        $params = Request::instance()->post(false);
//        $options = [
//            // ...
//            'mini_program' => [
//                'app_id' => appid,
//                'secret' => secret,
//                'token' => 'component-token',
//                'aes_key' => 'component-aes-key'
//            ],
//            // ...
//        ];
//        $app = new Application($options);
//        $miniProgram = $app->mini_program;
//        $sessionKey = $miniProgram->sns->getSessionKey($params['code']);
//        $Json = $miniProgram->encryptor->decryptData($sessionKey['session_key'], urldecode($params['iv']), urldecode($params['encryptData']));
        $Json['openId'] = $this->OpenId($params['code']);
        $Json['avatarUrl'] = $params['avatarUrl'];
        $Json['nickName'] = $params['nickName'];
//        dump($Json);
//        die;
//        if (!$Json['openId']) $this->error('授权失败,未获取用户令牌', 0);
        $OpenInfo = Db::name('user')->where(['open_id' => $Json['openId']])->find();
        $Token = $this->request->token();
        if (empty($OpenInfo)) {
            /*新用户*/
            $IsInsert = Db::name('user')->insert(['open_id' => $Json['openId'], 'avatar' => $Json['avatarUrl'], 'nickname' => $Json['nickName'], 'CountMinutes' => 0, 'vip' => 0, 'exp_time' => 0, 'status' => 0, 'type' => 0, 'token' => $Token, 'createtime' => time(), 'updatetime' => time()]);
            if (!$IsInsert) $this->error('注册失败', 0);
        } else {
            /*老用户*/
            $IsUpdate = Db::name('user')->where('id', $OpenInfo['id'])->update(['token' => $Token, 'updatetime' => time()]);
            if (!$IsUpdate) $this->error('登录失败', 0);
        }
        if (Db::name('user')->where('open_id', $Json['openId'])->value('type') == 1) $this->error('您已被拉黑', 0);
        $this->success('成功', Db::name('user')->where('open_id', $Json['openId'])->field('token,status,base_id')->find());
    }


    /**
     * @ApiTitle    (Code换Token)
     * @ApiSummary  (Code换Token)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/OtherToken)
     * @ApiParams   (name="code", type="string", required=true, description="code")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "token":登录令牌,
    }
    })
     */
    public function OtherToken()
    {
        $code = input('code');
        $IsOpen = Db::name('user')->where(['open_id' => $this->OpenId($code)])->find();
        $Token = $this->request->token();
        if ($IsOpen) Db::name('user')->where('id', $IsOpen['id'])->update(['token' => $Token, 'updatetime' => time()]);
        else $Token = '';
        $this->success('成功', $Token);
    }


    /**
     * @ApiTitle    (个人信息)
     * @ApiSummary  (个人信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UserInfo)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/dUsuTnYJVYOVz8aosicMWpO2KLTdjOTcW0baOaWstIyslUyXvFDe49GZ0skc4icIfNAMqHzDcxHePUgltkr12ibBQ/132",
    "nickname": "王叨叨",
    "CountMinutes": 0.02, //飞行时间
    "vip": 2,  //会员等级:0=*,1=普通会员,2=白金会员,3=终身会员
    "exp_time": "2021-06-11 13:36:01", //会员到期时间
    "status": 0, //身份:0=用户,1=教练,2=超级教练员
    "days": 64, //剩余天数
    "HaveMinutes": 3.33 //剩余小时数
    }
    })
     */
    public function UserInfo()
    {
        $UserId = $this->IsToken();
        $UserInfo = Db::name('user')->where('id', $UserId)->field('avatar,nickname,CountMinutes,vip,exp_time,status,mobile,card_num,learn_image,address,jingli_id')->find();
        if (empty($UserInfo)) $this->error('用户不存在', 0);
        $UserInfo['avatarUrl'] = $UserInfo['avatar'];
        $UserInfo['avatar'] = $this->UserAvatar($UserInfo['avatar']);
        $UserInfo['days'] = ($UserInfo['exp_time'] == 0) ? '*' : floor(($UserInfo['exp_time'] - time()) / 86400);
        $UserInfo['exp_time'] = ($UserInfo['exp_time'] == 0) ? '*' : date('Y-m-d H:i:s', $UserInfo['exp_time']);
        /*累计飞行时间*/
        $UserInfo['CountMinutes'] = ($UserInfo['CountMinutes'] == 0) ? 0 : round($UserInfo['CountMinutes'] / 60, 2);
        /*剩余飞行时间*/
        $UserInfo['HaveMinutes'] = ($this->HaveMinutes($UserId) == 0) ? 0 : round($this->HaveMinutes($UserId) / 60, 2);
        $UserInfo['VipContent'] = Db::name('vip')->where('id', $UserInfo['vip'])->value('content');
        $UserInfo['LearnImageUrl'] = ($UserInfo['learn_image'] == null) ? '' : $UserInfo['learn_image'];
        $UserInfo['learn_image'] = ($UserInfo['learn_image'] == null) ? '' : $this->UserAvatar($UserInfo['learn_image']);
        $UserInfo['jingli_name'] = ($UserInfo['jingli_id'] == null) ? '' : Db::name('jingli')->where('id', $UserInfo['jingli_id'])->value('name');
        $city = Db::name('address')->where('user_id', $UserId)->value('city');
        $UserInfo['address'] = ($city == null) ? '' : $city;
        $this->success('成功', $UserInfo);
    }


    /**
     * @ApiTitle    (更改个人信息)
     * @ApiSummary  (更改个人信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UpdateUserInfo)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="avatar", type="string", required=true, description="头像")
     * @ApiParams   (name="nickname", type="string", required=true, description="昵称")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="card_num", type="string", required=true, description="身份证号")
     * @ApiParams   (name="learn_image", type="string", required=true, description="学历证书")
     * @ApiParams   (name="jingli_id", type="string", required=true, description="客户经理 空穿null")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function UpdateUserInfo()
    {
        $UserId = $this->IsToken();
        $Params = $this->request->param();
        $data = [
            'avatar' => $Params['avatar'],
            'nickname' => $Params['nickname'],
            'mobile' => $Params['mobile'],
            'card_num' => $Params['card_num'],
            'learn_image' => $Params['learn_image'],
            'jingli_id' => $Params['jingli_id'],
        ];
        $Res = Db::name('user')->where('id', $UserId)->update($data);
        $this->Res($Res);
    }


    /**
     * @ApiTitle    (地址回显示)
     * @ApiSummary  (地址回显示)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UserAddress)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    })
     */
    public function UserAddress()
    {
        $UserId = $this->IsToken();
        $this->success('成功', Db::name('address')->where('user_id', $UserId)->find());
    }


    /**
     * @ApiTitle    (地址更改)
     * @ApiSummary  (地址更改)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UserUpdate)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="name", type="string", required=true, description="姓名")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="city", type="string", required=true, description="市区")
     * @ApiParams   (name="address", type="string", required=true, description="详细地址")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    })
     */
    public function UserUpdate()
    {
        $UserId = $this->IsToken();
        $Params = $this->request->param();
        $Info = Db::name('address')->where('user_id', $UserId)->find();
        if (empty($Info)) Db::name('address')->insert(['user_id' => $UserId, 'name' => $Params['name'], 'mobile' => $Params['mobile'], 'city' => $Params['city'], 'address' => $Params['address']]);
        else Db::name('address')->where('user_id', $UserId)->update(['name' => $Params['name'], 'mobile' => $Params['mobile'], 'city' => $Params['city'], 'address' => $Params['address'], 'updatetime' => time()]);
        $this->success('成功', 1);
    }


    /**
     * @ApiTitle    (会员页面)
     * @ApiSummary  (会员页面)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/VipHtml)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    //会员返回
    "data": {
    "vip": 1, //会员等级:0=*,1=普通会员,2=白金会员,3=终身会员
    "exp_time": "2021-04-10 14:07:46", //到期时间
    "days": 2, //剩余天数
    "content": "<p>普通会员普通会员</p>"
    }
    //非会员返回List数组
    "data": {
    "vip": 0, //会员等级:0=*,1=普通会员,2=白金会员,3=终身会员
    "List": [
    {
    "id": 1,
    "title": "普通会员",
    "days": 365,
    "content": "<p>普通会员普通会员</p>",
    "price": "0.01"
    },
    {
    "id": 2,
    "title": "白金会员",
    "days": 365,
    "content": "<p>白金会员白金会员白金会员</p>",
    "price": "0.01"
    }
    ]
    }
    })
     */
    public function VipHtml()
    {
        $UserId = $this->IsToken();
        $VipInfo = Db::name('user')->where('id', $UserId)->field('vip,exp_time')->find();
        if ($VipInfo['vip'] == 0)/*非会员*/ $data = ['vip' => $VipInfo['vip'], 'List' => Db::name('vip')->select()];
        else/*会员*/ $data = ['vip' => $VipInfo['vip'], 'exp_time' => ($VipInfo['vip'] == 3) ? '*' : date('Y-m-d H:i:s', $VipInfo['exp_time']), 'days' => ($VipInfo['vip'] == 3) ? '*' : floor(($VipInfo['exp_time'] - time()) / 86400), 'content' => Db::name('vip')->where('id', $VipInfo['vip'])->value('content')];
        $this->success('成功', $data);
    }


    /**
     * @ApiTitle    (客户经理列表)
     * @ApiSummary  (客户经理列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/JingLiList)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    })
     */
    public function JingLiList()
    {
        $this->success('成功', Db::name('jingli')->order('id desc')->select());
    }


    /**
     * @ApiTitle    (是否首次预约)
     * @ApiSummary  (是否首次预约)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/IsFirstYuyue)
     * @ApiParams   (name="id", type="string", required=true, description="预约单ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':1=首次,0=非首次
    })
     */
    public function IsFirstYuyue()
    {
        $UserId = Db::name('yuyue')->where('id', input('id'))->value('user_id');
        $CountMinutes = Db::name('user')->where('id', $UserId)->value('CountMinutes');
        $this->success('成功', ($CountMinutes == 0) ? 1 : 0);
    }
}