Gbook.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Gbook extends Base {
  5. // 设置数据表(不含前缀)
  6. protected $name = 'gbook';
  7. // 定义时间戳字段名
  8. protected $createTime = '';
  9. protected $updateTime = '';
  10. // 自动完成
  11. protected $auto = [];
  12. protected $insert = [];
  13. protected $update = [];
  14. public function getGbookStatusTextAttr($val,$data)
  15. {
  16. $arr = [0=>lang('disable'),1=>lang('enable')];
  17. return $arr[$data['gbook_status']];
  18. }
  19. public function listData($where,$order,$page=1,$limit=20,$start=0)
  20. {
  21. if(!is_array($where)){
  22. $where = json_decode($where,true);
  23. }
  24. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  25. $total = $this->where($where)->count();
  26. $list = Db::name('Gbook')->where($where)->order($order)->limit($limit_str)->select();
  27. foreach ($list as $k=>$v){
  28. $list[$k]['user_portrait'] = mac_get_user_portrait($v['user_id']);
  29. }
  30. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'limit'=>$limit,'total'=>$total,'list'=>$list];
  31. }
  32. public function listCacheData($lp)
  33. {
  34. if (!is_array($lp)) {
  35. $lp = json_decode($lp, true);
  36. }
  37. $order = $lp['order'];
  38. $by = $lp['by'];
  39. $paging = $lp['paging'];
  40. $start = intval(abs($lp['start']));
  41. $num = intval(abs($lp['num']));
  42. $rid = intval(abs($lp['rid']));
  43. $uid = intval(abs($lp['uid']));
  44. $half = intval(abs($lp['half']));
  45. $pageurl = $lp['pageurl'];
  46. $page = 1;
  47. $where = [];
  48. if(empty($num)){
  49. $num = 20;
  50. }
  51. if($start>1){
  52. $start--;
  53. }
  54. if (!in_array($paging, ['yes', 'no'])) {
  55. $paging = 'no';
  56. }
  57. if($paging=='yes') {
  58. $param = mac_param_url();
  59. if(!empty($param['rid'])){
  60. $rid = $param['rid'];
  61. }
  62. if(!empty($param['by'])){
  63. $by = $param['by'];
  64. }
  65. if(!empty($param['order'])){
  66. $order = $param['order'];
  67. }
  68. if(!empty($param['page'])){
  69. $page = intval($param['page']);
  70. }
  71. foreach($param as $k=>$v){
  72. if(empty($v)){
  73. unset($param[$k]);
  74. }
  75. }
  76. if(empty($pageurl)){
  77. $pageurl = 'gbook/index';
  78. }
  79. $param['page'] = 'PAGELINK';
  80. $pageurl = mac_url($pageurl,$param);
  81. }
  82. $where['gbook_status'] = ['eq',1];
  83. if(!empty($rid)){
  84. $where['gbook_rid'] = ['eq',$rid];
  85. }
  86. if(!empty($uid)){
  87. $where['user_id'] = ['eq',$uid];
  88. }
  89. if(!in_array($by, ['id', 'time','reply_time'])) {
  90. $by = 'time';
  91. }
  92. if(!in_array($order, ['asc', 'desc'])) {
  93. $order = 'desc';
  94. }
  95. $order= 'gbook_'.$by .' ' . $order;
  96. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('gbook_listcache_'.join('&',$where).'_'.$order.'_'.$page.'_'.$num.'_'.$start);
  97. $res = $this->listData($where,$order,$page,$num,$start);
  98. $res['pageurl'] = $pageurl;
  99. $res['half'] = $half;
  100. return $res;
  101. }
  102. public function infoData($where,$field='*')
  103. {
  104. if(empty($where) || !is_array($where)){
  105. return ['code'=>1001,'msg'=>lang('param_err')];
  106. }
  107. $info = $this->field($field)->where($where)->find();
  108. if(empty($info)){
  109. return ['code'=>1002,'msg'=>lang('obtain_err')];
  110. }
  111. $info = $info->toArray();
  112. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  113. }
  114. public function saveData($data)
  115. {
  116. $validate = \think\Loader::validate('Gbook');
  117. if(!$validate->check($data)){
  118. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  119. }
  120. // xss过滤
  121. $filter_fields = [
  122. 'gbook_name',
  123. 'gbook_content',
  124. 'gbook_reply',
  125. ];
  126. foreach ($filter_fields as $filter_field) {
  127. if (!isset($data[$filter_field])) {
  128. continue;
  129. }
  130. $data[$filter_field] = mac_filter_xss($data[$filter_field]);
  131. }
  132. if(!empty($data['gbook_id'])){
  133. if(!empty($data['gbook_reply'])){
  134. $data['gbook_reply_time'] = time();
  135. }
  136. $where=[];
  137. $where['gbook_id'] = ['eq',$data['gbook_id']];
  138. $res = $this->allowField(true)->where($where)->update($data);
  139. }
  140. else{
  141. $data['gbook_time'] = time();
  142. $res = $this->allowField(true)->insert($data);
  143. }
  144. if(false === $res){
  145. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  146. }
  147. return ['code'=>1,'msg'=>lang('save_ok')];
  148. }
  149. public function delData($where)
  150. {
  151. $res = $this->where($where)->delete();
  152. if($res===false){
  153. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  154. }
  155. return ['code'=>1,'msg'=>lang('del_ok')];
  156. }
  157. public function fieldData($where,$col,$val)
  158. {
  159. if(!isset($col) || !isset($val)){
  160. return ['code'=>1001,'msg'=>lang('param_err')];
  161. }
  162. $data = [];
  163. $data[$col] = $val;
  164. $res = $this->allowField(true)->where($where)->update($data);
  165. if($res===false){
  166. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  167. }
  168. return ['code'=>1,'msg'=>lang('set_ok')];
  169. }
  170. }