Type.php 12 KB

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