WechatNewsCategory.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\article\Article;
  5. use app\models\article\ArticleContent;
  6. use think\facade\Route as Url;
  7. use app\admin\model\article\Article as ArticleModel;
  8. use app\admin\model\wechat\{WechatReply, WechatUser, WechatNewsCategory as WechatNewsCategoryModel};
  9. use crmeb\services\{FormBuilder as Form, UtilService as Util, JsonService as Json, UtilService, WechatService};
  10. /**
  11. * 图文信息
  12. * Class WechatNewsCategory
  13. * @package app\admin\controller\wechat
  14. *
  15. */
  16. class WechatNewsCategory extends AuthController
  17. {
  18. public function select($callback = '_selectNews$eb')
  19. {
  20. $where = Util::getMore([
  21. ['cate_name', ''],
  22. ['type', 1]
  23. ], $this->request);
  24. $this->assign('where', $where);
  25. $this->assign('callback', $callback);
  26. if ($where['type'] == 1)
  27. $list = WechatNewsCategoryModel::getSendAll($where);
  28. else
  29. $list = WechatNewsCategoryModel::getAll($where);
  30. $newList = $list['list']->toArray();
  31. $newList = $newList['data'];
  32. $this->assign($list);
  33. $this->assign('type',$where['type']);
  34. $this->assign('newList', $newList);
  35. return $this->fetch();
  36. }
  37. public function index()
  38. {
  39. $where = Util::getMore([
  40. ['cate_name', ''],
  41. ], $this->request);
  42. $this->assign('where', $where);
  43. $this->assign(WechatNewsCategoryModel::getAll($where));
  44. return $this->fetch();
  45. }
  46. public function create()
  47. {
  48. $f = array();
  49. $f[] = Form::input('cate_name', '分类名称')->autofocus(1);
  50. $f[] = Form::select('new_id', '图文列表')->setOptions(function () {
  51. $list = ArticleModel::getNews();
  52. $options = [];
  53. foreach ($list as $id => $roleName) {
  54. $options[] = ['label' => $roleName, 'value' => $id];
  55. }
  56. return $options;
  57. })->filterable(1)->multiple(1);
  58. $form = Form::make_post_form('编辑菜单', $f, Url::buildUrl('save'));
  59. $this->assign(compact('form'));
  60. return $this->fetch('public/form-builder');
  61. }
  62. public function save()
  63. {
  64. $data = Util::postMore([
  65. 'cate_name',
  66. ['new_id', []],
  67. ['sort', 0],
  68. ['add_time', time()],
  69. ['status', 1],]);
  70. if (!$data['cate_name']) return Json::fail('请输入图文名称');
  71. if (empty($data['new_id'])) return Json::fail('请选择图文列表');
  72. $data['new_id'] = array_unique($data['new_id']);
  73. if (count($data['new_id']) > 8) {
  74. $data['new_id'] = array_slice($data['new_id'], 0, 8);
  75. };
  76. $data['new_id'] = implode(',', $data['new_id']);
  77. WechatNewsCategoryModel::create($data);
  78. return Json::successful('添加菜单成功!');
  79. }
  80. public function edit($id)
  81. {
  82. $menu = WechatNewsCategoryModel::get($id);
  83. if (!$menu) return Json::fail('数据不存在!');
  84. $arr_new_id = array_unique(explode(',', $menu->new_id));
  85. foreach ($arr_new_id as $k => $v) {
  86. $arr_new_id[$k] = intval($v);
  87. }
  88. $f = array();
  89. $f[] = Form::input('cate_name', '分类名称', $menu['cate_name'])->autofocus(1);
  90. $f[] = Form::select('new_id', '图文列表', $arr_new_id)->setOptions(function () {
  91. $list = ArticleModel::getNews();
  92. $options = [];
  93. foreach ($list as $id => $roleName) {
  94. $options[] = ['label' => $roleName, 'value' => $id];
  95. }
  96. return $options;
  97. })->filterable(1)->multiple(1);
  98. $form = Form::make_post_form('编辑图文', $f, Url::buildUrl('update', compact('id')));
  99. $this->assign(compact('form'));
  100. return $this->fetch('public/form-builder');
  101. }
  102. public function update($id)
  103. {
  104. $data = Util::postMore([
  105. 'cate_name',
  106. ['new_id', []],
  107. ['sort', 0],
  108. ['status', 1],]);
  109. if (!$data['cate_name']) return Json::fail('请输入图文名称');
  110. if (empty($data['new_id'])) return Json::fail('请选择图文列表');
  111. if (count($data['new_id']) > 8) {
  112. $data['new_id'] = array_slice($data['new_id'], 0, 8);
  113. };
  114. $data['new_id'] = implode(',', $data['new_id']);;
  115. if (!WechatNewsCategoryModel::get($id)) return Json::fail('编辑的记录不存在!');
  116. WechatNewsCategoryModel::edit($data, $id);
  117. return Json::successful('修改成功!');
  118. }
  119. public function delete($id)
  120. {
  121. if (!WechatNewsCategoryModel::del($id))
  122. return Json::fail(WechatNewsCategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  123. else
  124. return Json::successful('删除成功!');
  125. }
  126. /**
  127. * 发送消息
  128. * @param int $id
  129. * @param string $wechat
  130. * $wechat 不为空 发消息 / 空 群发消息
  131. */
  132. public function push($id = 0, $new_id = 0, $wechat = '')
  133. {
  134. if ((!$id) || (!$new_id)) return Json::fail('参数错误');
  135. $list = WechatNewsCategoryModel::getWechatNewsItem($id, $new_id);
  136. $wechatNews = [];
  137. if ($list) {
  138. if ($list['new'] && is_array($list['new'])) {
  139. foreach ($list['new'] as $kk => $vv) {
  140. $wechatNews[$kk]['title'] = $vv['title'];
  141. $wechatNews[$kk]['image'] = $vv['image_input'];
  142. $wechatNews[$kk]['date'] = date('m月d日', time());
  143. $wechatNews[$kk]['description'] = $vv['synopsis'];
  144. $wechatNews[$kk]['id'] = $vv['id'];
  145. }
  146. }
  147. }
  148. if ($wechat != '') {//客服消息
  149. $wechatNews = WechatReply::tidyNews($wechatNews);
  150. $message = WechatService::newsMessage($wechatNews);
  151. $errorLog = [];//发送失败的用户
  152. $user = WechatUser::where('uid', 'IN', $wechat)->column('nickname,subscribe,openid', 'uid');
  153. if ($user) {
  154. foreach ($user as $v) {
  155. if ($v['subscribe'] && $v['openid']) {
  156. try {
  157. WechatService::staffService()->message($message)->to($v['openid'])->send();
  158. } catch (\Exception $e) {
  159. $errorLog[] = $v['nickname'] . '发送失败';
  160. }
  161. } else {
  162. $errorLog[] = $v['nickname'] . '没有关注发送失败(不是微信公众号用户)';
  163. }
  164. }
  165. } else return Json::fail('发送失败,参数不正确');
  166. if (!count($errorLog)) return Json::successful('全部发送成功');
  167. else return Json::successful(implode(',', $errorLog) . ',剩余的发送成功');
  168. }
  169. }
  170. public function send_news($id = '')
  171. {
  172. if ($id == '') return $this->failed('参数错误');
  173. $where = Util::getMore([
  174. ['cate_name', ''],
  175. ['type', 0]
  176. ], $this->request);
  177. $this->assign('where', $where);
  178. $this->assign('wechat', $id);
  179. $this->assign(WechatNewsCategoryModel::getSendAll($where));
  180. return $this->fetch();
  181. }
  182. public function append()
  183. {
  184. $this->assign('list', [
  185. [
  186. 'id' => 0,
  187. 'title' => '',
  188. 'author' => $this->adminInfo->real_name,
  189. 'content' => '',
  190. 'image_input' => '/system/module/wechat/news/images/image.png',
  191. 'synopsis' => '',
  192. ]
  193. ]);
  194. $this->assign('id', 0);
  195. $this->assign('author', $this->adminInfo->real_name);
  196. return $this->fetch();
  197. }
  198. public function append_save()
  199. {
  200. $data = UtilService::postMore([
  201. ['list', []],
  202. ['id', 0]
  203. ]);
  204. $id = [];
  205. $countList = count($data['list']);
  206. if (!$countList) return Json::fail('请添加图文');
  207. Article::beginTrans();
  208. foreach ($data['list'] as $k => $v) {
  209. if ($v['title'] == '') return Json::fail('标题不能为空');
  210. if ($v['author'] == '') return Json::fail('作者不能为空');
  211. if ($v['content'] == '') return Json::fail('正文不能为空');
  212. if ($v['synopsis'] == '') return Json::fail('摘要不能为空');
  213. $v['status'] = 1;
  214. $v['add_time'] = time();
  215. if ($v['id']) {
  216. $idC = $v['id'];
  217. unset($v['id']);
  218. Article::edit($v, $idC);
  219. ArticleContent::where('nid', $idC)->update(['content' => $v['content']]);
  220. $data['list'][$k]['id'] = $idC;
  221. $id[] = $idC;
  222. } else {
  223. unset($v['id']);
  224. $res = Article::create($v)->toArray();
  225. $id[] = $res['id'];
  226. $data['list'][$k]['id'] = $res['id'];
  227. ArticleContent::insert(['content' => $v['content'], 'nid' => $res['id']]);
  228. }
  229. }
  230. $countId = count($id);
  231. if ($countId != $countList) {
  232. Article::checkTrans(false);
  233. if ($data['id']) return Json::fail('修改失败');
  234. else return Json::fail('添加失败');
  235. } else {
  236. Article::checkTrans(true);
  237. $newsCategory['cate_name'] = $data['list'][0]['title'];
  238. $newsCategory['new_id'] = implode(',', $id);
  239. $newsCategory['sort'] = 0;
  240. $newsCategory['add_time'] = time();
  241. $newsCategory['status'] = 1;
  242. if ($data['id']) {
  243. WechatNewsCategoryModel::edit($newsCategory, $data['id']);
  244. return Json::successful('修改成功');
  245. } else {
  246. WechatNewsCategoryModel::create($newsCategory);
  247. return Json::successful('添加成功');
  248. }
  249. }
  250. }
  251. public function modify($id)
  252. {
  253. if (!strlen(trim($id)) || !$id) return $this->failed('参数错误');
  254. $wechatNews = WechatNewsCategoryModel::get($id);
  255. if (!$wechatNews) return $this->failed('参数错误');
  256. $wechatNews = $wechatNews->toArray();
  257. $list = Article::getArticleList($wechatNews['new_id']);
  258. $this->assign('list', $list);
  259. $this->assign('id', $id);
  260. $this->assign('author', $this->adminInfo->real_name);
  261. return $this->fetch('append');
  262. }
  263. }