| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Cache;
- class Link extends Base {
- // 设置数据表(不含前缀)
- protected $name = 'link';
- // 定义时间戳字段名
- protected $createTime = '';
- protected $updateTime = '';
- // 自动完成
- protected $auto = [];
- protected $insert = [];
- protected $update = [];
- public function listData($where,$order,$page=1,$limit=20,$start=0)
- {
- if(!is_array($where)){
- $where = json_decode($where,true);
- }
- $limit_str = ($limit * ($page-1) + $start) .",".$limit;
- $total = $this->where($where)->count();
- $list = Db::name('Link')->where($where)->order($order)->limit($limit_str)->select();
- return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
- }
- public function listCacheData($lp)
- {
- if (!is_array($lp)) {
- $lp = json_decode($lp, true);
- }
- $order = $lp['order'];
- $by = $lp['by'];
- $type = $lp['type'];
- $start = intval(abs($lp['start']));
- $num = intval(abs($lp['num']));
- $cachetime = $lp['cachetime'];
- $not = $lp['not'];
- $page = 1;
- $where = [];
- if(empty($num)){
- $num = 20;
- }
- if($start>1){
- $start--;
- }
- if (!in_array($order, ['asc', 'desc'])) {
- $order = 'asc';
- }
- if (!in_array($by, ['id', 'sort'])) {
- $by = 'id';
- }
- if (in_array($type, ['font', 'pic'])) {
- $type = ($type === 'font') ? 0 : 1;
- $where['link_type'] = $type;
- }
- if(!empty($not)){
- $where['link_id'] = ['not in',explode(',',$not)];
- }
- $by = 'link_'.$by;
- $order = $by . ' ' . $order;
- $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' . md5('link_listcache_'.join('&',$where).'_'.$order.'_'.$page.'_'.$num.'_'.$start);
- $res = Cache::get($cach_name);
- if(empty($cachetime)){
- $cachetime = $GLOBALS['config']['app']['cache_time'];
- }
- if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
- $res = $this->listData($where, $order, $page, $num, $start);
- if($GLOBALS['config']['app']['cache_core']==1) {
- Cache::set($cach_name, $res, $cachetime);
- }
- }
- return $res;
- }
- public function infoData($where,$field='*')
- {
- if(empty($where) || !is_array($where)){
- return ['code'=>1001,'msg'=>lang('param_err')];
- }
- $info = $this->field($field)->where($where)->find();
- if(empty($info)){
- return ['code'=>1002,'msg'=>lang('obtain_err')];
- }
- $info = $info->toArray();
- return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
- }
- public function saveData($data)
- {
- $validate = \think\Loader::validate('Link');
- if(!$validate->check($data)){
- return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
- }
- $data['link_time'] = time();
- if(!empty($data['link_id'])){
- $where=[];
- $where['link_id'] = ['eq',$data['link_id']];
- $res = $this->allowField(true)->where($where)->update($data);
- }
- else{
- $data['link_add_time'] = time();
- $res = $this->allowField(true)->insert($data);
- }
- if(false === $res){
- return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
- }
- return ['code'=>1,'msg'=>lang('save_ok')];
- }
- public function delData($where)
- {
- $res = $this->where($where)->delete();
- if($res===false){
- return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
- }
- return ['code'=>1,'msg'=>lang('del_ok')];
- }
- public function fieldData($where,$col,$val)
- {
- if(!isset($col) || !isset($val)){
- return ['code'=>1001,'msg'=>lang('param_err')];
- }
- $data = [];
- $data[$col] = $val;
- $res = $this->allowField(true)->where($where)->update($data);
- if($res===false){
- return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
- }
- return ['code'=>1,'msg'=>lang('set_ok')];
- }
- }
|