template_message->send([ 'touser' => $openid, 'template_id' => $template_id, 'url' => $url, 'data' => [ 'first' => $first, 'keyword1' => $value_1, 'keyword3' => $value_2, 'remark' => $remark, ], ]); } public function UploadImg(Request $request) { $app = app('wechat.official_account'); $accessTokens = $app->access_token; $access_token = $accessTokens->getToken(); $token = $access_token['access_token']; $user_id = Auth::user()->id; $img = $request->input('imgList'); $re=[]; Log::info('token为:'.$token); foreach ($img as $k => $v) { // $paths='public/uploads/'.date("Ymd").'/'; // $path_url = $paths.date("His").$user_id.mt_rand(10000,99999).'.jpg'; // if(!is_dir($paths)){ // mkdir($paths,0755,true); // } // $new_urls = '/public/' . $path_url; $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" . $token . "&media_id=" . $v; $a = file_get_contents($url); if(strstr($a,'errcode')){ if(strstr($a,'access_token is invalid')){ $access_token = $accessTokens->getToken(true); $token = $access_token['access_token']; Log::info('更新token为:'.$token); $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" . $token . "&media_id=" . $v; $a = file_get_contents($url); }else{ Log::info('media_id获取图片错误提示:'.$a); continue; } } // $fileurl = base_path() . $new_urls; // $resource = fopen($fileurl, 'w+'); // // fwrite($resource, $a); // fclose($resource); $config = [ 'driver' => 'qiniu', 'domain' => 'http://image.chuliu.cc', //七牛域名 'access_key' => 'cIbo9JAqHBgO6uw97gT5fJOlEHiTiGrpB84unVEn', //AccessKey 'secret_key' => '4f0nTNNrvzOsFc6Rz0GC5DntiJRnk2GcfkyJFNQp', //SecretKey 'bucket' => 'daweiboshi-app', //Bucket名字 'dirname'=> 'daweiboshi-app' ]; // 构建鉴权对象 $auth = new Auths($config['access_key'], $config['secret_key']); // 生成上传 Token $token = $auth->uploadToken($config['bucket']); // 新文件名 $filename =date('His') . uniqid(mt_rand(10000,99999)) .'.jpg'; $dirname=$config['dirname']; // 初始化 UploadManager 对象并进行文件的上传。 $uploadMgr = new UploadManager(); // 调用 UploadManager 的 putFile 方法进行文件的上传。 $pathname= $dirname.'/audit/'.date('Ymd').'/'.$filename; try{ $uploadMgr->put($token, $pathname, $a); }catch(\Exception $e){ return $this->error('450001','','上传失败'); } // $data= @getimagesize(env('APP_URL') .'/'. $path_url); $url = $config['domain'].'/'.$pathname; if($url){ $re[$k] = $url; } // $re[$k] = env('APP_URL') .'/'. $path_url; } Log::info($user_id.'/shuju'); Log::info($re); return $this->success($re); } }