mirror of
https://gitee.com/liuxioabin/fengketrade.git
synced 2026-04-17 21:03:17 +08:00
149 lines
4.8 KiB
PHP
149 lines
4.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace addons\shopro\controller;
|
||
|
|
|
||
|
|
use addons\shopro\controller\Common;
|
||
|
|
use app\admin\model\User as UserModel;
|
||
|
|
use think\Exception;
|
||
|
|
use think\Log;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 建行生活用户登录控制器
|
||
|
|
*
|
||
|
|
* 功能:
|
||
|
|
* - 建行用户自动登录 (替代原有登录注册)
|
||
|
|
* - 用户信息同步
|
||
|
|
*
|
||
|
|
* @author Billy
|
||
|
|
* @date 2025-01-16
|
||
|
|
*/
|
||
|
|
class Ccblife extends Common
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 不需要登录的方法
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $noNeedLogin = ['autoLogin'];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 不需要权限的方法
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $noNeedRight = ['*'];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 建行用户自动登录
|
||
|
|
*
|
||
|
|
* 说明:
|
||
|
|
* 1. 弃用商城原有的登录注册功能
|
||
|
|
* 2. H5在建行App内打开时,自动通过JSBridge获取建行用户信息
|
||
|
|
* 3. 如果建行用户ID已存在则登录,不存在则自动创建商城用户并绑定
|
||
|
|
* 4. 返回商城Token用于后续API调用
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function autoLogin()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
// 1. 获取请求参数
|
||
|
|
$ccbUserId = $this->request->post('ccb_user_id', '');
|
||
|
|
$ccbParamSJ = $this->request->post('ccb_param_sj', '');
|
||
|
|
$mobile = $this->request->post('mobile', '');
|
||
|
|
$nickname = $this->request->post('nickname', '');
|
||
|
|
$avatar = $this->request->post('avatar', '');
|
||
|
|
|
||
|
|
// 2. 验证必需参数
|
||
|
|
if (empty($ccbUserId)) {
|
||
|
|
$this->error('建行用户ID不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 查询用户是否已存在
|
||
|
|
$user = UserModel::where('ccb_user_id', $ccbUserId)->find();
|
||
|
|
|
||
|
|
if ($user) {
|
||
|
|
// 用户已存在 - 直接登录
|
||
|
|
$isNewUser = false;
|
||
|
|
|
||
|
|
// 更新最后登录时间
|
||
|
|
$user->logintime = time();
|
||
|
|
$user->save();
|
||
|
|
|
||
|
|
Log::info('[建行登录] 用户登录 ' . json_encode([
|
||
|
|
'ccb_user_id' => $ccbUserId,
|
||
|
|
'user_id' => $user->id,
|
||
|
|
'is_new' => false,
|
||
|
|
], JSON_UNESCAPED_UNICODE));
|
||
|
|
|
||
|
|
} else {
|
||
|
|
// 用户不存在 - 创建新用户
|
||
|
|
$isNewUser = true;
|
||
|
|
|
||
|
|
$user = new UserModel();
|
||
|
|
$user->ccb_user_id = $ccbUserId;
|
||
|
|
$user->username = 'user_ccb_' . $ccbUserId; // 用户名: user_ccb_xxx
|
||
|
|
$user->nickname = $nickname ?: '建行用户_' . substr($ccbUserId, -4);
|
||
|
|
$user->mobile = $mobile;
|
||
|
|
$user->avatar = $avatar;
|
||
|
|
$user->salt = \fast\Random::alnum(16);
|
||
|
|
$user->password = md5(md5(\fast\Random::alnum(32)) . $user->salt); // 随机密码
|
||
|
|
$user->status = 'normal';
|
||
|
|
$user->joinip = $this->request->ip();
|
||
|
|
$user->jointime = time();
|
||
|
|
$user->logintime = time();
|
||
|
|
$user->loginip = $this->request->ip();
|
||
|
|
$user->prevtime = time();
|
||
|
|
$user->save();
|
||
|
|
|
||
|
|
Log::info('[建行登录] 新用户创建 ' . json_encode([
|
||
|
|
'ccb_user_id' => $ccbUserId,
|
||
|
|
'user_id' => $user->id,
|
||
|
|
'username' => $user->username,
|
||
|
|
], JSON_UNESCAPED_UNICODE));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. 使用Auth系统登录并生成Token
|
||
|
|
$this->auth->direct($user->id);
|
||
|
|
$token = $this->auth->getToken();
|
||
|
|
|
||
|
|
// 5. 返回结果
|
||
|
|
$this->success('登录成功', [
|
||
|
|
'token' => $token,
|
||
|
|
'user_id' => $user->id,
|
||
|
|
'is_new_user' => $isNewUser,
|
||
|
|
'userInfo' => [
|
||
|
|
'id' => $user->id,
|
||
|
|
'username' => $user->username,
|
||
|
|
'nickname' => $user->nickname,
|
||
|
|
'mobile' => $this->maskMobile($user->mobile),
|
||
|
|
'avatar' => $user->avatar,
|
||
|
|
'ccb_user_id' => $user->ccb_user_id,
|
||
|
|
'create_time' => date('Y-m-d H:i:s', $user->jointime),
|
||
|
|
],
|
||
|
|
]);
|
||
|
|
|
||
|
|
} catch (Exception $e) {
|
||
|
|
Log::error('[建行登录] 登录失败 ' . json_encode([
|
||
|
|
'error' => $e->getMessage(),
|
||
|
|
'trace' => $e->getTraceAsString(),
|
||
|
|
], JSON_UNESCAPED_UNICODE));
|
||
|
|
|
||
|
|
$this->error('登录失败: ' . $e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 手机号脱敏
|
||
|
|
*
|
||
|
|
* @param string $mobile 手机号
|
||
|
|
* @return string 脱敏后的手机号
|
||
|
|
*/
|
||
|
|
private function maskMobile($mobile)
|
||
|
|
{
|
||
|
|
if (empty($mobile) || strlen($mobile) !== 11) {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
|
||
|
|
return substr($mobile, 0, 3) . '****' . substr($mobile, -4);
|
||
|
|
}
|
||
|
|
}
|