Gbook.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. $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. $total = $this->where($where)->count();
  29. $list = Db::name('Gbook')->where($where)->order($order)->limit($limit_str)->select();
  30. foreach ($list as $k=>$v){
  31. $list[$k]['user_portrait'] = mac_get_user_portrait($v['user_id']);
  32. $list[$k]['gbook_content'] = mac_restore_htmlfilter($list[$k]['gbook_content']);
  33. $list[$k]['gbook_reply'] = mac_restore_htmlfilter($list[$k]['gbook_reply']);
  34. }
  35. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'limit'=>$limit,'total'=>$total,'list'=>$list];
  36. }
  37. public function listCacheData($lp)
  38. {
  39. if (!is_array($lp)) {
  40. $lp = json_decode($lp, true);
  41. }
  42. $order = $lp['order'];
  43. $by = $lp['by'];
  44. $paging = $lp['paging'];
  45. $start = intval(abs($lp['start']));
  46. $num = intval(abs($lp['num']));
  47. $rid = intval(abs($lp['rid']));
  48. $uid = intval(abs($lp['uid']));
  49. $half = intval(abs($lp['half']));
  50. $pageurl = $lp['pageurl'];
  51. $page = 1;
  52. $where = [];
  53. if(empty($num)){
  54. $num = 20;
  55. }
  56. if($start>1){
  57. $start--;
  58. }
  59. if (!in_array($paging, ['yes', 'no'])) {
  60. $paging = 'no';
  61. }
  62. if($paging=='yes') {
  63. $param = mac_param_url();
  64. if(!empty($param['rid'])){
  65. $rid = $param['rid'];
  66. }
  67. if(!empty($param['by'])){
  68. $by = $param['by'];
  69. }
  70. if(!empty($param['order'])){
  71. $order = $param['order'];
  72. }
  73. if(!empty($param['page'])){
  74. $page = intval($param['page']);
  75. }
  76. foreach($param as $k=>$v){
  77. if(empty($v)){
  78. unset($param[$k]);
  79. }
  80. }
  81. if(empty($pageurl)){
  82. $pageurl = 'gbook/index';
  83. }
  84. $param['page'] = 'PAGELINK';
  85. $pageurl = mac_url($pageurl,$param);
  86. }
  87. $where['gbook_status'] = ['eq',1];
  88. if(!empty($rid)){
  89. $where['gbook_rid'] = ['eq',$rid];
  90. }
  91. if(!empty($uid)){
  92. $where['user_id'] = ['eq',$uid];
  93. }
  94. if(!in_array($by, ['id', 'time','reply_time'])) {
  95. $by = 'time';
  96. }
  97. if(!in_array($order, ['asc', 'desc'])) {
  98. $order = 'desc';
  99. }
  100. $order= 'gbook_'.$by .' ' . $order;
  101. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('gbook_listcache_'.join('&',$where).'_'.$order.'_'.$page.'_'.$num.'_'.$start);
  102. $res = $this->listData($where,$order,$page,$num,$start);
  103. $res['pageurl'] = $pageurl;
  104. $res['half'] = $half;
  105. return $res;
  106. }
  107. public function infoData($where,$field='*')
  108. {
  109. if(empty($where) || !is_array($where)){
  110. return ['code'=>1001,'msg'=>lang('param_err')];
  111. }
  112. $info = $this->field($field)->where($where)->find();
  113. if(empty($info)){
  114. return ['code'=>1002,'msg'=>lang('obtain_err')];
  115. }
  116. $info = $info->toArray();
  117. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  118. }
  119. public function saveData($data)
  120. {
  121. $validate = \think\Loader::validate('Gbook');
  122. if(!$validate->check($data)){
  123. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  124. }
  125. // xss过滤
  126. $filter_fields = [
  127. 'gbook_name',
  128. 'gbook_content',
  129. 'gbook_reply',
  130. ];
  131. foreach ($filter_fields as $filter_field) {
  132. if (!isset($data[$filter_field])) {
  133. continue;
  134. }
  135. $data[$filter_field] = mac_filter_xss($data[$filter_field]);
  136. }
  137. if(!empty($data['gbook_id'])){
  138. if(!empty($data['gbook_reply'])){
  139. $data['gbook_reply_time'] = time();
  140. }
  141. $where=[];
  142. $where['gbook_id'] = ['eq',$data['gbook_id']];
  143. $res = $this->allowField(true)->where($where)->update($data);
  144. }
  145. else{
  146. $data['gbook_time'] = time();
  147. $res = $this->allowField(true)->insert($data);
  148. }
  149. if(false === $res){
  150. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  151. }
  152. return ['code'=>1,'msg'=>lang('save_ok')];
  153. }
  154. public function delData($where)
  155. {
  156. $res = $this->where($where)->delete();
  157. if($res===false){
  158. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  159. }
  160. return ['code'=>1,'msg'=>lang('del_ok')];
  161. }
  162. public function fieldData($where,$col,$val)
  163. {
  164. if(!isset($col) || !isset($val)){
  165. return ['code'=>1001,'msg'=>lang('param_err')];
  166. }
  167. $data = [];
  168. $data[$col] = $val;
  169. $res = $this->allowField(true)->where($where)->update($data);
  170. if($res===false){
  171. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  172. }
  173. return ['code'=>1,'msg'=>lang('set_ok')];
  174. }
  175. }