WechatNewsCategory.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\admin\v1\wechat;
  12. use app\controller\admin\AuthController;
  13. use app\services\article\ArticleServices;
  14. use app\services\user\UserBatchProcessServices;
  15. use app\services\wechat\WechatNewsCategoryServices;
  16. use think\facade\App;
  17. /**
  18. * 图文信息
  19. * Class WechatNewsCategory
  20. * @package app\controller\admin\v1\application\wechat
  21. *
  22. */
  23. class WechatNewsCategory extends AuthController
  24. {
  25. /**
  26. * 构造方法
  27. * Menus constructor.
  28. * @param App $app
  29. * @param WechatNewsCategoryServices $services
  30. */
  31. public function __construct(App $app, WechatNewsCategoryServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. }
  36. /**
  37. * 图文消息列表
  38. * @return mixed
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['page', 1],
  44. ['limit', 20],
  45. ['cate_name', '']
  46. ]);
  47. $list = $this->services->getAll($where);
  48. return $this->success($list);
  49. }
  50. /**
  51. * 图文详情
  52. * @param $id
  53. * @return mixed
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. * @throws \think\exception\DbException
  57. */
  58. public function read($id)
  59. {
  60. $info = $this->services->get($id);
  61. /** @var ArticleServices $services */
  62. $services = app()->make(ArticleServices::class);
  63. $new = $services->articlesList($info['new_id']);
  64. if ($new) $new = $new->toArray();
  65. $info['new'] = $new;
  66. return $this->success(compact('info'));
  67. }
  68. /**
  69. * 删除图文
  70. * @param $id
  71. * @return mixed
  72. */
  73. public function delete($id)
  74. {
  75. if (!$this->services->delete($id))
  76. return $this->fail('删除失败,请稍候再试!');
  77. else
  78. return $this->success('删除成功!');
  79. }
  80. /**
  81. * 新增或编辑保存
  82. * @return mixed
  83. * @throws \think\Exception
  84. * @throws \think\exception\PDOException
  85. */
  86. public function save()
  87. {
  88. $data = $this->request->postMore([
  89. ['id', 0]
  90. ]);
  91. $data['list'] = $this->request->param('list',[]);
  92. try {
  93. $id = [];
  94. $countList = count($data['list']);
  95. if (!$countList) return $this->fail('请添加图文');
  96. /** @var ArticleServices $services */
  97. $services = app()->make(ArticleServices::class);
  98. foreach ($data['list'] as $k => $v) {
  99. if ($v['title'] == '') return $this->fail('标题不能为空');
  100. if ($v['author'] == '') return $this->fail('作者不能为空');
  101. if ($v['content'] == '') return $this->fail('正文不能为空');
  102. if ($v['synopsis'] == '') return $this->fail('摘要不能为空');
  103. $v['status'] = 1;
  104. $v['add_time'] = time();
  105. if ($v['id']) {
  106. $idC = $v['id'];
  107. $services->save($v);
  108. unset($v['id']);
  109. $data['list'][$k]['id'] = $idC;
  110. $id[] = $idC;
  111. } else {
  112. $res = $services->save($v);
  113. unset($v['id']);
  114. $id[] = $res['id'];
  115. $data['list'][$k]['id'] = $res['id'];
  116. }
  117. }
  118. $countId = count($id);
  119. if ($countId != $countList) {
  120. if ($data['id']) return $this->fail('修改失败');
  121. else return $this->fail('添加失败');
  122. } else {
  123. $newsCategory['cate_name'] = $data['list'][0]['title'];
  124. $newsCategory['new_id'] = implode(',', $id);
  125. $newsCategory['sort'] = 0;
  126. $newsCategory['add_time'] = time();
  127. $newsCategory['status'] = 1;
  128. if ($data['id']) {
  129. $this->services->update($data['id'], $newsCategory, 'id');
  130. return $this->success('修改成功');
  131. } else {
  132. $this->services->save($newsCategory);
  133. return $this->success('添加成功');
  134. }
  135. }
  136. } catch (\Exception $e) {
  137. return $this->fail($e->getMessage());
  138. }
  139. }
  140. /**
  141. * 发送消息
  142. * @param int $id
  143. * @param string $wechat
  144. * $wechat 不为空 发消息 / 空 群发消息
  145. */
  146. public function push()
  147. {
  148. [$id, $uids, $all, $where] = $this->request->postMore([
  149. ['id', 0],
  150. ['user_ids', ''],
  151. ['all', 0],
  152. ['where', ""],
  153. ], true);
  154. if (!$id) return $this->fail('参数错误');
  155. if (!$uids && $all == 0) return $this->fail('请选择发送用户');
  156. $uids = is_string($uids) ? explode(',', $uids) : $uids;
  157. /** @var UserBatchProcessServices $userServices */
  158. $userServices = app()->make(UserBatchProcessServices::class);
  159. $userServices->batchProcess(99, $uids, ['id' => $id], !!$all, (array)$where);
  160. return app('json')->success('已加入消息队列,请稍后查看');
  161. }
  162. /**
  163. * 发送消息图文列表
  164. * @return mixed
  165. */
  166. public function send_news()
  167. {
  168. $where = $this->request->getMore([
  169. ['cate_name', ''],
  170. ['page', 1],
  171. ['limit', 10]
  172. ]);
  173. return $this->success($this->services->list($where));
  174. }
  175. }