优化代码

This commit is contained in:
2020-05-09 17:52:57 +08:00
parent ad7993478a
commit ed35d06208
6 changed files with 64 additions and 47 deletions

View File

@@ -45,15 +45,15 @@ class Base {
protected function success($msg, $url = '') { protected function success($msg, $url = '') {
$this->data['code'] = 1; $this->data['code'] = 1;
$this->data['msg'] = $msg; $this->data['msg'] = $msg;
$this->data['url'] = $url ? $url->__toString() : ''; $this->data['url'] = $url ? $url->__toString() : '';
return $this->data; return $this->data;
} }
protected function error($msg, $url = '') { protected function error($msg, $url = '') {
$this->data['code'] = 0; $this->data['code'] = 0;
$this->data['msg'] = $msg; $this->data['msg'] = $msg;
$this->data['url'] = $url ? $url->__toString() : ''; $this->data['url'] = $url ? $url->__toString() : '';
return $this->data; return $this->data;
} }
} }

View File

@@ -34,11 +34,11 @@ class ApiAuth {
} catch (TokenExpiredException $e) { } catch (TokenExpiredException $e) {
$this->data['msg'] = $e->getMessage(); $this->data['msg'] = $e->getMessage();
$this->data['code'] = 2001; $this->data['code'] = 2001;
return json($this->data)->code($this->data['code']); return json($this->data);
} catch (JWTException $e) { } catch (JWTException $e) {
$this->data['code'] = 2000; $this->data['code'] = 2000;
$this->data['msg'] = $e->getMessage(); $this->data['msg'] = $e->getMessage();
return json($this->data)->code($this->data['code']); return json($this->data);
} }
return $next($request); return $next($request);

View File

