作者 chencheng

0529-test-1

@@ -35,7 +35,57 @@ class Wechat extends Api @@ -35,7 +35,57 @@ class Wechat extends Api
35 }catch(\Exception $e){ 35 }catch(\Exception $e){
36 $this->error($e->getMessage()); 36 $this->error($e->getMessage());
37 } 37 }
38 - $this->success('活动获取成功', $config); 38 + $this->success('配置获取成功', json_decode($config,true));
39 } 39 }
  40 +
  41 + /*
  42 + 服务器跳转授权code测试
  43 + */
  44 + public function test_code(){
  45 + $code = request()->param('code');
  46 + if(!empty($code)){
  47 + //跳转官方
  48 + $APPID = config('site.appid');
  49 + $REDIRECT_URI = 'https://coupon.xp.yn.cn/index.php/api/v1/Wechat/test_code';
  50 + $SCOPE = 'snsapi_base';
  51 + $redirect = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$APPID&redirect_uri=$REDIRECT_URI&response_type=code&scope=$SCOPE&state=STATE#wechat_redirect";
  52 + }else{
  53 + //解析code
  54 + $app = WehcatModule::getInstance()->initWechat();
  55 + $user = $app->oauth->user();
  56 + var_dump(json_encode($user));
  57 + // $user 可以用的方法:
  58 + // $user->getId(); // 对应微信的 OPENID
  59 + // $user->getNickname(); // 对应微信的 nickname
  60 + // $user->getName(); // 对应微信的 nickname
  61 + // $user->getAvatar(); // 头像网址
  62 + // $user->getOriginal(); // 原始API返回的结果
  63 + // $user->getToken(); // access_token, 比如用于地址共享时使用
  64 + }
  65 + }
  66 +
  67 + /*
  68 + 2.网页授权
  69 + 2.1前端打开则跳转到授权页 由前端自行配置
  70 + appid
  71 + redirect_uri前端的路由首位,
  72 + scope=snsapi_base 静默只能获取openid,
  73 + =snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
  74 + https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
  75 + 2.2 由微信回跳后,获取code 和 state 参数,由前端post code给后台
  76 + */
  77 +
  78 + public function get_user_info_by_code (){
  79 + try{
  80 + $app = WehcatModule::getInstance()->initWechat();
  81 + $oauth = $app->oauth;
  82 + // 获取 OAuth 授权结果用户信息
  83 + $user = $oauth->user();
  84 + }catch(\Exception $e){
  85 + $this->error($e->getMessage());
  86 + }
  87 + $this->success('配置获取成功', json_decode($config,true));
  88 + }
  89 +
40 90
41 } 91 }
@@ -6,6 +6,9 @@ use EasyWeChat\Factory; @@ -6,6 +6,9 @@ use EasyWeChat\Factory;
6 6
7 /** 7 /**
8 * easyWechat 插件 8 * easyWechat 插件
  9 + 使用
  10 + $app = app\server\Wechat::getInstance()->initWechat();
  11 + $app 为 easyWechat的对象 根据官网接口调用或二开
9 */ 12 */
10 class Wechat { 13 class Wechat {
11 static public $app = null; 14 static public $app = null;
@@ -15,8 +18,12 @@ class Wechat { @@ -15,8 +18,12 @@ class Wechat {
15 static private $inc = null; 18 static private $inc = null;
16 19
17 function __construct(){ 20 function __construct(){
18 - self::$appid= config("site.appid");  
19 - self::$appsecret= config("site.appsecret"); 21 + //self::$appid= config("site.appid");
  22 + //self::$appsecret= config("site.appsecret");
  23 + //测试公众号
  24 + self::$appid= 'wxb9b259e7a17bda27';
  25 + self::$appsecret= '2af20d18bf969d8a26691dfd54cc6380';
  26 + //easyWechat配置
20 self::$config = [ 27 self::$config = [
21 'app_id' => self::$appid, 28 'app_id' => self::$appid,
22 'secret' => self::$appsecret, 29 'secret' => self::$appsecret,
@@ -52,6 +59,7 @@ class Wechat { @@ -52,6 +59,7 @@ class Wechat {
52 ]; 59 ];
53 } 60 }
54 61
  62 + //静态函数
55 static public function getInstance(){ 63 static public function getInstance(){
56 if(self::$inc == null){ 64 if(self::$inc == null){
57 self::$inc = new self(); 65 self::$inc = new self();
@@ -59,8 +67,9 @@ class Wechat { @@ -59,8 +67,9 @@ class Wechat {
59 return self::$inc; 67 return self::$inc;
60 } 68 }
61 69
  70 + //初始化 easywechat app
  71 +
62 static public function initWechat(){ 72 static public function initWechat(){
63 -  
64 self::$app = Factory::officialAccount(self::$config); 73 self::$app = Factory::officialAccount(self::$config);
65 return self::$app; 74 return self::$app;
66 } 75 }