ComplaintService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\ComplaintModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 投诉建议管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class ComplaintService
  20. * @package App\Services\Api
  21. */
  22. class ComplaintService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * ComplaintService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new ComplaintModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['a.mark' => 1];
  53. $status = isset($params['status']) ? $params['status'] : 0;
  54. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  55. if ($userId > 0) {
  56. $where['a.user_id'] = $userId;
  57. }
  58. if ($status > 0) {
  59. $where['a.status'] = $status;
  60. }
  61. $list = $this->model->from('complaint as a')
  62. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  63. ->where($where)
  64. ->where(function ($query) use ($params) {
  65. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  66. if ($keyword) {
  67. $query->where('a.content', 'like', "%{$keyword}%");
  68. }
  69. })
  70. ->select(['a.*'])
  71. ->orderBy('a.create_time', 'desc')
  72. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  73. $list = $list ? $list->toArray() : [];
  74. if ($list) {
  75. foreach ($list['data'] as &$item) {
  76. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  77. }
  78. }
  79. return [
  80. 'pageSize' => $pageSize,
  81. 'total' => isset($list['total']) ? $list['total'] : 0,
  82. 'list' => isset($list['data']) ? $list['data'] : []
  83. ];
  84. }
  85. /**
  86. * 提交内容
  87. * @param $userId
  88. * @param $params
  89. * @return mixed
  90. */
  91. public function submit($userId, $params)
  92. {
  93. $albums = isset($params['albums']) ? get_format_images($params['albums'], '') : '';
  94. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  95. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  96. $content = isset($params['content']) ? trim($params['content']) : '';
  97. if (empty($realname)) {
  98. $this->error = '请填写姓名';
  99. return false;
  100. }
  101. if (empty($mobile)) {
  102. $this->error = '请填写联系方式';
  103. return false;
  104. }
  105. if (empty($content)) {
  106. $this->error = '请填写您的问题';
  107. return false;
  108. }
  109. $cacheKey = "caches:members:complaint:{$userId}";
  110. if (RedisService::get($cacheKey)) {
  111. $this->error = '您近期已经提交过,请30秒后重试';
  112. return false;
  113. }
  114. $data = [
  115. 'user_id' => $userId,
  116. 'realname' => $realname,
  117. 'mobile' => $mobile,
  118. 'content' => $content,
  119. 'albums' => $albums,
  120. 'create_time' => time(),
  121. 'update_time' => time(),
  122. 'status' => 1,
  123. 'mark' => 1,
  124. ];
  125. if (!$id = $this->model->insertGetId($data)) {
  126. $this->error = '提交失败';
  127. return false;
  128. }
  129. $this->error = '提交成功';
  130. RedisService::set($cacheKey, $data, 30);
  131. return ['id' => $id];
  132. }
  133. }