Msg.php 3.3 KB

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