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