You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

179 lines
5.3 KiB

<?php
declare(strict_types=1);
/**
* User.php
* Created by PhpStorm.
* Date: 2026/10/17
* Time: 09:40
* Author: qian.guo
* E-mail: yiyezhiqiutel@gmail.com
*/
namespace app\controller;
use app\BaseController;
use app\model\UserModel;
use think\Request;
/**
* 用户资料控制器
* 设计说明:
* - 全部接口需要登录,路由层挂 LoginCheck,方法内直接用 $this->account_id
* - 只做业务校验(存在性/状态),参数非空与格式交给前端
* - 不提供登出接口:JWT 是无状态的,前端清掉本地 token 即完成登出
* 真要强制下线需要 Redis 黑名单(jti),尚未接入
* 与其给一个只在单机上有效的假登出,不如不做
*/
class User extends BaseController
{
/**
* 获取当前用户资料
* getProfileInfo
* @return \think\response\Json
*/
public function getProfileInfo()
{
$user_info = UserModel::getUserInfoById($this->account_id);
if (empty($user_info)) {
return json([
'code' => config('code.fail'),
'msg' => '用户不存在',
'data' => [
'err_code' => config('code.err.user_not_found')
]
]);
}
if (intval($user_info['is_enable']) !== 0) {
return json([
'code' => config('code.fail'),
'msg' => '账号已被限制',
'data' => [
'err_code' => config('code.err.user_banned')
]
]);
}
return json([
'code' => config('code.success'),
'msg' => 'success',
'data' => $this->formatUser($user_info)
]);
}
/**
* 更新当前用户资料
* profileUpdate
* @param Request $request 请求对象
* @return \think\response\Json
*
* 只取实际传入的字段:不能把未传的字段也写进待更新数组
* 否则未传时会写入空值,只改头像就把昵称清掉了
*/
public function profileUpdate(Request $request)
{
$user_info = UserModel::getUserInfoById($this->account_id);
if (empty($user_info)) {
return json([
'code' => config('code.fail'),
'msg' => '用户不存在',
'data' => [
'err_code' => config('code.err.user_not_found')
]
]);
}
$param = [];
if ($request->has('nickname')) {
$nickname = (string) $request->param('nickname', '');
$param['nickname'] = mb_substr($nickname, 0, 32);
}
if ($request->has('avatar')) {
$avatar = (string) $request->param('avatar', '');
$param['avatar'] = mb_substr($avatar, 0, 512);
}
if (empty($param)) {
return json([
'code' => config('code.fail'),
'msg' => '没有可更新的字段',
'data' => [
'err_code' => config('code.err.param')
]
]);
}
$param['update_time'] = date('Y-m-d H:i:s');
UserModel::userUpdate($this->account_id, $param);
$user_info = UserModel::getUserInfoById($this->account_id);
return json([
'code' => config('code.success'),
'msg' => 'success',
'data' => $this->formatUser($user_info)
]);
}
/**
* 注销账号
* accountDeactivate
* @return \think\response\Json
*
* 只置 is_delete=1,不物理删除:删掉后同一 openid 再次登录会变成全新用户
* 历史测评结果将永久失去归属,而用户预期是「注销后数据不再被使用」
* 不是「同一个人换个身份重新开始」。真正的数据清除应走独立的合规流程
*/
public function accountDeactivate()
{
$user_info = UserModel::getUserInfoById($this->account_id);
if (empty($user_info)) {
return json([
'code' => config('code.fail'),
'msg' => '用户不存在',
'data' => [
'err_code' => config('code.err.user_not_found')
]
]);
}
$param = [];
$param['is_delete'] = 1;
$param['update_time'] = date('Y-m-d H:i:s');
UserModel::userUpdate($this->account_id, $param);
return json([
'code' => config('code.success'),
'msg' => 'success',
'data' => [
'deactivated' => true
]
]);
}
/**
* 格式化对外用户数据
* formatUser
* @param array $user_info 用户信息
* @return array 只含对外字段
*
* 白名单:openid / unionid / login_num 等 服务端字段不出现在响应里
* 避免身份标识与运营数据外泄
*/
private function formatUser($user_info)
{
$data = [];
$data['user_id'] = intval($user_info['user_id']);
$data['nickname'] = (string) ($user_info['nickname'] ?? '');
$data['avatar'] = (string) ($user_info['avatar'] ?? '');
$data['create_time'] = (string) ($user_info['create_time'] ?? '');
return $data;
}
}