NotesController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wniu
  5. * Date: 2022/2/17
  6. * Time: 2:42 下午
  7. */
  8. namespace addons\OperatingNote\backend\controllers;
  9. use addons\OperatingNote\common\enums\NoteEnum;
  10. use addons\OperatingNote\common\models\AddonsOperatingNoteCate;
  11. use addons\OperatingNote\common\models\AddonsOperatingNoteComment;
  12. use addons\OperatingNote\common\models\AddonsOperatingNoteContent;
  13. use common\enums\StatusEnum;
  14. use common\models\goods\Goods;
  15. use common\models\user\User;
  16. use common\models\user\UserLevel;
  17. use Yii;
  18. use yii\db\Exception;
  19. class NotesController extends BaseController
  20. {
  21. public $enableCsrfValidation = false;
  22. public function actionIndex()
  23. {
  24. $request = Yii::$app->request;
  25. if ($request->isAjax) {
  26. if ($request->isPost) {
  27. $post = Yii::$app->request->post();
  28. // 检查提交的参数
  29. $res = Yii::$app->services->validate->validate($post, [
  30. [['id'], 'integer'],
  31. [['title', 'note_type', 'is_release', 'cate_id', 'mobile', 'user_name', 'user_id', 'date_start', 'date_end', 'check_status'], 'safe'],
  32. [['limit'], 'default', 'value' => '15'],
  33. ]);
  34. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  35. $post = AddonsOperatingNoteContent::exceptNullVal($post);
  36. // 根据搜索手机号查询用户id
  37. if (isset($post['mobile']) && !empty($post['mobile'])) {
  38. $user_ids = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'mobile', $post['mobile'] . '%', false])->asArray()->column();
  39. if (empty($user_ids)) $this->success('查无此用户');
  40. $where[] = ['in', 'user_id', $user_ids];
  41. }
  42. // 根据搜索会员昵称查询用户id
  43. if (isset($post['user_name']) && !empty($post['user_name'])) {
  44. $user_ids = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'nickname', $post['user_name'] . '%', false])->asArray()->column();
  45. $where[] = ['in', 'user_id', $user_ids];
  46. }
  47. if (isset($post['user_id']) && !empty($post['user_id'])) {
  48. $where[] = ['=', 'user_id', $post['user_id']];
  49. }
  50. if (isset($post['cate_id']) && !empty($post['cate_id'])) {
  51. $where[] = ['=', 'cate_id', $post['cate_id']];
  52. }
  53. if (isset($post['date_start']) && isset($post['date_end'])) {
  54. $where[] = ['between', 'created_at', strtotime($post['date_start']), strtotime($post['date_end'])];
  55. }
  56. if (isset($post['title']) && !empty($post['title'])) {
  57. $where[] = ['like', 'title', $post['title']];
  58. }
  59. if (isset($post['note_type']) && ($post['note_type'] > 0)) {
  60. $where[] = ['=', 'note_type', $post['note_type']];
  61. }
  62. if (isset($post['is_release']) && ($post['is_release'] >= 0)) {
  63. $where[] = ['=', 'is_release', $post['is_release']];
  64. }
  65. if (isset($post['check_status'])) {
  66. $where[] = ['=', 'check_status', $post['check_status']];
  67. } else {
  68. $where[] = ['=', 'check_status', StatusEnum::ENABLED];
  69. }
  70. $where[] = ['=', 'mall_id', $this->mall_id];
  71. // if (in_array($post['check_status'], [StatusEnum::ENABLED, StatusEnum::DISABLED, NoteEnum::STATUS_TWO])) $where[] = ['=', 'check_status', $post['check_status']]; else $where[] = ['=', 'check_status', StatusEnum::ENABLED];
  72. $where[] = ['=', 'status', StatusEnum::ENABLED];
  73. $order = 'id desc';
  74. $params = array('where' => $where, 'order' => $order, 'limit' => $post['limit'], 'with' => ['user', 'goods']);
  75. $list = AddonsOperatingNoteContent::listPage($params, false, true);
  76. $cate_params = [
  77. 'where' => [
  78. ['mall_id' => $this->mall_id],
  79. ['status' => StatusEnum::ENABLED]
  80. ],
  81. 'select' => 'id,name',
  82. 'index' => 'id'
  83. ];
  84. $cate = AddonsOperatingNoteCate::lists($cate_params);
  85. if ($list['list']) {
  86. $h5_domain = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.web_url');
  87. if (empty($h5_domain)) {
  88. $h5_domain = \Yii::$app->request->hostInfo;
  89. } else {
  90. $h5_domain = substr($h5_domain, 0, strpos($h5_domain, '?'));
  91. }
  92. $list['domain'] = $h5_domain . 'plugins/operating_note/detail?sign=' . $this->mall['mall_sign'];
  93. foreach ($list['list'] as $k => &$v) {
  94. if ($v['user_id'] == 0) {
  95. $v['user']['avatar_url'] = '';
  96. $v['user']['nickname'] = '后台发布';
  97. }
  98. if (isset($cate[$v['cate_id']])) {
  99. $list['list'][$k]['cate_name'] = $cate[$v['cate_id']]['name'];
  100. }
  101. if ($v['note_type'] == 1) {
  102. $v['note_type_name'] = '文章';
  103. $v['share_url_base'] = $h5_domain . 'plugins/operating_note/detail?sign=' . $this->mall['mall_sign'] . '&id=';
  104. } elseif ($v['note_type'] == 2) {
  105. $v['note_type_name'] = '视频';
  106. $v['share_url_base'] = $h5_domain . 'plugins/operating_note/video/index?sign=' . $this->mall['mall_sign'] . '&content_id=';
  107. } else {
  108. $v['note_type_name'] = '广告';
  109. $v['share_url_base'] = $h5_domain . 'plugins/operating_note/detail?sign=' . $this->mall['mall_sign'] . '&id=';
  110. }
  111. $list['list'][$k]['average_time'] = 1;
  112. }
  113. }
  114. $list['cate'] = $cate;
  115. $this->success('获取成功', $list);
  116. }
  117. }
  118. return $this->render($this->action->id);
  119. }
  120. public function actionCheck()
  121. {
  122. $request = Yii::$app->request;
  123. if ($request->isAjax) {
  124. //提交内容
  125. if ($request->isPost) {
  126. $params = Yii::$app->request->post();
  127. $params['check_time'] = time();
  128. if ($params['check_status'] == 1) {
  129. $params['is_release'] = 1;
  130. }
  131. AddonsOperatingNoteContent::setData($params, 'update');
  132. return $this->success('操作成功');
  133. }
  134. }
  135. return $this->render($this->action->id);
  136. }
  137. public function actionEdit()
  138. {
  139. $request = Yii::$app->request;
  140. if ($request->isAjax) {
  141. //提交内容
  142. if ($request->isPost) {
  143. $params = Yii::$app->request->post();
  144. if (empty($params['id'])) {
  145. $scenarios = 'create';
  146. $params['mall_id'] = $this->mall_id;
  147. } else {
  148. $scenarios = 'update';
  149. }
  150. if (is_array($params['cover_img'])) {
  151. $params['cover_img'] = json_encode($params['cover_img']);
  152. }
  153. AddonsOperatingNoteContent::setData($params, $scenarios);
  154. return $this->success('操作成功');
  155. } else {
  156. $params = Yii::$app->request->get();
  157. $detail = [];
  158. if (isset($params['id'])) {
  159. $detail = AddonsOperatingNoteContent::getOne([['mall_id' => $this->mall_id, 'id' => $params['id']]], true);
  160. if ($detail['goods_id'] > 0) {
  161. $goods_info = Goods::getOne([['id' => $detail['goods_id']]], true, '', '', 'goods_name,cover_pic,price');
  162. $detail['goods_name'] = $goods_info['goods_name'];
  163. $detail['cover_pic'] = $goods_info['cover_pic'];
  164. $detail['price'] = $goods_info['price'];
  165. }
  166. }
  167. $where = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'id,name', 'order' => 'id desc'];
  168. $cate_list = AddonsOperatingNoteCate::lists($where);
  169. $user_level_list = UserLevel::getUserLevel([['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]], [], 1, 'level,name,id');
  170. $this->success('获取成功', compact('user_level_list', 'cate_list', 'detail'));
  171. }
  172. }
  173. return $this->render($this->action->id);
  174. }
  175. /**
  176. * 获取公众号文章标题&内容
  177. * User: wniu
  178. * Date: 2022/2/26
  179. * Time: 3:09 下午
  180. *
  181. */
  182. public function actionUrlContent()
  183. {
  184. $request = Yii::$app->request;
  185. //提交内容
  186. if ($request->isPost) {
  187. $params = Yii::$app->request->post();
  188. // $params['url'] = 'https://mp.weixin.qq.com/s/wx7vIx5VVlNNkxvD5QH3ug';
  189. $data = AddonsOperatingNoteContent::getUrlContent($params['url']);
  190. return $this->success('获取成功', $data);
  191. }
  192. }
  193. /**
  194. * 后台发布评论
  195. * User: wniu
  196. * Date: 2022/2/26
  197. * Time: 3:09 下午
  198. *
  199. * @throws Exception
  200. */
  201. public function actionAdminReply()
  202. {
  203. $request = Yii::$app->request;
  204. if ($request->isAjax) {
  205. if ($request->isPost) {
  206. $params = Yii::$app->request->post();
  207. // 检查提交的参数
  208. $res = Yii::$app->services->validate->validate($params, [
  209. [['note_content_id'], 'integer'],
  210. [['content', 'reply_id'], 'safe'],
  211. ]);
  212. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  213. // $params['note_content_id'] = 1;
  214. // $params['content'] = '后台评论';
  215. $params['mall_id'] = $this->mall_id;
  216. $params['user_id'] = 0;
  217. AddonsOperatingNoteComment::setData($params);
  218. return $this->success('评论成功');
  219. }
  220. }
  221. }
  222. /**
  223. * 后台搜索评论
  224. * User: wniu
  225. * Date: 2022/2/26
  226. * Time: 3:18 下午
  227. *
  228. */
  229. public function actionSearchComment()
  230. {
  231. $request = Yii::$app->request;
  232. if ($request->isAjax) {
  233. if ($request->isPost) {
  234. $post = Yii::$app->request->post();
  235. $res = Yii::$app->services->validate->validate($post, [
  236. [['note_content_id'], 'integer'],
  237. [['keyword'], 'safe'],
  238. [['limit'], 'default', 'value' => '15'],
  239. ]);
  240. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  241. $post = AddonsOperatingNoteContent::exceptNullVal($post);
  242. if (!empty($post['keyword'])) {
  243. $user_list = User::find()->where(['or',
  244. ['like', 'id', $post['keyword']],
  245. ['like', 'mobile', $post['keyword']],
  246. ['like', 'nickname', $post['keyword']]])->select('id')->asArray()->all();
  247. $user_id_arr = array_column($user_list, 'id');
  248. $where[] = ['user_id' => $user_id_arr];
  249. }
  250. $where[] = ['=', 'note_content_id', $post['note_content_id']];
  251. $where[] = ['=', 'level', NoteEnum::STATUS_ONE];
  252. $where[] = ['=', 'status', StatusEnum::ENABLED];
  253. $order = 'is_top desc,id desc';
  254. $params = array('where' => $where, 'order' => $order, 'limit' => $post['limit'], 'with' => ['user']);
  255. $list = AddonsOperatingNoteComment::listPage($params, false, true);
  256. if ($list['list']) {
  257. $ids = array_column($list['list'], 'id');
  258. $reply = AddonsOperatingNoteComment::lists(['where' => [['in', 'reply_id', $ids], ['status' => StatusEnum::ENABLED]], 'order' => 'created_at desc', 'group' => 'reply_id', 'select' => 'user_id,reply_id,content,created_at,count(reply_id) as count']);
  259. if ($reply) {
  260. foreach ($list['list'] as $k => &$v) {
  261. foreach ($reply as $item => $value) {
  262. if ($v['id'] == $value['reply_id']) {
  263. $v['reply'] = $value;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. return $this->success('获取成功', $list);
  270. }
  271. }
  272. }
  273. public function actionGetReply()
  274. {
  275. $request = Yii::$app->request;
  276. if ($request->isAjax) {
  277. if ($request->isGet) {
  278. $get = Yii::$app->request->get();
  279. $res = Yii::$app->services->validate->validate($get, [
  280. [['reply_id', 'note_content_id'], 'integer'],
  281. // [['limit'], 'default', 'value' => '15'],
  282. ]);
  283. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  284. $get = AddonsOperatingNoteContent::exceptNullVal($get);
  285. $where[] = ['=', 'note_content_id', $get['note_content_id']];
  286. $where[] = ['=', 'reply_id', $get['reply_id']];
  287. $where[] = ['=', 'status', StatusEnum::ENABLED];
  288. $order = 'is_top desc';
  289. $params = array('where' => $where, 'order' => $order, 'with' => ['user']);
  290. $list = AddonsOperatingNoteComment::listPage($params, false, true);
  291. return $this->success('获取成功', $list);
  292. }
  293. }
  294. }
  295. public function actionCommentEdit()
  296. {
  297. $request = Yii::$app->request;
  298. if ($request->isAjax) {
  299. if ($request->isPost) {
  300. $post = Yii::$app->request->post();
  301. $res = Yii::$app->services->validate->validate($post, [
  302. [['note_comment_id', 'type'], 'integer'],
  303. ]);
  304. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  305. AddonsOperatingNoteComment::editComment($post);
  306. return $this->success('成功');
  307. }
  308. }
  309. }
  310. }