@@ -31,7 +31,7 @@ class Validate {
$scene = strtolower($controller[0]) . $request->action(); $scene = strtolower($controller[0]) . $request->action();
$validate = "app\\http\\validate\\" . ucfirst($controller[1]); $validate = "app\\http\\validate\\" . ucfirst($controller[1]);
//仅当验证器存在时 进行校验 //仅当验证器存在时 进行校验
if (class_exists($validate) && (strtolower($controller[0]) == 'admin' && $request->isPost())) { if (class_exists($validate) && $request->isPost()) {
$v = new $validate; $v = new $validate;
//仅当存在验证场景才校验 //仅当存在验证场景才校验
if ($v->hasScene($scene)) { if ($v->hasScene($scene)) {

View File

@@ -8,10 +8,10 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace app\model; namespace app\model;
use app\model\Model as Models;
use sent\tree\Tree;
use think\facade\Config; use think\facade\Config;
use think\facade\Db; use think\facade\Db;
use sent\tree\Tree;
use app\model\Model as Models;
/** /**
* 设置模型 * 设置模型
@@ -25,7 +25,7 @@ class Attribute extends \think\Model {
/** /**
* @title 新增后事件 * @title 新增后事件
*/ */
protected static function onAfterInsert($data) { protected static function onAfterInsert($data){
$data = $data->toArray(); $data = $data->toArray();
if ($data['model_id']) { if ($data['model_id']) {
$db = new \com\Datatable(); $db = new \com\Datatable();
@@ -38,7 +38,7 @@ class Attribute extends \think\Model {
/** /**
* @title 更新后事件 * @title 更新后事件
*/ */
protected static function onAfterUpdate($data) { protected static function onAfterUpdate($data){
$data = $data->toArray(); $data = $data->toArray();
if (isset($data['model_id']) && isset($data['name'])) { if (isset($data['model_id']) && isset($data['name'])) {
$tablename = Models::where('id', $data['model_id'])->value('name'); $tablename = Models::where('id', $data['model_id'])->value('name');
@@ -50,7 +50,7 @@ class Attribute extends \think\Model {
$data['after'] = self::where('name', '<>', $data['name'])->where('model_id', $data['model_id'])->order('sort asc, id asc')->value('name'); $data['after'] = self::where('name', '<>', $data['name'])->where('model_id', $data['model_id'])->order('sort asc, id asc')->value('name');
$result = $db->columField(strtolower($tablename), $data)->query(); $result = $db->columField(strtolower($tablename), $data)->query();
return $result; return $result;
} else { }else{
return false; return false;
} }
} }
@@ -58,7 +58,7 @@ class Attribute extends \think\Model {
/** /**
* @title 删除后事件 * @title 删除后事件
*/ */
protected static function onAfterDelete($data) { protected static function onAfterDelete($data){
$data = $data->toArray(); $data = $data->toArray();
if ($data['model_id']) { if ($data['model_id']) {
$tablename = Models::where('id', $data['model_id'])->value('name'); $tablename = Models::where('id', $data['model_id'])->value('name');
@@ -67,11 +67,11 @@ class Attribute extends \think\Model {
$db = new \com\Datatable(); $db = new \com\Datatable();
if (!$db->CheckField($tablename, $data['name'])) { if (!$db->CheckField($tablename, $data['name'])) {
$result = true; $result = true;
} else { }else{
$result = $db->delField($tablename, $data['name'])->query(); $result = $db->delField($tablename, $data['name'])->query();
} }
return $result; return $result;
} else { }else{
return false; return false;
} }
} }
@@ -85,7 +85,7 @@ class Attribute extends \think\Model {
return isset($type[$data['type']]) ? $type[$data['type']] : ''; return isset($type[$data['type']]) ? $type[$data['type']] : '';
} }
protected function getOptionAttr($value, $data) { protected function getOptionAttr($value, $data){
$list = []; $list = [];
if ($data == '') { if ($data == '') {
return $list; return $list;
@@ -96,13 +96,13 @@ class Attribute extends \think\Model {
if (strrpos($val, ":")) { if (strrpos($val, ":")) {
list($key, $label) = explode(":", $val); list($key, $label) = explode(":", $val);
$list[] = ['key' => $key, 'label' => $label]; $list[] = ['key' => $key, 'label' => $label];
} else { }else{
$list[] = ['key' => $k, 'label' => $val]; $list[] = ['key' => $k, 'label' => $val];
} }
} }
} elseif ($data['type'] == 'bool') { }elseif($data['type'] == 'bool'){
$list = [['key' => 0, 'label' => '禁用'], ['key' => 1, 'label' => '启用']]; $list = [['key'=>0,'label'=>'禁用'],['key'=>1,'label'=>'启用']];
} elseif ($data['type'] == 'bind') { }elseif($data['type'] == 'bind'){
$map = []; $map = [];
$db = new \com\Datatable(); $db = new \com\Datatable();
if (strrpos($data['extra'], ":")) { if (strrpos($data['extra'], ":")) {
@@ -110,34 +110,34 @@ class Attribute extends \think\Model {
if ($db->CheckField($extra[0], 'model_id')) { if ($db->CheckField($extra[0], 'model_id')) {
$map[] = ['model_id', '=', $data['model_id']]; $map[] = ['model_id', '=', $data['model_id']];
} }
$row = Db::name($extra[0])->where($map)->select()->toArray(); $row = Db::name($extra[0])->where($map)->select()->toArray();
if (empty($row)) { if(empty($row)){
return []; return [];
} }
if ($extra[1] == 'tree') { if ($extra[1] == 'tree') {
$row = (new Tree())->toFormatTree($row); $row = (new Tree())->toFormatTree($row);
foreach ($row as $val) { foreach ($row as $val) {
$list[] = ['key' => $val['id'], 'label' => $val['title_show']]; $list[] = ['key'=>$val['id'], 'label'=>$val['title_show']];
} }
} else { }else{
foreach ($row as $val) { foreach ($row as $val) {
$list[] = ['key' => $val['id'], 'label' => $val['title']]; $list[] = ['key'=>$val['id'], 'label'=>$val['title']];
} }
} }
} else { }else{
if ($db->CheckField($data['extra'], 'model_id')) { if ($db->CheckField($data['extra'], 'model_id')) {
$map[] = ['model_id', '=', $data['model_id']]; $map[] = ['model_id', '=', $data['model_id']];
} }
$row = Db::name($data['extra'])->where($map)->select()->toArray(); $row = Db::name($data['extra'])->select($map)->toArray();
foreach ($row as $val) { foreach ($row as $val) {
$list[] = ['key' => $val['id'], 'label' => $val['title']]; $list[] = ['key'=>$val['id'], 'label'=>$val['title']];
} }
} }
} }
return $list; return $list;
} }
public static function getField($model, $ac = "add") { public static function getField($model, $ac = "add"){
$list = []; $list = [];
$group = $model['attr_group']; $group = $model['attr_group'];
@@ -145,7 +145,7 @@ class Attribute extends \think\Model {
$map[] = ['model_id', '=', $model['id']]; $map[] = ['model_id', '=', $model['id']];
if ($ac == 'add') { if ($ac == 'add') {
$map[] = ['is_show', 'IN', [1, 2]]; $map[] = ['is_show', 'IN', [1, 2]];
} else if ($ac == 'edit') { }else if ($ac == 'edit') {
$map[] = ['is_show', 'IN', [1, 3]]; $map[] = ['is_show', 'IN', [1, 3]];
} }
@@ -156,7 +156,7 @@ class Attribute extends \think\Model {
foreach ($row as $key => $value) { foreach ($row as $key => $value) {
if (isset($group[$value['group_id']])) { if (isset($group[$value['group_id']])) {
$list[$group[$value['group_id']]['label']][] = $value; $list[$group[$value['group_id']]['label']][] = $value;
} else { }else{
$list[$value['group_id']][] = $value; $list[$value['group_id']][] = $value;
} }
} }
@@ -164,26 +164,26 @@ class Attribute extends \think\Model {
return $list; return $list;
} }
public static function getfieldList() { public static function getfieldList(){
$config = \think\facade\Cache::get('system_config_data'); $config = \think\facade\Cache::get('system_config_data');
$time = [['key' => 1, 'label' => '新增'], ['key' => 2, 'label' => '编辑'], ['key' => 3, 'label' => '始终']]; $time = [['key'=>1, 'label'=>'新增'],['key'=>2, 'label'=>'编辑'],['key'=>3, 'label'=>'始终']];
$auto_type = [['key' => 'function', 'label' => '函数'], ['key' => 'field', 'label' => '字段'], ['key' => 'string', 'label' => '字符串']]; $auto_type = [['key'=>'function', 'label'=>'函数'],['key'=>'field', 'label'=>'字段'],['key'=>'string', 'label'=>'字符串']];
$validate_type = [['key' => 'thinkphp', 'label' => 'thinkphp内置'], ['key' => 'regex', 'label' => '正则验证']]; $validate_type = [['key'=>'thinkphp', 'label'=>'thinkphp内置'],['key'=>'regex', 'label'=>'正则验证']];
return [ return [
'基础' => [ '基础' => [
['name' => 'id', 'title' => 'id', 'help' => '', 'type' => 'hidden'], ['name' => 'id', 'title' => 'id', 'help' => '', 'type' => 'hidden'],
['name' => 'model_id', 'title' => 'model_id', 'help' => '', 'type' => 'hidden'], ['name' => 'model_id', 'title' => 'model_id', 'help' => '', 'type' => 'hidden'],
['name' => 'name', 'title' => '字段名', 'help' => '英文字母开头长度不超过30', 'is_must' => true, 'type' => 'text'], ['name' => 'name', 'title' => '字段名', 'help' => '英文字母开头长度不超过30', 'is_must'=> true, 'type' => 'text'],
['name' => 'title', 'title' => '字段标题', 'help' => '请输入字段标题,用于表单显示', 'is_must' => true, 'type' => 'text'], ['name' => 'title', 'title' => '字段标题', 'help' => '请输入字段标题,用于表单显示', 'is_must'=> true, 'type' => 'text'],
['name' => 'type', 'title' => '字段类型', 'help' => '用于表单中的展示方式', 'type' => 'select', 'option' => $config['config_type_list'], 'help' => ''], ['name' => 'type', 'title' => '字段类型', 'help' => '用于表单中的展示方式', 'type' => 'select', 'option' => $config['config_type_list'], 'help' => ''],
['name' => 'length', 'title' => '字段长度', 'help' => '字段的长度值', 'type' => 'text'], ['name' => 'length', 'title' => '字段长度', 'help' => '字段的长度值', 'type' => 'text'],
['name' => 'extra', 'title' => '参数', 'help' => '布尔、枚举、多选字段类型的定义数据', 'type' => 'textarea'], ['name' => 'extra', 'title' => '参数', 'help' => '布尔、枚举、多选字段类型的定义数据', 'type' => 'textarea'],
['name' => 'value', 'title' => '默认值', 'help' => '字段的默认值', 'type' => 'text'], ['name' => 'value', 'title' => '默认值', 'help' => '字段的默认值', 'type' => 'text'],
['name' => 'remark', 'title' => '字段备注', 'help' => '用于表单中的提示', 'type' => 'text'], ['name' => 'remark', 'title' => '字段备注', 'help' => '用于表单中的提示', 'type' => 'text'],
['name' => 'is_show', 'title' => '是否显示', 'help' => '是否显示在表单中', 'type' => 'select', 'option' => [ ['name' => 'is_show', 'title' => '是否显示', 'help' => '是否显示在表单中', 'type' => 'select', 'option' => [
['key' => '1', 'label' => '始终显示'], ['key' => '2', 'label' => '新增显示'], ['key' => '3', 'label' => '编辑显示'], ['key' => '0', 'label' => '不显示'], ['key'=>'1', 'label' => '始终显示'], ['key'=>'2', 'label' => '新增显示'], ['key'=>'3', 'label' => '编辑显示'], ['key'=>'0', 'label' => '不显示']
], 'value' => 1], ], 'value' => 1],
['name' => 'is_must', 'title' => '是否必填', 'help' => '用于自动验证', 'type' => 'select', 'option' => [['key' => '0', 'label' => '否'], ['key' => '1', 'label' => '是']]], ['name' => 'is_must', 'title' => '是否必填', 'help' => '用于自动验证', 'type' => 'select', 'option' => [['key'=>'0', 'label' => '否'], ['key'=>'1', 'label' => '是']]],
], ],
'高级' => [ '高级' => [
// ['name' => 'validate_type', 'title' => '验证方式', 'type' => 'select', 'option' => $validate_type, 'help' => ''], // ['name' => 'validate_type', 'title' => '验证方式', 'type' => 'select', 'option' => $validate_type, 'help' => ''],

View File

@@ -74,7 +74,7 @@ class Member extends Model {
} }
protected function getAvatarAttr($value, $data) { protected function getAvatarAttr($value, $data) {
return avatar($data['uid']); return $value ? $value : request()->domain() . '/static/common/images/default_avatar.jpg';
} }
protected function setPasswordAttr($value, $data) { protected function setPasswordAttr($value, $data) {
@@ -90,6 +90,10 @@ class Member extends Model {
return JWTAuth::builder($token); //参数为用户认证的信息,请自行添加 return JWTAuth::builder($token); //参数为用户认证的信息,请自行添加
} }
protected function getNicknameAttr($value, $data){
return $value ? $value : $data['username'];
}
/** /**
* 用户登录 * 用户登录
*/ */
@@ -191,17 +195,26 @@ class Member extends Model {
}]; }];
} }
$list = self::with(['role'])->field('uid,username,nickname,status,email,mobile,department,reg_time')->where($map)->order($order)->paginate($request->pageConfig); $res = self::with(['role', 'group'])->field('uid,username,nickname,status,email,mobile,department,reg_time')->where($map)->order($order)->paginate($request->pageConfig);
return $list->append(['avatar', 'status_text']); $list = $res->append(['avatar', 'status_text'])->each(function($item){
if($item['group'] === null){
$item['group'] = ['title' => '未定义'];
$item['group_title'] = '未定义';
}else{
$item['group_title'] = $item['group']['title'];
}
return $item;
});
return $list;
} }
/** /**
* @title: 获取用户列表 * @title: 获取用户列表
*/ */
public function getUserDetail($request) { public function getUserDetail($request) {
$uid = $request->param('uid', $request->user['uid']); $uid = $request->param('uid');
if (!$uid) { if (!$uid) {
return false; $uid = $request->user['uid'];
} }
$info = $this->where('uid', $uid)->find(); $info = $this->where('uid', $uid)->find();
@@ -255,4 +268,8 @@ class Member extends Model {
public function role() { public function role() {
return $this->hasOne('RoleAccess', 'uid', 'uid'); return $this->hasOne('RoleAccess', 'uid', 'uid');
} }
public function group(){
return $this->hasOneThrough(AuthGroup::class, AuthGroupAccess::class, 'uid', 'id', 'uid', 'group_id');
}
} }

View File

@@ -114,8 +114,8 @@ class Datatable {
} else { } else {
$field_attr['length'] = ""; $field_attr['length'] = "";
} }
$field_attr['is_null'] = $attr['is_must'] ? 'NOT NULL' : 'NULL'; $field_attr['is_null'] = (isset($attr['is_must']) && $attr['is_must']) ? 'NOT NULL' : 'NULL';
$field_attr['default'] = $attr['value'] != '' ? 'DEFAULT "' . $attr['value'] . '"' : ''; $field_attr['default'] = (isset($attr['is_must']) && $attr['is_must'] !== '') ? 'DEFAULT "' . $attr['value'] . '"' : '';
$field_attr['comment'] = (isset($attr['remark']) && $attr['remark']) ? $attr['remark'] : $attr['title']; $field_attr['comment'] = (isset($attr['remark']) && $attr['remark']) ? $attr['remark'] : $attr['title'];
$field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `' . $attr['after'] . '`' : ' AFTER `id`'; $field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `' . $attr['after'] . '`' : ' AFTER `id`';