Wechat.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. <?php
  2. /**
  3. * 微信配置
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use app\api\controller\WechatApi;
  8. use think\facade\Db;
  9. use think\facade\Lang;
  10. /**
  11. *
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * 控制器
  16. */
  17. class Wechat extends AdminControl
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize(); // TODO: Change the autogenerated stub
  22. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/wechat.lang.php');
  23. }
  24. //公众号配置
  25. public function setting()
  26. {
  27. $wechat_model = model('wechat');
  28. if (!request()->isPost()) {
  29. //获取公众号配置信息
  30. $wx_config = $wechat_model->getOneWxconfig();
  31. View::assign('wx_config', $wx_config);
  32. //接口地址
  33. $wx_apiurl = HTTP_TYPE . $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], 'index.php')) . 'api/Wechat/index';
  34. View::assign('wx_apiurl', $wx_apiurl);
  35. return View::fetch();
  36. } else {
  37. $data = [
  38. 'token' => input('post.wx_token'),
  39. 'appid' => input('post.wx_appid'),
  40. 'appsecret' => input('post.wx_AppSecret'),
  41. 'wxname' => input('post.wx_name'),
  42. 'xcx_appid' => input('post.xcx_appid'),
  43. 'xcx_appsecret' => input('post.xcx_AppSecret'),
  44. ];
  45. //公众号二维码图片待处理
  46. $id = input('param.wx_id');
  47. if (empty($id)) {
  48. $res = $wechat_model->addWxconfig($data);
  49. } else {
  50. $res = $wechat_model->editWxconfig(array('id' => $id), $data);
  51. }
  52. if ($res) {
  53. $this->success(lang('ds_common_op_succ'));
  54. } else {
  55. $this->error(lang('ds_common_op_fail'));
  56. }
  57. }
  58. }
  59. //公众号菜单
  60. public function menu()
  61. {
  62. $wechat_model = model('wechat');
  63. $menu_list = $wechat_model->getWxmenuList(array(array('pid', '=', 0)));
  64. $menu_json = array();
  65. foreach ($menu_list as $key => $val) {
  66. $menu_json[$key] = array();
  67. $menu_json[$key][] = array('name' => 'name', 'value' => $val['name']);
  68. if ($val['type']) {
  69. $menu_json[$key][] = array('name' => 'type', 'value' => $val['type']);
  70. }
  71. if ($val['value']) {
  72. $val['value'] = json_decode($val['value'], true);
  73. foreach ($val['value'] as $k => $v) {
  74. if ($k == 'url') {
  75. if ($val['type'] == 'view') {
  76. $menu_json[$key][] = array('name' => 'url1', 'value' => $v);
  77. } else if ($val['type'] == 'miniprogram') {
  78. $menu_json[$key][] = array('name' => 'url2', 'value' => $v);
  79. }
  80. } else {
  81. $menu_json[$key][] = array('name' => $k, 'value' => $v);
  82. }
  83. }
  84. }
  85. $menu_json[$key][] = array('child' => []);
  86. $menu_sub_list = $wechat_model->getWxmenuList(array(array('pid', '=', $val['id'])));
  87. $menu_list[$key]['child'] = $menu_sub_list;
  88. if (!empty($menu_sub_list)) {
  89. foreach ($menu_sub_list as $key1 => $val1) {
  90. $temp = array();
  91. $temp[] = array('name' => 'name', 'value' => $val1['name']);
  92. if ($val1['type']) {
  93. $temp[] = array('name' => 'type', 'value' => $val1['type']);
  94. }
  95. if ($val1['value']) {
  96. $val1['value'] = json_decode($val1['value'], true);
  97. foreach ($val1['value'] as $k => $v) {
  98. if ($k == 'url') {
  99. if ($val1['type'] == 'view') {
  100. $temp[] = array('name' => 'url1', 'value' => $v);
  101. } else if ($val1['type'] == 'miniprogram') {
  102. $temp[] = array('name' => 'url2', 'value' => $v);
  103. }
  104. } else {
  105. $temp[] = array('name' => $k, 'value' => $v);
  106. }
  107. }
  108. }
  109. $menu_json[$key][count($menu_json[$key]) - 1]['child'][] = $temp;
  110. }
  111. }
  112. }
  113. View::assign('menu_list', $menu_list);
  114. View::assign('menu_json', $menu_json);
  115. $this->setAdminCurItem('menu');
  116. return View::fetch();
  117. }
  118. public function add_menu()
  119. {
  120. $menus = input('param.menus/a');
  121. $result = '';
  122. $menu_list = array();
  123. Db::startTrans();
  124. try {
  125. $wechat_model = model('wechat');
  126. $condition = array(array('id', '>', 0));
  127. $wechat_model->delWxmenu($condition);
  128. foreach ($menus as $key => $val) {
  129. $temp = array();
  130. foreach ($val as $k => $v) {
  131. if ($k != (count($val) - 1)) {
  132. $temp[$v['name']] = $v['value'];
  133. }
  134. }
  135. $id = count($menu_list) + 1;
  136. $menu_list[] = array('id' => $id, 'pid' => 0, 'child_count' => isset($val[count($val) - 1]['child']) ? count($val[count($val) - 1]['child']) : 0, 'value' => $temp, 'index1' => $key, 'index2' => -1);
  137. if (isset($val[count($val) - 1]['child'])) {
  138. foreach ($val[count($val) - 1]['child'] as $k1 => $v1) {
  139. $temp = array();
  140. foreach ($v1 as $k => $v) {
  141. $temp[$v['name']] = $v['value'];
  142. }
  143. $menu_list[] = array('id' => count($menu_list) + 1, 'pid' => $id, 'value' => $temp, 'index1' => $key, 'index2' => $k1);
  144. }
  145. }
  146. }
  147. $menu_array = array();
  148. foreach ($menu_list as $val) {
  149. if (trim($val['value']['name']) == '') {
  150. $result = array('index1' => $val['index1'], 'index2' => $val['index2'], 'name' => 'name');
  151. throw new \think\Exception('', 10006);
  152. }
  153. if (isset($val['child_count']) && $val['child_count'] > 0) {
  154. $menu_array[] = array(
  155. 'id' => $val['id'],
  156. 'pid' => $val['pid'],
  157. 'name' => $val['value']['name'],
  158. 'type' => '',
  159. 'value' => ''
  160. );
  161. } else {
  162. $temp = array(
  163. 'id' => $val['id'],
  164. 'pid' => $val['pid'],
  165. 'name' => $val['value']['name'],
  166. 'type' => $val['value']['type'],
  167. );
  168. switch ($val['value']['type']) {
  169. case 'article_id':
  170. if (trim($val['value']['article_id']) == '') {
  171. $result = array('index1' => $val['index1'], 'index2' => $val['index2'], 'name' => 'article_id');
  172. throw new \think\Exception('', 10006);
  173. }
  174. $temp['value'] = json_encode(array('article_id' => $val['value']['article_id']));
  175. break;
  176. case 'click':
  177. if (trim($val['value']['key']) == '') {
  178. $result = array('index1' => $val['index1'], 'index2' => $val['index2'], 'name' => 'key');
  179. throw new \think\Exception('', 10006);
  180. }
  181. $temp['value'] = json_encode(array('key' => $val['value']['key']));
  182. break;
  183. case 'view':
  184. if (trim($val['value']['url1']) == '') {
  185. $result = array('index1' => $val['index1'], 'index2' => $val['index2'], 'name' => 'url1');
  186. throw new \think\Exception('', 10006);
  187. }
  188. $temp['value'] = json_encode(array('url' => $val['value']['url1']));
  189. break;
  190. case 'miniprogram':
  191. if (trim($val['value']['url2']) == '' || trim($val['value']['appid']) == '' || trim($val['value']['pagepath']) == '') {
  192. $result = array('index1' => $val['index1'], 'index2' => $val['index2'], 'name' => ((trim($val['value']['url2']) == '') ? 'url2' : ((trim($val['value']['appid']) == '') ? 'appid' : 'pagepath')));
  193. throw new \think\Exception('', 10006);
  194. }
  195. $temp['value'] = json_encode(array('url' => $val['value']['url2'], 'appid' => $val['value']['appid'], 'pagepath' => $val['value']['pagepath']));
  196. break;
  197. default:
  198. $result = array('index1' => $val['index1'], 'index2' => $val['index2'], 'name' => 'type');
  199. throw new \think\Exception('', 10006);
  200. }
  201. $menu_array[] = $temp;
  202. }
  203. }
  204. if (!empty($menu_array)) {
  205. Db::name('wxmenu')->insertAll($menu_array);
  206. }
  207. } catch (\Exception $e) {
  208. Db::rollback();
  209. ds_json_encode(10001, $e->getMessage(), $result);
  210. }
  211. Db::commit();
  212. ds_json_encode(10000, lang('ds_common_op_succ'));
  213. }
  214. //更新公众号菜单
  215. public function pub_menu()
  216. {
  217. //获取父级菜单
  218. $wechat_model = model('wechat');
  219. //获取菜单
  220. $config = $wechat_model->getOneWxconfig();
  221. $p_menus = $wechat_model->getWxmenuList(array('pid' => 0), 'id ASC');
  222. $p_menus = ds_change_arraykey($p_menus, 'id');
  223. $post_str = $this->convert_menu($p_menus);
  224. // http post请求
  225. if (!count($p_menus) > 0) {
  226. $this->error(lang('no_menu'), 'Wechat/menu');
  227. exit;
  228. }
  229. //查看access_token是否过期
  230. $wechat = new WechatApi($config);
  231. $expire_time = $config['expires_in'];
  232. if ($expire_time > TIMESTAMP) {
  233. //有效期内
  234. $wechat->access_token_ = $config['access_token'];
  235. } else {
  236. $access_token = $wechat->checkAuth();
  237. if ($access_token == FALSE) {
  238. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg, 'Wechat/menu');
  239. }
  240. $web_expires = TIMESTAMP + 7000; // 提前200秒过期
  241. $condition = array();
  242. $condition[] = array('id', '=', $config['id']);
  243. $data = array('access_token' => $access_token, 'expires_in' => $web_expires);
  244. $wechat_model->editWxconfig($condition, $data);
  245. }
  246. $return = $wechat->createMenu($post_str);
  247. if ($return) {
  248. $this->success(lang('ds_common_op_succ'), 'Wechat/menu');
  249. } else {
  250. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  251. }
  252. }
  253. //菜单转换
  254. private function convert_menu($p_menus)
  255. {
  256. $wechat_model = model('wechat');
  257. $new_arr = array();
  258. $count = 0;
  259. foreach ($p_menus as $k => $v) {
  260. $new_arr[$count]['name'] = $v['name'];
  261. //获取子菜单
  262. $c_menus = $wechat_model->getMenulist(array('pid' => $k));
  263. if ($c_menus) {
  264. foreach ($c_menus as $kk => $vv) {
  265. $add = array();
  266. $add['name'] = $vv['name'];
  267. $add['type'] = $vv['type'];
  268. $add = array_merge($add, json_decode($vv['value'], true));
  269. $new_arr[$count]['sub_button'][] = $add;
  270. }
  271. } else {
  272. $new_arr[$count]['type'] = $v['type'];
  273. $new_arr[$count] = array_merge($new_arr[$count], json_decode($v['value'], true));
  274. }
  275. $count++;
  276. }
  277. return array('button' => $new_arr);
  278. }
  279. /**
  280. * 关键字文本回复
  281. */
  282. public function k_text()
  283. {
  284. $wechat_model = model('wechat');
  285. $wechat = $wechat_model->getOneWxconfig();
  286. if (empty($wechat)) {
  287. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  288. }
  289. $lists = $wechat_model->getWxkeywordList(array('type' => 'TEXT'), 'k.id,k.keyword,t.text', 10, 't.createtime DESC');
  290. View::assign('lists', $lists);
  291. View::assign('show_page', $wechat_model->page_info->render());
  292. return View::fetch();
  293. }
  294. /*
  295. * 添加文本回复
  296. */
  297. public function text_form()
  298. {
  299. $wechat_model = model('wechat');
  300. $wechat = $wechat_model->getOneWxconfig();
  301. if (empty($wechat)) {
  302. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  303. }
  304. if (request()->isPost()) {
  305. $kid = input('param.id');
  306. $add['keyword'] = input('param.keyword');
  307. $add['text'] = input('param.text');
  308. if (empty($kid)) {
  309. //添加模式
  310. $add['createtime'] = TIMESTAMP;
  311. $add['pid'] = $wechat_model->addWxtext($add);
  312. unset($add['text']);
  313. unset($add['createtime']);
  314. $add['type'] = 'TEXT';
  315. $row = $wechat_model->addWxkeyword($add);
  316. } else {
  317. //编辑模式
  318. $data = $wechat_model->getOneWxkeyword(array('id' => $kid));
  319. if ($data) {
  320. $update['keyword'] = $add['keyword'];
  321. $wechat_model->editWxkeyword(array('id' => $kid), $update);
  322. $row = $wechat_model->editWxtext(array('id' => $data['pid']), $add);
  323. }
  324. }
  325. $row >= 0 ? dsLayerOpenSuccess(lang('ds_common_op_succ')) : $this->error(lang('ds_common_op_fail'), 'Wechat/k_text');
  326. exit;
  327. } else {
  328. //编辑状态
  329. $id = intval(input('param.id'));
  330. $key = array();
  331. if ($id) {
  332. $where = "k.id={$id} AND k.type='TEXT'";
  333. $res = Db::name('wxkeyword')->alias('k')->join('wxtext t', 't.id=k.id', 'LEFT')->where($where)->field('k.id,k.keyword,t.text')->find();
  334. View::assign('key', $res);
  335. }
  336. return View::fetch();
  337. }
  338. }
  339. /*
  340. * 删除文本回复
  341. */
  342. public function del_text()
  343. {
  344. $wechat_model = model('wechat');
  345. $id = input('param.id');
  346. $row = $wechat_model->getOneWxkeyword(array('id' => $id));
  347. if ($row) {
  348. $wechat_model->delWxkeyword(array('id' => $id));
  349. $wechat_model->delWxtext(array('id' => $row['pid']));
  350. ds_json_encode(10000, lang('ds_common_del_succ'));
  351. } else {
  352. ds_json_encode(10001, lang('ds_common_del_fail'));
  353. }
  354. }
  355. /**
  356. * 删除消息推送
  357. */
  358. public function del_wxmsg()
  359. {
  360. $wechat_model = model('wechat');
  361. $id = input('param.id');
  362. $id_array = ds_delete_param($id);
  363. if ($id_array === FALSE) {
  364. ds_json_encode(10001, lang('param_error'));
  365. }
  366. $condition = array(array('id', 'in', $id_array));
  367. $result = $wechat_model->delWxmsg($condition);
  368. if ($result) {
  369. ds_json_encode(10000, lang('ds_common_del_succ'));
  370. } else {
  371. ds_json_encode(10001, lang('ds_common_del_fail'));
  372. }
  373. }
  374. /* * 微信注册会员列表 */
  375. public function member()
  376. {
  377. $wechat_model = model('wechat');
  378. $wxmember_list = $wechat_model->getWxmemberList();
  379. foreach ($wxmember_list as $key => $val) {
  380. @$wxmember_list[$key]['member_wxinfo'] = unserialize($val['member_wxinfo']);
  381. }
  382. View::assign('show_page', $wechat_model->page_info->render());
  383. View::assign('wxmember_list', $wxmember_list);
  384. return View::fetch('member');
  385. }
  386. /* * 消息推送 */
  387. public function msend()
  388. {
  389. $touser = input('param.openid');
  390. $id = input('param.member_id');
  391. if (request()->isPost()) {
  392. $wechat_model = model('wechat');
  393. $config = $wechat_model->getOneWxconfig();
  394. $wechat = new WechatApi($config);
  395. $type = input('param.type');
  396. if ($type == 'text') {
  397. //发送文本消息
  398. $content = input('param.text');
  399. $send = array(
  400. 'touser' => $touser, 'msgtype' => 'text', 'text' => array('content' => $content)
  401. );
  402. } else {
  403. //发送图文消息
  404. $title = input('param.title');
  405. $description = input('param.description');
  406. $url = input('param.url');
  407. $picUrl = '';
  408. if (!empty($_FILES['s_pic']['name'])) {
  409. $prefix = $id;
  410. $file_name = $prefix . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  411. $res = ds_upload_pic(DIR_ADMIN . DIRECTORY_SEPARATOR . 'wechat', 's_pic', $file_name);
  412. if ($res['code']) {
  413. $file_name = $res['data']['file_name'];
  414. } else {
  415. $this->error($res['msg']);
  416. }
  417. $picUrl = ds_get_pic(DIR_ADMIN . DIRECTORY_SEPARATOR . 'wechat', $file_name);
  418. }
  419. $content = array(
  420. array(
  421. 'title' => $title, 'description' => $description, 'url' => $url, 'picurl' => $picUrl
  422. )
  423. );
  424. $send = array(
  425. 'touser' => $touser, 'msgtype' => 'news', 'news' => array('articles' => $content)
  426. );
  427. }
  428. $SendInfo = serialize($send);
  429. $data['member_id'] = $id;
  430. $data['content'] = $SendInfo;
  431. $data['createtime'] = TIMESTAMP;
  432. $ret = $wechat->sendCustomMessage($send);
  433. if ($ret) {
  434. //添加至推送列表
  435. $data['issend'] = '1';
  436. $wechat_model->addWxmsg($data);
  437. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  438. } else {
  439. $data['issend'] = '0';
  440. $wechat_model->addWxmsg($data);
  441. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  442. }
  443. } else {
  444. return View::fetch();
  445. }
  446. }
  447. /* * 消息推送列表 */
  448. public function SendList()
  449. {
  450. $wechat_model = model('wechat');
  451. $list = $wechat_model->getWxmsgList();
  452. foreach ($list as $key => $val) {
  453. $info = unserialize($val['content']);
  454. $type = $info['msgtype'];
  455. $list[$key]['type'] = $type == 'text' ? lang('message_type_text') : lang('message_type_news');
  456. if ($type == 'text') {
  457. $list[$key]['content'] = $info['text']['content'];
  458. } else {
  459. $content = $info['news']['articles']['0'];
  460. $content = json_encode($content);
  461. $list[$key]['content'] = "<a href='javascript:void(0);' class='news' content=''>查看图文消息</a>";
  462. /* View::assign('title',$content['title']);
  463. View::assign('description',$content['description']);
  464. View::assign('url',$content['url']);
  465. echo View::fetch('news'); */
  466. }
  467. }
  468. View::assign('show_page', $wechat_model->page_info->render());
  469. View::assign('lists', $list);
  470. return View::fetch('list');
  471. }
  472. /* * 消息群发 */
  473. public function Sendgroup()
  474. {
  475. if (request()->isPost()) {
  476. $m_info = model('wechat')->getWxmemberList();
  477. $openid = '';
  478. foreach ($m_info as $k => $val) {
  479. $openid .= $val['member_wxopenid'] . ',';
  480. }
  481. $openid = explode(',', $openid);
  482. $content = input('param.text');
  483. $send = array(
  484. 'touser' => $openid,
  485. 'msgtype' => 'text',
  486. 'text' => array('content' => $content)
  487. );
  488. $config = model('wechat')->getOneWxconfig();
  489. $wechat = new WechatApi($config);
  490. $res = $wechat->massSend($send);
  491. if ($res) {
  492. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  493. } else {
  494. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  495. }
  496. } else {
  497. return View::fetch('sendgroup');
  498. }
  499. }
  500. public function material()
  501. {
  502. $wechat_model = model('wechat');
  503. $wechat = $wechat_model->getOneWxconfig();
  504. if (empty($wechat)) {
  505. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  506. }
  507. $template = array(
  508. 'offset' => (input('param.page', 1) - 1) * 10,
  509. 'count' => 10,
  510. );
  511. $res = $wechat_model->getMaterialList($template);
  512. if (!$res['code']) {
  513. $this->error($res['msg']);
  514. }
  515. $paginate = Db::name('wxconfig')->paginate(10, $res['data']['total_count'], ['query' => request()->param()]);
  516. View::assign('show_page', $paginate->render());
  517. View::assign('list', $res['data']['item']);
  518. $this->setAdminCurItem('material');
  519. return View::fetch();
  520. }
  521. public function freepublish()
  522. {
  523. $wechat_model = model('wechat');
  524. $wechat = $wechat_model->getOneWxconfig();
  525. if (empty($wechat)) {
  526. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  527. }
  528. $template = array(
  529. 'offset' => (input('param.page', 1) - 1) * 10,
  530. 'count' => 10,
  531. );
  532. $res = $wechat_model->getFreepublishList($template);
  533. if (!$res['code']) {
  534. $this->error($res['msg']);
  535. }
  536. $paginate = Db::name('wxconfig')->paginate(10, $res['data']['total_count'], ['query' => request()->param()]);
  537. View::assign('show_page', $paginate->render());
  538. View::assign('list', $res['data']['item']);
  539. $this->setAdminCurItem('freepublish');
  540. return View::fetch();
  541. }
  542. public function freepublish_del()
  543. {
  544. $wechat_model = model('wechat');
  545. $wechat = $wechat_model->getOneWxconfig();
  546. $res = $wechat_model->delFreepublish(input('param.article_id'));
  547. if (!$res['code']) {
  548. ds_json_encode(10001, $res['msg']);
  549. }
  550. ds_json_encode(10000, lang('ds_common_op_succ'));
  551. }
  552. public function material_select()
  553. {
  554. $wechat_model = model('wechat');
  555. $wechat = $wechat_model->getOneWxconfig();
  556. if (empty($wechat)) {
  557. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  558. }
  559. $template = array(
  560. 'type' => 'news',
  561. 'offset' => (input('param.page', 1) - 1) * 10,
  562. 'count' => 10,
  563. );
  564. $res = $wechat_model->getFreepublishList($template);
  565. if (!$res['code']) {
  566. $this->error($res['msg']);
  567. }
  568. $paginate = Db::name('wxconfig')->paginate(10, $res['data']['total_count'], ['query' => request()->param()]);
  569. View::assign('show_page', $paginate->render());
  570. View::assign('list', $res['data']['item']);
  571. return View::fetch();
  572. }
  573. public function material_add()
  574. {
  575. if (request()->isPost()) {
  576. $wechat_model = model('wechat');
  577. $wechat = $wechat_model->getOneWxconfig();
  578. $temp = input('param.articles/a');
  579. $articles = array();
  580. foreach ($temp as $key => $val) {
  581. $a = array();
  582. foreach ($val as $v) {
  583. if (in_array($v['name'], ['thumb_media_id', 'title', 'author', 'content', 'content_source_url', 'need_open_comment', 'only_fans_can_comment'])) {
  584. if (in_array($v['name'], ['thumb_media_id', 'title', 'content', 'content_source_url']) && $v['value'] == '') {
  585. ds_json_encode(10001, '', array('index' => $key, 'name' => $v['name']));
  586. }
  587. $a[$v['name']] = $v['value'];
  588. }
  589. }
  590. $articles[] = $a;
  591. }
  592. $template = array(
  593. 'articles' => $articles
  594. );
  595. $res = $wechat_model->addMaterial($template);
  596. if (!$res['code']) {
  597. ds_json_encode(10001, $res['msg']);
  598. }
  599. ds_json_encode(10000, lang('ds_common_op_succ'));
  600. } else {
  601. $this->setAdminCurItem('material_add');
  602. return View::fetch('material_form');
  603. }
  604. }
  605. public function get_freepublish()
  606. {
  607. $wechat_model = model('wechat');
  608. $wechat = $wechat_model->getOneWxconfig();
  609. $article_id = input('param.article_id');
  610. return $wechat_model->getFreepublishInfo($article_id);
  611. }
  612. public function material_edit()
  613. {
  614. $wechat_model = model('wechat');
  615. $wechat = $wechat_model->getOneWxconfig();
  616. $media_id = input('param.media_id');
  617. $res = $wechat_model->getMaterialInfo($media_id);
  618. if (request()->isPost()) {
  619. if (!$res['code']) {
  620. ds_json_encode(10001, $res['msg']);
  621. }
  622. $material_info = $res['data']['news_item'];
  623. $temp = input('param.articles/a');
  624. $index = 0;
  625. foreach ($temp as $key => $val) {
  626. $a = array();
  627. foreach ($val as $v) {
  628. if (in_array($v['name'], ['thumb_media_id', 'title', 'author', 'content', 'content_source_url', 'need_open_comment', 'only_fans_can_comment'])) {
  629. if (in_array($v['name'], ['thumb_media_id', 'title', 'content', 'content_source_url']) && $v['value'] == '') {
  630. ds_json_encode(10001, '', array('index' => $key, 'name' => $v['name']));
  631. }
  632. $a[$v['name']] = $v['value'];
  633. }
  634. }
  635. if (!isset($material_info[$index])) {
  636. ds_json_encode(10001, '图文消息(' . $index . ')不存在');
  637. }
  638. ksort($a);
  639. ksort($material_info[$index]);
  640. if (json_encode($a) != json_encode($material_info[$index])) {
  641. $template = array(
  642. 'media_id' => $media_id,
  643. 'index' => $index,
  644. 'articles' => $a
  645. );
  646. $res = $wechat_model->editMaterial($template);
  647. if (!$res['code']) {
  648. ds_json_encode(10001, $res['msg']);
  649. }
  650. }
  651. $index++;
  652. }
  653. ds_json_encode(10000, lang('ds_common_op_succ'));
  654. } else {
  655. if (!$res['code']) {
  656. $this->error($res['msg']);
  657. }
  658. foreach ($res['data']['news_item'] as $key => $val) {
  659. $res['data']['news_item'][$key]['content'] = str_replace('data-src', 'src', $val['content']);
  660. }
  661. View::assign('material_info', $res['data']['news_item']);
  662. $this->setAdminCurItem('material_edit');
  663. return View::fetch('material_form');
  664. }
  665. }
  666. public function material_del()
  667. {
  668. $wechat_model = model('wechat');
  669. $wechat = $wechat_model->getOneWxconfig();
  670. $res = $wechat_model->delMaterial(input('param.media_id'));
  671. if (!$res['code']) {
  672. ds_json_encode(10001, $res['msg']);
  673. }
  674. ds_json_encode(10000, lang('ds_common_op_succ'));
  675. }
  676. public function freepublish_submit()
  677. {
  678. $wechat_model = model('wechat');
  679. $wechat = $wechat_model->getOneWxconfig();
  680. $res = $wechat_model->submitFreepublish(input('param.media_id'));
  681. if (!$res['code']) {
  682. ds_json_encode(10001, $res['msg']);
  683. }
  684. ds_json_encode(10000, lang('ds_common_op_succ'));
  685. }
  686. public function get_material_image()
  687. {
  688. $wechat_model = model('wechat');
  689. $wechat = $wechat_model->getOneWxconfig();
  690. return $wechat_model->getImage(input('param.media_id'));
  691. }
  692. public function upload_material_image()
  693. {
  694. $type = intval(input('param.type'));
  695. $wechat_model = model('wechat');
  696. $wechat = $wechat_model->getOneWxconfig();
  697. $res = $wechat_model->uploadMaterialImage($_FILES['file'], $type);
  698. if (!$res['code']) {
  699. ds_json_encode(10001, $res['msg']);
  700. }
  701. ds_json_encode(10000, '', $res['data']);
  702. }
  703. protected function getAdminItemList()
  704. {
  705. if (strpos(request()->action(), 'material') !== false || strpos(request()->action(), 'freepublish') !== false) {
  706. $menu_array = array(
  707. array(
  708. 'name' => 'material',
  709. 'text' => lang('draft_list'),
  710. 'url' => (string) url('Wechat/material')
  711. ),
  712. array(
  713. 'name' => 'freepublish',
  714. 'text' => lang('freepublish_list'),
  715. 'url' => (string) url('Wechat/freepublish')
  716. ),
  717. );
  718. if (request()->action() == 'material_edit') {
  719. $menu_array[] = array(
  720. 'name' => 'material_edit',
  721. 'text' => lang('ds_edit'),
  722. 'url' => 'javascript:void(0)'
  723. );
  724. } else {
  725. $menu_array[] = array(
  726. 'name' => 'material_add',
  727. 'text' => lang('ds_new'),
  728. 'url' => (string) url('Wechat/material_add')
  729. );
  730. }
  731. } else {
  732. $menu_array = array(
  733. array(
  734. 'name' => 'menu',
  735. 'text' => lang('wechat_menu'),
  736. 'url' => (string) url('Wechat/menu')
  737. ),
  738. );
  739. }
  740. return $menu_array;
  741. }
  742. }