Link.php 4.4 KB

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