123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Http\Request;
- use Qiniu\Auth as Auths;
- use Qiniu\Storage\UploadManager;
- class WechatController extends Controller{
- public function SendMessage($openid,$template_id,$url,$first,$value_1,$value_2,$remark,$data){
- try{
- }catch (\Exception $exception){
- }
- $openid='';
- $template_id='MfqZmLlo0LqD2cq6HvIkc9LRTO-8V2p7Bqzbvp9vBA8';
- $url='http://api.woaidakele.cn/aapi/gzh';
- $first='';
- $value_1='';
- $value_2='';
- $remark='';
- $app=app('wechat.official_account');
- $app->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);
- }
- }
|