人人商城

post.ctrl.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. uni_user_permission_check('material_mass');
  8. $_W['page']['title'] = '新增素材-微信素材';
  9. $dos = array('edit', 'thumb', 'details', 'image', 'submit');
  10. $do = in_array($do, $dos) ? $do : 'edit';
  11. if($do == 'edit') {
  12. template('material/post');
  13. }
  14. if($do == 'thumb') {
  15. $post = $_GPC['__input'];
  16. $thumb = $post['val'];
  17. load()->func('file');
  18. $thumb = file_fetch(tomedia($thumb), 2048, 'material/images');
  19. if(is_error($thumb)) {
  20. message($thumb, '', 'ajax');
  21. }
  22. load()->model('account');
  23. $acc = WeAccount::create($_W['acid']);
  24. $token = $acc->getAccessToken();
  25. if (is_error($token)) {
  26. message(error(-1, $token['message']), '', 'ajax');
  27. }
  28. $fullname = ATTACHMENT_ROOT . $thumb;
  29. $sendapi = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type=thumb";
  30. $data = array(
  31. 'media' => '@'.$fullname
  32. );
  33. load()->func('communication');
  34. $resp = @ihttp_request($sendapi, $data);
  35. if(is_error($resp)) {
  36. message($resp, '', 'ajax');
  37. }
  38. $content = @json_decode($resp['content'], true);
  39. if(empty($content)) {
  40. message(error(-1, "接口调用失败, 元数据: {$resp['meta']}"), '', 'ajax');
  41. }
  42. if(!empty($content['errcode'])) {
  43. $message = "访问微信接口错误, 错误代码: {$content['errcode']}, 错误信息: {$content['errmsg']}";
  44. message(error(-1, $message), '', 'ajax');
  45. }
  46. message(error(0, $content), '', 'ajax');
  47. }
  48. if($do == 'details') {
  49. $post = $_GPC['__input'];
  50. $images = array();
  51. foreach($post as $key => $val) {
  52. $match = array();
  53. preg_match_all('/<img.*src=[\'"](.*\.(?:png|jpg|jpeg|jpe|gif))[\'"].*\/?>/iU', $val['val'], $match);
  54. if(!empty($match[1])) {
  55. foreach($match[1] as $val) {
  56. if((strexists($val, 'http://') || strexists($val, 'https://')) && !strexists($val, 'mmbiz.qlogo.cn') && !strexists($val, 'mmbiz.qpic.cn')) {
  57. $images[] = $val;
  58. } else {
  59. if(strexists($val, './attachment/images/')) {
  60. $images[] = tomedia($val);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. message(error(0, $images), '', 'ajax');
  67. }
  68. if($do == 'image') {
  69. $post = $_GPC['__input'];
  70. $thumb = $post['image'];
  71. $hasimgs = $post['hasimgs'];
  72. $wximgs = $post['wximgs'];
  73. if($index = in_array($thumb, $hasimgs)) {
  74. message(error(0, $wximgs[$index]), '', 'ajax');
  75. }
  76. if(empty($thumb)) {
  77. message(error(0, ''), '', 'ajax');
  78. }
  79. load()->func('file');
  80. $thumb = file_fetch(tomedia($thumb), 1024, 'material/images');
  81. if(is_error($thumb)) {
  82. message($thumb, '', 'ajax');
  83. }
  84. load()->model('account');
  85. $acc = WeAccount::create($_W['acid']);
  86. $token = $acc->getAccessToken();
  87. if (is_error($token)) {
  88. message(error(-1, $token['message']), '', 'ajax');
  89. }
  90. $fullname = ATTACHMENT_ROOT . $thumb;
  91. $sendapi = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$token}";
  92. $data = array(
  93. 'media' => '@'.$fullname
  94. );
  95. load()->func('communication');
  96. $resp = @ihttp_request($sendapi, $data);
  97. if(is_error($resp)) {
  98. message($resp, '', 'ajax');
  99. }
  100. $content = @json_decode($resp['content'], true);
  101. if(empty($content)) {
  102. message(error(-1, "接口调用失败, 元数据: {$resp['meta']}"), '', 'ajax');
  103. }
  104. if(!empty($content['errcode'])) {
  105. $message = "访问微信接口错误, 错误代码: {$content['errcode']}, 错误信息: {$content['errmsg']}";
  106. message(error(-1, $message), '', 'ajax');
  107. }
  108. message(error(0, $content['url']), '', 'ajax');
  109. }
  110. if($do == 'submit') {
  111. $post = $_GPC['__input'];
  112. $hasimgs = $post['hasimgs'];
  113. $wximgs = $post['wximgs'];
  114. $need_sort = false;
  115. foreach($post['items'] as $key => $val) {
  116. if ($val['displayorder'] != '0') {
  117. $need_sort = true;
  118. }
  119. $displayorder[$key] = $val['displayorder'];
  120. }
  121. if (!empty($need_sort)) {
  122. array_multisort($displayorder, SORT_DESC, SORT_NUMERIC, $post['items']);
  123. }
  124. foreach($post['items'] as &$reply) {
  125. if(!empty($hasimgs)) {
  126. $reply['content'] = str_replace($hasimgs, $wximgs, $reply['content']);
  127. }
  128. $row = array(
  129. 'title' => $reply['title'],
  130. 'author' => $reply['author'],
  131. 'digest' => $reply['description'],
  132. 'content' => $reply['content'],
  133. 'show_cover_pic' => intval($reply['incontent']),
  134. 'content_source_url' => $reply['url'],
  135. 'thumb_media_id' => $reply['media_id'],
  136. );
  137. $articles['articles'][] = $row;
  138. }
  139. $acc = WeAccount::create($_W['acid']);
  140. $result = $acc->addMatrialNews($articles);
  141. if(is_error($result)) {
  142. message($result, '', 'ajax');
  143. }
  144. message(error(0, ''), '', 'ajax');
  145. }