Gbook.php 5.8 KB

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