Role.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. use app\common\util\Pinyin;
  6. class Role extends Base {
  7. // 设置数据表(不含前缀)
  8. protected $name = 'role';
  9. // 定义时间戳字段名
  10. protected $createTime = '';
  11. protected $updateTime = '';
  12. // 自动完成
  13. protected $auto = [];
  14. protected $insert = [];
  15. protected $update = [];
  16. public function getRoleStatusTextAttr($val,$data)
  17. {
  18. $arr = [0=>lang('disable'),1=>lang('enable')];
  19. return $arr[$data['role_status']];
  20. }
  21. public function countData($where)
  22. {
  23. $total = $this->where($where)->count();
  24. return $total;
  25. }
  26. public function listData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1,$totalshow=1)
  27. {
  28. $page = $page > 0 ? (int)$page : 1;
  29. $limit = $limit ? (int)$limit : 20;
  30. $start = $start ? (int)$start : 0;
  31. if(!is_array($where)){
  32. $where = json_decode($where,true);
  33. }
  34. $where2='';
  35. if(!empty($where['_string'])){
  36. $where2 = $where['_string'];
  37. unset($where['_string']);
  38. }
  39. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  40. if($totalshow==1) {
  41. $total = $this->where($where)->count();
  42. }
  43. $list = Db::name('Role')->field($field)->where($where)->order($order)->limit($limit_str)->select();
  44. $vod_list=[];
  45. if($addition==1){
  46. $vod_ids=[];
  47. foreach($list as $k=>$v){
  48. $vod_ids[$v['role_rid']] = $v['role_rid'];
  49. }
  50. $where2=[];
  51. $where2['vod_id'] = ['in', implode(',',$vod_ids)];
  52. $tmp_list = model('Vod')->listData($where2,'vod_id desc',1,999,0);
  53. //$tmp_list = Db::name('Vod')->field('vod_id,vod_name,vod_en,type_id,type_id_1')->where($where2)->select();
  54. foreach($tmp_list['list'] as $k=>$v){
  55. $vod_list[$v['vod_id']] = $v;
  56. }
  57. }
  58. foreach($list as $k=>$v){
  59. if($addition==1){
  60. $list[$k]['data'] = $vod_list[$v['role_rid']];
  61. }
  62. }
  63. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  64. }
  65. public function listCacheData($lp)
  66. {
  67. if (!is_array($lp)) {
  68. $lp = json_decode($lp, true);
  69. }
  70. $order = $lp['order'];
  71. $by = $lp['by'];
  72. $ids = $lp['ids'];
  73. $paging = $lp['paging'];
  74. $pageurl = $lp['pageurl'];
  75. $level = $lp['level'];
  76. $wd = $lp['wd'];
  77. $actor = $lp['actor'];
  78. $name = $lp['name'];
  79. $rid = $lp['rid'];
  80. $letter = $lp['letter'];
  81. $start = intval(abs($lp['start']));
  82. $num = intval(abs($lp['num']));
  83. $half = intval(abs($lp['half']));
  84. $timeadd = $lp['timeadd'];
  85. $timehits = $lp['timehits'];
  86. $time = $lp['time'];
  87. $hitsmonth = $lp['hitsmonth'];
  88. $hitsweek = $lp['hitsweek'];
  89. $hitsday = $lp['hitsday'];
  90. $hits = $lp['hits'];
  91. $not = $lp['not'];
  92. $cachetime = $lp['cachetime'];
  93. $page = 1;
  94. $where = [];
  95. $totalshow=0;
  96. if(empty($num)){
  97. $num = 20;
  98. }
  99. if($start>1){
  100. $start--;
  101. }
  102. if(!in_array($paging, ['yes', 'no'])) {
  103. $paging = 'no';
  104. }
  105. $param = mac_param_url();
  106. if($paging=='yes') {
  107. $param = mac_search_len_check($param);
  108. $totalshow = 1;
  109. if(!empty($param['rid'])) {
  110. $rid = intval($param['rid']);
  111. }
  112. if(!empty($param['ids'])){
  113. $ids = $param['ids'];
  114. }
  115. if(!empty($param['level'])) {
  116. $level = $param['level'];
  117. }
  118. if(!empty($param['letter'])) {
  119. $letter = $param['letter'];
  120. }
  121. if(!empty($param['wd'])) {
  122. $wd = $param['wd'];
  123. }
  124. if(!empty($param['by'])){
  125. $by = $param['by'];
  126. }
  127. if(!empty($param['order'])){
  128. $order = $param['order'];
  129. }
  130. if(!empty($param['page'])){
  131. $page = intval($param['page']);
  132. }
  133. foreach($param as $k=>$v){
  134. if(empty($v)){
  135. unset($param[$k]);
  136. }
  137. }
  138. if(empty($pageurl)){
  139. $pageurl = 'role/index';
  140. }
  141. $param['page'] = 'PAGELINK';
  142. $pageurl = mac_url($pageurl,$param);
  143. }
  144. $where['role_status'] = ['eq',1];
  145. if(!empty($level)) {
  146. if($level=='all'){
  147. $level = '1,2,3,4,5,6,7,8,9';
  148. }
  149. $where['role_level'] = ['in',explode(',',$level)];
  150. }
  151. if(!empty($ids)) {
  152. if($ids!='all'){
  153. $where['role_id'] = ['in',explode(',',$ids)];
  154. }
  155. }
  156. if(!empty($not)){
  157. $where['role_id'] = ['not in',explode(',',$not)];
  158. }
  159. if(!empty($letter)){
  160. if(substr($letter,0,1)=='0' && substr($letter,2,1)=='9'){
  161. $letter='0,1,2,3,4,5,6,7,8,9';
  162. }
  163. $where['role_letter'] = ['in',explode(',',$letter)];
  164. }
  165. if(!empty($rid)) {
  166. $where['role_rid'] = ['eq',$rid];
  167. }
  168. if(!empty($timeadd)){
  169. $s = intval(strtotime($timeadd));
  170. $where['role_time_add'] =['gt',$s];
  171. }
  172. if(!empty($timehits)){
  173. $s = intval(strtotime($timehits));
  174. $where['role_time_hits'] =['gt',$s];
  175. }
  176. if(!empty($time)){
  177. $s = intval(strtotime($time));
  178. $where['role_time'] =['gt',$s];
  179. }
  180. if(!empty($hitsmonth)){
  181. $tmp = explode(' ',$hitsmonth);
  182. if(count($tmp)==1){
  183. $where['role_hits_month'] = ['gt', $tmp];
  184. }
  185. else{
  186. $where['role_hits_month'] = [$tmp[0],$tmp[1]];
  187. }
  188. }
  189. if(!empty($hitsweek)){
  190. $tmp = explode(' ',$hitsweek);
  191. if(count($tmp)==1){
  192. $where['role_hits_week'] = ['gt', $tmp];
  193. }
  194. else{
  195. $where['role_hits_week'] = [$tmp[0],$tmp[1]];
  196. }
  197. }
  198. if(!empty($hitsday)){
  199. $tmp = explode(' ',$hitsday);
  200. if(count($tmp)==1){
  201. $where['role_hits_day'] = ['gt', $tmp];
  202. }
  203. else{
  204. $where['role_hits_day'] = [$tmp[0],$tmp[1]];
  205. }
  206. }
  207. if(!empty($hits)){
  208. $tmp = explode(' ',$hits);
  209. if(count($tmp)==1){
  210. $where['role_hits'] = ['gt', $tmp];
  211. }
  212. else{
  213. $where['role_hits'] = [$tmp[0],$tmp[1]];
  214. }
  215. }
  216. if(!empty($actor)){
  217. $where['role_actor'] = ['in',explode(',',$actor) ];
  218. }
  219. if(!empty($name)){
  220. $where['role_name'] = ['in',explode(',',$name) ];
  221. }
  222. if(!empty($wd)) {
  223. $where['role_name|role_en'] = ['like', '%' . $wd . '%'];
  224. }
  225. if($by=='rnd'){
  226. $data_count = $this->countData($where);
  227. $page_total = floor($data_count / $lp['num']) + 1;
  228. if($data_count < $lp['num']){
  229. $lp['num'] = $data_count;
  230. }
  231. $randi = @mt_rand(1, $page_total);
  232. $page = $randi;
  233. $by = 'hits_week';
  234. $order = 'desc';
  235. }
  236. // https://github.com/magicblack/maccms10/issues/1050
  237. if(!in_array($by, ['id', 'time','time_add','score','hits','hits_day','hits_week','hits_month','up','down','level','rnd','sort'])) {
  238. $by = 'time';
  239. }
  240. if(!in_array($order, ['asc', 'desc'])) {
  241. $order = 'desc';
  242. }
  243. $order= 'role_'.$by .' ' . $order;
  244. $where_cache = $where;
  245. if(!empty($randi)){
  246. unset($where_cache['role_id']);
  247. $where_cache['order'] = 'rnd';
  248. }
  249. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' . md5('role_listcache_'.http_build_query($where_cache).'_'.$order.'_'.$page.'_'.$num.'_'.$start.'_'.$pageurl);
  250. $res = Cache::get($cach_name);
  251. if(empty($cachetime)){
  252. $cachetime = $GLOBALS['config']['app']['cache_time'];
  253. }
  254. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  255. $res = $this->listData($where,$order,$page,$num,$start,'*',1,$totalshow);
  256. if($GLOBALS['config']['app']['cache_core']==1) {
  257. Cache::set($cach_name, $res, $cachetime);
  258. }
  259. }
  260. $res['pageurl'] = $pageurl;
  261. $res['half'] = $half;
  262. return $res;
  263. }
  264. public function infoData($where,$field='*',$cache=0)
  265. {
  266. if(empty($where) || !is_array($where)){
  267. return ['code'=>1001,'msg'=>lang('param_err')];
  268. }
  269. $data_cache = false;
  270. $key = $GLOBALS['config']['app']['cache_flag']. '_'. 'role_detail_'.$where['role_id'][1].'_'.$where['role_en'][1];
  271. if($where['role_id'][0]=='eq' || $where['role_en'][0]=='eq'){
  272. $data_cache = true;
  273. }
  274. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache) {
  275. $info = Cache::get($key);
  276. }
  277. if($GLOBALS['config']['app']['cache_core']==0 || $cache==0 || empty($info['role_id'])) {
  278. $info = $this->field($field)->where($where)->find();
  279. if (empty($info)) {
  280. return ['code' => 1002, 'msg' => lang('obtain_err')];
  281. }
  282. $info = $info->toArray();
  283. $info['data'] = [];
  284. if(!empty($info['role_rid'])){
  285. $where2=[];
  286. $where2['vod_id'] = ['eq', $info['role_rid']];
  287. $vod_info = model('Vod')->infoData($where2);
  288. if($vod_info['code'] == 1){
  289. $info['data'] = $vod_info['info'];
  290. }
  291. }
  292. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache && $cache==1) {
  293. Cache::set($key, $info);
  294. }
  295. }
  296. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  297. }
  298. public function saveData($data)
  299. {
  300. $validate = \think\Loader::validate('Role');
  301. if(!$validate->check($data)){
  302. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  303. }
  304. $key = 'role_detail_'.$data['role_id'];
  305. Cache::rm($key);
  306. $key = 'role_detail_'.$data['role_en'];
  307. Cache::rm($key);
  308. $key = 'role_detail_'.$data['role_id'].'_'.$data['role_en'];
  309. Cache::rm($key);
  310. if(empty($data['role_en'])){
  311. $data['role_en'] = Pinyin::get($data['role_name']);
  312. }
  313. if(empty($data['role_letter'])){
  314. $data['role_letter'] = strtoupper(substr($data['role_en'],0,1));
  315. }
  316. if(!empty($data['role_content'])) {
  317. $pattern_src = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/';
  318. @preg_match_all($pattern_src, $data['role_content'], $match_src1);
  319. if (!empty($match_src1)) {
  320. foreach ($match_src1[1] as $v1) {
  321. $v2 = str_replace($GLOBALS['config']['upload']['protocol'] . ':', 'mac:', $v1);
  322. $data['role_content'] = str_replace($v1, $v2, $data['role_content']);
  323. }
  324. }
  325. unset($match_src1);
  326. }
  327. if($data['uptime']==1){
  328. $data['role_time'] = time();
  329. }
  330. unset($data['uptime']);
  331. if(!empty($data['role_id'])){
  332. $where=[];
  333. $where['role_id'] = ['eq',$data['role_id']];
  334. $res = $this->allowField(true)->where($where)->update($data);
  335. }
  336. else{
  337. $data['role_time_add'] = time();
  338. $data['role_time'] = time();
  339. $res = $this->allowField(true)->insert($data);
  340. }
  341. if(false === $res){
  342. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  343. }
  344. return ['code'=>1,'msg'=>lang('save_ok')];
  345. }
  346. public function delData($where)
  347. {
  348. $res = $this->where($where)->delete();
  349. if($res===false){
  350. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  351. }
  352. $list = $this->where($where)->select();
  353. $path = './';
  354. foreach($list as $k=>$v){
  355. $pic = $path.$v['role_pic'];
  356. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  357. unlink($pic);
  358. }
  359. }
  360. return ['code'=>1,'msg'=>lang('del_ok')];
  361. }
  362. public function fieldData($where,$col,$val)
  363. {
  364. if(!isset($col) || !isset($val)){
  365. return ['code'=>1001,'msg'=>lang('param_err')];
  366. }
  367. $data = [];
  368. $data[$col] = $val;
  369. $res = $this->allowField(true)->where($where)->update($data);
  370. if($res===false){
  371. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  372. }
  373. $list = $this->field('role_id,role_name,role_en')->where($where)->select();
  374. foreach($list as $k=>$v){
  375. $key = 'role_detail_'.$v['role_id'];
  376. Cache::rm($key);
  377. $key = 'role_detail_'.$v['role_en'];
  378. Cache::rm($key);
  379. }
  380. return ['code'=>1,'msg'=>lang('set_ok')];
  381. }
  382. }