Group.php 4.0 KB

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