Community.php 14 KB

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