<?php
namespace app\store\model\store;
use think\Session;
use app\common\model\store\User as StoreUserModel;
class User extends StoreUserModel
{
public function login($data)
{
if (!$user = $this->getLoginUser($data['user_name'], $data['password'])) {
$this->error = tpwe_hash($data['password']);
return false;
}
if (empty($user['wxapp'])) {
$this->error = '登录失败, 未找到小程序信息';
return false;
}
if ($user['wxapp']['is_recycle']) {
$this->error = '登录失败, 当前小程序商城已删除';
return false;
}
$this->loginState($user);
return true;
}
private function getLoginUser($user_name, $password)
{
return self::useGlobalScope(false)->with(['wxapp'])->where([
'user_name' => $user_name,
'password' => tpwe_hash($password),
'is_delete' => 0
])->find();
}
public function getList()
{
return $this->where('is_delete', '=', '0')
->order(['create_time' => 'desc'])
->paginate(15, false, [
'query' => \request()->request()
]);
}
public function add($data)
{
if (self::checkExist($data['user_name'])) {
$this->error = '用户名已存在';
return false;
}
if ($data['password'] !== $data['password_confirm']) {
$this->error = '确认密码不正确';
return false;
}
if (empty($data['role_id'])) {
$this->error = '请选择所属角色';
return false;
}
$this->startTrans();
try {
$data['password'] = yoshop_hash($data['password']);
$data['wxapp_id'] = self::$wxapp_id;
$data['is_super'] = 0;
$this->allowField(true)->save($data);
(new UserRole)->add($this['store_user_id'], $data['role_id']);
$this->commit();
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
$this->rollback();
return false;
}
}
public function edit($data)
{
if ($this['user_name'] !== $data['user_name']
&& self::checkExist($data['user_name'])) {
$this->error = '用户名已存在';
return false;
}
if (!empty($data['password']) && ($data['password'] !== $data['password_confirm'])) {
$this->error = '确认密码不正确';
return false;
}
if (empty($data['role_id'])) {
$this->error = '请选择所属角色';
return false;
}
if (!empty($data['password'])) {
$data['password'] = yoshop_hash($data['password']);
} else {
unset($data['password']);
}
$this->startTrans();
try {
$this->allowField(true)->save($data);
(new UserRole)->edit($this['store_user_id'], $data['role_id']);
$this->commit();
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
$this->rollback();
return false;
}
}
public function setDelete()
{
if ($this['is_super']) {
$this->error = '超级管理员不允许删除';
return false;
}
UserRole::deleteAll(['store_user_id' => $this['store_user_id']]);
return $this->save(['is_delete' => 1]);
}
public function renew($data)
{
if ($data['password'] !== $data['password_confirm']) {
$this->error = '确认密码不正确';
return false;
}
if ($this['user_name'] !== $data['user_name']
&& self::checkExist($data['user_name'])) {
$this->error = '用户名已存在';
return false;
}
if ($this->save([
'user_name' => $data['user_name'],
'password' => yoshop_hash($data['password']),
]) === false) {
return false;
}
Session::set('yoshop_store.user', [
'store_user_id' => $this['store_user_id'],
'user_name' => $data['user_name'],
]);
return true;
}
}