WechatController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Http\Request;
  6. use Qiniu\Auth as Auths;
  7. use Qiniu\Storage\UploadManager;
  8. class WechatController extends Controller{
  9. public function SendMessage($openid,$template_id,$url,$first,$value_1,$value_2,$remark,$data){
  10. try{
  11. }catch (\Exception $exception){
  12. }
  13. $openid='';
  14. $template_id='MfqZmLlo0LqD2cq6HvIkc9LRTO-8V2p7Bqzbvp9vBA8';
  15. $url='http://api.woaidakele.cn/aapi/gzh';
  16. $first='';
  17. $value_1='';
  18. $value_2='';
  19. $remark='';
  20. $app=app('wechat.official_account');
  21. $app->template_message->send([
  22. 'touser' => $openid,
  23. 'template_id' => $template_id,
  24. 'url' => $url,
  25. 'data' => [
  26. 'first' => $first,
  27. 'keyword1' => $value_1,
  28. 'keyword3' => $value_2,
  29. 'remark' => $remark,
  30. ],
  31. ]);
  32. }
  33. public function UploadImg(Request $request)
  34. {
  35. $app = app('wechat.official_account');
  36. $accessTokens = $app->access_token;
  37. $access_token = $accessTokens->getToken();
  38. $token = $access_token['access_token'];
  39. $user_id = Auth::user()->id;
  40. $img = $request->input('imgList');
  41. $re=[];
  42. Log::info('token为:'.$token);
  43. foreach ($img as $k => $v) {
  44. // $paths='public/uploads/'.date("Ymd").'/';
  45. // $path_url = $paths.date("His").$user_id.mt_rand(10000,99999).'.jpg';
  46. // if(!is_dir($paths)){
  47. // mkdir($paths,0755,true);
  48. // }
  49. // $new_urls = '/public/' . $path_url;
  50. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" . $token . "&media_id=" . $v;
  51. $a = file_get_contents($url);
  52. if(strstr($a,'errcode')){
  53. if(strstr($a,'access_token is invalid')){
  54. $access_token = $accessTokens->getToken(true);
  55. $token = $access_token['access_token'];
  56. Log::info('更新token为:'.$token);
  57. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" . $token . "&media_id=" . $v;
  58. $a = file_get_contents($url);
  59. }else{
  60. Log::info('media_id获取图片错误提示:'.$a);
  61. continue;
  62. }
  63. }
  64. // $fileurl = base_path() . $new_urls;
  65. // $resource = fopen($fileurl, 'w+');
  66. //
  67. // fwrite($resource, $a);
  68. // fclose($resource);
  69. $config = [
  70. 'driver' => 'qiniu',
  71. 'domain' => 'http://image.chuliu.cc', //七牛域名
  72. 'access_key' => 'cIbo9JAqHBgO6uw97gT5fJOlEHiTiGrpB84unVEn', //AccessKey
  73. 'secret_key' => '4f0nTNNrvzOsFc6Rz0GC5DntiJRnk2GcfkyJFNQp', //SecretKey
  74. 'bucket' => 'daweiboshi-app', //Bucket名字
  75. 'dirname'=> 'daweiboshi-app'
  76. ];
  77. // 构建鉴权对象
  78. $auth = new Auths($config['access_key'], $config['secret_key']);
  79. // 生成上传 Token
  80. $token = $auth->uploadToken($config['bucket']);
  81. // 新文件名
  82. $filename =date('His') . uniqid(mt_rand(10000,99999)) .'.jpg';
  83. $dirname=$config['dirname'];
  84. // 初始化 UploadManager 对象并进行文件的上传。
  85. $uploadMgr = new UploadManager();
  86. // 调用 UploadManager 的 putFile 方法进行文件的上传。
  87. $pathname= $dirname.'/audit/'.date('Ymd').'/'.$filename;
  88. try{
  89. $uploadMgr->put($token, $pathname, $a);
  90. }catch(\Exception $e){
  91. return $this->error('450001','','上传失败');
  92. }
  93. // $data= @getimagesize(env('APP_URL') .'/'. $path_url);
  94. $url = $config['domain'].'/'.$pathname;
  95. if($url){
  96. $re[$k] = $url;
  97. }
  98. // $re[$k] = env('APP_URL') .'/'. $path_url;
  99. }
  100. Log::info($user_id.'/shuju');
  101. Log::info($re);
  102. return $this->success($re);
  103. }
  104. }