Role.php 13 KB

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