File size: 4,080 Bytes
a4a352b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
<?php
namespace app\common\model;
use think\Cache;
use think\Db;
class Group extends Base {
// 设置数据表(不含前缀)
protected $name = 'group';
// 定义时间戳字段名
protected $createTime = '';
protected $updateTime = '';
// 自动完成
protected $auto = [];
protected $insert = [];
protected $update = [];
public function getGroupStatusTextAttr($val,$data)
{
$arr = [0=>lang('disable'),1=>lang('enable')];
return $arr[$data['group_status']];
}
public function listData($where,$order)
{
$total = $this->where($where)->count();
$tmp = Db::name('Group')->where($where)->order($order)->select();
$list = [];
foreach($tmp as $k=>$v){
$v['group_popedom'] = json_decode($v['group_popedom'],true);
$list[$v['group_id']] = $v;
}
return ['code'=>1,'msg'=>lang('data_list'),'total'=>$total,'list'=>$list];
}
public function infoData($where,$field='*')
{
if(empty($where) || !is_array($where)){
return ['code'=>1001,'msg'=>lang('param_err')];
}
$info = $this->field($field)->where($where)->find();
if(empty($info)){
return ['code'=>1002,'msg'=>lang('obtain_err')];
}
$info = $info->toArray();
$info['group_popedom'] = json_decode($info['group_popedom'],true);
return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
}
public function saveData($data)
{
if(!empty($data['group_type'])){
$data['group_type'] = ','.join(',',$data['group_type']) .',';
}else{
$data['group_type'] = '';
}
if(!empty($data['group_popedom'])){
$data['group_popedom'] = json_encode($data['group_popedom']);
}
else{
$data['group_popedom'] ='';
}
$validate = \think\Loader::validate('Group');
if(!$validate->check($data)){
return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
}
if(!empty($data['group_id'])){
$where=[];
$where['group_id'] = ['eq',$data['group_id']];
$res = $this->allowField(true)->where($where)->update($data);
}
else{
$res = $this->allowField(true)->insert($data);
}
if(false === $res){
return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
}
$this->setCache();
return ['code'=>1,'msg'=>lang('save_ok')];
}
public function delData($where)
{
$cc = model('User')->countData($where);
if($cc>0){
return ['code'=>1002,'msg'=>lang('del_err').':'.lang('model/group/have_user') ];
}
$res = $this->where($where)->delete();
if($res===false){
return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
}
$this->setCache();
return ['code'=>1,'msg'=>lang('del_ok')];
}
public function fieldData($where,$col,$val)
{
if(!isset($col) || !isset($val)){
return ['code'=>1001,'msg'=>lang('param_err')];
}
$data = [];
$data[$col] = $val;
$res = $this->allowField(true)->where($where)->update($data);
if($res===false){
return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
}
$this->setCache();
return ['code'=>1,'msg'=>lang('set_ok')];
}
public function setCache()
{
$res = $this->listData([],'group_id asc');
$list = $res['list'];
$key = $GLOBALS['config']['app']['cache_flag']. '_'.'group_list';
Cache::set($key,$list);
}
public function getCache($flag='group_list')
{
$key = $GLOBALS['config']['app']['cache_flag']. '_'.$flag;
$cache = Cache::get($key);
if(empty($cache)){
$this->setCache();
$cache = Cache::get($key);
}
return $cache;
}
} |