作者 开飞机的舒克

优化api文档

... ... @@ -16,7 +16,7 @@ use think\Db;
*/
class User extends Api
{
protected $noNeedLogin = ['login','gettoken'];
protected $noNeedLogin = ['login', 'gettoken'];
protected $noNeedRight = ['*'];
/**
... ... @@ -40,53 +40,54 @@ class User extends Api
'token':'token']
})
*/
public function login(){
public function login()
{
//接收传递信息
$config = Config::get('wechat');
$encryptedData = $this->request->post('encryptedData');
$iv = $this->request->post('iv');
$code = $this->request->post('code');
if(empty($code)){
if (empty($code)) {
$this->error('参数错误');
}
$params = [
'appid'=>$config['app_id'], //小程序appid
'secret'=>$config['secret'], //小程序appid的秘钥
'js_code'=>$code, //前端传来的code
'appid' => $config['app_id'], //小程序appid
'secret' => $config['secret'], //小程序appid的秘钥
'js_code' => $code, //前端传来的code
'grant_type' => 'authorization_code' //authorization_code — 授权码模式(即先登录获取code,再获取token)
];
$result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');//接口
if($result['ret']){
$json = json_decode($result['msg'],true); //json_decode()接收一个json格式数据将他变为php变量
if(isset($json['openid'])){
if ($result['ret']) {
$json = json_decode($result['msg'], true); //json_decode()接收一个json格式数据将他变为php变量
if (isset($json['openid'])) {
$options = [
'debug'=>true,
'app_id'=>$config['app_id'],
'secret'=>$config['secret'],
'token'=>$this->auth->getToken(), //获取token
'debug' => true,
'app_id' => $config['app_id'],
'secret' => $config['secret'],
'token' => $this->auth->getToken(), //获取token
'log' => [
'level' => 'debug',
'file' =>'/tmp/easywechat.log',
'file' => '/tmp/easywechat.log',
],
];
$sessionKey = $json['session_key'];
$app = Factory::miniProgram($options);
try {
$phone = $app->encryptor->decryptData($sessionKey,$iv,$encryptedData);
}catch (\Exception $e){
$phone = $app->encryptor->decryptData($sessionKey, $iv, $encryptedData);
} catch (\Exception $e) {
$this->error('网络不好,重新操作');
}
if (isset($phone['phoneNumber'])){
$userId = Db::name('third')->where(['apptype'=>'wxapp','openid'=>$json['openid']])->value('user_id');
if (isset($phone['phoneNumber'])) {
$userId = Db::name('third')->where(['apptype' => 'wxapp', 'openid' => $json['openid']])->value('user_id');
$auth = Auth::instance();
$ret = $auth->direct($userId);
if ($ret){ //假如登录上后获取信息
if ($ret) { //假如登录上后获取信息
$data = $auth->getUserinfo();
$aut = \db('user')->where('id',$userId)->find();
$aut = \db('user')->where('id', $userId)->find();
$data['avatar'] = cdnurl($data['avatar']); //cdnurl第三方存储
$data['mobile'] = $aut['mobile'];
$this->success('登录成功',$data);
}elseif(empty($ret)){
$this->success('登录成功', $data);
} elseif (empty($ret)) {
/*注册一个用户*/
$data = [
'nickname' => '微信用户',
... ... @@ -94,33 +95,33 @@ class User extends Api
'avatar' => '/assets/img/avatar.png',
'status' => 'normal',
'mobile' => $phone['phoneNumber'],
'jointime'=>time()
'jointime' => time()
];
//插入user
$id = \db('user')->insertGetId($data);
//插入third
\db('third')->insert(['user_id' => $id, 'apptype' => 'wxapp', 'openname' => '微信用户',
'access_token' => $json['session_key'], 'openid' => $json['openid']]);
$userId = Db::name('third')->where(['apptype'=> 'wxapp', 'openid'=>$json['openid']])->value('user_id');
$userId = Db::name('third')->where(['apptype' => 'wxapp', 'openid' => $json['openid']])->value('user_id');
$auth = Auth::instance();
$ret = $auth->direct($userId);
if ($ret){
if ($ret) {
$data = $auth->getUserinfo();
$aut = \db('user')->where('id',$userId)->find();
$aut = \db('user')->where('id', $userId)->find();
$data['avatar'] = cdnurl($data['avatar']);
$data['mobile'] = $aut['mobile'];
$this->success('登录成功',$data);
}else{
$this->success('登录成功', $data);
} else {
$this->error('连接失败');
}
}
}else{
} else {
$this->error('手机号未找到,请重新输入');
}
}else{
} else {
$this->error('获取openid失败');
}
}else{
} else {
$this->error('请求失败');
}
... ... @@ -146,7 +147,7 @@ class User extends Api
}
$data['xh'] = "输入手环ID绑定信息";
$data['sm'] = "扫码绑定信息";
$this->success('',$data);
$this->success('', $data);
}
... ... @@ -216,7 +217,7 @@ class User extends Api
if (!$data) {
$this->error('暂无内容', ['status' => 2]);
}
$this->success('获取成功',$data);
$this->success('获取成功', $data);
}
... ... @@ -235,13 +236,14 @@ class User extends Api
if (!$data) {
$this->error('暂无内容', ['status' => 2]);
}
$this->success('获取成功',$data);
$this->success('获取成功', $data);
}
/**
* 获取测试token
*/
public function gettoken(){
public function gettoken()
{
$this->auth->direct(2);
$token = $this->auth->getToken();
return $token;
... ...
... ... @@ -5634,7 +5634,7 @@
</div>
<div class="col-md-6" align="right">
Generated on 2023-03-22 11:15:20 <a href="./" target="_blank">校园活动</a>
Generated on 2023-03-22 11:20:42 <a href="./" target="_blank">校园活动</a>
</div>
</div>
... ...