ActivityService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\Common;
  12. use App\Models\ActionLogModel;
  13. use App\Models\ActivityModel;
  14. use App\Services\BaseService;
  15. use App\Services\MpService;
  16. use App\Services\RedisService;
  17. /**
  18. * 活动管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Common
  22. */
  23. class ActivityService extends BaseService
  24. {
  25. public static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new ActivityModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 获取数据列表
  47. * @param array $params 请求参数
  48. * @param int $pageSize 分页大小
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. try {
  53. $query = $this->model->where('mark', 1);
  54. // 状态筛选
  55. if (isset($params['status']) && $params['status']) {
  56. $query->where('status', $params['status']);
  57. }
  58. // 关键词搜索
  59. if (isset($params['keyword']) && $params['keyword']) {
  60. $keyword = $params['keyword'];
  61. $query->where(function ($q) use ($keyword) {
  62. $q->where('title', 'like', '%' . $keyword . '%');
  63. });
  64. }
  65. $list = $query->withCount(['signs'])
  66. ->orderBy('sort', 'desc')
  67. ->orderBy('id', 'desc')
  68. ->paginate($pageSize);
  69. $list = $list ? $list->toArray() : [];
  70. return [
  71. 'msg' => '操作成功',
  72. 'code' => 0,
  73. 'data' => $list['data'] ?? [],
  74. 'count' => $list['total'] ?? 0,
  75. ];
  76. } catch (\Exception $e) {
  77. return [
  78. 'msg' => '查询失败: ' . $e->getMessage(),
  79. 'code' => 1,
  80. 'data' => [],
  81. 'count' => 0,
  82. ];
  83. }
  84. }
  85. /**
  86. * 添加会编辑会员
  87. * @return array
  88. * @since 2020/11/11
  89. * @author laravel开发员
  90. */
  91. public function edit()
  92. {
  93. // 请求参数
  94. $data = request()->all();
  95. $id = isset($data['id']) ? $data['id'] : 0;
  96. $data['publish_at']=$data['publish_at']?$data['publish_at'] : date('Y-m-d H:i:s');
  97. // 设置日志
  98. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '编辑活动信息', 'content' => json_encode($data, 256), 'module' => 'admin']);
  99. ActionLogModel::record();
  100. RedisService::keyDel("caches:activity:info_{$id}*");
  101. return parent::edit($data);
  102. }
  103. /**
  104. * 获取详情
  105. */
  106. public function getInfo($id = null)
  107. {
  108. if ($id === null) {
  109. $id = request()->input('id');
  110. }
  111. $info = $this->model->where('id', $id)
  112. ->where('mark', 1)
  113. ->first();
  114. if (!$info) {
  115. return ['code' => 1, 'msg' => '活动不存在'];
  116. }
  117. $info = $info->toArray();
  118. $info['thumb'] = $info['thumb'] ? get_image_url($info['thumb']) : '';
  119. $info['qrcode'] = MpService::make()->getMiniQrcode('pagesSub/pages/activity/sign','sign@'.$info['id']);
  120. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  121. $info['qrcode_bg'] = get_image_url(\App\Services\ConfigService::make()->getConfigByCode('sign_qrcode_bg'));
  122. return [
  123. 'code' => 0,
  124. 'msg' => '获取成功',
  125. 'data' => $info
  126. ];
  127. }
  128. /**
  129. * 设置状态
  130. */
  131. public function status()
  132. {
  133. $params = request()->all();
  134. $id = $params['id'] ?? 0;
  135. $status = $params['status'] ?? 1;
  136. $info = $this->model->where('id', $id)
  137. ->where('mark', 1)
  138. ->first();
  139. if (!$info) {
  140. return ['code' => 1, 'msg' => '活动不存在'];
  141. }
  142. $info->status = $status;
  143. $info->update_time = time();
  144. $info->save();
  145. RedisService::clear("caches:activity:info_{$id}");
  146. return ['code' => 0, 'msg' => '设置成功'];
  147. }
  148. /**
  149. * 设置签到状态
  150. */
  151. public function signStatus()
  152. {
  153. $params = request()->all();
  154. $id = $params['id'] ?? 0;
  155. $status = $params['status'] ?? 1;
  156. $info = $this->model->where('id', $id)
  157. ->where('mark', 1)
  158. ->first();
  159. if (!$info) {
  160. return ['code' => 1, 'msg' => '活动不存在'];
  161. }
  162. $info->sign_status = $status;
  163. $info->update_time = time();
  164. $info->save();
  165. RedisService::clear("caches:activity:info_{$id}");
  166. return ['code' => 0, 'msg' => '设置成功'];
  167. }
  168. /**
  169. * 删除
  170. * @return array
  171. */
  172. public function delete()
  173. {
  174. $id = request()->input('id');
  175. $this->model->where(['mark'=>0])->where('update_time','<', time() - 600)->delete();
  176. if (is_array($id)) {
  177. // 批量删除
  178. $count = $this->model->whereIn('id', $id)
  179. ->where('mark', 1)
  180. ->update(['mark' => 0, 'update_time' => time()]);
  181. return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
  182. } else {
  183. // 单个删除
  184. $info = $this->model->where('id', $id)
  185. ->where('mark', 1)
  186. ->first();
  187. if (!$info) {
  188. return ['code' => 1, 'msg' => '活动不存在'];
  189. }
  190. $info->mark = 0;
  191. $info->update_time = time();
  192. $info->save();
  193. RedisService::clear("caches:activity:info_{$id}*");
  194. return ['code' => 0, 'msg' => '删除成功'];
  195. }
  196. }
  197. }