WechatNewsCategory.php 11 KB

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