feat(user): 后台用户管理增加重置密码功能,并将部署流程默认调整为纯代码部署(不同步数据库)

这个提交包含在:
2026-11-23 17:54:01 +08:00
父节点 c2f3fcc368
当前提交 45ed55b644
共有 13 个文件被更改,包括 272 次插入 和 184 次删除

查看文件

@@ -231,6 +231,81 @@ class User extends Backend
return $this->view->fetch();
}
/**
* 后台重置用户登录密码或支付密码
*/
public function resetpwd($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$uid = (int)$row['id'];
$security = Db::name('user_security')->where('user_id', $uid)->find();
if ($this->request->isPost()) {
$this->token();
$params = $this->request->post('row/a', []);
$type = trim((string)($params['type'] ?? ''));
$newPassword = (string)($params['new_password'] ?? '');
if (!in_array($type, ['login', 'payment', 'both'], true)) {
$this->error('请选择有效的重置类型');
}
if ($type === 'login' && $newPassword !== '' && !preg_match('/^\S{6,30}$/', $newPassword)) {
$this->error('新登录密码须为6至30位非空字符');
}
if ($type === 'payment' && $newPassword !== '' && !preg_match('/^\d{6}$/', $newPassword)) {
$this->error('新支付密码须为6位数字');
}
Db::startTrans();
try {
\app\common\library\AccountSecurity::ensure($uid);
if ($type === 'login' || $type === 'both') {
if ($type === 'login' && $newPassword !== '') {
$salt = \fast\Random::alnum();
$encrypted = Auth::instance()->getEncryptPassword($newPassword, $salt);
Db::name('user')->where('id', $uid)->update(['password' => $encrypted, 'salt' => $salt]);
} else {
Db::name('user')->where('id', $uid)->update(['password' => '', 'salt' => '']);
}
Db::name('user_security')->where('user_id', $uid)->update([
'failed_attempts' => 0,
'locked_until' => 0,
]);
}
if ($type === 'payment' || $type === 'both') {
$payHash = ($type === 'payment' && $newPassword !== '') ? password_hash($newPassword, PASSWORD_DEFAULT) : '';
Db::name('user_security')->where('user_id', $uid)->update([
'payment_password_hash' => $payHash,
'payment_failed_attempts' => 0,
'payment_locked_until' => 0,
]);
}
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
$this->error('重置失败:' . $e->getMessage());
}
if ($type === 'login' || $type === 'both') {
\app\common\library\Token::clear($uid);
}
$this->success('密码重置成功');
}
$this->view->assign([
'row' => $row,
'hasLoginPassword' => !empty($row['password']),
'hasPaymentPassword' => $security && (string)$security['payment_password_hash'] !== '',
]);
return $this->view->fetch();
}
/**
* 添加
*/

查看文件

@@ -24,6 +24,7 @@
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-detail="{:$auth->check('user/user/detail')}"
data-operate-recharge="{:$auth->check('user/user/recharge')}"
data-operate-resetpwd="{:$auth->check('user/user/resetpwd')}"
data-operate-edit="{:$auth->check('user/user/edit')}"
data-operate-del=""
width="100%">

查看文件

@@ -0,0 +1,50 @@
<form id="resetpwd-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
{:token()}
<div class="panel panel-default panel-form-section">
<div class="panel-heading"><h4 class="panel-title">重置用户密码</h4></div>
<div class="panel-body">
<div class="form-group">
<label class="control-label col-sm-3">目标用户:</label>
<div class="col-sm-8">
<p class="form-control-static">{$row.nickname|htmlentities}(ID:{$row.id},手机:{$row.mobile|default='未绑定'|htmlentities})</p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">当前状态:</label>
<div class="col-sm-8">
<p class="form-control-static">
登录密码:
{if $hasLoginPassword}<span class="label label-success">已设置</span>{else /}<span class="label label-default">未设置</span>{/if}
&nbsp;&nbsp;
支付密码:
{if $hasPaymentPassword}<span class="label label-success">已设置</span>{else /}<span class="label label-default">未设置</span>{/if}
</p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">重置项目:</label>
<div class="col-sm-8">
<div class="radio">
<label><input type="radio" name="row[type]" value="login" checked> 仅重置登录密码</label>
</div>
<div class="radio">
<label><input type="radio" name="row[type]" value="payment"> 仅重置支付密码</label>
</div>
<div class="radio">
<label><input type="radio" name="row[type]" value="both"> 同时清空登录密码与支付密码</label>
</div>
</div>
</div>
<div class="form-group" id="new-password-group">
<label class="control-label col-sm-3" for="c-new-password">新密码(选填):</label>
<div class="col-sm-8">
<input id="c-new-password" name="row[new_password]" class="form-control" type="password" autocomplete="new-password" placeholder="留空则清空密码,由用户在小程序端自行重新设置">
<p class="help-block" id="new-password-help">留空将清空原密码并解除锁定,用户可在“账号安全”中直接重设;若填写,登录密码须6-30位,支付密码须6位数字。</p>
</div>
</div>
</div>
<div class="panel-footer normal-footer text-center">
<button type="submit" class="btn btn-warning btn-embossed disabled">确认重置</button>
</div>
</div>
</form>

查看文件

@@ -9,16 +9,6 @@ use think\Db;
class Security extends Base
{
private function verifyBoundPhone($code, $mobile)
{
if (trim((string)$code) === '') $this->error('请先验证绑定的微信手机号');
$phone = WxLogin::getPhoneNumber((string)$code);
$verified = isset($phone['purePhoneNumber']) ? (string)$phone['purePhoneNumber'] : (string)($phone['phoneNumber'] ?? '');
if (isset($phone['error']) || $verified === '' || !hash_equals((string)$mobile, $verified)) {
$this->error('手机号验证失败,请使用绑定手机号重新验证');
}
}
public function status()
{
$user = Db::name('user')->field('password,mobile')->where('id', $this->uid())->find();
@@ -37,12 +27,11 @@ class Security extends Base
$new = (string)$this->request->post('new_password', '');
$old = (string)$this->request->post('old_password', '');
if (!preg_match('/^\S{6,30}$/', $new)) $this->error('登录密码须为6至30位非空字符');
$user = Db::name('user')->field('password,mobile')->where('id', $uid)->find();
$user = Db::name('user')->field('password')->where('id', $uid)->find();
if (!empty($user['password'])) {
try { AccountSecurity::verify($uid, 'login', $old); } catch (\Throwable $e) { $this->error($e->getMessage()); }
$ignoreOld = false;
} else {
$this->verifyBoundPhone($this->request->post('phone_code', ''), $user['mobile']);
$ignoreOld = true;
}
if (!$this->auth->changepwd($new, $old, $ignoreOld)) $this->error($this->auth->getError() ?: '设置失败');
@@ -55,8 +44,7 @@ class Security extends Base
$uid = $this->uid();
$new = (string)$this->request->post('new_password', '');
if (!preg_match('/^\d{6}$/', $new)) $this->error('支付密码须为6位数字');
try { AccountSecurity::verify($uid, 'login', (string)$this->request->post('login_password', '')); }
catch (\Throwable $e) { $this->error($e->getMessage()); }
AccountSecurity::ensure($uid);
$updated = Db::name('user_security')->where('user_id', $uid)->where('payment_password_hash', '')->update(['payment_password_hash' => password_hash($new, PASSWORD_DEFAULT)]);
if (!$updated) $this->error('支付密码已设置,请使用修改功能');
$this->success('支付密码设置成功');