CommunityRepository.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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\common\repositories\community;
  12. use app\common\dao\community\CommunityDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\order\StoreOrderProductRepository;
  15. use app\common\repositories\store\product\SpuRepository;
  16. use app\common\repositories\system\RelevanceRepository;
  17. use app\common\repositories\user\UserBrokerageRepository;
  18. use app\common\repositories\user\UserRepository;
  19. use crmeb\services\QrcodeService;
  20. use FormBuilder\Factory\Elm;
  21. use think\exception\ValidateException;
  22. use think\facade\Db;
  23. use think\facade\Route;
  24. class CommunityRepository extends BaseRepository
  25. {
  26. /**
  27. * @var CommunityDao
  28. */
  29. protected $dao;
  30. const IS_SHOW_WHERE = [
  31. 'is_show' => 1,
  32. 'status' => 1,
  33. 'is_del' => 0,
  34. ];
  35. public const COMMUNIT_TYPE_FONT = '1';
  36. public const COMMUNIT_TYPE_VIDEO = '2';
  37. /**
  38. * CommunityRepository constructor.
  39. * @param CommunityDao $dao
  40. */
  41. public function __construct(CommunityDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. public function title(array $where)
  46. {
  47. $where['is_type'] = self::COMMUNIT_TYPE_FONT;
  48. $list[] = [
  49. 'count' => $this->dao->search($where)->count(),
  50. 'title' => '图文列表',
  51. 'type' => self::COMMUNIT_TYPE_FONT,
  52. ];
  53. $where['is_type'] = self::COMMUNIT_TYPE_VIDEO;
  54. $list[] = [
  55. 'count' => $this->dao->search($where)->count(),
  56. 'title' => '短视频列表',
  57. 'type' => self::COMMUNIT_TYPE_VIDEO,
  58. ];
  59. return $list;
  60. }
  61. public function getList(array $where, int $page, int $limit)
  62. {
  63. $query = $this->dao->search($where)->with([
  64. 'author' => function($query) {
  65. $query->field('uid,real_name,status,avatar,nickname,count_start');
  66. },
  67. 'topic' => function($query) {
  68. $query->where('status', 1)->where('is_del',0);
  69. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  70. },
  71. 'category'
  72. ]);
  73. $count = $query->count();
  74. $list = $query->page($page, $limit)->select();
  75. return compact('count','list');
  76. }
  77. public function getApiList(array $where, int $page, int $limit, $userInfo)
  78. {
  79. $config = systemConfig("community_app_switch");
  80. if (!isset($where['is_type']) && $config) $where['is_type'] = $config;
  81. $where['is_del'] = 0;
  82. $query = $this->dao->search($where)->order('start DESC,Community.create_time DESC,community_id DESC');
  83. $query->with([
  84. 'author' => function($query) use($userInfo){
  85. $query->field('uid,real_name,status,avatar,nickname,count_start');
  86. },
  87. 'is_start' => function($query) use ($userInfo) {
  88. $query->where('left_id',$userInfo->uid ?? null);
  89. },
  90. 'topic' => function($query) {
  91. $query->where('status', 1)->where('is_del',0);
  92. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  93. },
  94. 'relevance' => [
  95. 'spu' => function($query) {
  96. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
  97. }
  98. ],
  99. 'is_fans' => function($query) use($userInfo){
  100. $query->where('left_id',$userInfo->uid?? 0);
  101. }
  102. ]);
  103. $count = $query->count();
  104. $list = $query->page($page, $limit)->setOption('field',[])
  105. ->field('community_id,title,image,topic_id,Community.count_start,count_reply,start,Community.create_time,Community.uid,Community.status,is_show,content,video_link,is_type,refusal')
  106. ->select()->append(['time']);
  107. if ($list) $list = $list->toArray();
  108. return compact('count','list');
  109. }
  110. public function getFirtVideo($where,$page, $userInfo)
  111. {
  112. $with =[];
  113. if ($page == 1) {
  114. $with = [
  115. 'author' => function($query) {
  116. $query->field('uid,real_name,status,avatar,nickname,count_start');
  117. },
  118. 'is_start' => function($query) use ($userInfo) {
  119. $query->where('left_id',$userInfo->uid ?? null);
  120. },
  121. 'topic' => function($query) {
  122. $query->where('status', 1)->where('is_del',0);
  123. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  124. },
  125. 'relevance' => [
  126. 'spu' => function($query) {
  127. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id,status');
  128. }
  129. ],
  130. 'is_fans' => function($query) use($userInfo){
  131. $query->where('left_id',$userInfo->uid?? 0);
  132. }
  133. ];
  134. }
  135. return $this->dao->getSearch($where)->with($with)->field('community_id,image,title,topic_id,count_start,count_reply,start,create_time,uid,status,is_show,content,video_link,is_type,refusal')->find();
  136. }
  137. public function getApiVideoList(array $where, int $page, int $limit, $userInfo, $type = 0)
  138. {
  139. $where['is_type'] = self::COMMUNIT_TYPE_VIDEO;
  140. $first = $this->getFirtVideo($where,$page, $userInfo);
  141. if ($type) { // 点赞过的内容
  142. $where['uid'] = $userInfo->uid;
  143. $where['community_ids'] = $this->dao->joinUser($where)->column('community_id');
  144. } else { // 条件视频
  145. if (!isset($where['uid']) && $first) $where['topic_id'] = $first['topic_id'];
  146. }
  147. $where['not_id'] = $where['community_id'];
  148. unset($where['community_id']);
  149. $data = $this->getApiList($where, $page, $limit, $userInfo);
  150. if (empty($data['list']) && isset($where['topic_id'])) {
  151. unset($where['topic_id']);
  152. $data = $this->getApiList($where, $page, $limit, $userInfo);
  153. }
  154. if ($page == 1 && $first) {
  155. array_unshift($data['list'],$first->toArray());
  156. }
  157. return $data;
  158. }
  159. /**
  160. * TODO 后台详情
  161. * @param int $id
  162. * @return array|\think\Model|null
  163. * @author Qinii
  164. * @day 10/28/21
  165. */
  166. public function detail(int $id)
  167. {
  168. $where = [
  169. $this->dao->getPk() => $id,
  170. 'is_del' => 0
  171. ];
  172. $config = systemConfig("community_app_switch");
  173. if ($config) $where['is_type'] = $config;
  174. return $this->dao->getSearch($where)->with([
  175. 'author' => function($query) {
  176. $query->field('uid,real_name,status,avatar,nickname,count_start');
  177. },
  178. 'topic',
  179. 'category',
  180. 'relevance.spu'
  181. ])->find();
  182. }
  183. /**
  184. * TODO 移动端详情展示
  185. * @param int $id
  186. * @param $user
  187. * @return array|\think\Model|null
  188. * @author Qinii
  189. * @day 10/27/21
  190. */
  191. public function show(int $id, $user)
  192. {
  193. $where = self::IS_SHOW_WHERE;
  194. $is_author = 0;
  195. if ($user && $this->dao->uidExists($id, $user->uid)) {
  196. $where = ['is_del' => 0];
  197. $is_author = 1;
  198. }
  199. $config = systemConfig("community_app_switch");
  200. if ($config) $where['is_type'] = $config;
  201. $where[$this->dao->getPk()] = $id;
  202. $data = $this->dao->getSearch($where)
  203. ->with([
  204. 'author' => function ($query) {
  205. $query->field('uid,real_name,status,avatar,nickname,count_start,member_level');
  206. if (systemConfig('member_status')) $query->with(['member' => function ($query) {
  207. $query->field('brokerage_icon,brokerage_level');
  208. }]);
  209. },
  210. 'relevance' => [
  211. 'spu' => function ($query) {
  212. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
  213. }
  214. ],
  215. 'topic' => function ($query) {
  216. $query->where('status', 1)->where('is_del', 0);
  217. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  218. },
  219. 'is_start' => function ($query) use ($user) {
  220. $query->where('left_id', $user->uid ?? '');
  221. },
  222. ])->hidden(['is_del'])->find();
  223. if (!$data) throw new ValidateException('内容不存在,可能已被删除了哦~');
  224. $data['is_author'] = $is_author;
  225. $is_fans = 0;
  226. if ($user && !$data['is_author'])
  227. $is_fans = app()->make(RelevanceRepository::class)->getWhereCount([
  228. 'left_id' => $user->uid,
  229. 'right_id' => $data['uid'],
  230. 'type' => RelevanceRepository::TYPE_COMMUNITY_FANS,
  231. ]);
  232. $data['is_fans'] = $is_fans;
  233. return $data;
  234. }
  235. public function getSpuByOrder($id, $uid)
  236. {
  237. $where = app()->make(StoreOrderProductRepository::class)->selectWhere(['order_id' => $id]);
  238. if (!$where) throw new ValidateException('商品已下架');
  239. $make = app()->make(SpuRepository::class);
  240. foreach ($where as $item) {
  241. switch ($item['product_type']){
  242. case 0:
  243. $id = $item['product_id'];
  244. // nobreak;
  245. case 1:
  246. $id = $item['product_id'];
  247. break;
  248. case 2:
  249. $id = $item['activity_id'];
  250. break;
  251. case 3:
  252. $id = $item['cart_info']['productAssistSet']['product_assist_id'];
  253. break;
  254. case 4:
  255. $id = $item['cart_info']['product']['productGroup']['product_group_id'];
  256. break;
  257. }
  258. $data[] = $make->getSpuData($id, $item['product_type'],0);
  259. }
  260. return $data;
  261. }
  262. /**
  263. * TODO 创建
  264. * @param array $data
  265. * @author Qinii
  266. * @day 10/29/21
  267. */
  268. public function create(array $data)
  269. {
  270. event('community.create.before',compact('data'));
  271. if ($data['topic_id']) {
  272. $getTopic = app()->make(CommunityTopicRepository::class)->get($data['topic_id']);
  273. if (!$getTopic || !$getTopic->status) throw new ValidateException('话题不存在或已关闭');
  274. $data['category_id'] = $getTopic->category_id;
  275. }
  276. return Db::transaction(function () use($data) {
  277. $community = $this->dao->create($data);
  278. if ($data['spu_id'])$this->joinProduct($community->community_id,$data['spu_id']);
  279. event('community.create',compact('community'));
  280. return $community->community_id;
  281. });
  282. }
  283. /**
  284. * TODO 编辑
  285. * @param int $id
  286. * @param array $data
  287. * @author Qinii
  288. * @day 10/29/21
  289. */
  290. public function edit(int $id, array $data)
  291. {
  292. event('community.update.before',compact('id','data'));
  293. if ($data['topic_id']) {
  294. $getTopic = app()->make(CommunityTopicRepository::class)->get($data['topic_id']);
  295. if (!$getTopic || !$getTopic->status) throw new ValidateException('话题不存在或已关闭');
  296. $data['category_id'] = $getTopic->category_id;
  297. }
  298. Db::transaction(function () use($id, $data) {
  299. $spuId = $data['spu_id'];
  300. unset($data['spu_id']);
  301. $community = $this->dao->update($id, $data);
  302. if ($spuId) $this->joinProduct($id, $spuId);
  303. event('community.update.before',compact('id','community'));
  304. });
  305. }
  306. public function joinProduct($id, array $data)
  307. {
  308. $make = app()->make(RelevanceRepository::class);
  309. $data = array_unique($data);
  310. $res = [];
  311. foreach ($data as $value) {
  312. if ($value) {
  313. $res[] = [
  314. 'left_id' => $id,
  315. 'right_id' => $value,
  316. 'type' => RelevanceRepository::TYPE_COMMUNITY_PRODUCT
  317. ];
  318. }
  319. }
  320. $make->clear($id,RelevanceRepository::TYPE_COMMUNITY_PRODUCT,'left_id');
  321. if($res) $make->insertAll($res);
  322. }
  323. /**
  324. * TODO 获取某用户信息
  325. * @param int $uid
  326. * @param null $self
  327. * @return mixed
  328. * @author Qinii
  329. * @day 10/29/21
  330. */
  331. public function getUserInfo(int $uid, $self = null)
  332. {
  333. $relevanceRepository = app()->make(RelevanceRepository::class);
  334. $data['focus'] = $relevanceRepository->getFieldCount('left_id', $uid,RelevanceRepository::TYPE_COMMUNITY_FANS);
  335. $is_start = $is_self = false;
  336. if ($self && $self->uid == $uid) {
  337. $user = $self;
  338. $is_self = true;
  339. } else {
  340. $user = app()->make(UserRepository::class)->get($uid);
  341. $is_start = $relevanceRepository->checkHas($self->uid, $uid, RelevanceRepository::TYPE_COMMUNITY_FANS) > 0;
  342. }
  343. $data['start'] = $user->count_start;
  344. $data['uid'] = $user->uid;
  345. $data['avatar'] = $user->avatar;
  346. $data['nickname'] = $user->nickname;
  347. $data['is_start'] = $is_start;
  348. $data['member_icon'] = systemConfig('member_status') ? ($user->member->brokerage_icon ?? '') : '';
  349. $data['is_self'] = $is_self;
  350. $data['fans'] = $user->count_fans;
  351. return $data;
  352. }
  353. public function setFocus(int $id, int $uid,int $status)
  354. {
  355. $make = app()->make(RelevanceRepository::class);
  356. $check = $make->checkHas($uid, $id, RelevanceRepository::TYPE_COMMUNITY_FANS);
  357. if ($status) {
  358. if ($check) throw new ValidateException('您已经关注过他了~');
  359. $make->create($uid, $id,RelevanceRepository::TYPE_COMMUNITY_FANS,true);
  360. app()->make(UserRepository::class)->incField($id, 'count_fans', 1);
  361. } else {
  362. if (!$check) throw new ValidateException('您还未关注他哦~');
  363. $make->destory($uid, $id,RelevanceRepository::TYPE_COMMUNITY_FANS);
  364. app()->make(UserRepository::class)->decField($id, 'count_fans', 1);
  365. }
  366. return ;
  367. }
  368. public function form($id)
  369. {
  370. $form = Elm::createForm(Route::buildUrl('systemCommunityUpdate', ['id' => $id])->build());
  371. $data = $this->dao->get($id);
  372. if (!$data) throw new ValidateException('数据不存在');
  373. $formData = $data->toArray();
  374. return $form->setRule([
  375. Elm::rate('start', '排序星级')->max(5)
  376. ])->setTitle('编辑星级')->formData($formData);
  377. }
  378. public function showForm($id)
  379. {
  380. $form = Elm::createForm(Route::buildUrl('systemCommunityStatus', ['id' => $id])->build());
  381. $data = $this->dao->get($id);
  382. if (!$data) throw new ValidateException('数据不存在');
  383. $formData = $data->toArray();
  384. return $form->setRule([
  385. Elm::radio('status', '强制下架')->options([
  386. ['value' => -2, 'label' => '下架'], ['value' => 1, 'label' => '上架']])->control([
  387. ['value' => -2, 'rule' => [
  388. Elm::textarea('refusal', '下架理由', '信息存在违规')->required()
  389. ]]
  390. ]),
  391. ])->setTitle('强制下架')->formData($formData);
  392. }
  393. public function setCommunityStart(int $id, $userInfo, int $status)
  394. {
  395. $make = app()->make(RelevanceRepository::class);
  396. $userRepository = app()->make(UserRepository::class);
  397. if ($status) {
  398. $res = $make->create($userInfo->uid, $id, RelevanceRepository::TYPE_COMMUNITY_START,true);
  399. if (!$res) throw new ValidateException('您已经点赞过了');
  400. $ret = $this->dao->get($id);
  401. $user = $userRepository->get($ret['uid']);
  402. $this->dao->incField($id,'count_start',1);
  403. if ($user) $userRepository->incField((int)$user->uid,'count_start',1);
  404. }
  405. if (!$status) {
  406. if (!$make->checkHas($userInfo->uid, $id, RelevanceRepository::TYPE_COMMUNITY_START))
  407. throw new ValidateException('您还没有点赞呢~');
  408. $make->destory($userInfo->uid, $id, RelevanceRepository::TYPE_COMMUNITY_START);
  409. $ret = $this->dao->get($id);
  410. $user = $userRepository->get($ret['uid']);
  411. $this->dao->decField($id,'count_start',1);
  412. if ($user) $userRepository->decField((int)$user->uid, 'count_start',1);
  413. }
  414. }
  415. public function setStatus($id, $data)
  416. {
  417. $ret = $this->dao->get($id);
  418. event('community.status.before',compact('id','data'));
  419. Db::transaction(function () use($ret,$id, $data) {
  420. if ($data['status'] == 1) {
  421. $make = app()->make(UserBrokerageRepository::class);
  422. $make->incMemberValue($ret['uid'], 'member_community_num', $id);
  423. }
  424. $data['status_time'] = date('Y-m-d H:i;s', time());
  425. $this->dao->update($id, $data);
  426. event('community.status',compact('id'));
  427. });
  428. }
  429. public function destory($id, $user = null)
  430. {
  431. event('community.delete.before',compact('id','user'));
  432. $this->dao->update($id, ['is_del' => 1]);
  433. event('community.delete',compact('id', 'user'));
  434. }
  435. public function getDataBySpu($spuId)
  436. {
  437. $where = array_merge(['spu_id' => $spuId], self::IS_SHOW_WHERE);
  438. return $this->dao->getSearch($where)
  439. ->field('community_id,title,image')
  440. ->limit(3)->select();
  441. }
  442. public function qrcode($id, $type,$user)
  443. {
  444. $res = $this->dao->search(['is_type' => self::COMMUNIT_TYPE_VIDEO,'community_id' => $id, 'status' => 1, 'is_show' => 1])->find();
  445. if (!$res) return false;
  446. $make = app()->make(QrcodeService::class);
  447. if ($type == 'routine') {
  448. $name = md5('rcwx' . $id . $type . $user->uid . $user['is_promoter'] . date('Ymd')) . '.jpg';
  449. $params = 'id=' . $id . '&spid=' . $user['uid'];
  450. $link = '/pages/short_video/nvueSwiper/index?id=';
  451. $make->getRoutineQrcodePath($name, $link, $params);
  452. } else {
  453. $name = md5('cwx' . $id . $type . $user->uid . $user['is_promoter'] . date('Ymd')) . '.jpg';
  454. $link = 'pages/short_video/nvueSwiper/index';
  455. $link = $link . '?id=' . $id . '&spid=' . $user['uid'];
  456. $key = 'com' . $type . '_' . $id . '_' . $user['uid'];
  457. return $make->getWechatQrcodePath($name, $link, false, $key);
  458. }
  459. }
  460. }