Visit.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if(!is_array($where)){
  22. $where = json_decode($where,true);
  23. }
  24. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  25. if($totalshow==1) {
  26. $total = $this->where($where)->count();
  27. }
  28. $list = Db::name('Visit')->field($field)->where($where)->order($order)->limit($limit_str)->select();
  29. foreach($list as $k=>$v){
  30. $visit_mid = 6;
  31. if($v['user_id']==0){
  32. $visit_mid = 11;
  33. }
  34. $list[$k]['visit_mid'] = $visit_mid;
  35. }
  36. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  37. }
  38. public function infoData($where,$field='*')
  39. {
  40. if(empty($where) || !is_array($where)){
  41. return ['code'=>1001,'msg'=>lang('param_err')];
  42. }
  43. $info = $this->field($field)->where($where)->find();
  44. if (empty($info)) {
  45. return ['code' => 1002, 'msg' => lang('obtain_err')];
  46. }
  47. $info = $info->toArray();
  48. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  49. }
  50. public function saveData($data)
  51. {
  52. $validate = \think\Loader::validate('Visit');
  53. if(!$validate->check($data)){
  54. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  55. }
  56. if(!empty($data['visit_id'])){
  57. $where=[];
  58. $where['visit_id'] = ['eq',$data['visit_id']];
  59. $res = $this->allowField(true)->where($where)->update($data);
  60. }
  61. else{
  62. $data['visit_time'] = time();
  63. $res = $this->allowField(true)->insert($data);
  64. }
  65. if(false === $res){
  66. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  67. }
  68. return ['code'=>1,'msg'=>lang('save_ok')];
  69. }
  70. public function delData($where)
  71. {
  72. $res = $this->where($where)->delete();
  73. if($res===false){
  74. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  75. }
  76. return ['code'=>1,'msg'=>lang('del_ok')];
  77. }
  78. public function fieldData($where,$col,$val)
  79. {
  80. if(!isset($col) || !isset($val)){
  81. return ['code'=>1001,'msg'=>lang('param_err')];
  82. }
  83. $data = [];
  84. $data[$col] = $val;
  85. $res = $this->allowField(true)->where($where)->update($data);
  86. if($res===false){
  87. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  88. }
  89. return ['code'=>1,'msg'=>lang('set_ok')];
  90. }
  91. }