【转】react-native-wechat--微信登录

1.前言
  在今天无论是游戏开发还是app开发,微信作为第三方登录必不可少,今天我们就用react-native-wechat实现微信登录,分享和支付同样的道理就不过多的介绍了。

2.属性

1)registerApp(appid):

  appid:String类型,从微信开放平台后台获取。

2)registerAppWithDescription(appid, appdesc):

  此方法只支持iOS; appid: String类型,从微信后台获取;  appdesc:String类型,描述信息。

3)openWXApp():

  打开微信app。

4)sendAuthRequest([scope[, state]]):

  微信登录请求,获取微信返回的token;  scope:应用授权作用域,如获取用户个人信息则填写snsapi_userinfo。

5) shareToTimeline(data):

  分享到朋友圈:

  {Object} data contain the message to send
  {String} thumbImage Thumb image of the message, which can be a uri or a resource id.  
  {String} type Type of this message. Can be {news|text|imageUrl|imageFile|imageResource|video|audio|file} 
  {String} webpageUrl Required if type equals news. The webpage link to share.  
  {String} imageUrl Provide a remote image if type equals image.  
  {String} videoUrl Provide a remote video if type equals video.  
  {String} musicUrl Provide a remote music if type equals audio.  
  {String} filePath Provide a local file if type equals file.  
  {String} fileExtension Provide the file type if type equals file.

6) shareToSession(data)

  分享到好友或群,数据结构跟分享到朋友圈类似。

3.使用实例

1)安装react-native-wechat:

  npm install react-native-wechat –save

2) 自动关联:

  rnpm link react-native-wechat

  非到万不得已的时候,最好不要手动关联

3)在MainApplication中加入如下代码

复制代码
import com.theweflex.react.WeChatPackage;       // Add this line before public class MainActivity
...

/**
 * A list of packages used by the app. If the app uses additional views
 * or modules besides the default ones, add more packages here.
 */
@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
    new MainReactPackage(), 
    new WeChatPackage()        // Add this line
  );
}
复制代码

4)创建Package

  名称为你应用的包名+ wxapi,在新建的包中创建一个名字为WXEntryActivity的类。如包名为com.platformproject,目录结构和代码如下

5)在AndroidManifest.xml中加入微信Activity,如下

6)在componentDidMount中调用registerApp

复制代码
componentDidMount() {
    try {
      WeChat.registerApp('xxxx');//从微信开放平台申请
    } catch (e) {
      console.error(e);
    }
    console.log(WeChat);
  }
复制代码

7)调用微信登录认证

import * as WeChat from 'react-native-wechat';//首先导入react-native-wechat
WeChat.sendAuthRequest("snsapi_userinfo");//在需要触发登录的时候调用

  如果成功此时会弹出微信登录的认证界面,认证完就可以获取到token了。拿到token之后可以通过一下url进一步取到昵称,性别,头像等信息。

https://api.weixin.qq.com/sns/oauth2/access_token?appid="+Config.wechat_Appid+"&secret="+Config.wechat_AppSecret+"&code="+token+"&grant_type=authorization_code

  如果弹出Scope参数错误或没有Scope权限,则需要从微信开放平台,认证开发者,申请开通登录等权限。

8)注意

  微信开放平台,后台需要填写应用包名和应用签名,应用签名是使用微信开放平台提供的Android小工具生成的,手机安装小工具之后,输入应用的包名,即可生成对应的应用签名。

4.效果

  暂无,如果有任何问题,可留言讨论交流。

 



原文链接

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值