Visit.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Visit extends Base {
  5. // 设置数据表(不含前缀)
  6. protected $name = 'visit';
  7. // 定义时间戳字段名
  8. protected $createTime = '';
  9. protected $updateTime = '';
  10. // 自动完成
  11. protected $auto = [];
  12. protected $insert = [];
  13. protected $update = [];
  14. public function countData($where)
  15. {
  16. $total = $this->where($where)->count();
  17. return $total;
  18. }
  19. public function listData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1,$totalshow=1)
  20. {
  21. $page = $page > 0 ? (int)$page : 1;
  22. $limit = $limit ? (int)$limit : 20;
  23. $start = $start ? (int)$start : 0;
  24. if(!is_array($where)){
  25. $where = json_decode($where,true);
  26. }
  27. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  28. if($totalshow==1) {
  29. $total = $this->where($where)->count();
  30. }
  31. $list = Db::name('Visit')->field($field)->where($where)->order($order)->limit($limit_str)->select();
  32. foreach($list as $k=>$v){
  33. $visit_mid = 6;
  34. if($v['user_id']==0){
  35. $visit_mid = 11;
  36. }
  37. $list[$k]['visit_mid'] = $visit_mid;
  38. }
  39. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  40. }
  41. public function infoData($where,$field='*')
  42. {
  43. if(empty($where) || !is_array($where)){
  44. return ['code'=>1001,'msg'=>lang('param_err')];
  45. }
  46. $info = $this->field($field)->where($where)->find();
  47. if (empty($info)) {
  48. return ['code' => 1002, 'msg' => lang('obtain_err')];
  49. }
  50. $info = $info->toArray();
  51. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  52. }
  53. public function saveData($data)
  54. {
  55. $validate = \think\Loader::validate('Visit');
  56. if(!$validate->check($data)){
  57. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  58. }
  59. if(!empty($data['visit_id'])){
  60. $where=[];
  61. $where['visit_id'] = ['eq',$data['visit_id']];
  62. $res = $this->allowField(true)->where($where)->update($data);
  63. }
  64. else{
  65. $data['visit_time'] = time();
  66. $res = $this->allowField(true)->insert($data);
  67. }
  68. if(false === $res){
  69. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  70. }
  71. return ['code'=>1,'msg'=>lang('save_ok')];
  72. }
  73. public function delData($where)
  74. {
  75. $res = $this->where($where)->delete();
  76. if($res===false){
  77. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  78. }
  79. return ['code'=>1,'msg'=>lang('del_ok')];
  80. }
  81. public function fieldData($where,$col,$val)
  82. {
  83. if(!isset($col) || !isset($val)){
  84. return ['code'=>1001,'msg'=>lang('param_err')];
  85. }
  86. $data = [];
  87. $data[$col] = $val;
  88. $res = $this->allowField(true)->where($where)->update($data);
  89. if($res===false){
  90. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  91. }
  92. return ['code'=>1,'msg'=>lang('set_ok')];
  93. }
  94. }