268 lines
9.4 KiB
PHP
268 lines
9.4 KiB
PHP
<?php
|
||
|
||
namespace app\store\controller;
|
||
|
||
use think\Db;
|
||
use think\facade\Log;
|
||
use app\common\service\UserApiKeyService;
|
||
|
||
/**
|
||
* 用户管理控制器
|
||
*/
|
||
class UserController extends BaseController
|
||
{
|
||
/**
|
||
* 获取用户资料
|
||
* GET /v2/store/user/profile
|
||
*
|
||
* @return \think\response\Json
|
||
*/
|
||
public function getProfile()
|
||
{
|
||
try {
|
||
$userId = $this->userInfo['id'] ?? 0;
|
||
$companyId = $this->userInfo['companyId'] ?? 0;
|
||
|
||
if (empty($userId)) {
|
||
return json(['code' => 401, 'msg' => '请先登录']);
|
||
}
|
||
|
||
// 获取用户基本信息
|
||
$user = Db::name('users')
|
||
->where([
|
||
['id', '=', $userId],
|
||
['companyId', '=', $companyId],
|
||
['typeId', '=', 2], // 门店端用户
|
||
['deleteTime', '=', 0]
|
||
])
|
||
->field('id, account, username, phone, avatar, companyId, typeId, status, createTime')
|
||
->find();
|
||
|
||
if (empty($user)) {
|
||
return json(['code' => 404, 'msg' => '用户不存在']);
|
||
}
|
||
|
||
// 获取算力信息
|
||
$tokensCompany = Db::name('tokens_company')
|
||
->where([
|
||
['userId', '=', $userId],
|
||
['companyId', '=', $companyId]
|
||
])
|
||
->find();
|
||
|
||
$remainingTokens = $tokensCompany ? intval($tokensCompany['tokens'] ?? 0) : 0;
|
||
|
||
// 统计今日消费
|
||
$todayStart = strtotime(date('Y-m-d 00:00:00'));
|
||
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
|
||
$todayUsed = Db::name('tokens_record')
|
||
->where([
|
||
['userId', '=', $userId],
|
||
['companyId', '=', $companyId],
|
||
['type', '=', 0], // 0为减少(消费)
|
||
['createTime', '>=', $todayStart],
|
||
['createTime', '<=', $todayEnd]
|
||
])
|
||
->sum('tokens');
|
||
$todayUsed = intval($todayUsed);
|
||
|
||
// 统计本月消费
|
||
$monthStart = strtotime(date('Y-m-01 00:00:00'));
|
||
$monthEnd = strtotime(date('Y-m-t 23:59:59'));
|
||
$monthUsed = Db::name('tokens_record')
|
||
->where([
|
||
['userId', '=', $userId],
|
||
['companyId', '=', $companyId],
|
||
['type', '=', 0], // 0为减少(消费)
|
||
['createTime', '>=', $monthStart],
|
||
['createTime', '<=', $monthEnd]
|
||
])
|
||
->sum('tokens');
|
||
$monthUsed = intval($monthUsed);
|
||
|
||
// 总充值算力
|
||
$totalRecharged = Db::name('tokens_record')
|
||
->where([
|
||
['userId', '=', $userId],
|
||
['companyId', '=', $companyId],
|
||
['type', '=', 1] // 1为增加(充值)
|
||
])
|
||
->sum('tokens');
|
||
$totalRecharged = intval($totalRecharged);
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '获取成功',
|
||
'data' => [
|
||
'id' => intval($user['id']),
|
||
'account' => $user['account'] ?? '',
|
||
'username' => $user['username'] ?? '',
|
||
'phone' => $user['phone'] ?? '',
|
||
'avatar' => $user['avatar'] ?? 'https://img.icons8.com/color/512/circled-user-male-skin-type-7.png',
|
||
'companyId' => intval($user['companyId']),
|
||
'typeId' => intval($user['typeId']),
|
||
'status' => intval($user['status']),
|
||
'createTime' => !empty($user['createTime']) && is_numeric($user['createTime']) ? date('Y-m-d H:i:s', intval($user['createTime'])) : '',
|
||
// 算力信息
|
||
'tokens' => [
|
||
'remainingTokens' => $remainingTokens, // 剩余算力
|
||
'totalRecharged' => $totalRecharged, // 总算力(累计充值)
|
||
'todayUsed' => $todayUsed, // 今日使用
|
||
'monthUsed' => $monthUsed, // 本月使用
|
||
]
|
||
]
|
||
]);
|
||
} catch (\Exception $e) {
|
||
Log::error('获取用户资料失败: ' . $e->getMessage());
|
||
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新用户资料
|
||
* PUT /v2/store/user/profile
|
||
*
|
||
* @return \think\response\Json
|
||
*/
|
||
public function updateProfile()
|
||
{
|
||
try {
|
||
$userId = $this->userInfo['id'] ?? 0;
|
||
$companyId = $this->userInfo['companyId'] ?? 0;
|
||
|
||
if (empty($userId)) {
|
||
return json(['code' => 401, 'msg' => '请先登录']);
|
||
}
|
||
|
||
// 获取更新参数
|
||
$username = $this->request->param('username', '');
|
||
$avatar = $this->request->param('avatar', '');
|
||
$oldPassword = $this->request->param('oldPassword', '');
|
||
$newPassword = $this->request->param('newPassword', '');
|
||
|
||
// 检查用户是否存在
|
||
$user = Db::name('users')
|
||
->where([
|
||
['id', '=', $userId],
|
||
['companyId', '=', $companyId],
|
||
['typeId', '=', 2],
|
||
['deleteTime', '=', 0]
|
||
])
|
||
->find();
|
||
|
||
if (empty($user)) {
|
||
return json(['code' => 404, 'msg' => '用户不存在']);
|
||
}
|
||
|
||
$updateData = [];
|
||
$updateFields = [];
|
||
|
||
// 更新昵称
|
||
if ($username !== '') {
|
||
$updateData['username'] = $username;
|
||
$updateFields[] = '昵称';
|
||
}
|
||
|
||
// 更新头像
|
||
if ($avatar !== '') {
|
||
$updateData['avatar'] = $avatar;
|
||
$updateFields[] = '头像';
|
||
}
|
||
|
||
// 更新密码
|
||
if (!empty($oldPassword) && !empty($newPassword)) {
|
||
// 验证旧密码
|
||
$oldPasswordMd5 = md5($oldPassword);
|
||
if ($user['passwordMd5'] !== $oldPasswordMd5) {
|
||
return json(['code' => 400, 'msg' => '旧密码不正确']);
|
||
}
|
||
|
||
// 验证新密码长度
|
||
if (strlen($newPassword) < 6) {
|
||
return json(['code' => 400, 'msg' => '新密码长度不能少于6位']);
|
||
}
|
||
|
||
$updateData['passwordMd5'] = md5($newPassword);
|
||
$updateFields[] = '密码';
|
||
}
|
||
|
||
// 如果没有需要更新的字段
|
||
if (empty($updateData)) {
|
||
return json(['code' => 400, 'msg' => '没有需要更新的字段']);
|
||
}
|
||
|
||
// 更新数据
|
||
$updateData['updateTime'] = time();
|
||
$result = Db::name('users')
|
||
->where('id', $userId)
|
||
->update($updateData);
|
||
|
||
if ($result !== false) {
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '更新成功',
|
||
'data' => [
|
||
'updatedFields' => $updateFields
|
||
]
|
||
]);
|
||
} else {
|
||
return json(['code' => 500, 'msg' => '更新失败']);
|
||
}
|
||
} catch (\Exception $e) {
|
||
Log::error('更新用户资料失败: ' . $e->getMessage());
|
||
return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取当前用户的对外 API Key(没有则自动生成)
|
||
* GET /v2/store/user/api-key
|
||
*/
|
||
public function getApiKey()
|
||
{
|
||
$userId = $this->userInfo['id'] ?? 0;
|
||
if (empty($userId)) {
|
||
return json(['code' => 401, 'msg' => '请先登录']);
|
||
}
|
||
|
||
try {
|
||
$apiKey = UserApiKeyService::bindOrGet((int)$userId);
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => 'success',
|
||
'data' => ['apiKey' => $apiKey],
|
||
]);
|
||
} catch (\Exception $e) {
|
||
Log::error('获取 apiKey 失败: ' . $e->getMessage());
|
||
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 重新生成当前用户的对外 API Key(会覆盖旧 Key)
|
||
* POST /v2/store/user/api-key/regenerate
|
||
*/
|
||
public function regenerateApiKey()
|
||
{
|
||
$userId = $this->userInfo['id'] ?? 0;
|
||
if (empty($userId)) {
|
||
return json(['code' => 401, 'msg' => '请先登录']);
|
||
}
|
||
|
||
try {
|
||
$apiKey = UserApiKeyService::forceGenerate((int)$userId);
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '重新生成成功,请妥善保存新 Key,旧 Key 已失效',
|
||
'data' => ['apiKey' => $apiKey],
|
||
]);
|
||
} catch (\Exception $e) {
|
||
Log::error('重新生成 apiKey 失败: ' . $e->getMessage());
|
||
return json(['code' => 500, 'msg' => '重新生成失败:' . $e->getMessage()]);
|
||
}
|
||
}
|
||
}
|
||
|