Msg.php 3.2 KB

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