| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\ActionLogModel;
- use App\Models\ActivityModel;
- use App\Services\BaseService;
- use App\Services\MpService;
- use App\Services\RedisService;
- /**
- * 活动管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class ActivityService extends BaseService
- {
- public static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new ActivityModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取数据列表
- * @param array $params 请求参数
- * @param int $pageSize 分页大小
- */
- public function getDataList($params, $pageSize = 15)
- {
- try {
- $query = $this->model->where('mark', 1);
- // 状态筛选
- if (isset($params['status']) && $params['status']) {
- $query->where('status', $params['status']);
- }
- // 关键词搜索
- if (isset($params['keyword']) && $params['keyword']) {
- $keyword = $params['keyword'];
- $query->where(function ($q) use ($keyword) {
- $q->where('title', 'like', '%' . $keyword . '%');
- });
- }
- $list = $query->withCount(['signs'])
- ->orderBy('sort', 'desc')
- ->orderBy('id', 'desc')
- ->paginate($pageSize);
- $list = $list ? $list->toArray() : [];
- return [
- 'msg' => '操作成功',
- 'code' => 0,
- 'data' => $list['data'] ?? [],
- 'count' => $list['total'] ?? 0,
- ];
- } catch (\Exception $e) {
- return [
- 'msg' => '查询失败: ' . $e->getMessage(),
- 'code' => 1,
- 'data' => [],
- 'count' => 0,
- ];
- }
- }
- /**
- * 添加会编辑会员
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- // 请求参数
- $data = request()->all();
- $id = isset($data['id']) ? $data['id'] : 0;
- $data['publish_at']=$data['publish_at']?$data['publish_at'] : date('Y-m-d H:i:s');
- // 设置日志
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '编辑活动信息', 'content' => json_encode($data, 256), 'module' => 'admin']);
- ActionLogModel::record();
- RedisService::keyDel("caches:activity:info_{$id}*");
- return parent::edit($data);
- }
- /**
- * 获取详情
- */
- public function getInfo($id = null)
- {
- if ($id === null) {
- $id = request()->input('id');
- }
- $info = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '活动不存在'];
- }
- $info = $info->toArray();
- $info['thumb'] = $info['thumb'] ? get_image_url($info['thumb']) : '';
- $info['qrcode'] = MpService::make()->getMiniQrcode('pagesSub/pages/activity/sign','sign@'.$info['id']);
- $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
- $info['qrcode_bg'] = get_image_url(\App\Services\ConfigService::make()->getConfigByCode('sign_qrcode_bg'));
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $info
- ];
- }
- /**
- * 设置状态
- */
- public function status()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $status = $params['status'] ?? 1;
- $info = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '活动不存在'];
- }
- $info->status = $status;
- $info->update_time = time();
- $info->save();
- RedisService::clear("caches:activity:info_{$id}");
- return ['code' => 0, 'msg' => '设置成功'];
- }
- /**
- * 设置签到状态
- */
- public function signStatus()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $status = $params['status'] ?? 1;
- $info = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '活动不存在'];
- }
- $info->sign_status = $status;
- $info->update_time = time();
- $info->save();
- RedisService::clear("caches:activity:info_{$id}");
- return ['code' => 0, 'msg' => '设置成功'];
- }
- /**
- * 删除
- * @return array
- */
- public function delete()
- {
- $id = request()->input('id');
- $this->model->where(['mark'=>0])->where('update_time','<', time() - 600)->delete();
- if (is_array($id)) {
- // 批量删除
- $count = $this->model->whereIn('id', $id)
- ->where('mark', 1)
- ->update(['mark' => 0, 'update_time' => time()]);
- return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
- } else {
- // 单个删除
- $info = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '活动不存在'];
- }
- $info->mark = 0;
- $info->update_time = time();
- $info->save();
- RedisService::clear("caches:activity:info_{$id}*");
- return ['code' => 0, 'msg' => '删除成功'];
- }
- }
- }
|