EasyWeChat 分享 扫一扫 微信支付 授权 生成菜单 使用方法

环境需求

composer 安装命令:

$ composer require overtrue/wechat:~4.0 -vvv

下载到Thinkphp vendor下


控制器引入文件
use EasyWeChat\Factory;

class Weixin
{
private $appid;
private $secret;
private $token;
private $app;
public function __construct(){
     //查出需要的配置参数
$webconfig = Db::name('Webconfig')->find();
$this->appid = $webconfig['appid'];
$this->secret = $webconfig['secret'];
$this->token = $webconfig['token'];
$config = [
'app_id' => $this->appid,
'secret' => $this->secret,
'token' => $this->token,
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => __DIR__.'/wechat.log',
],
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => '/index/weixin/shouquan_callback',
],
];
$this->app = Factory::officialAccount($config);
}

  

  授权 

    微信第一次授权需要访问  

    public function shouquan(){
    $response = $this->app->server->serve();
    $response->send();
    }
   获取微信code 
  public function get_code($href=''){
   $href = urlencode(json_encode($arr));
   $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid.
   "&redirect_uri=".urlencode('http://pdel.bluecat-network.com/index/weixin/getaccesstoken?href='.$href).
  "&response_type=code&scope=snsapi_userinfo&state=".''."#wechat_redirect";
     return $url;
  }
  使用code  去微信获取用户信息
  public function getaccesstoken(){
$code = input('code');
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->secret.'&code='.$code.'&grant_type=authorization_code';
$res = file_get_contents($url);
$res = json_decode($res,true); openid
   $info = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$res['access_token'].'&openid='.$res['openid'].'&lang=zh_CN';
    $res_info = file_get_contents($url1);
    $res_info = json_decode($res_info,true);用户详细信息sex,nickname,openid,headimgurl
  }
  
编辑菜单
  public function caidan(){
  $buttons = [
   [
   "type" => "view",
  "name" => "名称",
  "url" => "路径(http://xxxxxxxxx/xxx/xx/#)"
   ],
  ];
  $this->app->menu->create($buttons);
  echo 'success';
  }
  public function caidan(){
   $buttons = [
   [
   "name"=>"xxxx",
   "sub_button"=>[
   [
   "type"=>"view",
  "name"=>"xxxxxx",
  "url"=>'xxxxxxxxxx'
  ],
  [
   "type"=>"view",
   "name"=>"xxxxxxx",
   "url"=>'xxxxx'
   ],
   ]
   ],
  [
   "type" => "view",
  "name" => "xxx",
  "url"=>'xxxxxxx'
   ],
   [
   "name"=>"xxxx",
   "sub_button"=>[
   [
   "type"=>"view",
   "name"=>"xxxx",
   "url"=>'xxxxxxxxx'
  ],  
   ]
  ],
  ];

分享 jssdk
    public function js_sdk(){
      $request = request();
      $id = $request->param('id');
      $info = Db::name('Sgood')->field(['id','name','img','share'])->where(['id'=>$id,'order_status'=>1])->find();    
       if(empty($info)){
          $res['code'] = 66066;
   $res['msg'] = '分享失败';
    return json($res);
        }
    $url = input('url');
        $this->app->jssdk->setUrl($url);
     $arr = $this->app->jssdk->buildConfig(['updateAppMessageShareData','updateTimelineShareData '], $debug = false, $beta = false, $json = true);
    $res['code'] = 0;
    $res['msg'] = '数据获取成功';
        $res['title'] = $info['name'];
     $res['share'] = $info['share'];
    $res['imgUrl'] = $info['img'];
    $res['arr'] = json_decode($arr,true);
    return json_encode($res);
   }
  扫一扫

  
public function saoyisao(){
      $this->app->jssdk->setUrl(config('url'));
   $arr = $this->app->jssdk->buildConfig(['scanQRCode'], $debug = false, $beta = false, $json = true);
  return $arr;
  }
 

猜你喜欢

转载自www.cnblogs.com/syay/p/10870092.html
今日推荐