项目作者: pj0579

项目描述 :
flutter wechat
高级语言: Dart
项目地址: git://github.com/pj0579/flutter_wechat.git
创建时间: 2018-04-18T06:19:03Z
项目社区:https://github.com/pj0579/flutter_wechat

开源协议:MIT License

下载


flutter_wechat

A flutter plugin to use wechat.
测试使用暂时不维护了 有需要的请使用 https://github.com/OpenFlutter/fluwx

Features

  • [X] Add Login
  • [X] Add Pay
  • Support LocalImage
  • Support 分享小程序

    Install

    Add this to your package’s pubspec.yaml file:
    1. dependencies:
    2. flutter_wechat: "^0.1.5"

    Getting Started

  • Android
    • For Android, you must do the following before you can use the plugin:
      Add the permissions to your AndroidManifest.xml
      1. <uses-permission android:name="android.permission.INTERNET" ></uses-permission>
      2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
      3. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>
      4. <uses-permission android:name="android.permission.READ_PHONE_STATE" ></uses-permission>
      5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
      6. <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
      如果想要响应微信分享登录resp需要在mainActivity同级目录下创建wxapi文件夹
      下面新建WXEntryACtivity集成Activity(需要在AndroidManifest.xml注册)
      1. <activity android:name=".wxapi.WXEntryActivity" android:exported="true" android:launchMode="singleTop"></activity>
  1. private IWXAPI api;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. api = WXAPIFactory.createWXAPI(this, "wxb25d3dec3db1affc", false);
  6. onNewIntent(getIntent());
  7. finish();
  8. }
  9. @Override
  10. public void onReq(BaseReq baseReq) {
  11. }
  12. @Override
  13. public void onResp(BaseResp baseResp) {
  14. sendBroadcastToWechat(baseResp);
  15. }
  16. private void sendBroadcastToWechat(BaseResp baseResp) {
  17. Intent intent = new Intent();
  18. intent.setAction("sendResp");
  19. if (baseResp instanceof SendAuth.Resp) {
  20. SendAuth.Resp resp = (SendAuth.Resp) (baseResp);
  21. intent.putExtra("code", resp.errCode == 0 ? resp.code : "-1");
  22. intent.putExtra("type", "SendAuthResp");
  23. sendBroadcast(intent);
  24. } else {
  25. intent.setAction("sendResp");
  26. intent.putExtra("code", baseResp.errCode + "");
  27. intent.putExtra("type", "ShareResp");
  28. sendBroadcast(intent);
  29. }
  30. }
  31. protected void onNewIntent(Intent intent) {
  32. api.handleIntent(intent, this);
  33. }

如果想要响应微信支付resp需要在mainActivity同级目录下创建wxapi文件夹(需要在AndroidManifest.xml注册)

  1. <activity
  2. android:name=".wxapi.WXPayEntryActivity"
  3. android:exported="true"
  4. android:launchMode="singleTop">
  5. <intent-filter>
  6. <action android:name="android.intent.action.VIEW"></action>
  7. <category android:name="android.intent.category.DEFAULT"></category>
  8. <data android:scheme="your AppId"></data>
  9. </intent-filter>
  10. </activity>
  11. 下面新建WXEntryACtivity集成Activity
  1. public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
  2. private IWXAPI api;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. api = WXAPIFactory.createWXAPI(this, "wxb25d3dec3db1affc", false);
  7. onNewIntent(getIntent());
  8. finish();
  9. }
  10. @Override
  11. public void onReq(BaseReq baseReq) {
  12. }
  13. @Override
  14. public void onResp(BaseResp baseResp) {
  15. sendBroadcastToWechat(baseResp);
  16. }
  17. private void sendBroadcastToWechat(BaseResp baseResp) {
  18. Intent intent = new Intent();
  19. intent.setAction("sendResp");
  20. intent.putExtra("code", baseResp.errCode + "");
  21. intent.putExtra("type", "PayResp");
  22. sendBroadcast(intent);
  23. }
  24. protected void onNewIntent(Intent intent) {
  25. api.handleIntent(intent, this);
  26. }
  27. }
  • (BOOL)application:(UIApplication )application openURL:(NSURL )url
    sourceApplication:(NSString )sourceApplication annotation:(id)annotation
    {
    NSString
    urlStr = [url absoluteString];
    [[NSNotificationCenter defaultCenter]
    postNotificationName:@”WeChat” object:nil userInfo:@{@”url”:urlStr}];
    return YES;
    }

    // ios 9.0+
  • (BOOL)application:(UIApplication )application openURL:(NSURL )url
    1. options:(NSDictionary<NSString*, id> *)options
    {
    NSString * urlStr = [url absoluteString];
    [[NSNotificationCenter defaultCenter]
    postNotificationName:@”WeChat” object:nil userInfo:@{@”url”:urlStr}];
    return YES;
    }
    ```

How to use

  1. import 'package:flutter_wechat/flutter_wechat.dart';
  1. 分享返回错误码 0为正确 -1 -2 -3- 4- 5
  2. 登录成功返回 access_code 登录错误返回 -1
  3. 支付成功返回 0为正确 -1 -2
  1. 注册
  2. await FlutterWechat.registerWechat("wxxxxxx").then((state){print(state)});// 微信注册需要在你需要的地方注册,最好是app首页
  3. 分享
  4. await FlutterWechat.shareText(text: "test", type:0,).then((state){print(state)});//文字分享 type 0 聊天页面 1 朋友圈
  5. await FlutterWechat.shareImage(imgUrl: "xxx", type:0,).then((state){print(state)});
  6. await FlutterWechat.shareMusic(imgUrl: "xxx", musicUrl:"",title:"",description:"",musicDataUrl:"",musicLowBandDataUrl:"",musicLowBandUrl:"",type:0,).then((state){print(state)});
  7. await FlutterWechat.shareVideo(imgUrl: "xxx", videoUrl:"",title:"",description:"",videoLowBandUrl:"",type:0,).then((state){print(state)});
  8. await FlutterWechat.shareWebPage(imgUrl: "xxx", webpageUrl:"",title:"",description:"",type:0,).then((state){print(state)});
  9. 登录
  10. await FlutterWechat.login(scope:"",state:"").then((state){print(state)});
  11. 支付
  12. await FlutterWechat.pay(partnerId: "",prepayId: "",nonceStr: "",timeStamp: "",sign: "",package: "",appId: "",).then((state){print(state)});//具体参考微信参数传递