Annex.php 3.5 KB

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