Community.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\merchant\store\content;
  12. use app\common\repositories\user\UserMerchantRepository;
  13. use app\common\repositories\community\CommunityRepository;
  14. use app\common\repositories\community\CommunityTopicRepository;
  15. use app\common\repositories\community\CommunityReplyRepository;
  16. use app\common\repositories\community\CommunityCategoryRepository;
  17. use app\common\repositories\store\order\StoreOrderProductRepository;
  18. use app\common\repositories\system\RelevanceRepository;
  19. use app\common\repositories\user\UserHistoryRepository;
  20. use app\common\repositories\user\UserRelationRepository;
  21. use app\common\repositories\user\UserRepository;
  22. use app\validate\api\CommunityValidate;
  23. use crmeb\basic\BaseController;
  24. use crmeb\services\MiniProgramService;
  25. use think\App;
  26. use app\common\repositories\community\CommunityRepository as repository;
  27. use think\exception\ValidateException;
  28. /**
  29. * Class Community
  30. * app\controller\api\community
  31. * 逛逛社区
  32. */
  33. class Community extends BaseController
  34. {
  35. /**
  36. * @var repository
  37. */
  38. protected $repository;
  39. protected $user = null;
  40. /**
  41. * User constructor.
  42. * @param App $app
  43. * @param $repository
  44. */
  45. public function __construct(App $app, repository $repository)
  46. {
  47. parent::__construct($app);
  48. $this->repository = $repository;
  49. if (!systemConfig('community_status') )
  50. throw new ValidateException('未开启社区功能');
  51. }
  52. public function getUser($uid)
  53. {
  54. $where['mer_id'] = $this->request->merId();
  55. $where['status'] = 1;
  56. $merRepository = app()->make(UserMerchantRepository::class);
  57. $uids = $merRepository->getManagerUid($where);
  58. if (!in_array($uid,$uids))
  59. throw new ValidateException('此用户不是您的员工用户');
  60. $repository = app()->make(UserRepository::class);
  61. $userInfo = $repository->userInfo($uid);
  62. $this->user = $userInfo;
  63. }
  64. /**
  65. * 文章列表
  66. * @return \think\response\Json
  67. * @author Qinii
  68. * @day 10/29/21
  69. */
  70. public function lst()
  71. {
  72. $where = $this->request->params([
  73. ['keyword',''],
  74. ['topic_id',''],
  75. ['category_id',''],
  76. ['status',''],
  77. ['is_type',''],
  78. ['search_type','content'],
  79. ['is_del',0],
  80. ]);
  81. [$page, $limit] = $this->getPage();
  82. $where['mer_id'] = $this->request->merId();
  83. return app('json')->success($this->repository->getList($where, $page, $limit));
  84. }
  85. public function create()
  86. {
  87. $uid = $this->request->param('uid');
  88. $this->getUser($uid);
  89. $data = $this->checkParams();
  90. $data['mer_id'] = $this->request->merId();
  91. $this->checkUserAuth();
  92. $data['uid'] = $uid;
  93. $res = $this->repository->create($data);
  94. return app('json')->success('添加成功');
  95. }
  96. public function update($id)
  97. {
  98. $res = $this->repository->get($id);
  99. if (!$res['mer_id'] || $res['mer_id'] != $this->request->merId())
  100. return app('json')->success('内容不存在或不属于您');
  101. $this->getUser($res['uid']);
  102. $data = $this->checkParams();
  103. $this->checkUserAuth();
  104. $this->repository->edit($id, $data);
  105. return app('json')->success('编辑成功');
  106. }
  107. public function detail($id)
  108. {
  109. if (!$this->repository->exists($id))
  110. return app('json')->fail('数据不存在');
  111. return app('json')->success($this->repository->detail($id));
  112. }
  113. public function delete($id)
  114. {
  115. if (!$this->repository->exists($id))
  116. return app('json')->fail('数据不存在');
  117. $res = $this->repository->get($id);
  118. if (!$res['mer_id'] || $res['mer_id'] != $this->request->merId())
  119. return app('json')->success('内容不存在或不属于您');
  120. $this->repository->destory($id);
  121. return app('json')->success('删除成功');
  122. }
  123. public function checkParams()
  124. {
  125. $data = $this->request->params(['image','topic_id','content','spu_id','order_id',['is_type',1],'video_link']);
  126. $config = systemConfig(["community_app_switch",'community_audit','community_video_audit']);
  127. $data['status'] = 0;
  128. $data['is_show'] = 0;
  129. if ($data['is_type'] == 1) {
  130. if (!in_array($this->repository::COMMUNIT_TYPE_FONT,$config['community_app_switch']))
  131. throw new ValidateException('社区图文未开启');
  132. if ($config['community_audit']) {
  133. $data['status'] = 1;
  134. $data['is_show'] = 1;
  135. $data['status_time'] = date('Y-m-d H:i:s', time());
  136. }
  137. } else {
  138. if (!in_array($this->repository::COMMUNIT_TYPE_VIDEO,$config['community_app_switch']))
  139. throw new ValidateException('短视频未开启');
  140. if ($config['community_video_audit']) {
  141. $data['status'] = 1;
  142. $data['is_show'] = 1;
  143. $data['status_time'] = date('Y-m-d H:i:s', time());
  144. }
  145. if (!$data['video_link']) throw new ValidateException('请上传视频');
  146. }
  147. $data['content'] = filter_emoji($data['content']);
  148. MiniProgramService::create()->msgSecCheck($this->user, $data['content'],3,0);
  149. app()->make(CommunityValidate::class)->check($data);
  150. $arr = explode("\n", $data['content']);
  151. $title = rtrim(ltrim($arr[0]));
  152. if (mb_strlen($title) > 40 ){
  153. $data['title'] = mb_substr($title,0,30,'utf-8');
  154. } else {
  155. $data['title'] = $title;
  156. }
  157. if ($data['image']) $data['image'] = implode(',',$data['image']);
  158. return $data;
  159. }
  160. public function checkUserAuth()
  161. {
  162. if ( systemConfig('community_auth') ) {
  163. if ($this->user->phone) {
  164. return true;
  165. }
  166. throw new ValidateException('请员工用户先绑定的手机号');
  167. } else {
  168. return true;
  169. }
  170. }
  171. public function cateLst(CommunityTopicRepository $repository)
  172. {
  173. $res = $repository->getSearch(['status' => 1])->column('topic_id,topic_name, status');
  174. return app('json')->success($res);
  175. }
  176. public function reply($id)
  177. {
  178. $where['community_id'] = $id;
  179. [$page, $limit] = $this->getPage();
  180. $replyRepository = app()->make(CommunityReplyRepository::class);
  181. $res = $replyRepository->getList($where, $page, $limit);
  182. return app('json')->success($res);
  183. }
  184. }