Files
huangjingfen/pro_v3.5.1/app/services/pc/LoginServices.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

141 lines
4.9 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\pc;
use app\services\BaseServices;
use app\services\user\UserServices;
use app\services\wechat\WechatUserServices;
use crmeb\services\CacheService;
use crmeb\services\wechat\OfficialAccount;
use think\exception\ValidateException;
class LoginServices extends BaseServices
{
/**
* 扫码登陆
* @param string $key
* @return array|int[]
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function scanLogin(string $key)
{
$hasKey = CacheService::has($key);
if ($hasKey === false) {
$status = 0;//不存在需要刷新二维码
} else {
$keyValue = CacheService::get($key);
if ($keyValue === 0) {
$status = 1;//正在扫描中
/** @var UserServices $user */
$user = app()->make(UserServices::class);
$userInfo = $user->get(['uniqid' => $key], ['account', 'uniqid']);
if ($userInfo) {
$tokenInfo = $this->authLogin($userInfo->account);
$tokenInfo['status'] = 3;
$userInfo->uniqid = '';
$userInfo->save();
CacheService::delete($key);
return $tokenInfo;
}
} else {
$status = 2;//没有扫描
}
}
return ['status' => $status];
}
/**
* 扫码登陆
* @param string $account
* @param string|null $password
* @return array
*/
public function authLogin(string $account, string $password = null)
{
/** @var UserServices $user */
$user = app()->make(UserServices::class);
$userInfo = $user->get(['account' => $account]);
if (!$userInfo) {
throw new ValidateException('没有此用户');
}
if ($password && !password_verify($password, $userInfo->pwd)) {
throw new ValidateException('账号或密码错误');
}
if (!$userInfo->status) {
throw new ValidateException('您已被禁止登录');
}
$uid = (int)$userInfo->uid;
$token = $this->createToken($uid, 'api', $userInfo->pwd ?? '');
$userInfo->update_time = time();
$userInfo->ip = request()->ip();
$userInfo->save();
// 用户登录成功事件
event('user.login', [$uid, app('request')->ip()]);
return [
'token' => $token['token'],
'exp_time' => $token['params']['exp'],
'userInfo' => $userInfo->toArray()
];
}
/**
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function wechatAuth()
{
/** @var OfficialAccount $service */
$service = app()->make(OfficialAccount::class);
$info = $service->setAccessEnd(OfficialAccount::PC)->userFromCode();
if (!$info) {
throw new ValidateException('授权失败');
}
$wechatInfo = $info;
if (!isset($wechatInfo['unionid'])) {
throw new ValidateException('unionid不存在');
}
if (!isset($wechatInfo['nickname'])) {
$wechatInfo = OfficialAccount::instance()->user()->get($wechatInfo['openid'])->toArray();
if (!isset($wechatInfo['nickname']))
throw new ValidateException('授权失败');
if (isset($wechatInfo['tagid_list']))
$wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
} else {
if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
/** @var WechatUserServices $wechatUser */
$wechatUser = app()->make(WechatUserServices::class);
if (!$wechatUser->getOne(['openid' => $wechatInfo['openid']])) {
$wechatInfo['subscribe'] = 0;
}
}
$wechatInfo['user_type'] = 'pc';
$openid = $wechatInfo['openid'];
/** @var WechatUserServices $wechatUserServices */
$wechatUserServices = app()->make(WechatUserServices::class);
$user = $wechatUserServices->getAuthUserInfo($openid, 'pc');
$createData = [$openid, $wechatInfo, 0, 'pc', 'pc'];
if (!$user) {
$user = $wechatUserServices->wechatOauthAfter($createData);
} else {
//更新用户信息
$wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
}
$token = $this->createToken((int)$user->uid, 'api', $user->pwd ?? '');
return [
'token' => $token['token'],
'exp_time' => $token['params']['exp']
];
}
}