Link.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. class Link extends Base {
  6. // 设置数据表(不含前缀)
  7. protected $name = 'link';
  8. // 定义时间戳字段名
  9. protected $createTime = '';
  10. protected $updateTime = '';
  11. // 自动完成
  12. protected $auto = [];
  13. protected $insert = [];
  14. protected $update = [];
  15. public function listData($where,$order,$page=1,$limit=20,$start=0)
  16. {
  17. $page = $page > 0 ? (int)$page : 1;
  18. $limit = $limit ? (int)$limit : 20;
  19. $start = $start ? (int)$start : 0;
  20. if(!is_array($where)){
  21. $where = json_decode($where,true);
  22. }
  23. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  24. $total = $this->where($where)->count();
  25. $list = Db::name('Link')->where($where)->order($order)->limit($limit_str)->select();
  26. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  27. }
  28. public function listCacheData($lp)
  29. {
  30. if (!is_array($lp)) {
  31. $lp = json_decode($lp, true);
  32. }
  33. $order = $lp['order'];
  34. $by = $lp['by'];
  35. $type = $lp['type'];
  36. $start = intval(abs($lp['start']));
  37. $num = intval(abs($lp['num']));
  38. $cachetime = $lp['cachetime'];
  39. $not = $lp['not'];
  40. $page = 1;
  41. $where = [];
  42. if(empty($num)){
  43. $num = 20;
  44. }
  45. if($start>1){
  46. $start--;
  47. }
  48. if (!in_array($order, ['asc', 'desc'])) {
  49. $order = 'asc';
  50. }
  51. if (!in_array($by, ['id', 'sort'])) {
  52. $by = 'id';
  53. }
  54. if (in_array($type, ['font', 'pic'])) {
  55. $type = ($type === 'font') ? 0 : 1;
  56. $where['link_type'] = $type;
  57. }
  58. if(!empty($not)){
  59. $where['link_id'] = ['not in',explode(',',$not)];
  60. }
  61. $by = 'link_'.$by;
  62. $order = $by . ' ' . $order;
  63. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' . md5('link_listcache_'.join('&',$where).'_'.$order.'_'.$page.'_'.$num.'_'.$start);
  64. $res = Cache::get($cach_name);
  65. if(empty($cachetime)){
  66. $cachetime = $GLOBALS['config']['app']['cache_time'];
  67. }
  68. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  69. $res = $this->listData($where, $order, $page, $num, $start);
  70. if($GLOBALS['config']['app']['cache_core']==1) {
  71. Cache::set($cach_name, $res, $cachetime);
  72. }
  73. }
  74. return $res;
  75. }
  76. public function infoData($where,$field='*')
  77. {
  78. if(empty($where) || !is_array($where)){
  79. return ['code'=>1001,'msg'=>lang('param_err')];
  80. }
  81. $info = $this->field($field)->where($where)->find();
  82. if(empty($info)){
  83. return ['code'=>1002,'msg'=>lang('obtain_err')];
  84. }
  85. $info = $info->toArray();
  86. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  87. }
  88. public function saveData($data)
  89. {
  90. $validate = \think\Loader::validate('Link');
  91. if(!$validate->check($data)){
  92. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  93. }
  94. $data['link_time'] = time();
  95. if(!empty($data['link_id'])){
  96. $where=[];
  97. $where['link_id'] = ['eq',$data['link_id']];
  98. $res = $this->allowField(true)->where($where)->update($data);
  99. }
  100. else{
  101. $data['link_add_time'] = time();
  102. $res = $this->allowField(true)->insert($data);
  103. }
  104. if(false === $res){
  105. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  106. }
  107. return ['code'=>1,'msg'=>lang('save_ok')];
  108. }
  109. public function delData($where)
  110. {
  111. $res = $this->where($where)->delete();
  112. if($res===false){
  113. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  114. }
  115. return ['code'=>1,'msg'=>lang('del_ok')];
  116. }
  117. public function fieldData($where,$col,$val)
  118. {
  119. if(!isset($col) || !isset($val)){
  120. return ['code'=>1001,'msg'=>lang('param_err')];
  121. }
  122. $data = [];
  123. $data[$col] = $val;
  124. $res = $this->allowField(true)->where($where)->update($data);
  125. if($res===false){
  126. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  127. }
  128. return ['code'=>1,'msg'=>lang('set_ok')];
  129. }
  130. }