作者 王智

修改

... ... @@ -90,4 +90,59 @@ class User extends Api
$this->success('成功', $return);
}
/**
* 用户接口
* @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);
}
}
}
... ...