...
|
...
|
@@ -4,6 +4,10 @@ namespace app\admin\controller\user; |
|
|
|
|
|
use app\common\controller\Backend;
|
|
|
use app\common\library\Auth;
|
|
|
use fast\Random;
|
|
|
use think\Db;
|
|
|
use think\exception\PDOException;
|
|
|
use think\exception\ValidateException;
|
|
|
|
|
|
/**
|
|
|
* 会员管理
|
...
|
...
|
@@ -61,10 +65,40 @@ class User extends Backend |
|
|
*/
|
|
|
public function add()
|
|
|
{
|
|
|
if ($this->request->isPost()) {
|
|
|
$this->token();
|
|
|
if (false === $this->request->isPost()) {
|
|
|
return $this->view->fetch();
|
|
|
}
|
|
|
return parent::add();
|
|
|
$params = $this->request->post('row/a');
|
|
|
if (empty($params)) {
|
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
|
}
|
|
|
$params = $this->preExcludeFields($params);
|
|
|
|
|
|
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
|
|
$params[$this->dataLimitField] = $this->auth->id;
|
|
|
}
|
|
|
$result = false;
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
//是否采用模型验证
|
|
|
if ($this->modelValidate) {
|
|
|
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
|
|
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
|
|
$this->model->validateFailException()->validate($validate);
|
|
|
}
|
|
|
$params['salt']=Random::alnum();
|
|
|
$params['nickname']=$params['username'];
|
|
|
$params['password'] = $this->getEncryptPassword($params['password'], $params['salt']);
|
|
|
$result = $this->model->allowField(true)->save($params);
|
|
|
Db::commit();
|
|
|
} catch (ValidateException|PDOException|Exception $e) {
|
|
|
Db::rollback();
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
if ($result === false) {
|
|
|
$this->error(__('No rows were inserted'));
|
|
|
}
|
|
|
$this->success();
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -72,34 +106,97 @@ class User extends Backend |
|
|
*/
|
|
|
public function edit($ids = null)
|
|
|
{
|
|
|
if ($this->request->isPost()) {
|
|
|
$this->token();
|
|
|
}
|
|
|
$row = $this->model->get($ids);
|
|
|
$this->modelValidate = true;
|
|
|
if (!$row) {
|
|
|
$this->error(__('No Results were found'));
|
|
|
}
|
|
|
$this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
|
|
|
return parent::edit($ids);
|
|
|
$adminIds = $this->getDataLimitAdminIds();
|
|
|
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
|
|
$this->error(__('You have no permission'));
|
|
|
}
|
|
|
if (false === $this->request->isPost()) {
|
|
|
$this->view->assign('row', $row);
|
|
|
return $this->view->fetch();
|
|
|
}
|
|
|
$params = $this->request->post('row/a');
|
|
|
if (empty($params)) {
|
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
|
}
|
|
|
$params['salt']=Random::alnum();
|
|
|
$params['nickname']=$params['username'];
|
|
|
$params['password'] = $this->getEncryptPassword($params['password'], $params['salt']);
|
|
|
$params = $this->preExcludeFields($params);
|
|
|
$result = false;
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
//是否采用模型验证
|
|
|
if ($this->modelValidate) {
|
|
|
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
|
|
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
|
|
$row->validateFailException()->validate($validate);
|
|
|
}
|
|
|
if(isset($params['password']) && $params['password'] !== "") {
|
|
|
$params['password']=$this->getEncryptPassword($params['password'], $row->salt);
|
|
|
}
|
|
|
$result = $row->allowField(true)->save($params);
|
|
|
Db::commit();
|
|
|
} catch (ValidateException|PDOException|Exception $e) {
|
|
|
Db::rollback();
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
if (false === $result) {
|
|
|
$this->error(__('No rows were updated'));
|
|
|
}
|
|
|
$this->success();
|
|
|
}
|
|
|
/**
|
|
|
* 设置错误信息
|
|
|
*
|
|
|
* @param string $error 错误信息
|
|
|
* @return Auth
|
|
|
*/
|
|
|
public function setError($error)
|
|
|
{
|
|
|
$this->_error = $error;
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除
|
|
|
*/
|
|
|
public function del($ids = "")
|
|
|
public function del($ids = null)
|
|
|
{
|
|
|
if (!$this->request->isPost()) {
|
|
|
if (false === $this->request->isPost()) {
|
|
|
$this->error(__("Invalid parameters"));
|
|
|
}
|
|
|
$ids = $ids ? $ids : $this->request->post("ids");
|
|
|
$row = $this->model->get($ids);
|
|
|
$this->modelValidate = true;
|
|
|
if (!$row) {
|
|
|
$this->error(__('No Results were found'));
|
|
|
$ids = $ids ?: $this->request->post("ids");
|
|
|
if (empty($ids)) {
|
|
|
$this->error(__('Parameter %s can not be empty', 'ids'));
|
|
|
}
|
|
|
Auth::instance()->delete($row['id']);
|
|
|
$this->success();
|
|
|
$pk = $this->model->getPk();
|
|
|
$adminIds = $this->getDataLimitAdminIds();
|
|
|
if (is_array($adminIds)) {
|
|
|
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
|
|
}
|
|
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
|
|
|
|
|
$count = 0;
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
foreach ($list as $item) {
|
|
|
$count += $item->delete();
|
|
|
}
|
|
|
Db::commit();
|
|
|
} catch (PDOException|Exception $e) {
|
|
|
Db::rollback();
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
if ($count) {
|
|
|
$this->success();
|
|
|
}
|
|
|
$this->error(__('No rows were deleted'));
|
|
|
}
|
|
|
public function getEncryptPassword($password, $salt = '')
|
|
|
{
|
|
|
return md5(md5($password) . $salt);
|
|
|
}
|
|
|
} |
...
|
...
|
|