审查视图

application/api/controller/User.php 4.9 KB
王智 authored
1 2 3 4
<?php

namespace app\api\controller;
王智 authored
5
use think\Db;
王智 authored
6 7 8
use app\common\controller\Api;

/**
王智 authored
9
 * 用户接口
王智 authored
10 11 12
 */
class User extends Api
{
王智 authored
13
    protected $noNeedLogin = ['*'];
王智 authored
14 15 16 17 18 19 20 21 22
    protected $noNeedRight = '*';

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


    /**
王智 authored
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
     * 用户接口
     * @ApiTitle    (用户登陆)
     * @ApiSummary  (用户登陆)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UserLogin)
     * @ApiParams   (name="code", type="integer", required=true, description="Code")
     * @ApiParams   (name="nickname", type="string", required=true, description="微信名")
     * @ApiParams   (name="avatar", 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':'返回成功',
    'data':{
    'token' => token,
    })
王智 authored
39
     */
王智 authored
40
    public function UserLogin()
王智 authored
41
    {
王智 authored
42 43 44
        $param = $this->request->param();
//        授权登录
        $ch = curl_init();
王智 authored
45 46
        $appid = "wx6a9080f20326f817";
        $secret = "8fe9780e13dd1fa64b886c4f716cd366";
王智 authored
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        $code = $param['code'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        if ($output === FALSE) {
            echo "CURL Error:" . curl_error($ch);
        }
        curl_close($ch);
        $curl_result = json_decode($output, true);
        $openid = $curl_result['openid'];
        $is_open = Db::name('user')->where(['openid' => $openid])->find();
        if (empty($is_open)) {
            $data = [
                'openid' => $openid,
                'updatetime' => time(),
                'createtime' => time(),
                'avatar' => $param['avatar'],
                'nickname' => $param['nickname'],
            ];
            Db::name('user')->insert($data);
        }
        $token = $this->request->token();
        $arr = [
            'nickname' => $param['nickname'],
            'avatar' => $param['avatar'],
            'token' => $token,
            'updatetime' => time(),
        ];
        $res = Db::name("user")->where(['openid' => $openid])->update($arr);
        if (!$res) {
            $this->error('授权失败', 0);
            die;
        }
        $rult = Db::name("user")->where(['openid' => $openid])->find();
        $return = [
            'token' => $rult['token'],
王智 authored
87 88
            'avatar' => $param['avatar'],
            'nickname' => $param['nickname']
王智 authored
89 90
        ];
        $this->success('成功', $return);
王智 authored
91 92
    }
王智 authored
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    /**
     * 用户接口
     * @ApiTitle    (Code换token)
     * @ApiSummary  (Code换token)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UserCode)
     * @ApiParams   (name="code", type="integer", 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' => token,
    })
     */
    public function UserCode()
    {
        $param = $this->request->param();
//        授权登录
        $ch = curl_init();
        $appid = "wx6a9080f20326f817";
        $secret = "8fe9780e13dd1fa64b886c4f716cd366";
        $code = $param['code'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        if ($output === FALSE) {
            echo "CURL Error:" . curl_error($ch);
        }
        curl_close($ch);
        $curl_result = json_decode($output, true);
        $openid = $curl_result['openid'];
        $is_open = Db::name('user')->where(['openid' => $openid])->find();
        if (empty($is_open)) {
            $this->error('请先注册授权', '', 99991);
        } else {
            $token = $this->request->token();
            $arr = [
                'token' => $token,
                'updatetime' => time(),
            ];
            $res = Db::name("user")->where(['openid' => $openid])->update($arr);
            if (!$res) {
                $this->error('Token更新失败', 0);
                die;
            }
            $this->success('成功', $token);
        }
    }
王智 authored
148
}