MaterialsController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace App\Http\Controllers\Wechat\Material;
  3. use App\Http\Requests;
  4. use Illuminate\Http\Request;
  5. use EasyWeChat\Foundation\Application;
  6. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  7. use EasyWeChat\Message\Image;
  8. use EasyWeChat\Message\Text;
  9. use EasyWeChat\Message\Article;
  10. use GuzzleHttp\Client;
  11. /**
  12. * 素材类,包括图片,图文
  13. *
  14. */
  15. class MaterialsController
  16. {
  17. private $_errorMsg = [];
  18. public function __construct($_param)
  19. {
  20. $this->param = $_param;
  21. $this->app = $_param['app'];
  22. // 永久素材
  23. $this->material = $this->app->material;
  24. // 临时素材
  25. $this->temporary = $this->app->material_temporary;
  26. // 客服消息
  27. $this->staff = $this->app->staff;
  28. }
  29. /**
  30. * 上传客服二维码永久素材
  31. */
  32. public function upload_contact_cusomer_img($group_nick, $path)
  33. {
  34. v('upload_contact_cusomer_img:' . $group_nick . ' path:' . $path);
  35. $result = $this->upload_materials($path);
  36. v('upload_contact_cusomer_img_result');
  37. v($result);
  38. return $result;
  39. }
  40. /**
  41. * 上传永久素材
  42. */
  43. public function upload_material_img($group_nick, $path)
  44. {
  45. v('upload_material_img:' . $group_nick . ' path:' . $path);
  46. $result = $this->upload_materials($path);
  47. v('upload_material_img_result');
  48. v($result);
  49. return $result;
  50. }
  51. /**
  52. * 上传永久缩略图素材
  53. */
  54. public function upload_thumb_material_img($group_nick, $path)
  55. {
  56. v('upload_thumb_material_img:' . $group_nick . ' path:' . $path);
  57. $result = $this->upload_thumb_materials($path);
  58. v('upload_thumb_material_img_result');
  59. v($result);
  60. return $result;
  61. }
  62. /**
  63. * 上传永久素材
  64. * 可能有网络问题,最多尝试3次
  65. */
  66. public function upload_materials($path, $try_time = 0)
  67. {
  68. v('upload_materials:path:' . $path . ' try_time:' . $try_time);
  69. $result = '';
  70. if ($try_time > 2) {
  71. return false;
  72. }
  73. try {
  74. return $this->material->uploadImage($path);
  75. } catch (\Exception $e) {
  76. $this->_errorMsg['upload_materials'] = $e->getMessage();
  77. v('upload_material_img_except:' . $e->getMessage());
  78. return $this->upload_materials($path, $try_time + 1);
  79. }
  80. }
  81. /**
  82. * 上传永久缩量图素材
  83. * 可能有网络问题,最多尝试3次
  84. */
  85. public function upload_thumb_materials($path, $try_time = 0)
  86. {
  87. v('upload_thumb_materials:path:' . $path . ' try_time:' . $try_time);
  88. $result = '';
  89. if ($try_time > 2) {
  90. return false;
  91. }
  92. try {
  93. return $this->material->uploadThumb($path);
  94. } catch (\Exception $e) {
  95. v('upload_material_img_except:' . $e->getMessage());
  96. return $this->upload_thumb_materials($path, $try_time + 1);
  97. }
  98. }
  99. /**
  100. * 获取永久素材
  101. */
  102. public function get_materials(Request $request)
  103. {
  104. $result = $this->material->lists('image', $offset = 0, $count = 5);
  105. v($result);
  106. }
  107. /**
  108. * 获取永久素材列表
  109. */
  110. public function get_material_img_list()
  111. {
  112. $result = $this->material->lists('image', 0, 10);
  113. v($result);
  114. return $result;
  115. }
  116. /**
  117. * 获取永久图文素材
  118. */
  119. public function get_material_resource($media_id)
  120. {
  121. $result = $this->material->get($media_id);
  122. v($result);
  123. return $result;
  124. }
  125. /**
  126. * 检查素材是否存在了,如果存在则返回素材实体,不存在则上传
  127. */
  128. public function check_material_img_exist($name)
  129. {
  130. v('origin_name:' . $name);
  131. $img_list = $this->material->lists('image', 0, 100);
  132. foreach ($img_list->item as $img) {
  133. v($img);
  134. v('material_name:' . $img['name']);
  135. if ($img['name'] == $name) {
  136. return $img;
  137. }
  138. }
  139. return false;
  140. }
  141. /**
  142. * 得到素材实体
  143. */
  144. public function get_material_img($name)
  145. {
  146. $mateial_img = $this->check_material_img_exist($name);
  147. if (empty($mateial_img)) {
  148. // 可能会网络问题
  149. try {
  150. $update_img = $this->upload_materials($path);
  151. } catch (\Exception $e) {
  152. v('upload_material_img_except:' . $e->getMessage());
  153. }
  154. return $update_img;
  155. } else {
  156. return $mateial_img;
  157. }
  158. }
  159. /**
  160. * 推送图片消息
  161. * 48小时外:response out of time limit or subscription is canceled hint: [kSi1ga0292ge30]
  162. */
  163. public function send_image($media_id, $openid)
  164. {
  165. v('send_image_media_id:' . $media_id . ' openid:' . $openid);
  166. try {
  167. $message = new Image(['media_id' => $media_id]);
  168. $result = $this->staff->message($message)->to($openid)->send();
  169. v('send_image_result:');
  170. v($result);
  171. } catch (\Exception $e) {
  172. v('send_image_except:' . $openid . ' info:' . $e->getMessage());
  173. }
  174. }
  175. /**
  176. * 上传临时素材
  177. */
  178. public function upload_temporary_materials($path)
  179. {
  180. v('upload_temporary_materials:' . $path);
  181. // $path = "/imgs/common/luqu_notice.png";
  182. $upload_result = $this->temporary->uploadImage($path);
  183. v('upload_temporary_materials_result:');
  184. v($upload_result);
  185. $media_id = isset($upload_result->media_id) ? $upload_result->media_id : '';
  186. v('media_id:' . $media_id);
  187. return $media_id;
  188. }
  189. /**
  190. * 上传临时素材
  191. */
  192. public function upload_temporary_thumb_materials($path)
  193. {
  194. v('upload_temporary_materials:' . $path);
  195. // $path = "/imgs/common/luqu_notice.png";
  196. $upload_result = $this->temporary->uploadThumb($path);
  197. v('upload_temporary_materials_result:');
  198. v($upload_result);
  199. $media_id = isset($upload_result->media_id) ? $upload_result->media_id : '';
  200. v('media_id:' . $media_id);
  201. return $media_id;
  202. }
  203. /**
  204. * 获取临时素材
  205. */
  206. public function get_temporary_single_material($mediaId)
  207. {
  208. v('get_temporary_single_material:' . $mediaId);
  209. $content = $this->temporary->getStream($mediaId);
  210. v('get_temporary_single_material_content:' . $content);
  211. file_put_contents(public_path("/admin/imgs/download/luqu_notice.png"));
  212. return empty($content) ? false : true;
  213. }
  214. /**
  215. * 上传单篇图文
  216. */
  217. public function upload_article($data)
  218. {
  219. // 上传单篇图文
  220. $article = new Article([
  221. 'title' => $data['title'],
  222. 'thumb_media_id' => $data['thumb_media_id'],
  223. "show_cover_pic" => $data['show_cover_pic'],
  224. "author" => $data['author'],
  225. "digest" => $data['digest'],
  226. "content" => $data['content'],
  227. "content_source_url" => $data['content_source_url'],
  228. //...
  229. ]);
  230. v('$article');
  231. v($article);
  232. $result = $this->material->uploadArticle($article);
  233. v('upload_article_result:' . json_encode($result));
  234. return $result;
  235. }
  236. /**
  237. * 上传临时素材
  238. */
  239. public function upload_test($appid, $path)
  240. {
  241. v('upload_test');
  242. $upload_result = $this->material->uploadArticalMediaImage($path);
  243. v('upload_article_result:' . json_encode($upload_result));
  244. die();
  245. $component_access_token = app('wechat_op')->access_token->getToken();
  246. v('$component_access_token:' . $component_access_token);
  247. // 先上传缩量图
  248. try {
  249. $client = new Client();
  250. $form_params = [
  251. "media" => 'https://cdn-novel.iycdm.com/document_covers/2018111201/1383.jpg?x-oss-process=image/resize,w_900/format,jpg',
  252. // "media"=>$path
  253. ];
  254. v($form_params);
  255. $upload_res = $client->request("post", "https://api.weixin.qq.com/cgi-bin/media/add_material?type=thumb&access_token=" . $component_access_token,
  256. ['json' => $form_params, 'connect_timeout' => 3]
  257. )->getBody()->getContents();
  258. $upload_res = json_decode($upload_res, true);
  259. v('$upload_res:' . $appid);
  260. v($upload_res);
  261. } catch (\Exception $e) {
  262. v('upload_test_ept_appid:' . $appid . ' ept:' . $e->getMessage());
  263. }
  264. return $upload_res;
  265. }
  266. /**
  267. * 上传多篇图文,框架没方法自己写。
  268. */
  269. public function upload_articles($appid, $articles)
  270. {
  271. $articles = objectToArray(json_decode($articles));
  272. v('articles');
  273. v($articles);
  274. $component_access_token = app('wechat_op')->access_token->getToken();
  275. v('$component_access_token:' . $component_access_token);
  276. $last_articles = [];
  277. $result = [];
  278. try {
  279. foreach ($articles as $article) {
  280. $last_articles[] = new Article([
  281. 'title' => $article['title'],
  282. 'thumb_media_id' => $article['thumb_media_id'],
  283. "show_cover_pic" => $article['show_cover_pic'],
  284. "author" => $article['author'],
  285. "digest" => $article['digest'],
  286. "content" => $article['content'],
  287. "content_source_url" => $article['content_source_url'],
  288. //...
  289. ]);
  290. // 测试是否只有1本书可以上传?
  291. // $last_articles = new Article([
  292. // 'title' => $article['title'],
  293. // 'thumb_media_id' => $article['thumb_media_id'],
  294. // "show_cover_pic" => $article['show_cover_pic'],
  295. // "author" => $article['author'],
  296. // "digest" => $article['digest'],
  297. // "content" => $article['content'],
  298. // "content_source_url" =>$article['content_source_url'],
  299. // //...
  300. // ]);break;
  301. }
  302. v('upload_article_before:' . json_encode($last_articles));
  303. $result = $this->material->uploadArticle($last_articles);
  304. v('upload_article_result:' . json_encode($result));
  305. $media_id = isset($result->media_id) ? $result->media_id : '';
  306. v('upload_article_media_id:' . $media_id);
  307. } catch (\Exception $e) {
  308. $this->_errorMsg['upload_articles'] = $e->getMessage();
  309. v('upload_articles_ept:' . $e->getMessage());
  310. }
  311. return $result;
  312. // $upload_res = '';
  313. // try{
  314. // $client = new Client();
  315. // $form_params = [
  316. // "touser"=>'oAcqg1LRHNKN2jaEkJ5v56HOwPEQ',
  317. // "mpnews"=>json_encode(['media_id'=>$media_id]),
  318. // "msgtype"=>"mpnews",
  319. // "send_ignore_reprint"=>0
  320. // ];
  321. // v($form_params);
  322. // $upload_res = $client->request("post","https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$component_access_token,
  323. // ['json'=>$form_params,'connect_timeout' => 3]
  324. // )->getBody()->getContents();
  325. // $upload_res = json_decode($upload_res,true);
  326. // v('$upload_res:'.$appid);v($upload_res);
  327. // }catch(\Exception $e){
  328. // v('upload_articles_ept_appid:'.$appid.' ept:'.$e->getMessage());
  329. // }
  330. // v('upload_articles_result:'.json_encode($upload_res));
  331. // return $upload_res;
  332. }
  333. public function getErrorMsg($type, $msg = '')
  334. {
  335. return getProp($this->_errorMsg, $type, $msg);
  336. }
  337. }