增加钉钉内应用登录、增加钉钉账号绑定
Signed-off-by: vilson <545522390@qq.com>
This commit is contained in:
parent
c712f9ee2a
commit
a344aaa61e
application
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace app\common\Model;
|
namespace app\common\Model;
|
||||||
|
|
||||||
|
use PDOStatement;
|
||||||
use service\JwtService;
|
use service\JwtService;
|
||||||
use service\NodeService;
|
use service\NodeService;
|
||||||
use service\RandomService;
|
use service\RandomService;
|
||||||
@ -124,27 +125,60 @@ class Member extends CommonModel
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 钉钉登录
|
||||||
|
* @param $userInfo
|
||||||
|
* @return Member|array|PDOStatement|string|\think\Model|null
|
||||||
|
* @throws \think\db\exception\DataNotFoundException
|
||||||
|
* @throws \think\db\exception\ModelNotFoundException
|
||||||
|
* @throws \think\exception\DbException
|
||||||
|
*/
|
||||||
public static function dingtalkLogin($userInfo)
|
public static function dingtalkLogin($userInfo)
|
||||||
{
|
{
|
||||||
|
$currentMember = getCurrentMember();
|
||||||
|
$where = ['code' => $currentMember['code']];
|
||||||
|
$currentMember = self::where($where)->find();
|
||||||
|
|
||||||
$unionid = $userInfo['unionid'];
|
$unionid = $userInfo['unionid'];
|
||||||
$openid = $userInfo['openid'];
|
$openid = $userInfo['openid'];
|
||||||
$member = self::where(['dingtalk_unionid' => $unionid])->find();
|
$member = self::where(['dingtalk_unionid' => $unionid])->find();
|
||||||
|
$memberData = [
|
||||||
|
'dingtalk_openid' => $openid,
|
||||||
|
'dingtalk_unionid' => $unionid,
|
||||||
|
'dingtalk_userid' => isset($userInfo['userId']) ? $userInfo['userId'] : '',
|
||||||
|
];
|
||||||
if (!$member) {
|
if (!$member) {
|
||||||
$memberData = [
|
$memberData['name'] = $userInfo['nick'];
|
||||||
'dingtalk_openid' => $openid,
|
$memberData['avatar'] = isset($userInfo['avatar']) ? $userInfo['avatar'] : '';
|
||||||
'dingtalk_unionid' => $unionid,
|
$memberData['mobile'] = isset($userInfo['mobile']) ? $userInfo['mobile'] : '';
|
||||||
'name' => $userInfo['nick'],
|
$memberData['email'] = isset($userInfo['email']) ? $userInfo['email'] : '';
|
||||||
'dingtalk_userid' => isset($userInfo['userId']) ? $userInfo['userId'] : '',
|
if (!$currentMember) {
|
||||||
'avatar' => isset($userInfo['avatar']) ? $userInfo['avatar'] : '',
|
$member = self::createMember($memberData);
|
||||||
'mobile' => isset($userInfo['mobile']) ? $userInfo['mobile'] : '',
|
} else {
|
||||||
'email' => isset($userInfo['email']) ? $userInfo['email'] : '',
|
//已登录
|
||||||
];
|
$member = $currentMember;
|
||||||
$member = self::createMember($memberData);
|
//且未绑定,则绑定
|
||||||
|
if (!$member['dingtalk_unionid']) {
|
||||||
|
self::update($memberData, $where);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($member['dingtalk_unionid'] != $currentMember['dingtalk_unionid']) {
|
||||||
|
return error('1', '您想要绑定的第三方帐号已经被绑定给其他帐号,请先用该第三方帐号登录后,解绑释放它,再切回当前帐号发起绑定');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self::login($member);
|
self::login($member);
|
||||||
return $member;
|
return $member;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function logout()
|
||||||
|
{
|
||||||
|
!empty($_SESSION) && $_SESSION = [];
|
||||||
|
[session_unset(), session_destroy()];
|
||||||
|
setCurrentMember(null);
|
||||||
|
session('loginInfo', null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param File $file
|
* @param File $file
|
||||||
* @return array|bool
|
* @return array|bool
|
||||||
|
@ -13,6 +13,9 @@ use app\common\Model\Member;
|
|||||||
use controller\BasicApi;
|
use controller\BasicApi;
|
||||||
use EasyDingTalk\Application;
|
use EasyDingTalk\Application;
|
||||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
|
use think\db\exception\DataNotFoundException;
|
||||||
|
use think\db\exception\ModelNotFoundException;
|
||||||
|
use think\exception\DbException;
|
||||||
use think\facade\Log;
|
use think\facade\Log;
|
||||||
use think\facade\Request;
|
use think\facade\Request;
|
||||||
use think\response\Redirect;
|
use think\response\Redirect;
|
||||||
@ -24,20 +27,24 @@ class Oauth extends BasicApi
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取钉钉登录授权
|
||||||
|
* @return Redirect
|
||||||
|
*/
|
||||||
public function dingTalkOauth()
|
public function dingTalkOauth()
|
||||||
{
|
{
|
||||||
|
$redirectPath = Request::param('redirectPath', '');
|
||||||
|
$bindDingtalk = Request::param('bindDingtalk', '');
|
||||||
$currentMember = getCurrentMember();
|
$currentMember = getCurrentMember();
|
||||||
if (!$currentMember) {
|
if (!$currentMember || $bindDingtalk) {
|
||||||
$app = new Application(config('dingtalk.'));
|
$app = new Application(config('dingtalk.'));
|
||||||
// $response = $app->oauth->use('app-01')->redirect();
|
// $response = $app->oauth->use('app-01')->redirect();
|
||||||
$response = $app->oauth->use('app-01')->withQrConnect()->redirect();
|
$response = $app->oauth->use('app-01')->withQrConnect()->redirect();
|
||||||
$redirect = $response->getTargetUrl();
|
$redirect = $response->getTargetUrl();
|
||||||
if ($redirect) {
|
if ($redirectPath) {
|
||||||
if ($redirect == 'undefined') {
|
session('redirectPath', $redirectPath);
|
||||||
$redirect = '/';
|
|
||||||
}
|
|
||||||
$_SESSION['target_url'] = $redirect;
|
|
||||||
}
|
}
|
||||||
|
$redirectPath && $redirect .= '&$redirectPath=' . $redirectPath;
|
||||||
return redirect($redirect);
|
return redirect($redirect);
|
||||||
} else {
|
} else {
|
||||||
//已登录跳转
|
//已登录跳转
|
||||||
@ -45,10 +52,47 @@ class Oauth extends BasicApi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 钉钉应用内免密登录
|
||||||
|
* @throws DataNotFoundException
|
||||||
|
* @throws ModelNotFoundException
|
||||||
|
* @throws DbException
|
||||||
|
*/
|
||||||
|
public function dingTalkLoginByCode()
|
||||||
|
{
|
||||||
|
$code = Request::param('code');
|
||||||
|
logRecord($code);
|
||||||
|
$app = new Application(config('dingtalk.'));
|
||||||
|
$result = $app->user->getUserByCode($code);
|
||||||
|
logRecord($result);
|
||||||
|
if (!$result['errcode']) {
|
||||||
|
$userId = $result['userid'];
|
||||||
|
$user = $app->user->get($userId, $lang = null);
|
||||||
|
logRecord($user);
|
||||||
|
if (!$user['errcode']) {
|
||||||
|
$userInfo = [
|
||||||
|
'unionid' => $user['unionid'],
|
||||||
|
'openid' => $user['openId'],
|
||||||
|
'userId' => $user['userid'],
|
||||||
|
'name' => $user['name'],
|
||||||
|
'avatar' => $user['avatar'],
|
||||||
|
'mobile' => $user['mobile'],
|
||||||
|
'email' => $user['email'],
|
||||||
|
];
|
||||||
|
Member::dingtalkLogin($userInfo);
|
||||||
|
$loginInfo = session('loginInfo');
|
||||||
|
$this->success('', $loginInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->error($result['errmsg']);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取钉钉授权
|
* 钉钉授权回调
|
||||||
* @return RedirectResponse|Redirect
|
* @return RedirectResponse|Redirect
|
||||||
|
* @throws DataNotFoundException
|
||||||
|
* @throws ModelNotFoundException
|
||||||
|
* @throws DbException
|
||||||
*/
|
*/
|
||||||
public function dingTalkOauthCallback()
|
public function dingTalkOauthCallback()
|
||||||
{
|
{
|
||||||
@ -60,8 +104,17 @@ class Oauth extends BasicApi
|
|||||||
$redirect = $response->getTargetUrl();
|
$redirect = $response->getTargetUrl();
|
||||||
logRecord($redirect);
|
logRecord($redirect);
|
||||||
logRecord($user);
|
logRecord($user);
|
||||||
//用户注册/绑定微信
|
//用户注册/绑定钉钉
|
||||||
$targetUrl = '/index.html#/member/login?logged=1';
|
$redirectPath = session('redirectPath');
|
||||||
|
$targetUrl = '/index.html#/member/login';
|
||||||
|
if ($redirectPath) {
|
||||||
|
$targetUrl = '/index.html#' . $redirectPath;
|
||||||
|
}
|
||||||
|
if (strpos($targetUrl, '?') === false) {
|
||||||
|
$targetUrl .= '?logged=1';
|
||||||
|
} else {
|
||||||
|
$targetUrl .= '&logged=1';
|
||||||
|
}
|
||||||
if (!$user['errcode']) {
|
if (!$user['errcode']) {
|
||||||
$result = $app->user->getUseridByUnionid($user['user_info']['unionid']);
|
$result = $app->user->getUseridByUnionid($user['user_info']['unionid']);
|
||||||
if (!$result['errcode']) {
|
if (!$result['errcode']) {
|
||||||
@ -74,8 +127,15 @@ class Oauth extends BasicApi
|
|||||||
$user['user_info']['userId'] = $userId;
|
$user['user_info']['userId'] = $userId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Member::dingtalkLogin($user['user_info']);
|
$result = Member::dingtalkLogin($user['user_info']);
|
||||||
}else{
|
//跳转地址携带message可以在应用内展示通知消息
|
||||||
|
//logged=1表示业务处理成功
|
||||||
|
if (isError($result)) {
|
||||||
|
Member::logout();
|
||||||
|
$targetUrl .= '&message=' . $result['msg'];
|
||||||
|
// $targetUrl = '/index.html#/member/login?logged=0&message='.$result['msg'];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
$targetUrl = '/index.html#/member/login?logged=0&message=钉钉登录失败,请重试';
|
$targetUrl = '/index.html#/member/login?logged=0&message=钉钉登录失败,请重试';
|
||||||
}
|
}
|
||||||
// 登录成功跳转
|
// 登录成功跳转
|
||||||
|
@ -256,6 +256,31 @@ class Login extends BasicApi
|
|||||||
$this->success('发送邮件成功');
|
$this->success('发送邮件成功');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解绑钉钉
|
||||||
|
* @throws DataNotFoundException
|
||||||
|
* @throws DbException
|
||||||
|
* @throws ModelNotFoundException
|
||||||
|
*/
|
||||||
|
public function _unbindDingtalk()
|
||||||
|
{
|
||||||
|
$currentMember = getCurrentMember();
|
||||||
|
$memberCode = $currentMember['code'];
|
||||||
|
$member = Member::where(['code' => $memberCode])->find();
|
||||||
|
if (!$member['mobile'] && !$member['email']) {
|
||||||
|
$this->error('您未绑定手机或邮箱,无法解绑 ');
|
||||||
|
}
|
||||||
|
$member->dingtalk_openid = '';
|
||||||
|
$member->dingtalk_unionid = '';
|
||||||
|
$member->dingtalk_userid = '';
|
||||||
|
$member->save();
|
||||||
|
$currentMember['dingtalk_openid'] = '';
|
||||||
|
$currentMember['dingtalk_unionid'] = '';
|
||||||
|
$currentMember['dingtalk_userid'] = '';
|
||||||
|
setCurrentMember($currentMember);
|
||||||
|
$this->success('解除绑定成功', $currentMember);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证绑定邮箱
|
* 验证绑定邮箱
|
||||||
* @throws DataNotFoundException
|
* @throws DataNotFoundException
|
||||||
@ -281,22 +306,30 @@ class Login extends BasicApi
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测登陆信息
|
||||||
|
*/
|
||||||
public function _checkLogin()
|
public function _checkLogin()
|
||||||
{
|
{
|
||||||
$loginInfo = session('loginInfo');
|
$loginInfo = session('loginInfo');
|
||||||
$this->success('', $loginInfo);
|
$this->success('', $loginInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取当前用户信息
|
||||||
|
*/
|
||||||
|
public function _currentMember()
|
||||||
|
{
|
||||||
|
$this->success('', getCurrentMember());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 退出登录
|
* 退出登录
|
||||||
*/
|
*/
|
||||||
public function _out()
|
public function _out()
|
||||||
{
|
{
|
||||||
session('member') && LogService::write('系统管理', '用户退出系统成功');
|
Member::logout();
|
||||||
!empty($_SESSION) && $_SESSION = [];
|
|
||||||
[session_unset(), session_destroy()];
|
|
||||||
session('loginInfo', null);
|
|
||||||
$this->success('');
|
$this->success('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user