223 lines
6.9 KiB
PHP
223 lines
6.9 KiB
PHP
<?php
|
||
|
||
namespace app\cunkebao\controller;
|
||
|
||
use app\api\controller\AccountController;
|
||
use app\api\controller\UserController;
|
||
use app\common\service\ClassTableService;
|
||
use app\common\util\JwtUtil;
|
||
use library\ResponseHelper;
|
||
use think\Controller;
|
||
use think\Db;
|
||
|
||
/**
|
||
* 设备管理控制器
|
||
*/
|
||
class BaseController extends Controller
|
||
{
|
||
/**
|
||
* 用户信息
|
||
* @var object
|
||
*/
|
||
protected $user;
|
||
|
||
/**
|
||
* @var ClassTableService
|
||
*/
|
||
protected $classTable;
|
||
|
||
/**
|
||
* @inheritDoc
|
||
*/
|
||
public function __construct(ClassTableService $classTable)
|
||
{
|
||
$this->classTable = $classTable;
|
||
|
||
parent::__construct();
|
||
}
|
||
|
||
/**
|
||
* 初始化
|
||
*/
|
||
protected function initialize()
|
||
{
|
||
parent::initialize();
|
||
|
||
date_default_timezone_set('Asia/Shanghai');
|
||
}
|
||
|
||
/**
|
||
* 获取用户信息
|
||
*
|
||
* @param string $column
|
||
* @return mixed
|
||
* @throws \Exception
|
||
*/
|
||
protected function getUserInfo(?string $column = null)
|
||
{
|
||
static $userCache = null;
|
||
|
||
if ($userCache === null) {
|
||
$token = JwtUtil::getRequestToken();
|
||
$userCache = $token ? JwtUtil::verifyToken($token) : false;
|
||
if ($userCache === false) {
|
||
$userCache = $this->request->param('userInfo/a', null);
|
||
}
|
||
}
|
||
|
||
if (!$userCache) {
|
||
throw new \Exception('未授权访问,缺少有效的身份凭证', 401);
|
||
}
|
||
|
||
return ($column !== null && $column !== '') ? ($userCache[$column] ?? null) : $userCache;
|
||
}
|
||
|
||
|
||
public function editUserInfo()
|
||
{
|
||
$authUserId = (int)$this->getUserInfo('id');
|
||
$userId = (int)$this->request->param('userId', 0);
|
||
if ($userId <= 0 || $userId !== $authUserId) {
|
||
$userId = $authUserId;
|
||
}
|
||
|
||
$nickname = trim((string)$this->request->param('nickname', ''));
|
||
$avatar = trim((string)$this->request->param('avatar', ''));
|
||
$phone = trim((string)$this->request->param('phone', ''));
|
||
$account = trim((string)$this->request->param('account', ''));
|
||
$isAdminParam = $this->request->param('isAdmin', '');
|
||
|
||
if ($userId <= 0) {
|
||
return ResponseHelper::error('用户id不能为空');
|
||
}
|
||
|
||
if ($nickname === '' && $avatar === '' && $phone === '' && $account === '' && $isAdminParam === '') {
|
||
return ResponseHelper::error('修改的用户信息不能为空');
|
||
}
|
||
|
||
// 以库表为准(避免 JWT companyId 滞后导致「用户不存在」)
|
||
$user = Db::name('users')->where('id', $userId)->where('deleteTime', 0)->find();
|
||
if (empty($user)) {
|
||
return ResponseHelper::error('用户不存在');
|
||
}
|
||
$companyId = (int)$user['companyId'];
|
||
|
||
if ($phone !== '') {
|
||
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
|
||
return ResponseHelper::error('手机号格式不正确');
|
||
}
|
||
$exists = Db::name('users')->where('phone', $phone)->where('id', '<>', $userId)->find();
|
||
if ($exists) {
|
||
return ResponseHelper::error('该手机号已被其他账号使用');
|
||
}
|
||
}
|
||
|
||
if ($account !== '') {
|
||
$exists = Db::name('users')->where('account', $account)->where('id', '<>', $userId)->find();
|
||
if ($exists) {
|
||
return ResponseHelper::error('该登录账号已被使用');
|
||
}
|
||
}
|
||
|
||
$localData = ['updateTime' => time()];
|
||
if ($nickname !== '') {
|
||
$localData['username'] = $nickname;
|
||
}
|
||
if ($avatar !== '') {
|
||
$localData['avatar'] = $avatar;
|
||
}
|
||
if ($phone !== '') {
|
||
$localData['phone'] = $phone;
|
||
}
|
||
if ($account !== '') {
|
||
$localData['account'] = $account;
|
||
}
|
||
if ($isAdminParam !== '' && (int)$user['isAdmin'] === 1) {
|
||
$localData['isAdmin'] = (int)$isAdminParam ? 1 : 0;
|
||
}
|
||
|
||
// 尝试同步 S2(失败不阻断本地,与超管 UnifiedAccountService 一致)
|
||
if (!empty($user['s2_accountId'])) {
|
||
$s2Payload = ['id' => $user['s2_accountId']];
|
||
if ($nickname !== '') {
|
||
$s2Payload['nickname'] = $nickname;
|
||
}
|
||
if ($avatar !== '') {
|
||
$s2Payload['avatar'] = $avatar;
|
||
}
|
||
if ($account !== '') {
|
||
$s2Payload['userName'] = $account;
|
||
}
|
||
try {
|
||
$AccountControllel = new AccountController();
|
||
$res = $AccountControllel->accountModify($s2Payload);
|
||
if ($res instanceof \think\response\Json) {
|
||
$res = $res->getData();
|
||
} elseif (is_string($res)) {
|
||
$res = json_decode($res, true) ?: [];
|
||
}
|
||
} catch (\Throwable $e) {
|
||
// 本地优先
|
||
}
|
||
}
|
||
|
||
Db::name('users')->where('id', $userId)->update($localData);
|
||
$fresh = Db::name('users')->where('id', $userId)->find();
|
||
|
||
return ResponseHelper::success([
|
||
'id' => (int)$fresh['id'],
|
||
'username' => $fresh['username'],
|
||
'account' => $fresh['account'],
|
||
'phone' => $fresh['phone'],
|
||
'avatar' => $fresh['avatar'],
|
||
'isAdmin' => (int)$fresh['isAdmin'],
|
||
'companyId' => (int)$fresh['companyId'],
|
||
], '更新成功');
|
||
}
|
||
|
||
|
||
public function editPassWord()
|
||
{
|
||
$authUserId = (int)$this->getUserInfo('id');
|
||
$userId = (int)$this->request->param('userId', 0);
|
||
if ($userId <= 0 || $userId !== $authUserId) {
|
||
$userId = $authUserId;
|
||
}
|
||
$passWord = $this->request->param('passWord', '');
|
||
if ($userId <= 0) {
|
||
return ResponseHelper::error('用户id不能为空');
|
||
}
|
||
|
||
if (empty($passWord)) {
|
||
return ResponseHelper::error('密码不能为空');
|
||
}
|
||
|
||
$user = Db::name('users')->where('id', $userId)->where('deleteTime', 0)->find();
|
||
if (empty($user)) {
|
||
return ResponseHelper::error('用户不存在');
|
||
}
|
||
$companyId = (int)$user['companyId'];
|
||
if ($user['passwordMd5'] == md5($passWord)) {
|
||
return ResponseHelper::error('新密码与旧密码一致');
|
||
}
|
||
|
||
$data = [
|
||
'passwordMd5' => md5($passWord),
|
||
'passwordLocal' => localEncrypt($passWord),
|
||
'updateTime' => time()
|
||
];
|
||
|
||
$res = Db::name('users')->where(['id' => $userId, 'companyId' => $companyId])->update($data);
|
||
if (!empty($res)) {
|
||
if ($user['typeId'] == 1 && !empty($user['s2_accountId'])) {
|
||
$UserController = new UserController();
|
||
$UserController->modifyPwd(['id' => $user['s2_accountId'],'pwd' => $passWord]);
|
||
}
|
||
|
||
|
||
return ResponseHelper::success('密码修改成功');
|
||
} else {
|
||
return ResponseHelper::error('密码修改失败');
|
||
}
|
||
}
|
||
}
|