Wechat.php 31 KB

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