CommunityRepository.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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\common\repositories\community;
  12. use app\common\dao\community\CommunityDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use app\common\repositories\store\order\StoreOrderProductRepository;
  16. use app\common\repositories\store\product\SpuRepository;
  17. use app\common\repositories\system\RelevanceRepository;
  18. use app\common\repositories\user\UserBrokerageRepository;
  19. use app\common\repositories\user\UserRepository;
  20. use crmeb\services\QrcodeService;
  21. use FormBuilder\Factory\Elm;
  22. use app\common\repositories\user\UserBillRepository;
  23. use think\exception\ValidateException;
  24. use think\facade\Cache;
  25. use think\facade\Db;
  26. use think\facade\Route;
  27. /**
  28. * 社区图文
  29. */
  30. class CommunityRepository extends BaseRepository
  31. {
  32. /**
  33. * @var CommunityDao
  34. */
  35. protected $dao;
  36. const IS_SHOW_WHERE = [
  37. 'is_show' => 1,
  38. 'status' => 1,
  39. 'is_del' => 0,
  40. ];
  41. public const COMMUNIT_TYPE_FONT = '1';
  42. public const COMMUNIT_TYPE_VIDEO = '2';
  43. /**
  44. * CommunityRepository constructor.
  45. * @param CommunityDao $dao
  46. */
  47. public function __construct(CommunityDao $dao)
  48. {
  49. $this->dao = $dao;
  50. }
  51. /**
  52. * 后台列表头部统计
  53. * @param array $where
  54. * @return array
  55. * @author Qinii
  56. */
  57. public function title(array $where)
  58. {
  59. $where['is_type'] = self::COMMUNIT_TYPE_FONT;
  60. $list[] = [
  61. 'count' => $this->dao->search($where)->count(),
  62. 'title' => '图文列表',
  63. 'type' => self::COMMUNIT_TYPE_FONT,
  64. ];
  65. $where['is_type'] = self::COMMUNIT_TYPE_VIDEO;
  66. $list[] = [
  67. 'count' => $this->dao->search($where)->count(),
  68. 'title' => '短视频列表',
  69. 'type' => self::COMMUNIT_TYPE_VIDEO,
  70. ];
  71. return $list;
  72. }
  73. /**
  74. * 获取列表数据
  75. * 根据给定的条件、分页和限制获取数据列表,包括作者、主题和分类信息。
  76. *
  77. * @param array $where 查询条件
  78. * @param int $page 当前页码
  79. * @param int $limit 每页数据数量
  80. * @return array 返回包含总数和列表数据的数组
  81. */
  82. public function getList(array $where, int $page, int $limit)
  83. {
  84. // 根据条件查询数据,并包含关联信息:作者、主题和分类
  85. $query = $this->dao->search($where)->with([
  86. 'author' => function ($query) {
  87. // 选择作者的相关字段,包括uid, real_name, status, avatar, nickname, count_start
  88. $query->field('uid,real_name,status,avatar,nickname,count_start');
  89. },
  90. 'topic' => function ($query) {
  91. // 筛选主题的状态为1,且未删除的数据,选择特定字段
  92. $query->where('status', 1)->where('is_del', 0);
  93. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  94. },
  95. 'category' // 包含分类信息,没有指定字段,则默认包含所有字段
  96. ]);
  97. // 计算满足条件的数据总数
  98. $count = $query->count();
  99. // 根据当前页码和每页数据数量进行分页查询,并获取数据列表
  100. $list = $query->page($page, $limit)->select();
  101. // 返回包含数据总数和数据列表的数组
  102. return compact('count', 'list');
  103. }
  104. /**
  105. * 移动端列表
  106. * @param array $where
  107. * @param int $page
  108. * @param int $limit
  109. * @param $userInfo
  110. * @return array
  111. * @author Qinii
  112. */
  113. public function getApiList(array $where, int $page, int $limit, $userInfo)
  114. {
  115. $config = systemConfig("community_app_switch");
  116. if (!isset($where['is_type']) && $config) $where['is_type'] = $config;
  117. $where['is_del'] = 0;
  118. $query = $this->dao->search($where)->order('start DESC,Community.create_time DESC,community_id DESC');
  119. $query->with([
  120. 'author' => function ($query) use ($userInfo) {
  121. $query->field('uid,real_name,phone,status,avatar,nickname,count_start,count_fans,count_content');
  122. },
  123. 'is_start' => function ($query) use ($userInfo) {
  124. $query->where('left_id', $userInfo->uid ?? null);
  125. },
  126. 'topic' => function ($query) {
  127. $query->where('status', 1)->where('is_del', 0);
  128. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  129. },
  130. 'relevance' => [
  131. 'spu' => function ($query) {
  132. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
  133. }
  134. ],
  135. 'is_fanss' => function ($query) use ($userInfo) {
  136. $query->where('left_id', $userInfo->uid ?? 0);
  137. }
  138. ]);
  139. if (isset($where['search_type']) && $where['search_type'] == 'user') {
  140. $query->group('Community.uid');
  141. }
  142. $count = $query->count();
  143. $list = $query->page($page, $limit)->setOption('field', [])
  144. ->field('community_id,title,image,topic_id,Community.count_start,count_reply,start,Community.create_time,Community.uid,Community.status,Community.pv,is_show,content,video_link,is_type,refusal')
  145. ->select()->append(['time']);
  146. return compact('count', 'list');
  147. }
  148. /**
  149. * 视频下滑列表第一个视频
  150. * @param $community_id
  151. * @param $userInfo
  152. * @return array|mixed|\think\db\BaseQuery|\think\Model|null
  153. * @author Qinii
  154. */
  155. public function getFirst($community_id, $userInfo)
  156. {
  157. $where['is_del'] = 0;
  158. $where['community_id'] = $community_id;
  159. $info = $this->dao->search($where)
  160. ->with([
  161. 'author' => function ($query) use ($userInfo) {
  162. $query->field('uid,real_name,status,avatar,nickname,count_start');
  163. },
  164. 'is_start' => function ($query) use ($userInfo) {
  165. $query->where('left_id', $userInfo->uid ?? null);
  166. },
  167. 'topic' => function ($query) {
  168. $query->where('status', 1)->where('is_del', 0);
  169. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  170. },
  171. 'relevance' => [
  172. 'spu' => function ($query) {
  173. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
  174. }
  175. ],
  176. 'is_fanss' => function ($query) use ($userInfo) {
  177. $query->where('left_id', $userInfo->uid ?? 0);
  178. }
  179. ])
  180. ->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')
  181. ->find();
  182. if ($info) {
  183. $info = $info->append(['time']);
  184. }
  185. return $info;
  186. }
  187. /**
  188. * 视频列表
  189. * @param array $where
  190. * @param int $page
  191. * @param int $limit
  192. * @param $userInfo
  193. * @param $type
  194. * @return array
  195. * @author Qinii
  196. */
  197. public function getApiVideoList(array $where, int $page, int $limit, $userInfo, $type = 0)
  198. {
  199. $where['is_type'] = self::COMMUNIT_TYPE_VIDEO;
  200. $first = $this->getFirst($where['community_id'], $userInfo);
  201. if ($type) { // 点赞过的内容
  202. $where['uid'] = $userInfo->uid;
  203. $where['community_ids'] = $this->dao->joinUser($where)->column('community_id');
  204. } else { // 条件视频
  205. if (!isset($where['uid']) && $first) $where['topic_id'] = $first['topic_id'];
  206. }
  207. if ($first && $page == 1) {
  208. $where['not_id'] = $where['community_id'];
  209. $limit--;
  210. }
  211. unset($where['community_id']);
  212. $data = $this->getApiList($where, $page, $limit, $userInfo);
  213. if ($data['list']->isEmpty() && isset($where['topic_id'])) {
  214. unset($where['topic_id']);
  215. $data = $this->getApiList($where, $page, $limit, $userInfo);
  216. }
  217. if ($first && $page == 1) {
  218. $data['list']->unshift($first);
  219. $data['count']++;
  220. }
  221. return $data;
  222. }
  223. /**
  224. * 后台详情
  225. * @param int $id
  226. * @return array|\think\Model|null
  227. * @author Qinii
  228. * @day 10/28/21
  229. */
  230. public function detail(int $id)
  231. {
  232. $where = [
  233. $this->dao->getPk() => $id,
  234. 'is_del' => 0
  235. ];
  236. $config = systemConfig("community_app_switch");
  237. if ($config) $where['is_type'] = $config;
  238. $res = $this->dao->getSearch($where)->with([
  239. 'author' => function ($query) {$query->field('uid,real_name,status,avatar,nickname,count_start');},
  240. 'topic', 'category', 'relevance'
  241. ])->find()->toArray();
  242. $product_ids = array_column($res['relevance'], 'right_id');
  243. $product = app()->make(ProductRepository::class)->search($res['mer_id'], ['product_ids' => $product_ids])->select();
  244. if ($product) $product = $product->toArray();
  245. $res['spu_id'] = array_column($product,'spu_id');
  246. $res['product'] = $product;
  247. unset($res['relevance']);
  248. return $res;
  249. }
  250. /**
  251. * 移动端详情展示
  252. * @param int $id
  253. * @param $user
  254. * @return array|\think\Model|null
  255. * @author Qinii
  256. * @day 10/27/21
  257. */
  258. public function show(int $id, $user)
  259. {
  260. $where = self::IS_SHOW_WHERE;
  261. $is_author = 0;
  262. if ($user && $this->dao->uidExists($id, $user->uid)) {
  263. $where = ['is_del' => 0];
  264. $is_author = 1;
  265. }
  266. $config = systemConfig("community_app_switch");
  267. if ($config) $where['is_type'] = $config;
  268. $where[$this->dao->getPk()] = $id;
  269. $data = $this->dao->getSearch($where)
  270. ->with([
  271. 'author' => function ($query) {
  272. $query->field('uid,real_name,status,avatar,nickname,count_start,member_level');
  273. if (systemConfig('member_status')) $query->with(['member' => function ($query) {
  274. $query->field('brokerage_icon,brokerage_level');
  275. }]);
  276. },
  277. 'relevance' => [
  278. 'spu' => function ($query) {
  279. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
  280. }
  281. ],
  282. 'topic' => function ($query) {
  283. $query->where('status', 1)->where('is_del', 0);
  284. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  285. },
  286. 'is_start' => function ($query) use ($user) {
  287. $query->where('left_id', $user->uid ?? '');
  288. },
  289. ])->hidden(['is_del'])->find();
  290. $relevance = [];
  291. if ($data['relevance']) {
  292. foreach ($data['relevance'] as $item) {
  293. if ($item['spu']) $relevance[] = $item;
  294. }
  295. }
  296. $data['relevance'] = $relevance;
  297. if (!$data) throw new ValidateException('内容不存在,可能已被删除了哦~');
  298. $data['is_author'] = $is_author;
  299. $is_fans = 0;
  300. if ($user && !$data['is_author'])
  301. $is_fans = app()->make(RelevanceRepository::class)->getWhereCount([
  302. 'left_id' => $user->uid,
  303. 'right_id' => $data['uid'],
  304. 'type' => RelevanceRepository::TYPE_COMMUNITY_FANS,
  305. ]);
  306. $data['is_fans'] = $is_fans;
  307. //增加浏览量
  308. if($data['status'] == 1) {
  309. $this->dao->incField($id, 'pv');
  310. }
  311. return $data;
  312. }
  313. /**
  314. * 根据订单信息 获取订单下的商品信息
  315. * @param $id
  316. * @return array
  317. * @author Qinii
  318. */
  319. public function getSpuByOrder($id)
  320. {
  321. $where = app()->make(StoreOrderProductRepository::class)->selectWhere(['order_id' => $id]);
  322. if (!$where) throw new ValidateException('商品已下架');
  323. $make = app()->make(SpuRepository::class);
  324. foreach ($where as $item) {
  325. switch ($item['product_type']) {
  326. case 0:
  327. $sid = $item['product_id'];
  328. // nobreak;
  329. case 1:
  330. $sid = $item['product_id'];
  331. break;
  332. case 2:
  333. $sid = $item['activity_id'];
  334. break;
  335. case 3:
  336. $sid = $item['cart_info']['productAssistSet']['product_assist_id'];
  337. break;
  338. case 4:
  339. $sid = $item['cart_info']['product']['productGroup']['product_group_id'];
  340. break;
  341. default:
  342. $sid = $item['product_id'];
  343. break;
  344. }
  345. $data[] = $make->getSpuData($sid, $item['product_type'], 0);
  346. }
  347. return $data;
  348. }
  349. /**
  350. * 创建
  351. * @param array $data
  352. * @author Qinii
  353. * @day 10/29/21
  354. */
  355. public function create(array $data)
  356. {
  357. event('community.create.before', compact('data'));
  358. if ($data['topic_id']) {
  359. $getTopic = app()->make(CommunityTopicRepository::class)->get($data['topic_id']);
  360. if (!$getTopic || !$getTopic->status) throw new ValidateException('话题不存在或已关闭');
  361. $data['category_id'] = $getTopic->category_id;
  362. }
  363. return Db::transaction(function () use ($data) {
  364. $community = $this->dao->create($data);
  365. if ($data['spu_id']) $this->joinProduct($community->community_id, $data['spu_id']);
  366. event('community.create', compact('community'));
  367. // 内容数统计
  368. app()->make(UserRepository::class)->incField((int)$data['uid'], 'count_content');
  369. if ($data['status'] == 1) { // 免审核 增加经验值
  370. $make = app()->make(UserBrokerageRepository::class);
  371. $make->incMemberValue($data['uid'], 'member_community_num', $community->community_id);
  372. $this->giveIntegral($community);
  373. }
  374. return $community->community_id;
  375. });
  376. }
  377. /**
  378. * 编辑
  379. * @param int $id
  380. * @param array $data
  381. * @author Qinii
  382. * @day 10/29/21
  383. */
  384. public function edit(int $id, array $data)
  385. {
  386. event('community.update.before', compact('id', 'data'));
  387. if ($data['topic_id']) {
  388. $getTopic = app()->make(CommunityTopicRepository::class)->get($data['topic_id']);
  389. if (!$getTopic || !$getTopic->status) throw new ValidateException('话题不存在或已关闭');
  390. $data['category_id'] = $getTopic->category_id;
  391. }
  392. Db::transaction(function () use ($id, $data) {
  393. $spuId = $data['spu_id'];
  394. unset($data['spu_id']);
  395. $community = $this->dao->update($id, $data);
  396. if ($spuId) $this->joinProduct($id, $spuId);
  397. event('community.update', compact('id', 'community'));
  398. });
  399. }
  400. /**
  401. * 关联商品
  402. * @param int $id
  403. * @param array $data
  404. * @author Qinii
  405. * @day 10/29/21
  406. */
  407. public function joinProduct($id, array $data)
  408. {
  409. $make = app()->make(RelevanceRepository::class);
  410. $data = array_unique($data);
  411. $res = [];
  412. foreach ($data as $value) {
  413. if ($value) {
  414. $res[] = [
  415. 'left_id' => $id,
  416. 'right_id' => $value,
  417. 'type' => RelevanceRepository::TYPE_COMMUNITY_PRODUCT
  418. ];
  419. }
  420. }
  421. $make->clear($id, RelevanceRepository::TYPE_COMMUNITY_PRODUCT, 'left_id');
  422. if ($res) $make->insertAll($res);
  423. }
  424. /**
  425. * 获取某用户信息
  426. * @param int $uid
  427. * @param null $self
  428. * @return mixed
  429. * @author Qinii
  430. * @day 10/29/21
  431. */
  432. public function getUserInfo(int $uid, $self = null)
  433. {
  434. $relevanceRepository = app()->make(RelevanceRepository::class);
  435. $data['focus'] = $relevanceRepository->getFieldCount('left_id', $uid, RelevanceRepository::TYPE_COMMUNITY_FANS);
  436. $is_start = $is_self = false;
  437. if ($self && $self->uid == $uid) {
  438. $user = $self;
  439. $is_self = true;
  440. } else {
  441. $user = app()->make(UserRepository::class)->get($uid);
  442. $is_start = $relevanceRepository->checkHas($self->uid, $uid, RelevanceRepository::TYPE_COMMUNITY_FANS) > 0;
  443. }
  444. $data['start'] = $user->count_start;
  445. $data['uid'] = $user->uid;
  446. $data['avatar'] = $user->avatar;
  447. $data['nickname'] = $user->nickname;
  448. $data['is_start'] = $is_start;
  449. $data['member_icon'] = systemConfig('member_status') ? ($user->member->brokerage_icon ?? '') : '';
  450. $data['is_self'] = $is_self;
  451. $data['fans'] = $user->count_fans;
  452. return $data;
  453. }
  454. /**
  455. * 关注
  456. * @param int $id
  457. * @param int $uid
  458. * @param int $status
  459. * @author Qinii
  460. * @day 10/29/21
  461. */
  462. public function setFocus(int $id, int $uid, int $status)
  463. {
  464. $make = app()->make(RelevanceRepository::class);
  465. $check = $make->checkHas($uid, $id, RelevanceRepository::TYPE_COMMUNITY_FANS);
  466. if ($status) {
  467. if ($check) throw new ValidateException('您已经关注过他了~');
  468. $make->create($uid, $id, RelevanceRepository::TYPE_COMMUNITY_FANS, true);
  469. app()->make(UserRepository::class)->incField($id, 'count_fans', 1);
  470. } else {
  471. if (!$check) throw new ValidateException('您还未关注他哦~');
  472. $make->destory($uid, $id, RelevanceRepository::TYPE_COMMUNITY_FANS);
  473. app()->make(UserRepository::class)->decField($id, 'count_fans', 1);
  474. }
  475. return;
  476. }
  477. /**
  478. * 设置文章排序星际
  479. * @param int $id
  480. * @param null $self
  481. * @return mixed
  482. * @author Qinii
  483. * @day 10/29/21
  484. */
  485. public function form($id)
  486. {
  487. $form = Elm::createForm(Route::buildUrl('systemCommunityUpdate', ['id' => $id])->build());
  488. $data = $this->dao->get($id);
  489. if (!$data) throw new ValidateException('数据不存在');
  490. $formData = $data->toArray();
  491. return $form->setRule([
  492. Elm::rate('start', '排序星级:')->max(5)
  493. ])->setTitle('编辑星级')->formData($formData);
  494. }
  495. /**
  496. * 后台强制下架操作
  497. * @param $id
  498. * @return \FormBuilder\Form
  499. * @author Qinii
  500. */
  501. public function showForm($id)
  502. {
  503. $form = Elm::createForm(Route::buildUrl('systemCommunityStatus', ['id' => $id])->build());
  504. $data = $this->dao->get($id);
  505. if (!$data) throw new ValidateException('数据不存在');
  506. return $form->setRule([
  507. Elm::hidden('status', -1),
  508. Elm::textarea('refusal', '下架理由:', '信息存在违规')->placeholder('请输入下架理由')->required()
  509. ])->setTitle('强制下架');
  510. }
  511. /**
  512. * 给文章点赞
  513. * @param int $id
  514. * @param $userInfo
  515. * @param int $status
  516. * @return void
  517. * @author Qinii
  518. */
  519. public function setCommunityStart(int $id, $userInfo, int $status)
  520. {
  521. $make = app()->make(RelevanceRepository::class);
  522. $userRepository = app()->make(UserRepository::class);
  523. if ($status) {
  524. $res = $make->create($userInfo->uid, $id, RelevanceRepository::TYPE_COMMUNITY_START, true);
  525. if (!$res) throw new ValidateException('您已经点赞过了');
  526. $ret = $this->dao->get($id);
  527. $user = $userRepository->get($ret['uid']);
  528. $this->dao->incField($id, 'count_start', 1);
  529. if ($user) $userRepository->incField((int)$user->uid, 'count_start', 1);
  530. }
  531. if (!$status) {
  532. if (!$make->checkHas($userInfo->uid, $id, RelevanceRepository::TYPE_COMMUNITY_START))
  533. throw new ValidateException('您还没有点赞呢~');
  534. $make->destory($userInfo->uid, $id, RelevanceRepository::TYPE_COMMUNITY_START);
  535. $ret = $this->dao->get($id);
  536. $user = $userRepository->get($ret['uid']);
  537. $this->dao->decField($id, 'count_start', 1);
  538. if ($user) $userRepository->decField((int)$user->uid, 'count_start', 1);
  539. }
  540. }
  541. /**
  542. * 审核
  543. * @param $id
  544. * @param $data
  545. * @return void
  546. * @author Qinii
  547. */
  548. public function setStatus($id, $data)
  549. {
  550. $ret = $this->dao->get($id);
  551. event('community.status.before', compact('id', 'data'));
  552. Db::transaction(function () use ($ret, $id, $data) {
  553. $data['status_time'] = date('Y-m-d H:i;s', time());
  554. $this->dao->update($id, $data);
  555. if ($data['status'] == 1) {
  556. $make = app()->make(UserBrokerageRepository::class);
  557. $make->incMemberValue($ret['uid'], 'member_community_num', $id);
  558. $this->giveIntegral($ret);
  559. }
  560. event('community.status', compact('id'));
  561. });
  562. }
  563. /**
  564. * 种草完成给用户增加积分
  565. *
  566. * @param object $communityInfo 种草信息
  567. * @return void
  568. */
  569. public function giveIntegral(object $communityInfo)
  570. {
  571. $giveIntegralConfig = systemConfig(['integral_community_give', 'integral_community_give_limit']);
  572. if (!$giveIntegralConfig['integral_community_give'] || !$giveIntegralConfig['integral_community_give_limit']) {
  573. return false;
  574. }
  575. $uid = $communityInfo->uid;
  576. $createDay = date('Y-m-d', strtotime($communityInfo->create_time));
  577. // 计算用户在当天发布的审核通过的图文内容数量, 判断是否超过每日限制
  578. $communityCount = $this->getWhereCount(['uid' => $uid, 'status' => 1, ['create_time', 'like', $createDay.'%']]);
  579. if($communityCount <= $giveIntegralConfig['integral_community_give_limit']) {
  580. // 使用依赖注入的方式创建用户账单仓库实例,并增加用户的积分账单
  581. app()->make(UserBillRepository::class)->incBill($uid, 'integral', 'lock', [
  582. 'link_id' => $communityInfo->community_id, // 图文内容id,关联的订单ID,用于记录积分的来源
  583. 'status' => 0, // 积分状态,这里假设0表示积分锁定,即还未完全发放
  584. 'title' => '种草发帖送积分', // 积分的描述,表明积分的来源是种草发帖
  585. 'number' => $giveIntegralConfig['integral_community_give'], // 赠送的积分数量
  586. 'mark' => $communityInfo->author->nickname.'【用户ID: '.$uid.'】于'.$createDay.', 成功发布第' . floatval($communityCount) . '篇种草, 赠送积分' . floatval($giveIntegralConfig['integral_community_give']), // 积分的备注信息
  587. 'balance' => $communityInfo->author->integral // 用户当前的积分余额,用于记录积分的变化
  588. ]);
  589. }
  590. }
  591. /**
  592. * 删除社区内容
  593. *
  594. * 此函数用于处理社区内容的删除操作。它首先触发一个名为'community.delete.before'的事件,
  595. * 允许任何监听此事件的组件在实际删除操作之前进行干预。接下来,它从数据库中获取指定ID的内容信息,
  596. * 并将该内容的删除标记设置为1,执行实际的删除逻辑。随后,它减少与该内容关联的用户的内容计数,
  597. * 这反映了内容数量的变化。最后,它触发'community.delete'事件,允许其他组件在删除操作完成后执行额外的操作。
  598. *
  599. * @param int $id 内容的唯一标识符
  600. * @param null|$user 删除操作的用户信息,默认为null,表示任何用户都可以执行此操作
  601. */
  602. public function destory($id, $user = null)
  603. {
  604. // 在执行删除操作之前触发事件,允许其他组件或功能进行干预
  605. event('community.delete.before', compact('id', 'user'));
  606. // 从数据库中获取指定ID的内容信息
  607. $info = $this->dao->get($id);
  608. // 将内容的删除状态设置为已删除
  609. $this->dao->update($id, ['is_del' => 1]);
  610. // 减少与该内容关联的用户的内容计数,反映内容的删除
  611. // 内容数统计
  612. app()->make(UserRepository::class)->decField((int)$info['uid'], 'count_content');
  613. // 删除操作完成后触发事件,允许其他组件或功能执行后续操作
  614. event('community.delete', compact('id', 'user'));
  615. }
  616. /**
  617. * 根据SPU ID获取相关数据
  618. *
  619. * 本函数通过SPU ID查询特定的数据集,这些数据集特定于应用程序的业务逻辑。
  620. * 它合并了查询条件,选择了特定的字段,排序方式,并限制了返回的记录数。
  621. *
  622. * @param int $spuId 商品规格ID,用于精确查询特定SPU的数据。
  623. * @return array 返回一个包含社区ID、标题、图片、类型标志和创建时间的记录集数组,最多包含3条记录。
  624. */
  625. public function getDataBySpu($spuId)
  626. {
  627. // 合并查询条件,确保查询的SPU ID准确,并包含显示状态的条件
  628. $where = array_merge(['spu_id' => $spuId], self::IS_SHOW_WHERE);
  629. // 执行查询,选择特定字段,按创建时间降序排序,并限制返回结果的数量
  630. $result = $this->dao->getSearch($where)
  631. ->field('community_id,title,image,is_type,create_time')
  632. ->order('create_time DESC')
  633. ->limit(3)->select();
  634. // 返回查询结果
  635. return $result;
  636. }
  637. /**
  638. * 生成视频社区的二维码。
  639. * 根据传入的类型和用户信息,生成相应的二维码用于视频社区的访问或推广。
  640. *
  641. * @param int $id 社区ID,标识特定的视频社区。
  642. * @param string $type 二维码类型,区分常规二维码和小程序二维码。
  643. * @param object|null $user 用户信息,用于生成带有用户标识的二维码。
  644. * @return string|boolean 返回二维码的路径或者在生成失败时返回false。
  645. */
  646. public function qrcode($id, $type, $user)
  647. {
  648. // 查询视频社区信息,确保社区存在且状态正常
  649. $res = $this->dao->search(['is_type' => self::COMMUNIT_TYPE_VIDEO, 'community_id' => $id, 'status' => 1, 'is_show' => 1])->find();
  650. if (!$res) return false;
  651. // 增加视频社区的访问量
  652. // 增加视频播放量
  653. $this->dao->incField($id, 'pv');
  654. // 创建二维码服务实例
  655. $make = app()->make(QrcodeService::class);
  656. // 根据二维码类型生成不同的二维码内容和名称
  657. if ($type == 'routine') {
  658. // 生成小程序二维码的名称和参数
  659. $name = md5('rcwx' . $id . $type . ($user ? $user->uid . $user['is_promoter'] : '') . date('Ymd')) . '.jpg';
  660. $params = 'id=' . $id . ($user ? '&spid=' . $user['uid'] : '');
  661. $link = 'pages/short_video/nvueSwiper/index';
  662. // 生成小程序二维码并返回路径
  663. return $make->getRoutineQrcodePath($name, $link, $params);
  664. } else {
  665. // 生成普通二维码的名称和链接
  666. $name = md5('cwx' . $id . $type . ($user ? $user->uid . $user['is_promoter'] : '') . date('Ymd')) . '.jpg';
  667. $link = 'pages/short_video/nvueSwiper/index';
  668. $link .= '?id=' . $id . ($user ? '&spid=' . $user['uid'] : '');
  669. $key = 'com' . $type . '_' . $id . '_' . ($user['uid'] ?? 0);
  670. // 生成普通二维码并返回路径
  671. return $make->getWechatQrcodePath($name, $link, false, $key);
  672. }
  673. }
  674. }