Community.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\community;
  12. use app\common\repositories\community\CommunityRepository;
  13. use app\common\repositories\store\order\StoreOrderProductRepository;
  14. use app\common\repositories\system\RelevanceRepository;
  15. use app\common\repositories\user\UserHistoryRepository;
  16. use app\common\repositories\user\UserRelationRepository;
  17. use app\common\repositories\user\UserRepository;
  18. use app\validate\api\CommunityValidate;
  19. use crmeb\basic\BaseController;
  20. use crmeb\services\MiniProgramService;
  21. use think\App;
  22. use app\common\repositories\community\CommunityRepository as repository;
  23. use think\exception\ValidateException;
  24. /**
  25. * Class Community
  26. * app\controller\api\community
  27. * 逛逛社区
  28. */
  29. class Community extends BaseController
  30. {
  31. /**
  32. * @var CommunityRepository
  33. */
  34. protected $repository;
  35. protected $user;
  36. /**
  37. * User constructor.
  38. * @param App $app
  39. * @param $repository
  40. */
  41. public function __construct(App $app, repository $repository)
  42. {
  43. parent::__construct($app);
  44. $this->repository = $repository;
  45. $this->user = $this->request->isLogin() ? $this->request->userInfo() : null;
  46. if (!systemConfig('community_status') ) throw new ValidateException('未开启社区功能');
  47. }
  48. /**
  49. * 文章列表
  50. * @return \think\response\Json
  51. * @author Qinii
  52. * @day 10/29/21
  53. */
  54. public function lst()
  55. {
  56. $where = $this->request->params(['keyword','topic_id','is_hot','category_id','spu_id','search_type']);
  57. if (!$where['category_id']) {
  58. unset($where['category_id']);
  59. } else if ($where['category_id'] == -1) {
  60. $where['is_type'] = $this->repository::COMMUNIT_TYPE_VIDEO;
  61. unset($where['category_id']);
  62. }
  63. $where = array_merge($where,$this->repository::IS_SHOW_WHERE);
  64. [$page, $limit] = $this->getPage();
  65. return app('json')->success($this->repository->getApiList($where, $page, $limit, $this->user));
  66. }
  67. /**
  68. * 用户列表
  69. * @return \think\response\Json
  70. * @author Qinii
  71. * @day 10/29/21
  72. */
  73. public function userList()
  74. {
  75. $where = $this->request->params(['keyword']);
  76. [$page, $limit] = $this->getPage();
  77. $userRepository = app()->make(UserRepository::class);
  78. return app('json')->success($userRepository->getCommunityUserList($where, $page, $limit));
  79. }
  80. /**
  81. * 视频列表
  82. * @return \think\response\Json
  83. * @author Qinii
  84. * @day 2022/11/29
  85. */
  86. public function videoShow()
  87. {
  88. [$page, $limit] = $this->getPage();
  89. $where = $this->repository::IS_SHOW_WHERE;
  90. $where['community_id'] = $this->request->param('id','');
  91. return app('json')->success($this->repository->getApiVideoList($where, $page, $limit, $this->user));
  92. }
  93. /**
  94. * 关注的人的文章
  95. * @param RelevanceRepository $relevanceRepository
  96. * @return \think\response\Json
  97. * @author Qinii
  98. * @day 11/2/21
  99. */
  100. public function focuslst(RelevanceRepository $relevanceRepository)
  101. {
  102. $where = $this->repository::IS_SHOW_WHERE;
  103. $where_ = [
  104. 'left_id' => $this->user->uid ?? null ,
  105. 'type' => RelevanceRepository::TYPE_COMMUNITY_FANS,
  106. ];
  107. $where['uids'] = $relevanceRepository->getSearch($where_)->column('right_id');
  108. [$page, $limit] = $this->getPage();
  109. $type = $this->request->param('type',0);
  110. if ($type) {
  111. $where['is_type'] = $this->repository::COMMUNIT_TYPE_VIDEO;
  112. }
  113. return app('json')->success($this->repository->getApiList($where, $page, $limit, $this->user));
  114. }
  115. /**
  116. * 某个用户的文章
  117. * @param $id
  118. * @return \think\response\Json
  119. * @author Qinii
  120. * @day 10/29/21
  121. */
  122. public function userCommunitylst($id)
  123. {
  124. $where = [];
  125. if (!$this->user || $this->user->uid != $id) {
  126. $where = $this->repository::IS_SHOW_WHERE;
  127. }
  128. $where['uid'] = $id;
  129. [$page, $limit] = $this->getPage();
  130. return app('json')->success($this->repository->getApiList($where, $page, $limit, $this->user));
  131. }
  132. /**
  133. * 某个用户的视频
  134. * @param $id
  135. * @return \think\response\Json
  136. * @author Qinii
  137. * @day 10/29/21
  138. */
  139. public function userCommunityVideolst($id)
  140. {
  141. $where = [];
  142. [$page, $limit] = $this->getPage();
  143. $is_start = $this->request->param('is_star',0);
  144. if ($is_start) {
  145. //某人赞过的视频
  146. $where = $this->repository::IS_SHOW_WHERE;
  147. } else {
  148. //某个人的视频
  149. if (!$this->user || $this->user->uid != $id) {
  150. $where =$this->repository::IS_SHOW_WHERE;
  151. }
  152. $where['uid'] = $id;
  153. }
  154. $where['is_del'] = 0;
  155. $where['community_id'] = $this->request->param('community_id','');
  156. $data = $this->repository->getApiVideoList($where, $page, $limit, $this->user,$is_start);
  157. return app('json')->success($data);
  158. }
  159. /**
  160. * 我赞过的文章
  161. * @param RelevanceRepository $relevanceRepository
  162. * @return \think\response\Json
  163. * @author Qinii
  164. * @day 10/28/21
  165. */
  166. public function getUserStartCommunity(RelevanceRepository $relevanceRepository)
  167. {
  168. [$page, $limit] = $this->getPage();
  169. $where['uid'] = $this->user->uid;
  170. $data = $relevanceRepository->getUserStartCommunity($where,$page, $limit);
  171. return app('json')->success($data);
  172. }
  173. /**
  174. * 文章详情
  175. * @param $id
  176. * @return mixed
  177. * @author Qinii
  178. */
  179. public function show($id)
  180. {
  181. return app('json')->success($this->repository->show((int)$id, $this->user));
  182. }
  183. /**
  184. * 已购商品
  185. * @return \think\response\Json
  186. * @author Qinii
  187. * @day 10/28/21
  188. */
  189. public function payList()
  190. {
  191. [$page, $limit] = $this->getPage();
  192. $keyword = $this->request->param('keyword');
  193. $data = app()->make(StoreOrderProductRepository::class)->getUserPayProduct($keyword, $this->user->uid, $page, $limit);
  194. return app('json')->success($data);
  195. }
  196. /**
  197. * 收藏商品
  198. * @return \think\response\Json
  199. * @author Qinii
  200. * @day 10/28/21
  201. */
  202. public function relationList()
  203. {
  204. [$page, $limit] = $this->getPage();
  205. $keyword = $this->request->param('keyword');
  206. $data = app()->make(UserRelationRepository::class)->getUserProductToCommunity($keyword, $this->user->uid, $page, $limit);
  207. return app('json')->success($data);
  208. }
  209. /**
  210. * 历史记录
  211. * @return \think\response\Json
  212. * @author Qinii
  213. * @day 10/28/21
  214. */
  215. public function historyList()
  216. {
  217. [$page, $limit] = $this->getPage();
  218. $where['keyword'] = $this->request->param('keyword');
  219. $where['uid'] = $this->request->userInfo()->uid;
  220. $where['type'] = 1;
  221. $data = app()->make(UserHistoryRepository::class)->historyLst($where, $page,$limit);
  222. return app('json')->success($data);
  223. }
  224. /**
  225. * 发布文章
  226. * @return \think\response\Json
  227. * @author Qinii
  228. * @day 10/29/21
  229. */
  230. public function create()
  231. {
  232. $data = $this->checkParams();
  233. $this->checkUserAuth();
  234. $data['uid'] = $this->request->uid();
  235. $res = $this->repository->create($data);
  236. return app('json')->success(['community_id' => $res]);
  237. }
  238. /**
  239. * 检测用户是否可以发贴
  240. * @return bool|\think\response\Json
  241. * @author Qinii
  242. * @day 10/30/21
  243. */
  244. public function checkUserAuth()
  245. {
  246. $user = $this->request->userInfo();
  247. if ( systemConfig('community_auth') ) {
  248. if ($user->phone) {
  249. return true;
  250. }
  251. throw new ValidateException('请先绑定您的手机号');
  252. } else {
  253. return true;
  254. }
  255. }
  256. /**
  257. * 编辑
  258. * @param $id
  259. * @return \think\response\Json
  260. * @author Qinii
  261. * @day 10/29/21
  262. */
  263. public function update($id)
  264. {
  265. $data = $this->checkParams();
  266. $this->checkUserAuth();
  267. if(!$this->repository->uidExists($id, $this->user->uid))
  268. return app('json')->success('内容不存在或不属于您');
  269. $this->repository->edit($id, $data);
  270. return app('json')->success(['community_id' => $id]);
  271. }
  272. /**
  273. * 验证数据
  274. * @param $id
  275. * @return \think\response\Json
  276. * @author Qinii
  277. * @day 10/29/21
  278. */
  279. public function checkParams()
  280. {
  281. $data = $this->request->params(['image','topic_id','content','spu_id','order_id',['is_type',1],'video_link']);
  282. $config = systemConfig(["community_app_switch",'community_audit','community_video_audit']);
  283. $data['status'] = 0;
  284. $data['is_show'] = 0;
  285. if ($data['is_type'] == 1) {
  286. if (!in_array($this->repository::COMMUNIT_TYPE_FONT,$config['community_app_switch']))
  287. throw new ValidateException('社区图文未开启');
  288. if ($config['community_audit']) {
  289. $data['status'] = 1;
  290. $data['is_show'] = 1;
  291. $data['status_time'] = date('Y-m-d H:i:s', time());
  292. }
  293. } else {
  294. if (!in_array($this->repository::COMMUNIT_TYPE_VIDEO,$config['community_app_switch']))
  295. throw new ValidateException('短视频未开启');
  296. if ($config['community_video_audit']) {
  297. $data['status'] = 1;
  298. $data['is_show'] = 1;
  299. $data['status_time'] = date('Y-m-d H:i:s', time());
  300. }
  301. if (!$data['video_link']) throw new ValidateException('请上传视频');
  302. }
  303. $data['content'] = filter_emoji($data['content']);
  304. MiniProgramService::create()->msgSecCheck($this->request->userInfo(), $data['content'],3,0);
  305. app()->make(CommunityValidate::class)->check($data);
  306. $arr = explode("\n", $data['content']);
  307. $title = rtrim(ltrim($arr[0]));
  308. if (mb_strlen($title) > 40 ){
  309. $data['title'] = mb_substr($title,0,30,'utf-8');
  310. } else {
  311. $data['title'] = $title;
  312. }
  313. if ($data['image']) $data['image'] = implode(',',$data['image']);
  314. return $data;
  315. }
  316. /**
  317. * 删除
  318. * @param $id
  319. * @return mixed
  320. * @author Qinii
  321. */
  322. public function delete($id)
  323. {
  324. if (!$this->repository->uidExists($id, $this->user->uid))
  325. return app('json')->fail('内容不存在或不属于您');
  326. $this->repository->destory($id, $this->user);
  327. return app('json')->success('删除成功');
  328. }
  329. /**
  330. * 文章点赞/取消
  331. * @param $id
  332. * @param RelevanceRepository $relevanceRepository
  333. * @return \think\response\Json
  334. * @author Qinii
  335. * @day 10/28/21
  336. */
  337. public function startCommunity($id)
  338. {
  339. $status = $this->request->param('status') == 1 ? 1 :0;
  340. if (!$this->repository->isApprove($id))
  341. return app('json')->fail('内容不存在或未审核通过,不能点赞');
  342. $this->repository->setCommunityStart($id, $this->user, $status);
  343. if ($status) {
  344. return app('json')->success('点赞成功');
  345. } else {
  346. return app('json')->success('取消点赞');
  347. }
  348. }
  349. /**
  350. * 用户关注/取消
  351. * @param $id
  352. * @param RelevanceRepository $relevanceRepository
  353. * @return \think\response\Json
  354. * @author Qinii
  355. * @day 10/28/21
  356. */
  357. public function setFocus($id)
  358. {
  359. $id = (int)$id;
  360. $status = $this->request->param('status') == 1 ? 1 :0;
  361. if ($this->user->uid == $id)
  362. return app('json')->fail('请勿关注自己');
  363. $make = app()->make(UserRepository::class);
  364. if (!$user = $make->get($id)) return app('json')->fail('未查询到该用户');
  365. $this->repository->setFocus($id, $this->user->uid, $status);
  366. if ($status) {
  367. return app('json')->success('关注成功');
  368. } else {
  369. return app('json')->success('取消关注');
  370. }
  371. }
  372. /**
  373. * 我的粉丝
  374. * @param RelevanceRepository $relevanceRepository
  375. * @return \think\response\Json
  376. * @author Qinii
  377. * @day 10/28/21
  378. */
  379. public function getUserFans(RelevanceRepository $relevanceRepository)
  380. {
  381. [$page, $limit] = $this->getPage();
  382. $fans = $relevanceRepository->getUserFans($this->user->uid, $page, $limit);
  383. return app('json')->success($fans);
  384. }
  385. /**
  386. * 我的关注
  387. * @param RelevanceRepository $relevanceRepository
  388. * @return \think\response\Json
  389. * @author Qinii
  390. * @day 10/28/21
  391. */
  392. public function getUserFocus(RelevanceRepository $relevanceRepository)
  393. {
  394. [$page, $limit] = $this->getPage();
  395. $start = $relevanceRepository->getUserFocus($this->user->uid, $page, $limit);
  396. return app('json')->success($start);
  397. }
  398. /**
  399. * 用户信息
  400. * @param $id
  401. * @return \think\response\Json
  402. * @author Qinii
  403. * @day 10/28/21
  404. */
  405. public function userInfo($id)
  406. {
  407. if (!$id) return app('json')->fail('缺少参数');
  408. $data = $this->repository->getUserInfo($id, $this->user);
  409. return app('json')->success($data);
  410. }
  411. /**
  412. * 根据订单获取商品
  413. * @param $id
  414. * @return \think\response\Json
  415. * @author Qinii
  416. */
  417. public function getSpuByOrder($id)
  418. {
  419. $data = $this->repository->getSpuByOrder($id);
  420. return app('json')->success($data);
  421. }
  422. /**
  423. * 生成分享二维码
  424. * @param $id
  425. * @return \think\response\Json
  426. * @author Qinii
  427. */
  428. public function qrcode($id)
  429. {
  430. $id = (int)$id;
  431. $type = $this->request->param('type');
  432. $url = $this->repository->qrcode($id, $type, $this->user);
  433. if (!$url) return app('json')->fail('二维码生成失败');
  434. return app('json')->success(compact('url'));
  435. }
  436. }