ActivityService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\ActivityModel;
  13. use App\Services\BaseService;
  14. use App\Services\ConfigService;
  15. use App\Services\RedisService;
  16. /**
  17. * 活动-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Api
  21. */
  22. class ActivityService extends BaseService
  23. {
  24. // 静态对象
  25. protected 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. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $query = $this->getQuery($params);
  53. $list = $query->select(['a.id','a.title','a.thumb','a.start_at','a.publish_at','a.sort','a.description','a.status'])
  54. ->orderBy('a.sort','desc')
  55. ->orderBy('a.publish_at','desc')
  56. ->orderBy('a.id','desc')
  57. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  58. $list = $list? $list->toArray() :[];
  59. if($list){
  60. foreach($list['data'] as &$item){
  61. $item['start_at'] = $item['start_at']? datetime($item['start_at']) : datetime($item['create_time']);
  62. $item['thumb'] = $item['thumb']? get_image_url($item['thumb']) : '';
  63. }
  64. }
  65. return [
  66. 'pageSize'=> $pageSize,
  67. 'total'=>isset($list['total'])? $list['total'] : 0,
  68. 'list'=> isset($list['data'])? $list['data'] : []
  69. ];
  70. }
  71. /**
  72. * 查询
  73. * @param $params
  74. * @return mixed
  75. */
  76. public function getQuery($params)
  77. {
  78. $where = ['a.mark' => 1];
  79. $status = isset($params['status'])? $params['status'] : 0;
  80. $type = isset($params['type'])? $params['type'] : 0;
  81. if($status>0){
  82. $where['a.status'] = $status;
  83. }
  84. if($type>0 && $cateId<=0){
  85. $where['a.type'] = $type;
  86. }
  87. return $this->model->from('activitys as a')
  88. ->where($where)
  89. ->where(function ($query) use($params){
  90. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  91. if($keyword){
  92. $query->where('a.title','like',"%{$keyword}%");
  93. }
  94. });
  95. }
  96. /**
  97. * 获取详情
  98. * @param $id
  99. * @return array|mixed
  100. */
  101. public function getInfo($id)
  102. {
  103. $cacheKey = "caches:activity:info_{$id}";
  104. $info = RedisService::get($cacheKey);
  105. if($info){
  106. return $info;
  107. }
  108. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  109. ->first();
  110. $info = $info? $info->toArray() : [];
  111. if($info){
  112. // $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H.i.s') : '';
  113. $info['create_time'] = $info['publish_at']? datetime($info['publish_at'],'Y-m-d H.i.s') : datetime($info['create_time'],'Y-m-d H.i.s');
  114. $info['cover'] = get_image_url($info['cover']);
  115. if($info['content_type'] == 2){
  116. $info['content'] = json_decode(format_content($info['content']),true);
  117. }else{
  118. $info['content'] = get_format_content($info['content']);
  119. }
  120. $this->model->where(['id'=> $id])->increment('view_num',1);
  121. $info['view_num'] += intval($info['view_num']);
  122. RedisService::set($cacheKey, $info, rand(3600,7200));
  123. }
  124. return $info;
  125. }
  126. /**
  127. * 按类型获取文章
  128. * @param int $type
  129. * @return array|mixed
  130. */
  131. public function getListByType($type=1)
  132. {
  133. $cacheKey = "caches:articles:indexList_{$type}";
  134. $datas = RedisService::get($cacheKey);
  135. if($datas){
  136. return $datas;
  137. }
  138. $limit = ConfigService::make()->getConfigByCode('index_article_num',6);
  139. $datas = ArticleCateModel::with(['articles'])->where(['type'=>$type,'status'=>1,'mark'=>1])
  140. ->select(['id','name','sort','type'])
  141. ->orderBy('sort','desc')
  142. ->orderBy('id','desc')
  143. ->get();
  144. $datas = $datas? $datas->toArray() : [];
  145. if($datas){
  146. foreach ($datas as &$item){
  147. $item['articles'] = $item['articles']? array_slice($item['articles'],0,$limit) :[];
  148. }
  149. unset($item);
  150. RedisService::set($cacheKey, $datas, rand(300,600));
  151. }
  152. return $datas;
  153. }
  154. /**
  155. * 获取文章推荐分类
  156. * @param int $type
  157. * @return array|mixed
  158. */
  159. public function getCateList($type=1)
  160. {
  161. $cacheKey = "caches:articles:cateList_{$type}";
  162. $datas = RedisService::get($cacheKey);
  163. if($datas){
  164. return $datas;
  165. }
  166. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  167. ->select(['id','name','sort','type'])
  168. ->orderBy('sort','desc')
  169. ->orderBy('id','desc')
  170. ->get();
  171. $datas = $datas? $datas->toArray() : [];
  172. if($datas){
  173. RedisService::set($cacheKey, $datas, rand(300,600));
  174. }
  175. return $datas;
  176. }
  177. /**
  178. * 咨询信息提交
  179. * @param $userId
  180. * @param $params
  181. * @return array|false
  182. */
  183. public function consultSubmit($userId, $params)
  184. {
  185. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  186. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  187. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  188. $industry = isset($params['industry']) ? trim($params['industry']) : '';
  189. $position = isset($params['position']) ? trim($params['position']) : '';
  190. if (empty($realname)) {
  191. $this->error = '请填写姓名';
  192. return false;
  193. }
  194. if (empty($mobile)) {
  195. $this->error = '请填写联系方式';
  196. return false;
  197. }
  198. if (empty($industry)) {
  199. $this->error = '请填写所在行业';
  200. return false;
  201. }
  202. if (empty($position)) {
  203. $this->error = '请填写当前角色/职务';
  204. return false;
  205. }
  206. $cacheKey = "caches:articles:consult:{$userId}_{$sourceId}";
  207. if (RedisService::get($cacheKey)) {
  208. $this->error = '您近期已经提交过,请30秒后重试';
  209. return false;
  210. }
  211. $data = [
  212. 'user_id' => $userId,
  213. 'source_id' => $sourceId,
  214. 'realname' => $realname,
  215. 'mobile' => $mobile,
  216. 'industry' => $industry,
  217. 'position' => $position,
  218. 'work_year' => isset($params['work_year'])? trim($params['work_year']) : '',
  219. 'interest' => isset($params['interest'])? trim($params['interest']) : '',
  220. 'experiences' => isset($params['experiences'])? trim($params['experiences']) : '',
  221. 'create_time' => time(),
  222. 'update_time' => time(),
  223. 'status' => 1,
  224. 'mark' => 1,
  225. ];
  226. if (!$id = ArticleConsultRecordsModel::where(['source_id'=>$sourceId,'user_id'=>$userId])->value('id')) {
  227. if(!$id = ArticleConsultRecordsModel::insertGetId($data)){
  228. RedisService::clear($cacheKey);
  229. $this->error = '提交失败';
  230. return false;
  231. }
  232. }else{
  233. ArticleConsultRecordsModel::where(['id'=>$id])->update($data);
  234. }
  235. $this->error = '提交成功';
  236. RedisService::set($cacheKey, $data, 30);
  237. return ['id' => $id];
  238. }
  239. }