admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
///微信授权帮助类
 
import 'package:flutter/cupertino.dart';
import 'package:fluwx_no_pay/fluwx_no_pay.dart' as fluwx;
 
import '../../api/http.dart';
import '../ui/widget/refresh_listview.dart';
import 'ui_utils.dart';
 
class WXAuthUtil {
  static init(BuildContext context, StringCallback callback) {
    fluwx
        .registerWxApi(
        appId: "wx420b62b9213e0fc9",
        universalLink: "https://your.univerallink.com/link/")
        .then((value) {
      print("微信初始化完成:$value");
      fluwx.weChatResponseEventHandler.listen((res) {
        dismissDialog(context);
        print("微信回调:${res.errCode} -  ${res.errStr}");
        if (res is fluwx.WeChatAuthResponse) {
          fluwx.WeChatAuthResponse response = res;
          print("微信获取到的code: ${response.code}");
          int errCode = response.errCode;
          if (errCode == 0) {
            String? code = response.code;
            if (code == null) {
              ToastUtil.toast("微信授权失败,请稍后再试", context);
              return;
            }
            callback(code);
          } else if (errCode == -4) {
            //showToast("用户拒绝授权");
          } else if (errCode == -2) {
            // showToast("用户取消授权");
          } else {
            ToastUtil.toast("微信授权失败,请稍后再试", context);
            return;
          }
        }
      });
    });
  }
 
  ///开始授权
  static startAuth(BuildContext context) async {
    bool installed = await wxInstalled();
    if (!installed) {
      ToastUtil.toast("尚未安装微信", context);
      return;
    }
 
    showLoading(context);
    fluwx
        .sendWeChatAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test")
        .then((value) {
      print("微信授权登录回调:$value");
    });
  }
 
  ///是否安装了微信
  static Future<bool> wxInstalled() async {
    return await fluwx.isWeChatInstalled;
  }
}