DefaultController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. namespace addons\OperatingNote\api\modules\v1\controllers;
  3. use addons\OperatingNote\common\enums\NoteEnum;
  4. use addons\OperatingNote\common\models\AddonsOperatingNoteAd;
  5. use addons\OperatingNote\common\models\AddonsOperatingNoteBuy;
  6. use addons\OperatingNote\common\models\AddonsOperatingNoteCate;
  7. use addons\OperatingNote\common\models\AddonsOperatingNoteComment;
  8. use addons\OperatingNote\common\models\AddonsOperatingNoteContent;
  9. use addons\OperatingNote\common\models\AddonsOperatingNoteRewardRecord;
  10. use addons\OperatingNote\common\models\AddonsOperatingNoteShareRecord;
  11. use addons\OperatingNote\common\models\AddonsOperatingNoteUserLike;
  12. use common\enums\StatusEnum;
  13. use common\models\common\PlatformSetting;
  14. use common\models\user\User;
  15. use common\models\user\UserFollow;
  16. use Yii;
  17. use api\controllers\OnAuthController;
  18. /**
  19. * 默认控制器
  20. *
  21. * Class DefaultController
  22. * @package addons\OperatingNote\api\modules\v1\controllers
  23. */
  24. class DefaultController extends OnAuthController
  25. {
  26. public $modelClass = '';
  27. /**
  28. * 不用进行登录验证的方法
  29. *
  30. * 例如: ['index', 'update', 'create', 'view', 'delete']
  31. * 默认全部需要验证
  32. *
  33. * @var array
  34. */
  35. protected $authOptional = ['index', 'article-list', 'get-comment', 'config', 'detail', 'view-reward'];
  36. /**
  37. * @desc:首页
  38. * @Author: hua
  39. * @Date: 2022/2/22
  40. * @Time: 14:33
  41. * @Copyright: copyright (c) 2021 广东七件事集团
  42. * @return mixed|void
  43. */
  44. public function actionIndex()
  45. {
  46. $data['is_login'] = $this->user_id ? 1 : 0;
  47. $data['avatar_url'] = $this->user['avatar_url'] ?? '';
  48. $ad_list = AddonsOperatingNoteAd::lists([
  49. 'where' => [
  50. ['mall_id' => $this->mall_id],
  51. ['status' => StatusEnum::ENABLED],
  52. ],
  53. 'order' => 'sort desc',
  54. 'select' => 'id,title,img,jump_href'
  55. ]);
  56. $data['ad_list'] = $ad_list ?? [];
  57. $cate_list = AddonsOperatingNoteCate::lists([
  58. 'order' => 'sort desc, id desc',
  59. 'where' => [
  60. ['mall_id' => $this->mall_id],
  61. ['status' => StatusEnum::ENABLED]
  62. ],
  63. 'select' => 'id,name',
  64. ]);
  65. $data['cate_list'] = $cate_list ?? [];
  66. return $this->success('操作成功', $data);
  67. }
  68. /**
  69. * @desc:文章列表
  70. * @Author: hua
  71. * @Date: 2022/2/22
  72. * @Time: 14:26
  73. * @Copyright: copyright (c) 2021 广东七件事集团
  74. * @return mixed|void
  75. */
  76. public function actionArticleList()
  77. {
  78. $params = \Yii::$app->request->get();
  79. $params['where'][] = ['mall_id' => $this->mall_id];
  80. $params['where'][] = ['check_status' => 1];
  81. $params['where'][] = ['is_release' => 1];
  82. $params['where'][] = ['status' => StatusEnum::ENABLED];
  83. if (!empty($params['keyword'])) $params['where'][] = ['like', 'title', '%' . $params['keyword'] . '%', false];
  84. if (!empty($params['cate_id'])) $params['where'][] = ['cate_id' => $params['cate_id']];
  85. if (!empty($params['note_type'])) $params['where'][] = ['note_type' => $params['note_type']];
  86. if (!empty($params['id'])) $params['where'][] = ['!=', 'id', $params['id']];
  87. $top = [];
  88. if (isset($params['page']) && $params['page'] == 1 && isset($params['id'])) {
  89. $top = AddonsOperatingNoteContent::getOne([['id' => $params['id']]], true, ['user', 'goods']);
  90. }
  91. $params['with'] = ['user', 'goods'];
  92. $params['order'] = 'is_top desc,sort desc,created_at desc';
  93. $list = AddonsOperatingNoteContent::listPage($params, false, true);
  94. if ($top) {
  95. array_unshift($list['list'], $top);
  96. }
  97. $like = [];
  98. if ($list['list']) {
  99. $ids = array_column($list['list'], 'id');
  100. $user_ids = array_unique(array_diff(array_column($list['list'], 'user_id'), [0]));
  101. $like = AddonsOperatingNoteUserLike::lists(['where' => [['in', 'note_content_id', $ids], ['=', 'user_id', $this->user_id], ['=', 'is_like', 1]], 'index' => 'note_content_id', 'select' => 'id,user_id,is_like,note_content_id']);
  102. $buy_list = AddonsOperatingNoteBuy::lists(['where' => [['in', 'note_id', $ids], ['=', 'user_id', $this->user_id], ['=', 'is_pay', NoteEnum::STATUS_ONE]], 'index' => 'note_id']);
  103. $follow_list = [];
  104. if ($user_ids) {
  105. $follow['where'][] = ['=', 'status', StatusEnum::ENABLED];
  106. $follow['where'][] = ['=', 'user_id', $this->user_id];
  107. $follow['where'][] = ['in', 'follow_id', $user_ids];
  108. $follow['index'][] = 'follow_id';
  109. $follow_lists = UserFollow::listPage($follow);
  110. $follow_list = $follow_lists['list'];
  111. }
  112. $view_record_count = AddonsOperatingNoteRewardRecord::lists(['where' => [['=', 'user_id', $this->user_id], ['=', 'reward_source', NoteEnum::STATUS_ONE], ['in', 'note_content_id', $ids], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'note_content_id,status,count(id) as num', 'group' => 'note_content_id', 'index' => 'note_content_id']);
  113. $share_record_count = AddonsOperatingNoteRewardRecord::lists(['where' => [['=', 'user_id', $this->user_id], ['=', 'reward_source', NoteEnum::STATUS_TWO], ['in', 'note_content_id', $ids], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'note_content_id,status,count(id) as num', 'group' => 'note_content_id', 'index' => 'note_content_id']);
  114. foreach ($list['list'] as $k => &$v) {
  115. $v['views_num'] = $v['views_num'] + $v['virtual_views_num'];
  116. $v['likes_num'] = $v['likes_num'] + $v['virtual_likes_num'];
  117. $v['is_like'] = 0;
  118. $v['can_ready'] = false;
  119. $v['view_record_count'] = 0;
  120. $v['share_record_count'] = 0;
  121. $v['is_follow'] = 0;
  122. if (isset($follow_list[$v['user_id']])) {
  123. $v['is_follow'] = 1;
  124. }
  125. if (isset($view_record_count[$v['id']])) {
  126. $v['view_record_count'] = $view_record_count[$v['id']]['num'];
  127. }
  128. if (isset($share_record_count[$v['id']])) {
  129. $v['share_record_count'] = $share_record_count[$v['id']]['num'];
  130. }
  131. if (isset($like[$v['id']])) {
  132. $v['is_like'] = 1;
  133. }
  134. if ($this->user_id == $v['user_id']) {
  135. $v['can_ready'] = true;
  136. } else {
  137. if ($v['note_type'] != NoteEnum::STATUS_THREE) {
  138. if ($v['view_limit'] == NoteEnum::STATUS_ZERO) {
  139. if ($this->user_id == $v['user_id'] && $v['user_id'] > 0) {
  140. $v['can_ready'] = true;
  141. } else {
  142. if ($v['view_limit_content'] == 'all') {
  143. $v['can_ready'] = true;
  144. } else {
  145. $view_limit_content = \Qiniu\json_decode($v['view_limit_content']);
  146. if (in_array($this->user['level'], $view_limit_content)) {
  147. $v['can_ready'] = true;
  148. }
  149. }
  150. }
  151. } else {
  152. if (isset($buy_list[$v['id']])) {
  153. $v['can_ready'] = true;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. return $this->success('操作成功', $list);
  161. }
  162. /**
  163. * @desc:发布
  164. * @Author: hua
  165. * @Date: 2022/2/22
  166. * @Time: 14:09
  167. * @Copyright: copyright (c) 2021 广东七件事集团
  168. * @return mixed|void
  169. * @throws \yii\db\Exception
  170. */
  171. public function actionRelease()
  172. {
  173. $params = \Yii::$app->request->post();
  174. $res = \Yii::$app->services->validate->validate($params, [
  175. [['title', 'cate_id'], 'required'],
  176. [['title'], 'string', 'max' => 30],
  177. [['goods_id', 'source','cover_img', 'content_href', 'id', 'cover_type', 'main_text', 'content_type'], 'safe'],
  178. ]);
  179. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage() == 'Title只能包含至多30个字符。' ? '标题最多只能30个字符' : \Yii::$app->services->validate->getErrorMessage());
  180. $params['mall_id'] = $this->mall_id;
  181. $params['user_id'] = $this->user_id;
  182. if(!empty($params['main_text'])) $params['content'] = $params['main_text'];
  183. if(!empty($params['content_type'])) $params['note_type'] = $params['content_type'];
  184. if(!empty($params['source'])){
  185. $params['content_href'] = [];
  186. if($params['content_type']==1){
  187. if(!empty($params['source']['source'])){
  188. $params['content_href'] = $params['source']['source'];
  189. //$params['cover_img'] =$params['source']['source'];
  190. }
  191. $params['content_href'] = json_encode( $params['content_href']);
  192. }
  193. if($params['content_type']==2){
  194. //if(!empty($params['source']['cover']))$params['cover_img'] =[$params['source']['cover']];
  195. if(!empty($params['source']['source'])) $params['content_href'] = $params['source']['source'];
  196. }
  197. }
  198. if (!isset($params['id']) && !isset($params['cover_type'])) {
  199. return $this->error('请选择封面图类型');
  200. }
  201. if (isset($params['cover_type'])) {
  202. $cover_count = NoteEnum::getCoverTypeCount($params['cover_type']);
  203. if (is_array($params['cover_img']) && count($params['cover_img']) != $cover_count) {
  204. return $this->error('请上传'. $cover_count . '张封面图');
  205. }
  206. }
  207. $config = Yii::$app->services->setting->addons->get($this->mall_id, NoteEnum::ADDONS_NAME, 'basic', true);
  208. if ($config['allow_user_create'] == 0) {
  209. return $this->error('暂时未开放会员发布内容');
  210. }
  211. if (isset($params['cover_type']) && !$params['cover_type']) unset($params['cover_type']);
  212. if (!isset($params['id'])) {
  213. $scenarios = 'create';
  214. $params['share_reward_type'] = 0;
  215. $params['view_reward_type'] = 0;
  216. $params['share_title_type'] = 1;
  217. $params['view_limit'] = 0;
  218. $params['share_reward'] = 0;
  219. $params['share_reward_max_time'] = 0;
  220. $params['view_reward'] = 0;
  221. $params['view_reward_max_time'] = 0;
  222. if ($config['need_check'] == 0) {
  223. $params['check_status'] = 1;
  224. $params['is_release'] = 1;
  225. }
  226. } else {
  227. if (isset($params['cover_type'])) {
  228. unset($params['cover_type']);
  229. }
  230. $scenarios = 'update';
  231. if ($config['need_check'] == 1) {
  232. $params['check_status'] = NoteEnum::STATUS_ZERO;
  233. }
  234. }
  235. $res = AddonsOperatingNoteContent::setData($params, $scenarios, false);
  236. if ($res === false) return $this->error(AddonsOperatingNoteContent::getStaticError());
  237. return $this->success('操作成功', []);
  238. }
  239. /**
  240. * @desc:分类列表
  241. * @Author: hua
  242. * @Date: 2022/2/22
  243. * @Time: 14:13
  244. * @Copyright: copyright (c) 2021 广东七件事集团
  245. * @return mixed|void
  246. */
  247. public function actionCateList()
  248. {
  249. $params = [
  250. 'where' => [
  251. ['mall_id' => $this->mall_id],
  252. ['status' => StatusEnum::ENABLED]
  253. ],
  254. 'select' => 'id,name'
  255. ];
  256. $list = AddonsOperatingNoteCate::lists($params);
  257. return $this->success('操作成功', $list);
  258. }
  259. /**
  260. * @desc:我的笔记
  261. * @Author: hua
  262. * @Date: 2022/2/22
  263. * @Time: 18:14
  264. * @Copyright: copyright (c) 2021 广东七件事集团
  265. * @return mixed|void
  266. */
  267. public function actionMyNote()
  268. {
  269. $get = \Yii::$app->request->get();
  270. if (isset($get['user_id']) && $get['user_id'] != 0 && $get['user_id'] != $this->user_id) {
  271. $params['where'][] = ['user_id' => $get['user_id']];
  272. $params['where'][] = ['is_release' => StatusEnum::ENABLED];
  273. $params['where'][] = ['check_status' => StatusEnum::ENABLED];
  274. } else {
  275. $params['where'][] = ['user_id' => $this->user_id];
  276. }
  277. $params['where'][] = ['mall_id' => $this->mall_id];
  278. $params['where'][] = ['status' => StatusEnum::ENABLED];
  279. $params['order'] = 'id desc';
  280. $params['select'] = 'id,title,created_at,check_status,check_remark,cover_img,views_num,likes_num,content_href,note_type,is_release,reply_num';
  281. $list = AddonsOperatingNoteContent::listPage($params, true, true);
  282. return $this->success('操作成功', $list);
  283. }
  284. public function actionDel()
  285. {
  286. $params = \Yii::$app->request->post();
  287. $res = \Yii::$app->services->validate->validate($params, [
  288. [['id'], 'required'],
  289. ]);
  290. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  291. $params['status'] = StatusEnum::DELETE;
  292. $params['user_id'] = $this->user_id;
  293. $res = AddonsOperatingNoteContent::setData($params, 'update', false);
  294. return $this->success('操作成功', $res);
  295. }
  296. public function actionBuyList()
  297. {
  298. $params = \Yii::$app->request->get();
  299. $params['where'][] = ['mall_id' => $this->mall_id];
  300. $params['where'][] = ['user_id' => $this->user_id];
  301. $params['where'][] = ['is_pay' => StatusEnum::ENABLED];
  302. $params['where'][] = ['status' => StatusEnum::ENABLED];
  303. $params['order'] = 'created_at desc';
  304. $params['with'] = ['content'];
  305. $list = AddonsOperatingNoteBuy::listPage($params, true, true);
  306. $list['list'] = array_column($list['list'], 'content');
  307. return $this->success('操作成功', $list);
  308. }
  309. /**
  310. * 获取详情
  311. * User: wniu
  312. * Date: 2022/2/24
  313. * Time: 2:31 下午
  314. *
  315. */
  316. public function actionDetail()
  317. {
  318. $params = \Yii::$app->request->get();
  319. $detail = AddonsOperatingNoteContent::getDetail($params['id'], $this->user);
  320. if (!$detail) return $this->error(AddonsOperatingNoteContent::getStaticError());
  321. return $this->success('获取成功', $detail);
  322. }
  323. /**
  324. * 发表评论
  325. * User: wniu
  326. * Date: 2022/2/25
  327. * Time: 3:14 下午
  328. *
  329. * @throws \yii\db\Exception
  330. */
  331. public function actionCreateReply()
  332. {
  333. $params = \Yii::$app->request->post();
  334. $res = \Yii::$app->services->validate->validate($params, [
  335. [['note_content_id', 'content'], 'required'],
  336. [['reply_id'], 'safe'],
  337. ]);
  338. $params = AddonsOperatingNoteComment::exceptNullVal($params);
  339. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  340. $params['mall_id'] = $this->mall_id;
  341. $params['user_id'] = $this->user_id;
  342. $model = AddonsOperatingNoteComment::setData($params);
  343. if ($model) {
  344. $user['id'] = $this->user->id;
  345. $user['mobile'] = $this->user->mobile;
  346. $user['nickname'] = $this->user->nickname;
  347. $user['nickname'] = $this->user->nickname;
  348. $user['avatar_url'] = $this->user->avatar_url;
  349. $user['parent_id'] = $this->user->parent_id;
  350. $user['username'] = $this->user->username;
  351. $model['user'] = $user;
  352. } else {
  353. return $this->success('不允许评论');
  354. }
  355. return $this->success('发表评论成功', $model);
  356. }
  357. /**
  358. * 获取一级评论
  359. * User: wniu
  360. * Date: 2022/2/23
  361. * Time: 6:22 下午
  362. *
  363. */
  364. public function actionGetComment()
  365. {
  366. $params = \Yii::$app->request->get();
  367. $params['where'][] = ['mall_id' => $this->mall_id];
  368. $params['where'][] = ['note_content_id' => $params['id']];
  369. $params['where'][] = ['level' => 1];
  370. $params['where'][] = ['status' => StatusEnum::ENABLED];
  371. $params['order'] = 'is_top desc,top_time desc,created_at desc';
  372. $params['with'] = ['user'];
  373. $list = AddonsOperatingNoteComment::listPage($params, false, true);
  374. if ($list['list']) {
  375. $ids = array_column($list['list'], 'id');
  376. $reply = AddonsOperatingNoteComment::lists(['where' => [['in', 'reply_id', $ids], ['status' => StatusEnum::ENABLED]], 'order' => 'created_at desc', 'group' => 'reply_id', 'select' => 'count(*) as count,user_id,reply_id,content,created_at']);
  377. if ($reply) {
  378. foreach ($list['list'] as $k => &$v) {
  379. foreach ($reply as $item => $value) {
  380. if ($v['id'] == $value['reply_id']) {
  381. $v['reply'][] = $value;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. return $this->success('操作成功', $list);
  388. }
  389. /**
  390. * 获取二级评论
  391. * User: wniu
  392. * Date: 2022/2/23
  393. * Time: 6:22 下午
  394. *
  395. */
  396. public function actionGetReply()
  397. {
  398. $params = \Yii::$app->request->get();
  399. $params['where'][] = ['reply_id' => $params['reply_id']];
  400. $params['where'][] = ['status' => StatusEnum::ENABLED];
  401. $params['with'] = ['user'];
  402. $list = AddonsOperatingNoteComment::listPage($params, false, true);
  403. if (isset($params['page']) && $params['page'] == 1) {
  404. if (isset($list['list'][0])) {
  405. array_shift($list['list']);
  406. }
  407. }
  408. return $this->success('操作成功', $list);
  409. }
  410. /**
  411. * 分享
  412. * User: wniu
  413. * Date: 2022/2/24
  414. * Time: 3:49 下午
  415. *
  416. * @throws \yii\db\Exception
  417. */
  418. public function actionShare()
  419. {
  420. $params = \Yii::$app->request->post();
  421. $res = \Yii::$app->services->validate->validate($params, [
  422. [['id',], 'required'],
  423. ]);
  424. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  425. $data = AddonsOperatingNoteShareRecord::setData($params['id'], $this->user_id);
  426. return $this->success('分享成功', $data);
  427. }
  428. /**
  429. * 点赞
  430. * User: wniu
  431. * Date: 2022/2/24
  432. * Time: 3:50 下午
  433. *
  434. */
  435. public function actionLikes()
  436. {
  437. $params = \Yii::$app->request->post();
  438. $res = \Yii::$app->services->validate->validate($params, [
  439. [['id',], 'required'],
  440. ]);
  441. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  442. $msg = AddonsOperatingNoteUserLike::setData($params['id'], $this->user_id, $this->mall_id);
  443. return $this->success($msg . '成功');
  444. }
  445. /**
  446. * 购买笔记
  447. * User: wniu
  448. * Date: 2022/2/25
  449. * Time: 3:17 下午
  450. *
  451. */
  452. public function actionBuy()
  453. {
  454. $params = \Yii::$app->request->post();
  455. $res = \Yii::$app->services->validate->validate($params, [
  456. [['id',], 'required'],
  457. ]);
  458. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  459. $data = AddonsOperatingNoteBuy::userBuyNote($params['id'], $this->user_id);
  460. if ($data === false) return $this->error(AddonsOperatingNoteBuy::getStaticError());
  461. return $this->success('成功', $data);
  462. }
  463. /**
  464. * 获取浏览奖励
  465. * User: wniu
  466. * Date: 2022/3/9
  467. * Time: 4:18 下午
  468. *
  469. */
  470. public function actionViewReward()
  471. {
  472. $params = \Yii::$app->request->post();
  473. $res = \Yii::$app->services->validate->validate($params, [
  474. [['id',], 'required'],
  475. ]);
  476. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  477. $data = AddonsOperatingNoteContent::viewReward($params['id'], $this->user);
  478. return $this->success('成功', $data);
  479. }
  480. public function actionConfig()
  481. {
  482. $Platform_config = PlatformSetting::getConfByGroup('system', true);
  483. $config = Yii::$app->services->setting->addons->get($this->mall_id, NoteEnum::ADDONS_NAME, 'basic');
  484. $config['web_url'] = $Platform_config['h5_url']['value'];
  485. $config['mall_sign'] = $this->mall['mall_sign'];
  486. $this->success('获取成功', $config);
  487. }
  488. public function actionGetUserInfo()
  489. {
  490. $params = \Yii::$app->request->get();
  491. $res = \Yii::$app->services->validate->validate($params, [
  492. [['user_id',], 'required'],
  493. ]);
  494. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  495. $data = User::getUser([['id' => $params['user_id']]], 'nickname,avatar_url,mobile');
  496. $this->success('获取成功', $data);
  497. }
  498. }