作者 开飞机的舒克

后台修改

... ... @@ -7,7 +7,7 @@ return [
'Is_sift' => '是否为精选',
'Is_sift 0' => '否',
'Is_sift 1' => '是',
'Score' => '积分',
'Score' => '可获积分',
'Createtime' => '创建时间',
'Updatetime' => '更新时间',
'Item.title' => '名称'
... ...
... ... @@ -20,78 +20,111 @@ class User extends Api
protected $noNeedRight = ['*'];
/**
*手机号授权登录
*
* @ApiTitle (小程序登录)
* @ApiSummary (微信小程序登录)
* @ApiTitle (手机号授权登录)
* @ApiMethod (POST)
* @ApiParams (name="code", type="string", required=true, description="code")
* @ApiParams (name="phone", type="string", required=true, description="phone")
* @ApiRoute (/api/myinfo/login)
* @ApiParams (name="code", type="string", required=true, description="code-wx.login的code")
* @ApiParams (name="encryptedData", type="string", required=true, description="encryptedData")
* @ApiParams (name="iv", type="string", required=true, description="iv")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
'data':[
'id':'用户id'
'nickname':'用户昵称'
'mobile':'手机号'
'avatar':'头像'
'token':'token']
})
*/
public function login()
{
$config = Config::get('wxapp');
$code = $this->request->param('code');
$phone = $this->request->param('phone');
if (empty($code)) {
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)){
$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 (isset($phone)) {
$userId = Db::name('third')->where(['apptype' => 'wxapp', 'openid' => $json['openid']])->value('user_id');
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
'log' => [
'level' => 'debug',
'file' =>'/tmp/easywechat.log',
],
];
$sessionKey = $json['session_key'];
$app = Factory::miniProgram($options);
try {
$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');
$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' => '微信用户',
'username' => '微信用户',
'avatar' => '/assets/img/avatar.png',
'status' => 'normal',
'mobile' => $phone,
'jointime' => time()
'mobile' => $phone['phoneNumber'],
'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('请求失败');
}
}
/**
... ...
... ... @@ -155,7 +155,7 @@
<div class="list-group panel">
<a href="#个人中心" class="list-group-item" data-toggle="collapse" data-parent="#sidebar">个人中心 <i class="fa fa-caret-down"></i></a>
<div class="child collapse" id="个人中心">
<a href="javascript:;" data-id="27" class="list-group-item">小程序登录 <span class="tag">
<a href="javascript:;" data-id="27" class="list-group-item">手机号授权登录 <span class="tag">
</span>
</a>
<a href="javascript:;" data-id="28" class="list-group-item">我的首页 <span class="tag">
... ... @@ -307,7 +307,7 @@
<div class="panel-heading" id="heading-27">
<h4 class="panel-title">
<span class="label label-primary">POST</span>
<a data-toggle="collapse" data-parent="#accordion27" href="#collapseOne27"> 小程序登录 <span class="text-muted">/api/user/login</span></a>
<a data-toggle="collapse" data-parent="#accordion27" href="#collapseOne27"> 手机号授权登录 <span class="text-muted">/api/myinfo/login</span></a>
</h4>
</div>
<div id="collapseOne27" class="panel-collapse collapse">
... ... @@ -325,7 +325,7 @@
<div class="tab-pane active" id="info27">
<div class="well">
微信小程序登录 </div>
手机号授权登录 </div>
<div class="panel panel-default">
<div class="panel-heading"><strong>权限</strong></div>
<div class="panel-body">
... ... @@ -366,13 +366,19 @@
<td>code</td>
<td>string</td>
<td></td>
<td>code</td>
<td>code-wx.login的code</td>
</tr>
<tr>
<td>encryptedData</td>
<td>string</td>
<td></td>
<td>encryptedData</td>
</tr>
<tr>
<td>phone</td>
<td>iv</td>
<td>string</td>
<td></td>
<td>phone</td>
<td>iv</td>
</tr>
</tbody>
</table>
... ... @@ -395,14 +401,18 @@
</div>
</div>
<div class="panel-body">
<form enctype="application/x-www-form-urlencoded" role="form" action="/api/user/login" method="POST" name="form27" id="form27">
<form enctype="application/x-www-form-urlencoded" role="form" action="/api/myinfo/login" method="POST" name="form27" id="form27">
<div class="form-group">
<label class="control-label" for="code">code</label>
<input type="string" class="form-control input-sm" id="code" required placeholder="code" name="code">
<input type="string" class="form-control input-sm" id="code" required placeholder="code-wx.login的code" name="code">
</div>
<div class="form-group">
<label class="control-label" for="encryptedData">encryptedData</label>
<input type="string" class="form-control input-sm" id="encryptedData" required placeholder="encryptedData" name="encryptedData">
</div>
<div class="form-group">
<label class="control-label" for="phone">phone</label>
<input type="string" class="form-control input-sm" id="phone" required placeholder="phone" name="phone">
<label class="control-label" for="iv">iv</label>
<input type="string" class="form-control input-sm" id="iv" required placeholder="iv" name="iv">
</div>
<div class="form-group form-group-submit">
<button type="submit" class="btn btn-success send" rel="27">提交</button>
... ... @@ -425,7 +435,27 @@
<div class="panel panel-default">
<div class="panel-heading"><strong>返回参数</strong></div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>名称</th>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>code</td>
<td>integer</td>
<td></td>
</tr>
<tr>
<td>msg</td>
<td>string</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
... ... @@ -435,7 +465,16 @@
<div class="tab-pane" id="sample27">
<div class="row">
<div class="col-md-12">
<pre id="sample_response27"></pre>
<pre id="sample_response27">{
'code':'1',
'msg':'返回成功'
'data':[
'id':'用户id'
'nickname':'用户昵称'
'mobile':'手机号'
'avatar':'头像'
'token':'token']
}</pre>
</div>
</div>
</div><!-- #sample -->
... ... @@ -4813,7 +4852,7 @@
</div>
<div class="col-md-6" align="right">
Generated on 2023-03-13 09:57:22 <a href="./" target="_blank">校园活动</a>
Generated on 2023-03-13 10:24:59 <a href="./" target="_blank">校园活动</a>
</div>
</div>
... ...