作者 何书鹏
1 个管道 的构建 通过 耗费 0 秒

微信授权登录

... ... @@ -28,6 +28,7 @@ use app\mobile\model\TopicAppraiseGood;
use app\mobile\model\UserJob;
use app\mobile\model\CompanyJob;
use app\mobile\model\Statistic;
use app\mobile\model\UserThird;
use addons\epay\library\Service;
use Endroid\QrCode\QrCode;
... ... @@ -303,6 +304,104 @@ class User extends Api
}
/**
* 第三方登录
* @ApiWeigh (91)
*
* @ApiTitle (第三方登录)
* @ApiSummary (第三方登录)
* @ApiMethod (POST)
*
* @ApiParams (name="nickname", type="integer", required=true, description="第三方账号昵称")
* @ApiParams (name="avatar", type="integer", required=false, description="第三方账号头像")
* @ApiParams (name="gender", type="integer", required=false, description="第三方性别")
* @ApiParams (name="openid", type="string", required=true, description="第三方登录返回的唯一识别数据")
* @ApiParams (name="unionid", type="string", required=false, description="第三方用户多个产品中的唯一id,如:微信开放平台")
*
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"token": 用户token
}
})
*/
public function userThird() {
$param = $this->request->param();
$validate = new Validate([
'nickname' => 'require',
'openid' => 'require',
]);
$validate->message([
'nickname.require' => '缺少参数nickname!',
'openid.require' => '缺少参数openid!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
// 判断用户是否已绑定该openid
$time = time();
$ip = request()->ip();
$third = UserThird::where('openid',$param['openid'])->find();
Db::startTrans();
if($third) {
// 修改第三方信息
$result = UserThird::where('id',$third['id'])->update([
'openname' => $param['nickname'],
'logintime'=>$time
]);
// 修改用户信息
$user = \app\common\model\User::get($third['user_id']);
if(!empty($param['nickname']) && empty($user['nickname'])){
$user->nickname = $param['nickname'];
}
if(!empty($param['avatar']) && (empty($user['image']) || stripos($user['image'], 'http') !== false)){
$user->image = $param['avatar'];
}
if(!empty($param['gender']) && !isset($user['sex'])){
$user->sex = $param['gender'] == 2 ? 0 : $param['gender'];
}
$user->loginip = $ip;
$user->logintime = $time;
$user->updatetime = $time;
$results = $user->save();
// 登录
$login = $this->auth->direct($third['user_id']);
} else {
// 添加用户信息
$result = \app\common\model\User::create([
'nickname' => $param['nickname'],
'sex' => $param['gender'] == 2 ? 0 : $param['gender'],
'image' => $param['avatar'],
'status' => 'normal',
'jointime' => $time,
'joinip' => $ip,
'logintime' => $time,
'loginip' => $ip,
'prevtime' => $time
]);
// 添加第三方信息
$results = UserThird::create([
'user_id' => $result['id'],
'openname' => $param['nickname'],
'platform' => 'wechat',
'openid' => $param['openid'],
'createtime' => $time,
'updatetime' => $time,
'logintime' => $time,
'unionid' => $param['unionid'],
]);
// 登录
$login = $this->auth->direct($result['id']);
}
if(!$result || !$results || !$login) {
Db::rollback();
$this->error('授权登录失败');
}
Db::commit();
$this->success('成功',['token'=>$this->auth->getToken()]);
}
/**
* @ApiWeigh (89)
* @ApiTitle (忘记密码)
* @ApiSummary (忘记密码)
... ... @@ -507,6 +606,85 @@ class User extends Api
}
/**
* @ApiWeigh (77)
* @ApiTitle (绑定微信)
* @ApiSummary (绑定微信)
* @ApiMethod (POST)
*
* @ApiParams (name="nickname", type="integer", required=true, description="第三方账号昵称")
* @ApiParams (name="avatar", type="integer", required=false, description="第三方账号头像")
* @ApiParams (name="gender", type="integer", required=false, description="第三方性别")
* @ApiParams (name="openid", type="string", required=true, description="第三方登录返回的唯一识别数据")
* @ApiParams (name="unionid", type="string", required=false, description="第三方用户多个产品中的唯一id,如:微信开放平台")
*
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"token": 用户token
}
})
*/
public function bindThird() {
$param = $this->request->param();
$validate = new Validate([
'nickname' => 'require',
'openid' => 'require',
]);
$validate->message([
'nickname.require' => '缺少参数nickname!',
'openid.require' => '缺少参数openid!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
// 判断用户是否已绑定该openid
$time = time();
$ip = request()->ip();
$third = UserThird::where('openid',$param['openid'])->find();
Db::startTrans();
if($third) {
$user = \app\common\model\User::get($third['user_id']);
if($user['id'] != $this->auth->id){
$this->error('该微信已绑定其他账号');
}
$result = UserThird::where('id',$third['id'])->update([
'openname' => $param['nickname'],
]);
} else {
UserThird::where('user_id',$this->auth->id)->delete();
$user = \app\common\model\User::get($this->auth->id);
$result = UserThird::create([
'user_id' => $this->auth->id,
'openname' => $param['nickname'],
'platform' => 'wechat',
'openid' => $param['openid'],
'createtime' => $time,
'updatetime' => $time,
'logintime' => $time,
'unionid' => $param['unionid'],
]);
}
if(!empty($param['nickname']) && empty($user['nickname'])){
$user->nickname = $param['nickname'];
}
if(!empty($param['avatar']) && (empty($user['image']) || stripos($user['image'], 'http') !== false)){
$user->image = $param['avatar'];
}
if(!empty($param['gender']) && !isset($user['sex'])){
$user->sex = $param['gender'] == 2 ? 0 : $param['gender'];
}
$user->updatetime = $time;
$results = $user->save();
if(!$result || !$results) {
Db::rollback();
$this->error('绑定微信失败');
}
Db::commit();
$this->success('成功');
}
/**
* @ApiWeigh (76)
* @ApiTitle (加入企业-查询企业)
* @ApiSummary (加入企业-查询企业)
... ... @@ -928,6 +1106,7 @@ class User extends Api
{
$user = \app\mobile\model\User::get($this->auth->id);
empty($user['image']) && $this->error('请先上传用户头像');
empty($user['exam']) && $this->error('请先选择考试');
//将用户的头像保存到本地
$user_dir = 'uploads/user';
if (!file_exists($user_dir)){
... ...
<?php
namespace app\mobile\model;
use think\Model;
class UserThird extends Model
{
// 表名
protected $name = 'mobile_user_third';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
}
\ No newline at end of file
... ...