Gbook.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\api\controller;
  3. use think\Request;
  4. class Gbook extends Base
  5. {
  6. use PublicApi;
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->check_config();
  11. }
  12. public function index()
  13. {
  14. }
  15. /**
  16. * 获取列表
  17. *
  18. * @param Request $request
  19. * @return \think\response\Json
  20. */
  21. public function get_list(Request $request)
  22. {
  23. // 参数校验
  24. $param = $request->param();
  25. $validate = validate($request->controller());
  26. if (!$validate->scene($request->action())->check($param)) {
  27. return json([
  28. 'code' => 1001,
  29. 'msg' => '参数错误: ' . $validate->getError(),
  30. ]);
  31. }
  32. // 查询条件组装
  33. $where = [];
  34. $offset = isset($param['offset']) ? (int)$param['offset'] : 0;
  35. $limit = isset($param['limit']) ? (int)$param['limit'] : 20;
  36. if (isset($param['id'])) {
  37. $where['gbook_id'] = (int)$param['id'];
  38. }
  39. if (isset($param['rid'])) {
  40. $where['gbook_rid'] = (int)$param['rid'];
  41. }
  42. if (isset($param['user_id'])) {
  43. $where['user_id'] = (int)$param['user_id'];
  44. }
  45. if (isset($param['status'])) {
  46. $where['gbook_status'] = (int)$param['status'];
  47. }
  48. if (isset($param['name']) && strlen($param['name']) > 0) {
  49. $where['gbook_name'] = ['like', '%' . $this->format_sql_string($param['name']) . '%'];
  50. }
  51. if (isset($param['content']) && strlen($param['content']) > 0) {
  52. $where['gbook_content'] = ['like', '%' . $this->format_sql_string($param['content']) . '%'];
  53. }
  54. if (isset($param['time_end']) && isset($param['time_start'])) {
  55. $where['gbook_time'] = ['between', [(int)$param['time_start'], (int)$param['time_end']]];
  56. }elseif (isset($param['time_end'])) {
  57. $where['gbook_time'] = ['<', (int)$param['time_end']];
  58. }elseif (isset($param['time_start'])) {
  59. $where['gbook_time'] = ['>', (int)$param['time_start']];
  60. }
  61. // 数据获取
  62. $total = model('Gbook')->getCountByCond($where);
  63. $list = [];
  64. if ($total > 0) {
  65. // 排序
  66. $order = "gbook_time DESC";
  67. $field = '*';
  68. if (strlen($param['orderby']) > 0) {
  69. $order = 'gbook_' . $param['orderby'] . " DESC";
  70. }
  71. $list = model('Gbook')->getListByCond($offset, $limit, $where, $order, $field, []);
  72. }
  73. // 返回
  74. return json([
  75. 'code' => 1,
  76. 'msg' => '获取成功',
  77. 'info' => [
  78. 'offset' => $offset,
  79. 'limit' => $limit,
  80. 'total' => $total,
  81. 'rows' => $list,
  82. ],
  83. ]);
  84. }
  85. /**
  86. * 提交留言
  87. * api.php/gbook/submit (POST)
  88. * 参数: gbook_content, [gbook_name]
  89. */
  90. public function submit(Request $request)
  91. {
  92. $content = trim($request->param('gbook_content', ''));
  93. if (empty($content)) return json(['code' => 1004, 'msg' => lang('index/require_content')]);
  94. $cookie = 'gbook_timespan';
  95. if (!empty(cookie($cookie))) return json(['code' => 1005, 'msg' => lang('frequently')]);
  96. if ($GLOBALS['config']['gbook']['login'] == 1) {
  97. $check = model('User')->checkLogin();
  98. if ($check['code'] > 1) return json(['code' => 1003, 'msg' => lang('index/require_login')]);
  99. }
  100. $data = [];
  101. $data['gbook_content'] = htmlentities(mac_filter_words($content));
  102. $data['gbook_reply'] = '';
  103. $data['gbook_ip'] = mac_get_client_ip();
  104. $data['gbook_time'] = time();
  105. if (!empty(cookie('user_id'))) {
  106. $uinfo = model('User')->field('user_nick_name,user_name')->where(['user_id' => intval(cookie('user_id'))])->find();
  107. $data['user_id'] = intval(cookie('user_id'));
  108. $data['gbook_name'] = htmlentities($uinfo['user_nick_name'] ?: $uinfo['user_name']);
  109. } else {
  110. $data['user_id'] = 0;
  111. $name = trim($request->param('gbook_name', ''));
  112. $data['gbook_name'] = htmlentities($name ?: lang('controller/visitor'));
  113. }
  114. $data['gbook_status'] = ($GLOBALS['config']['gbook']['audit'] == 1) ? 0 : 1;
  115. $res = model('Gbook')->saveData($data);
  116. cookie($cookie, 't', 30);
  117. return json($res);
  118. }
  119. /**
  120. * 举报留言
  121. * api.php/gbook/report?id=1
  122. */
  123. public function report(Request $request)
  124. {
  125. $id = intval($request->param('id', 0));
  126. if ($id < 1) return json(['code' => 1001, 'msg' => '参数错误']);
  127. $cookie = 'gbook-report-' . $id;
  128. if (!empty(cookie($cookie))) return json(['code' => 1002, 'msg' => lang('index/haved')]);
  129. model('Gbook')->where(['gbook_id' => $id])->setInc('gbook_up');
  130. cookie($cookie, 't', 86400);
  131. return json(['code' => 1, 'msg' => 'ok']);
  132. }
  133. }