Annex.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Annex extends Base {
  5. // 设置数据表(不含前缀)
  6. protected $name = 'annex';
  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('Annex')->field($field)->where($where)->order($order)->limit($limit_str)->select();
  32. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  33. }
  34. public function infoData($where,$field='*')
  35. {
  36. if(empty($where) || !is_array($where)){
  37. return ['code'=>1001,'msg'=>lang('param_err')];
  38. }
  39. $info = $this->field($field)->where($where)->find();
  40. if (empty($info)) {
  41. return ['code' => 1002, 'msg' => lang('obtain_err')];
  42. }
  43. $info = $info->toArray();
  44. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  45. }
  46. public function saveData($data)
  47. {
  48. $validate = \think\Loader::validate('Annex');
  49. if(!$validate->check($data)){
  50. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  51. }
  52. if(!empty($data['annex_id'])){
  53. $where=[];
  54. $where['annex_id'] = ['eq',$data['annex_id']];
  55. $res = $this->allowField(true)->where($where)->update($data);
  56. }
  57. else{
  58. $data['annex_time'] = time();
  59. $res = $this->allowField(true)->insert($data);
  60. }
  61. if(false === $res){
  62. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  63. }
  64. return ['code'=>1,'msg'=>lang('save_ok')];
  65. }
  66. public function delData($where)
  67. {
  68. $list = $this->listData($where,'',1,9999);
  69. if($list['code'] !==1){
  70. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  71. }
  72. $path = './';
  73. foreach($list['list'] as $k=>$v){
  74. if (stripos($v['annex_file'], '../') !== false) {
  75. continue;
  76. }
  77. $pic = $path.$v['annex_file'];
  78. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  79. unlink($pic);
  80. }
  81. }
  82. $res = $this->where($where)->delete();
  83. if($res===false){
  84. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  85. }
  86. return ['code'=>1,'msg'=>lang('del_ok')];
  87. }
  88. public function fieldData($where,$col,$val)
  89. {
  90. if(!isset($col) || !isset($val)){
  91. return ['code'=>1001,'msg'=>lang('param_err')];
  92. }
  93. $data = [];
  94. $data[$col] = $val;
  95. $res = $this->allowField(true)->where($where)->update($data);
  96. if($res===false){
  97. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  98. }
  99. return ['code'=>1,'msg'=>lang('set_ok')];
  100. }
  101. }