DynamicService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ArticleModel;
  13. use App\Models\CollectModel;
  14. use App\Models\DynamicCommentModel;
  15. use App\Models\DynamicModel;
  16. use App\Models\FollowModel;
  17. use App\Models\MemberModel;
  18. /**
  19. * 动态管理-服务类
  20. * @author wesmiler
  21. * @since 2020/11/11
  22. * Class DynamicService
  23. * @package App\Services
  24. */
  25. class DynamicService extends BaseService
  26. {
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author wesmiler
  31. * @since 2020/11/11
  32. * DynamicService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new DynamicModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return DynamicService|null
  41. */
  42. public static function make(){
  43. if(!self::$instance){
  44. self::$instance = new DynamicService();
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 获取列表
  50. * @return array
  51. * @since 2020/11/11
  52. * @author wesmiler
  53. */
  54. public function getList()
  55. {
  56. $params = request()->all();
  57. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  58. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  59. $dataList = $this->model::from('dynamic as a')
  60. ->where(function ($query) use ($params) {
  61. $query->where('a.mark', 1);
  62. $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
  63. if ($isRecommand > 0) {
  64. $query->where('a.is_recommand', $isRecommand);
  65. }
  66. $status = isset($params['status']) ? $params['status'] : 0;
  67. if ($status > 0) {
  68. $query->where('a.status', $status);
  69. } else {
  70. $query->whereIn('a.status', [1, 2]);
  71. }
  72. })
  73. ->select(['a.id', 'a.user_id', 'a.comment_close', 'a.source_id', 'a.is_recommand', 'a.content', 'a.albums', 'a.status', 'a.create_time', 'a.update_time'])
  74. ->orderBy('a.update_time', 'desc')
  75. ->paginate($pageSize);
  76. $dataList = $dataList ? $dataList->toArray() : [];
  77. if ($dataList) {
  78. foreach ($dataList['data'] as &$item) {
  79. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  80. }
  81. unset($item);
  82. }
  83. return [
  84. 'code' => 0,
  85. 'success'=> true,
  86. 'msg' => '操作成功',
  87. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  88. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  89. ];
  90. }
  91. /**
  92. * 获取列表
  93. * @return array
  94. * @since 2020/11/11
  95. * @author wesmiler
  96. */
  97. public function getDataList($params)
  98. {
  99. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  100. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  101. $dataList = $this->model::from('dynamic as a')
  102. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  103. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  104. ->where(['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1])
  105. ->where('m.id','>',0)
  106. ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a.albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
  107. ->orderBy('a.update_time', 'desc')
  108. ->paginate($pageSize);
  109. $dataList = $dataList ? $dataList->toArray() : [];
  110. if ($dataList) {
  111. foreach ($dataList['data'] as &$item) {
  112. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  113. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  114. $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : [];
  115. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  116. if($item['albums']){
  117. foreach ($item['albums'] as &$v){
  118. $v = $v? get_image_url($v) : '';
  119. }
  120. unset($v);
  121. }
  122. // 关注数量
  123. $item['collect'] = 0;
  124. if($item['id']){
  125. $count = CollectModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  126. $item['collect'] = $count? $count : 0;
  127. }
  128. // 评论数量
  129. $item['comment'] = 0;
  130. if($item['id']){
  131. $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  132. $item['comment'] = $count? $count : 0;
  133. }
  134. }
  135. unset($item);
  136. }
  137. return [
  138. 'code' => 0,
  139. 'success'=> true,
  140. 'msg' => '操作成功',
  141. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  142. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  143. ];
  144. }
  145. /**
  146. * 获取详情
  147. * @param $id
  148. */
  149. public function getDetail($id, $userId=0){
  150. $info = $this->model::from('dynamic as a')
  151. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  152. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  153. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id,'m.mark'=> 1,'m.status'=> 1])
  154. ->where('m.id','>',0)
  155. ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a.albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
  156. ->first();
  157. $info = $info? $info->toArray() : [];
  158. if($info){
  159. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  160. $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : '';
  161. $info['create_time'] = $info['create_time']? datetime( $info['create_time'],'Y-m-d H:i:s') : '';
  162. $info['albums'] = $info['albums']? json_decode($info['albums']) : [];
  163. if($info['albums']){
  164. foreach ($info['albums'] as &$v){
  165. $v = $v? get_image_url($v) : '';
  166. }
  167. unset($v);
  168. }
  169. $info['is_follow'] = 0;
  170. $info[''] = 0;
  171. if($info['user_id']){
  172. $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
  173. $info['author'] = $author? $author : '报恩寺';
  174. }
  175. // 是否已收藏
  176. $info['is_collect'] = 0;
  177. if($userId){
  178. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){
  179. $info['is_collect'] = 1;
  180. }
  181. }
  182. }
  183. return $info;
  184. }
  185. /**
  186. * 添加或编辑
  187. * @return array
  188. * @since 2020/11/11
  189. * @author wesmiler
  190. */
  191. public function edit()
  192. {
  193. $data = request()->all();
  194. // 图片处理
  195. $type = isset($data['type'])? intval($data['type']) : 0;
  196. $image = $data['thumb']? trim($data['thumb']) : '';
  197. $id = isset($data['id']) ? $data['id'] : 0;
  198. if (!$id && !$image && $type == 1) {
  199. return message('请上传封面图片', false);
  200. }
  201. if (strpos($image, "temp")) {
  202. $data['thumb'] = save_image($image, 'item');
  203. } else {
  204. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  205. }
  206. $data['update_time'] = time();
  207. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  208. return parent::edit($data); // TODO: Change the autogenerated stub
  209. }
  210. /**
  211. * 发布动态
  212. * @param $userId
  213. * @return array
  214. */
  215. public function publish($userId){
  216. $params = request()->all();
  217. $content = isset($params['content'])? htmlspecialchars($params['content']) : '';
  218. $albums = isset($params['fileList'])? $params['fileList'] : [];
  219. $sourceId = isset($params['source_id'])? intval($params['source_id']) : 0;
  220. if($sourceId && !ArticleModel::where(['id'=> $sourceId, 'mark'=> 1,'status'=> 1])->value('id')){
  221. return message('抱歉,当前文章状态不可分享到动态', false);
  222. }
  223. // 验证用户
  224. $memberInfo = MemberModel::where(['id'=> $userId, 'mark'=> 1,'status'=> 1])
  225. ->select(['id','openid','nickname'])
  226. ->first();
  227. if(!$memberInfo){
  228. return message('账户不可操作或已冻结,请联系客服', false);
  229. }
  230. $confirm = ConfigService::make()->getConfigByCode('dynamic_confirm');
  231. $confirm = intval($confirm)? intval($confirm) : 2;
  232. $data = [
  233. 'user_id'=> $userId,
  234. 'source_id'=> $sourceId,
  235. 'comment_close'=> isset($params['comment_close']) && $params['comment_close']? 1 : 2,
  236. 'content'=> $content,
  237. 'albums'=> $albums? json_encode($albums, 256) : '',
  238. 'create_time'=> time(),
  239. 'update_time'=> time(),
  240. 'status'=> $confirm,
  241. ];
  242. if($id = $this->model::insertGetId($data)){
  243. return message('发布成功', true, ['id'=> $id]);
  244. }
  245. return message('发布失败', false);
  246. }
  247. }