Files

174 lines
4.5 KiB
PHP

<?php
/*
* @Author: YwxApp <ywx@ywxapp.cn>
* @Date: 2026-04-29 02:32:33
* @LastEditors: YwxApp <ywx@ywxapp.cn>
* @LastEditTime: 2026-07-21 23:20:37
* @Description:
* @FilePath: \ywxapp_dev\app\backend\controller\Profile.php
* @CustomString: Copyright (c) 2026 YwxApp
*/
declare (strict_types = 1);
namespace app\backend\controller;
use think\Request;
use ywxapp\controller\BackendBase;
use ywxapp\model\BackendAdmin as AdminModel;
use ywxapp\service\FileStorageService;
/**
* Profile 类
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Profile extends BackendBase
{
/**
* Summary of needLogin
* @var array
*/
protected $noNeedLogin = [];
/**
* Summary of needRight
* @var array
*/
protected $noNeedVerify = ['*'];
/**
* 控制器初始化 initialize
* @return void
*/
protected function initialize()
{
$this->model = new AdminModel;
}
/**
* 个人资料页
*/
public function index()
{
$info = $this->auth->info;
$this->assign('info', $info);
return $this->fetch();
}
/**
* 头像设置页 / 头像上传
*/
public function avatar()
{
if ($this->request->isPost()) {
$file = $this->request->file('file');
if (! $file || ! $file->isValid()) {
$this->result->error('请选择上传文件', 400);
}
$type = $this->request->param('type', 'avatar');
$storage = new FileStorageService();
$result = $storage->upload($file, $type . '/' . date('Ymd'));
$info = $this->auth->model;
if ($info) {
$info->avatar = $result['storage'] == 'local' ? '/storage/' . $result['path'] : $result['url'];
$info->save();
$this->result->success(['src' => $info->avatar], '头像上传成功');
} else {
$this->result->error($file->getError(), 500);
}
}
return $this->fetch();
}
/**
* 修改密码页
*/
public function password()
{
return $this->fetch();
}
/**
* 保存新密码
*/
public function passwordSave()
{
if (! $this->request->isPost()) {
$this->result->error('请求方式错误');
}
$old = (string) $this->request->param('old_password', '');
$new = (string) $this->request->param('password', '');
$new2 = (string) $this->request->param('password_confirm', '');
if ($new === '' || $new !== $new2) {
$this->result->error('两次新密码不一致或为空');
}
$info = $this->auth->model;
if (! $info->checkPassword($old)) {
$this->result->error('原密码错误');
}
$info->resetPassword($new);
$this->result->success('密码修改成功');
}
/**
* 获取当前管理员资料
*/
public function read($id)
{
$id = $id ?: ($this->auth->info['id'] ?? 0);
$info = AdminModel::with(['roles'])->find($id);
if (! $info) {
$this->result->error('管理员不存在', 404);
}
$this->result->success(['info' => $info]);
}
/**
* 编辑表单数据
*/
public function edit($id = null)
{
$id = $id ?: ($this->auth->info['id'] ?? 0);
$info = AdminModel::with(['roles'])->find($id);
if (! $info) {
$this->result->error('管理员不存在', 404);
}
$this->result->success(['info' => $info]);
}
/**
* 更新个人资料(昵称/邮箱/头像/手机/密码)
*/
public function update(Request $request, $id)
{
$id = $id ?: ($this->auth->info['id'] ?? 0);
$info = AdminModel::find($id);
if (! $info) {
$this->result->error('管理员不存在', 404);
}
$data = $request->only(['nickname', 'email', 'avatar', 'mobile'], 'put');
if ($request->param('password')) {
$info->password = $request->param('password'); // 模型自动哈希
}
$info->save($data);
$this->result->success($info, '资料更新成功');
}
/**
* 个人资料不支持新建
*/
public function save(Request $request)
{
$this->result->error('个人资料不支持该操作');
}
/**
* 个人资料不支持删除
*/
public function delete($id)
{
$this->result->error('个人资料不支持该操作');
}
}