///微信授权帮助类 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 wxInstalled() async { return await fluwx.isWeChatInstalled; } }