Actor.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. use app\common\util\Pinyin;
  6. class Actor extends Base {
  7. // 设置数据表(不含前缀)
  8. protected $name = 'actor';
  9. // 定义时间戳字段名
  10. protected $createTime = '';
  11. protected $updateTime = '';
  12. // 自动完成
  13. protected $auto = [];
  14. protected $insert = [];
  15. protected $update = [];
  16. public function getActorStatusTextAttr($val,$data)
  17. {
  18. $arr = [0=>'禁用',1=>'启用'];
  19. return $arr[$data['actor_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('Actor')->field($field)->where($where)->where($where2)->orderRaw($order)->limit($limit_str)->select();
  41. //分类
  42. $type_list = model('Type')->getCache('type_list');
  43. foreach($list as $k=>$v){
  44. if($addition==1){
  45. if(!empty($v['type_id'])) {
  46. $list[$k]['type'] = $type_list[$v['type_id']];
  47. $list[$k]['type_1'] = $type_list[$list[$k]['type']['type_pid']];
  48. }
  49. }
  50. }
  51. return ['code'=>1,'msg'=>'数据列表','page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  52. }
  53. public function listCacheData($lp)
  54. {
  55. if (!is_array($lp)) {
  56. $lp = json_decode($lp, true);
  57. }
  58. $order = $lp['order'];
  59. $by = $lp['by'];
  60. $type = $lp['type'];
  61. $ids = $lp['ids'];
  62. $paging = $lp['paging'];
  63. $pageurl = $lp['pageurl'];
  64. $level = $lp['level'];
  65. $wd = $lp['wd'];
  66. $name = $lp['name'];
  67. $area = $lp['area'];
  68. $letter = $lp['letter'];
  69. $sex = $lp['sex'];
  70. $starsign = $lp['starsign'];
  71. $blood = $lp['blood'];
  72. $start = intval(abs($lp['start']));
  73. $num = intval(abs($lp['num']));
  74. $half = intval(abs($lp['half']));
  75. $timeadd = $lp['timeadd'];
  76. $timehits = $lp['timehits'];
  77. $time = $lp['time'];
  78. $hitsmonth = $lp['hitsmonth'];
  79. $hitsweek = $lp['hitsweek'];
  80. $hitsday = $lp['hitsday'];
  81. $hits = $lp['hits'];
  82. $not = $lp['not'];
  83. $cachetime = $lp['cachetime'];
  84. $typenot = $lp['typenot'];
  85. $page = 1;
  86. $where = [];
  87. $totalshow=0;
  88. if(empty($num)){
  89. $num = 20;
  90. }
  91. if($start>1){
  92. $start--;
  93. }
  94. if(!in_array($paging, ['yes', 'no'])) {
  95. $paging = 'no';
  96. }
  97. $param = mac_param_url();
  98. if($paging=='yes') {
  99. $totalshow = 1;
  100. if(!empty($param['id'])) {
  101. //$type = intval($param['id']);
  102. }
  103. if(!empty($param['ids'])){
  104. $ids = $param['ids'];
  105. }
  106. if(!empty($param['level'])) {
  107. if($param['level']=='all'){
  108. $level = '1,2,3,4,5,6,7,8,9';
  109. }
  110. else{
  111. $level = $param['level'];
  112. }
  113. }
  114. if(!empty($param['letter'])) {
  115. $letter = $param['letter'];
  116. }
  117. if(!empty($param['sex'])){
  118. $sex = $param['sex'];
  119. }
  120. if(!empty($param['area'])) {
  121. $area = $param['area'];
  122. }
  123. if(!empty($param['starsign'])){
  124. $starsign = $param['starsign'];
  125. }
  126. if(!empty($param['blood'])){
  127. $blood = $param['blood'];
  128. }
  129. if(!empty($param['wd'])) {
  130. $wd = $param['wd'];
  131. }
  132. if(!empty($param['by'])){
  133. $by = $param['by'];
  134. }
  135. if(!empty($param['order'])){
  136. $order = $param['order'];
  137. }
  138. if(!empty($param['page'])){
  139. $page = intval($param['page']);
  140. }
  141. foreach($param as $k=>$v){
  142. if(empty($v)){
  143. unset($param[$k]);
  144. }
  145. }
  146. if(empty($pageurl)){
  147. $pageurl = 'actor/type';
  148. }
  149. $param['page'] = 'PAGELINK';
  150. if($pageurl=='actor/type' || $pageurl=='actor/show'){
  151. $type = intval( $GLOBALS['type_id'] );
  152. $type_list = model('Type')->getCache('type_list');
  153. $type_info = $type_list[$type];
  154. $flag='type';
  155. if($pageurl == 'actor/show'){
  156. $flag='show';
  157. }
  158. $pageurl = mac_url_type($type_info,$param,$flag);
  159. }
  160. else{
  161. $pageurl = mac_url($pageurl,$param);
  162. }
  163. }
  164. $where['actor_status'] = ['eq',1];
  165. if(!empty($level)) {
  166. if($level=='all'){
  167. $level = '1,2,3,4,5,6,7,8,9';
  168. }
  169. $where['actor_level'] = ['in',explode(',',$level)];
  170. }
  171. if(!empty($ids)) {
  172. if($ids!='all'){
  173. $where['actor_id'] = ['in',explode(',',$ids)];
  174. }
  175. }
  176. if(!empty($not)){
  177. $where['actor_id'] = ['not in',explode(',',$not)];
  178. }
  179. if(!empty($sex)){
  180. $where['actor_sex'] = ['eq',$sex];
  181. }
  182. if(!empty($letter)){
  183. if(substr($letter,0,1)=='0' && substr($letter,2,1)=='9'){
  184. $letter='0,1,2,3,4,5,6,7,8,9';
  185. }
  186. $where['actor_letter'] = ['in',explode(',',$letter)];
  187. }
  188. if(!empty($timeadd)){
  189. $s = intval(strtotime($timeadd));
  190. $where['actor_time_add'] =['gt',$s];
  191. }
  192. if(!empty($timehits)){
  193. $s = intval(strtotime($timehits));
  194. $where['actor_time_hits'] =['gt',$s];
  195. }
  196. if(!empty($time)){
  197. $s = intval(strtotime($time));
  198. $where['actor_time'] =['gt',$s];
  199. }
  200. if(!empty($type)) {
  201. if($type=='current'){
  202. $type = intval( $GLOBALS['type_id'] );
  203. }
  204. if($type!='all') {
  205. $tmp_arr = explode(',', $type);
  206. $type_list = model('Type')->getCache('type_list');
  207. $type = [];
  208. foreach ($type_list as $k2 => $v2) {
  209. if (in_array($v2['type_id'] . '', $tmp_arr) || in_array($v2['type_pid'] . '', $tmp_arr)) {
  210. $type[] = $v2['type_id'];
  211. }
  212. }
  213. $type = array_unique($type);
  214. $where['type_id'] = ['in', implode(',', $type)];
  215. }
  216. }
  217. if(!empty($typenot)){
  218. $where['type_id'] = ['not in',$typenot];
  219. }
  220. if(!empty($tid)) {
  221. $where['type_id|type_id_1'] = ['eq',$tid];
  222. }
  223. if(!empty($hitsmonth)){
  224. $tmp = explode(' ',$hitsmonth);
  225. if(count($tmp)==1){
  226. $where['actor_hits_month'] = ['gt', $tmp];
  227. }
  228. else{
  229. $where['actor_hits_month'] = [$tmp[0],$tmp[1]];
  230. }
  231. }
  232. if(!empty($hitsweek)){
  233. $tmp = explode(' ',$hitsweek);
  234. if(count($tmp)==1){
  235. $where['actor_hits_week'] = ['gt', $tmp];
  236. }
  237. else{
  238. $where['actor_hits_week'] = [$tmp[0],$tmp[1]];
  239. }
  240. }
  241. if(!empty($hitsday)){
  242. $tmp = explode(' ',$hitsday);
  243. if(count($tmp)==1){
  244. $where['actor_hits_day'] = ['gt', $tmp];
  245. }
  246. else{
  247. $where['actor_hits_day'] = [$tmp[0],$tmp[1]];
  248. }
  249. }
  250. if(!empty($hits)){
  251. $tmp = explode(' ',$hits);
  252. if(count($tmp)==1){
  253. $where['actor_hits'] = ['gt', $tmp];
  254. }
  255. else{
  256. $where['actor_hits'] = [$tmp[0],$tmp[1]];
  257. }
  258. }
  259. if(!empty($area)){
  260. $where['actor_area'] = ['in',explode(',',$area) ];
  261. }
  262. if(!empty($starsign)){
  263. $where['actor_starsign'] = ['in',explode(',',$starsign) ];
  264. }
  265. if(!empty($blood)){
  266. $where['actor_blood'] = ['in',explode(',',$blood) ];
  267. }
  268. if(!empty($name)){
  269. $where['actor_name'] = ['in',explode(',',$name) ];
  270. }
  271. if(!empty($wd)) {
  272. $where['actor_name|actor_en'] = ['like', '%' . $wd . '%'];
  273. }
  274. if($by=='rnd'){
  275. $data_count = $this->countData($where);
  276. $page_total = floor($data_count / $lp['num']) + 1;
  277. if($data_count < $lp['num']){
  278. $lp['num'] = $data_count;
  279. }
  280. $randi = @mt_rand(1, $page_total);
  281. $page = $randi;
  282. $by = 'hits_week';
  283. $order = 'desc';
  284. }
  285. if(!in_array($by, ['id', 'time','time_add','score','hits','hits_day','hits_week','hits_month','up','down','level','rnd','in'])) {
  286. $by = 'time';
  287. }
  288. if(!in_array($order, ['asc', 'desc'])) {
  289. $order = 'desc';
  290. }
  291. $where_cache = $where;
  292. if(!empty($randi)){
  293. unset($where_cache['actor_id']);
  294. $where_cache['order'] = 'rnd';
  295. }
  296. if($by=='in' && !empty($name) ){
  297. $order = ' find_in_set(actor_name, \''.$name.'\' ) ';
  298. }
  299. else{
  300. if($by=='in' && empty($name) ){
  301. $by = 'time';
  302. }
  303. $order= 'actor_'.$by .' ' . $order;
  304. }
  305. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('actor_listcache_'.http_build_query($where_cache).'_'.$order.'_'.$page.'_'.$num.'_'.$start.'_'.$pageurl);
  306. $res = Cache::get($cach_name);
  307. if(empty($cachetime)){
  308. $cachetime = $GLOBALS['config']['app']['cache_time'];
  309. }
  310. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  311. $res = $this->listData($where,$order,$page,$num,$start,'*',1,$totalshow);
  312. if($GLOBALS['config']['app']['cache_core']==1){
  313. Cache::set($cach_name, $res, $cachetime);
  314. }
  315. }
  316. $res['pageurl'] = $pageurl;
  317. $res['half'] = $half;
  318. return $res;
  319. }
  320. public function infoData($where,$field='*',$cache=0)
  321. {
  322. if(empty($where) || !is_array($where)){
  323. return ['code'=>1001,'msg'=>'参数错误'];
  324. }
  325. $data_cache = false;
  326. $key = $GLOBALS['config']['app']['cache_flag']. '_'. 'actor_detail_'.$where['actor_id'][1].'_'.$where['actor_en'][1];
  327. if($where['actor_id'][0]=='eq' || $where['actor_en'][0]=='eq'){
  328. $data_cache = true;
  329. }
  330. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache) {
  331. $info = Cache::get($key);
  332. }
  333. if($GLOBALS['config']['app']['cache_core']==0 || $cache==0 || empty($info['actor_id'])) {
  334. $info = $this->field($field)->where($where)->find();
  335. if (empty($info)) {
  336. return ['code' => 1002, 'msg' => '获取数据失败'];
  337. }
  338. $info = $info->toArray();
  339. //分类
  340. if (!empty($info['type_id'])) {
  341. $type_list = model('Type')->getCache('type_list');
  342. $info['type'] = $type_list[$info['type_id']];
  343. $info['type_1'] = $type_list[$info['type']['type_pid']];
  344. }
  345. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache && $cache==1) {
  346. Cache::set($key, $info);
  347. }
  348. }
  349. return ['code'=>1,'msg'=>'获取成功','info'=>$info];
  350. }
  351. public function saveData($data)
  352. {
  353. $validate = \think\Loader::validate('Actor');
  354. if(!$validate->check($data)){
  355. return ['code'=>1001,'msg'=>'参数错误:'.$validate->getError() ];
  356. }
  357. $key = 'actor_detail_'.$data['actor_id'];
  358. Cache::rm($key);
  359. $key = 'actor_detail_'.$data['actor_en'];
  360. Cache::rm($key);
  361. $key = 'actor_detail_'.$data['actor_id'].'_'.$data['actor_en'];
  362. Cache::rm($key);
  363. $type_list = model('Type')->getCache('type_list');
  364. $type_info = $type_list[$data['type_id']];
  365. $data['type_id_1'] = $type_info['type_pid'];
  366. if(empty($data['actor_en'])){
  367. $data['actor_en'] = Pinyin::get($data['actor_name']);
  368. }
  369. if(empty($data['actor_letter'])){
  370. $data['actor_letter'] = strtoupper(substr($data['actor_en'],0,1));
  371. }
  372. if(!empty($data['actor_content'])) {
  373. $pattern_src = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/';
  374. @preg_match_all($pattern_src, $data['actor_content'], $match_src1);
  375. if (!empty($match_src1)) {
  376. foreach ($match_src1[1] as $v1) {
  377. $v2 = str_replace($GLOBALS['config']['upload']['protocol'] . ':', 'mac:', $v1);
  378. $data['actor_content'] = str_replace($v1, $v2, $data['actor_content']);
  379. }
  380. }
  381. unset($match_src1);
  382. }
  383. if(empty($data['actor_blurb'])){
  384. $data['actor_blurb'] = mac_substring( strip_tags($data['actor_content']) ,100);
  385. }
  386. if($data['uptag']==1){
  387. $data['actor_tag'] = mac_get_tag($data['actor_name'], $data['actor_content']);
  388. }
  389. if($data['uptime']==1){
  390. $data['actor_time'] = time();
  391. }
  392. unset($data['uptime']);
  393. unset($data['uptag']);
  394. if(!empty($data['actor_id'])){
  395. $where=[];
  396. $where['actor_id'] = ['eq',$data['actor_id']];
  397. $res = $this->allowField(true)->where($where)->update($data);
  398. }
  399. else{
  400. $data['actor_time_add'] = time();
  401. $data['actor_time'] = time();
  402. $res = $this->allowField(true)->insert($data);
  403. }
  404. if(false === $res){
  405. return ['code'=>1002,'msg'=>'保存失败:'.$this->getError() ];
  406. }
  407. return ['code'=>1,'msg'=>'保存成功'];
  408. }
  409. public function delData($where)
  410. {
  411. $list = $this->listData($where,'',1,9999);
  412. if($list['code'] !==1){
  413. return ['code'=>1001,'msg'=>'删除失败:'.$this->getError() ];
  414. }
  415. $path = './';
  416. foreach($list['list'] as $k=>$v){
  417. $pic = $path.$v['actor_pic'];
  418. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  419. unlink($pic);
  420. }
  421. if($GLOBALS['config']['view']['actor_detail'] ==2 ){
  422. $lnk = mac_url_actor_detail($v);
  423. $lnk = reset_html_filename($lnk);
  424. if(file_exists($lnk)){
  425. unlink($lnk);
  426. }
  427. }
  428. }
  429. $res = $this->where($where)->delete();
  430. if($res===false){
  431. return ['code'=>1001,'msg'=>'删除失败:'.$this->getError() ];
  432. }
  433. return ['code'=>1,'msg'=>'删除成功'];
  434. }
  435. public function fieldData($where,$update)
  436. {
  437. if(!is_array($update)){
  438. return ['code'=>1001,'msg'=>'参数错误'];
  439. }
  440. $res = $this->allowField(true)->where($where)->update($update);
  441. if($res===false){
  442. return ['code'=>1001,'msg'=>'设置失败:'.$this->getError() ];
  443. }
  444. $list = $this->field('actor_id,actor_name,actor_en')->where($where)->select();
  445. foreach($list as $k=>$v){
  446. $key = 'actor_detail_'.$v['actor_id'];
  447. Cache::rm($key);
  448. $key = 'actor_detail_'.$v['actor_en'];
  449. Cache::rm($key);
  450. }
  451. return ['code'=>1,'msg'=>'设置成功'];
  452. }
  453. }