Type.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. use app\common\util\Pinyin;
  6. class Type extends Base {
  7. // 设置数据表(不含前缀)
  8. protected $name = 'type';
  9. // 定义时间戳字段名
  10. protected $createTime = '';
  11. protected $updateTime = '';
  12. // 自动完成
  13. protected $auto = [];
  14. protected $insert = [];
  15. protected $update = [];
  16. //自定义初始化
  17. protected function initialize()
  18. {
  19. //需要调用`Model`的`initialize`方法
  20. parent::initialize();
  21. //TODO:自定义的初始化
  22. }
  23. public function countData($where)
  24. {
  25. $total = $this->where($where)->count();
  26. return $total;
  27. }
  28. public function listData($where,$order,$format='def',$mid=0,$limit=999,$start=0,$totalshow=1)
  29. {
  30. if(!is_array($where)){
  31. $where = json_decode($where,true);
  32. }
  33. $limit_str = ($limit * (1-1) + $start) .",".$limit;
  34. if($totalshow==1) {
  35. $total = $this->where($where)->count();
  36. }
  37. else{
  38. }
  39. $tmp = Db::name('Type')->where($where)->order($order)->limit($limit_str)->select();
  40. $list = [];
  41. $childs=[];
  42. foreach($tmp as $k=>$v){
  43. $v['type_extend'] = json_decode($v['type_extend'],true);
  44. $list[$v['type_id']] = $v;
  45. $childs[$v['type_pid']][] = $v['type_id'];
  46. }
  47. $rc=false;
  48. foreach($list as $k=>$v){
  49. if($v['type_pid']==0){
  50. if(!empty($where)){
  51. if(!$rc){
  52. $type_list = model('Type')->getCache('type_list');
  53. $rc=true;
  54. }
  55. $list[$k]['childids'] = $type_list[$v['type_id']]['childids'];
  56. }
  57. else {
  58. $list[$k]['childids'] = join(',', (array)$childs[$v['type_id']]);
  59. }
  60. }
  61. else {
  62. $list[$k]['type_1'] = $list[$v['type_pid']];
  63. }
  64. }
  65. if($mid>0){
  66. foreach($list as $k=>$v){
  67. if($v['type_mid'] !=$mid) {
  68. unset($list[$k]);
  69. }
  70. }
  71. }
  72. if($format=='tree'){
  73. $list = mac_list_to_tree($list,'type_id','type_pid');
  74. }
  75. return ['code'=>1,'msg'=>lang('data_list'),'total'=>$total,'list'=>$list];
  76. }
  77. public function listCacheData($lp)
  78. {
  79. if (!is_array($lp)) {
  80. $lp = json_decode($lp, true);
  81. }
  82. $order = $lp['order'];
  83. $by = $lp['by'];
  84. $mid = $lp['mid'];
  85. $ids = $lp['ids'];
  86. $parent = $lp['parent'];
  87. $format = $lp['format'];
  88. $flag = $lp['flag'];
  89. $start = intval(abs($lp['start']));
  90. $num = intval(abs($lp['num']));
  91. $cachetime = $lp['cachetime'];
  92. $not = $lp['not'];
  93. $page=1;
  94. $where = [];
  95. if(empty($num)){
  96. $num = 20;
  97. }
  98. if($start>1){
  99. $start--;
  100. }
  101. if (!in_array($order, ['asc', 'desc'])) {
  102. $order = 'desc';
  103. }
  104. if (!in_array($by, ['id', 'sort'])) {
  105. $by = 'id';
  106. }
  107. if (!in_array($format, ['def', 'tree'])) {
  108. $format = 'def';
  109. }
  110. if (in_array($mid, ['1', '2','8','11'])) {
  111. $where['type_mid'] = ['eq',$mid];
  112. }
  113. if(!empty($flag)){
  114. if($flag=='vod'){
  115. $where['type_mid'] = ['eq',1];
  116. }
  117. elseif($flag=='art'){
  118. $where['type_mid'] = ['eq',2];
  119. }
  120. }
  121. $param = mac_param_url();
  122. if (!empty($ids)) {
  123. if($ids=='parent'){
  124. $where['type_pid'] = ['eq',0];
  125. }
  126. elseif($ids=='child'){
  127. $where['type_pid'] = ['gt',0];
  128. }
  129. elseif($ids=='current'){
  130. $type_info = $this->getCacheInfo($param['id']);
  131. $doid = $param['id'];
  132. $childs = $type_info['childids'];
  133. if($type_info['type_pid']>0){//二级分类->一级
  134. $doid = $type_info['type_pid'];
  135. $type_info1 = $this->getCacheInfo($doid);
  136. $childs = $type_info1['childids'];
  137. }
  138. $where['type_id'] = ['in',$childs];
  139. }
  140. else{
  141. $where['type_id'] = ['in',$ids];
  142. }
  143. }
  144. if(!empty($parent)){
  145. if($parent=='current'){
  146. $type_info = $this->getCacheInfo($param['id']);
  147. $parent = intval($type_info['type_id']);
  148. if($type_info['type_pid'] !=0){
  149. //$parent = $type_info['type_pid'];
  150. }
  151. }
  152. $where['type_pid'] = ['in',$parent];
  153. }
  154. if(!empty($not)){
  155. $where['type_id'] = ['not in',$not];
  156. }
  157. if(defined('ENTRANCE') && ENTRANCE == 'index' && $GLOBALS['config']['app']['popedom_filter'] ==1){
  158. $type_ids = mac_get_popedom_filter($GLOBALS['user']['group']['group_type']);
  159. if(!empty($type_ids)){
  160. if(!empty($where['type_id'])){
  161. $where['type_id'] = [ $where['type_id'],['not in', explode(',',$type_ids)] ];
  162. }
  163. else{
  164. $where['type_id'] = ['not in', explode(',',$type_ids)];
  165. }
  166. }
  167. }
  168. $where['type_status'] = ['eq',1];
  169. $by = 'type_'.$by;
  170. $order = 'type_pid asc,'. $by . ' ' . $order;
  171. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('type_listcache_'.http_build_query($where).'_'.$order.'_'.$num.'_'.$start);
  172. $res = Cache::get($cach_name);
  173. if(empty($cachetime)){
  174. $cachetime = $GLOBALS['config']['app']['cache_time'];
  175. }
  176. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  177. $res = $this->listData($where,$order,$format,$mid,$num,$start,0);
  178. $res['list'] = array_values($res['list']);
  179. if($GLOBALS['config']['app']['cache_core']==1) {
  180. Cache::set($cach_name, $res, $cachetime);
  181. }
  182. }
  183. return $res;
  184. }
  185. public function infoData($where,$field='*')
  186. {
  187. if(empty($where) || !is_array($where)){
  188. return ['code'=>1001,'msg'=>lang('param_err')];
  189. }
  190. $info = $this->field($field)->where($where)->find();
  191. if(empty($info)){
  192. return ['code'=>1002,'msg'=>lang('obtain_err')];
  193. }
  194. $info = $info->toArray();
  195. if(!empty($info['type_extend'])){
  196. $info['type_extend'] = json_decode($info['type_extend'],true);
  197. }
  198. else{
  199. $info['type_extend'] = json_decode('{"type":"","area":"","lang":"","year":"","star":"","director":"","state":"","version":""}',true);
  200. }
  201. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  202. }
  203. public function saveData($data)
  204. {
  205. $validate = \think\Loader::validate('Type');
  206. if(!$validate->check($data)){
  207. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  208. }
  209. if(!empty($data['type_extend'])){
  210. $data['type_extend'] = json_encode($data['type_extend']);
  211. }
  212. if(empty($data['type_en'])){
  213. $data['type_en'] = Pinyin::get($data['type_name']);
  214. }
  215. // xss过滤
  216. $filter_fields = [
  217. 'type_name',
  218. 'type_en',
  219. 'type_tpl',
  220. 'type_tpl_list',
  221. 'type_tpl_detail',
  222. 'type_tpl_play',
  223. 'type_tpl_down',
  224. 'type_key',
  225. 'type_des',
  226. 'type_title',
  227. 'type_union',
  228. 'type_logo',
  229. 'type_pic',
  230. 'type_jumpurl',
  231. ];
  232. foreach ($filter_fields as $filter_field) {
  233. if (!isset($data[$filter_field])) {
  234. continue;
  235. }
  236. $data[$filter_field] = mac_filter_xss($data[$filter_field]);
  237. }
  238. if(!empty($data['type_id'])){
  239. $where=[];
  240. $where['type_id'] = ['eq',$data['type_id']];
  241. $res = $this->allowField(true)->where($where)->update($data);
  242. }
  243. else{
  244. $res = $this->allowField(true)->insert($data);
  245. }
  246. if(false === $res){
  247. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  248. }
  249. $this->setCache();
  250. return ['code'=>1,'msg'=>lang('save_ok')];
  251. }
  252. public function delData($where)
  253. {
  254. $list = $this->where($where)->select();
  255. foreach($list as $k=>$v){
  256. $where2=[];
  257. $where2['type_id|type_id_1'] = ['eq',$v['type_id']];
  258. $flag = $v['type_mid'] == 1 ? 'Vod' : 'Art';
  259. $cc = model($flag)->where($where2)->count();
  260. if($cc > 0){
  261. return ['code'=>1021,'msg'=>lang('del_err').':'. $v['type_name'].'还有'.$cc.'条数据,请先删除或转移' ];
  262. }
  263. }
  264. $res = $this->where($where)->delete();
  265. if($res===false){
  266. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  267. }
  268. $this->setCache();
  269. return ['code'=>1,'msg'=>lang('del_ok')];
  270. }
  271. public function fieldData($where,$col,$val)
  272. {
  273. if(!isset($col) || !isset($val)){
  274. return ['code'=>1001,'msg'=>lang('param_err')];
  275. }
  276. $data = [];
  277. $data[$col] = $val;
  278. $res = $this->allowField(true)->where($where)->update($data);
  279. if($res===false){
  280. return ['code'=>1002,'msg'=>lang('set_err').':'.$this->getError() ];
  281. }
  282. $this->setCache();
  283. return ['code'=>1,'msg'=>lang('set_ok')];
  284. }
  285. public function moveData($where,$val)
  286. {
  287. $list = $this->where($where)->select();
  288. $type_info = $this->getCacheInfo($val);
  289. if(empty($type_info)){
  290. return ['code'=>1011,'msg'=>lang('model/type/to_info_err')];
  291. }
  292. foreach($list as $k=>$v){
  293. $where2=[];
  294. $where2['type_id|type_id_1'] = ['eq',$v['type_id']];
  295. $update=[];
  296. $update['type_id'] = $val;
  297. $update['type_id_1'] = $type_info['type_pid'];
  298. $flag = $v['type_mid'] == 1 ? 'Vod' : 'Art';
  299. $cc = model($flag)->where($where2)->update($update);
  300. if($cc ===false){
  301. return ['code'=>1012,'msg'=>lang('model/type/move_err').':'. $v['type_name'].''.$this->getError() ];
  302. }
  303. }
  304. return ['code'=>1,'msg'=>lang('model/type/move_ok')];
  305. }
  306. public function setCache()
  307. {
  308. $res = $this->listData([],'type_id asc');
  309. $list = $res['list'];
  310. $key = $GLOBALS['config']['app']['cache_flag']. '_'.'type_list';
  311. Cache::set($key,$list);
  312. $type_tree = mac_list_to_tree($list,'type_id','type_pid');
  313. $key = $GLOBALS['config']['app']['cache_flag']. '_'.'type_tree';
  314. Cache::set($key,$type_tree);
  315. }
  316. public function getCache($flag='type_list')
  317. {
  318. $key = $GLOBALS['config']['app']['cache_flag']. '_'.$flag;
  319. $cache = Cache::get($key);
  320. if(empty($cache)){
  321. $this->setCache();
  322. $cache = Cache::get($key);
  323. }
  324. return $cache;
  325. }
  326. public function getCacheInfo($id)
  327. {
  328. $type_list = $this->getCache('type_list');
  329. if(is_numeric($id)) {
  330. return $type_list[$id];
  331. }
  332. else{
  333. foreach($type_list as $k=>$v){
  334. if($v['type_en'] == $id){
  335. return $type_list[$k];
  336. }
  337. }
  338. }
  339. }
  340. }