php管理和接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

215 lines
6.8 KiB

<?php
namespace app\admin\controller\user;
use app\admin\model\User;
use app\common\controller\AdminController;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;
class Admin extends AdminController
{
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new User();
}
/**
* @NodeAnotation(title="列表")
*/
public function index()
{
if ($this->request->isAjax()) {
if (input('selectFields')) {
return $this->selectList();
}
list($page, $limit, $where) = $this->buildTableParames();
$count = $this->model
->where($where)
->count();
$list = $this->model
->withoutField('password,salt,delete_time')
->field('uid as id')
->where($where)
->page($page, $limit)
->order('uid')
->select();
$data = [
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $list
];
return json($data);
}
return $this->fetch();
}
/**
* @NodeAnotation(title="添加")
*/
public function add()
{
if ($this->request->isPost()) {
$post = $this->request->post();
$rule = [
'avatar|用户头像' => 'require',
'nick_name|用户名' => 'require|unique:user',
'password|密码' => 'require',
'qr_password|确认密码' => 'require|confirm:password',
'mobile|手机' => 'require|mobile',
'gender|性别' => 'require|in:1,2,3',
'status|状态' => 'require|in:0,1'
];
$this->validate($post, $rule);
try {
$salt = makeSalt(6);
$post['salt'] = $salt;
$post['password'] = password($post['password'] . $salt);
$save = $this->model->save($post);
} catch (\Exception $e) {
$this->error('保存失败');
}
$save ? $this->success('保存成功') : $this->error('保存失败');
}
$this->assign('genderArr', $this->model->genderArr);
return $this->fetch();
}
/**
* @NodeAnotation(title="编辑")
*/
public function edit($id)
{
$row = $this->model->where('uid', $id)->find();
empty($row) && $this->error('数据不存在');
if ($this->request->isPost()) {
$post = $this->request->post();
$rule = [
'avatar|用户头像' => 'require',
'mobile|手机' => 'require|mobile',
'gender|性别' => 'require|in:1,2,3',
'status|状态' => 'require|in:0,1'
];
$this->validate($post, $rule);
try {
$save = $row->save($post);
} catch (\Exception $e) {
$this->error('保存失败');
}
$save ? $this->success('保存成功') : $this->error('保存失败');
}
$this->assign('row', $row);
$this->assign('genderArr', $this->model->genderArr);
return $this->fetch();
}
/**
* @NodeAnotation(title="编辑")
*/
public function password($id)
{
$row = $this->model->where('uid', $id)->find();
empty($row) && $this->error('数据不存在');
if ($this->request->isAjax()) {
$this->checkPostRequest();
$post = $this->request->post();
$rule = [
'password|密码' => 'require',
'qr_password|确认密码' => 'require|confirm:password',
];
$this->validate($post, $rule);
try {
$salt = makeSalt(6);
$post['salt'] = $salt;
$save = $row->save([
'salt' => $salt,
'password' => password($post['password'] . $salt)
]);
} catch (\Exception $e) {
$this->error('保存失败');
}
$save ? $this->success('保存成功') : $this->error('保存失败');
}
$this->assign('row', $row);
return $this->fetch();
}
/**
* @NodeAnotation(title="删除")
*/
public function delete($id)
{
$this->checkPostRequest();
$row = $this->model->whereIn('uid', $id)->select();
$row->isEmpty() && $this->error('数据不存在');
try {
$save = false;
foreach ($row as $value) {
$save = $this->model->where('uid', $value['uid'])->useSoftDelete('delete_time',time())->delete();
}
} catch (\Exception $e) {
$this->error('删除失败');
}
$save ? $this->success('删除成功') : $this->error('删除失败');
}
/**
* @NodeAnotation(title="属性修改")
*/
public function modify()
{
$this->checkPostRequest();
$post = $this->request->post();
$rule = [
'id|ID' => 'require',
'field|字段' => 'require',
'value|值' => 'require',
];
$this->validate($post, $rule);
if (!in_array($post['field'], $this->allowModifyFields)) {
$this->error('该字段不允许修改:' . $post['field']);
}
$row = $this->model->where('uid',$post['id'])->find();
empty($row) && $this->error('数据不存在');
try {
$row->save([
$post['field'] => $post['value'],
]);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('保存成功');
}
/**
* @NodeAnotation(title="导出")
*/
public function export()
{
list($page, $limit, $where) = $this->buildTableParames();
$tableName = $this->model->getName();
$tableName = CommonTool::humpToLine(lcfirst($tableName));
$prefix = config('database.connections.mysql.prefix');
$dbList = Db::query("show full columns from {$prefix}{$tableName}");
$header = [];
foreach ($dbList as $vo) {
$comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
if (!in_array($vo['Field'], $this->noExportFields)) {
$header[] = [$comment, $vo['Field']];
}
}
$list = $this->model
->where($where)
->limit(100000)
->order('uid', 'desc')
->select()
->toArray();
$fileName = time();
return Excel::exportData($list, $header, $fileName, 'xlsx');
}
}