Flutter 实现Android,IOS 微信登录

  1. 登录微信开放平台open.weixin.qq.com/
  2. 配置Android 端APP信息

3. 配置IOS 端APP信息

4.登录苹果开发者后台,在Identifiers 中打开 Associated Domains

  1. 创建 apple-app-site-association文件,内容如下
{ 复制代码
    "applinks": {
        "details": [
            {
                "appID": "开发者账号的ID,苹果开发者后台获取",
                "paths": ["/目录/*""]
            }
        ]
    }
}

此步骤的目录决定了第二步中域名后拼接的目录地址 6.将步骤五创建的文件放到xxxx.com服务器 对应的根目录或者.well-known目录下 7.Xcode info.plist中添加一下内容 图形化展示:

文本展示:

  1. 引入Fluwx
makefile 复制代码
fluwx: ^5.4.1
  1. 代码实现工具类
dart 复制代码
import 'dart:convert';

import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:music_flutter/constants/get_constants.dart';
import 'package:music_flutter/http/request/request.dart';
import 'package:music_flutter/model/model.dart';
import 'package:music_flutter/modules/account/login/wechat_bind_phone_page.dart';
import 'package:music_flutter/utils/toast.dart';

import '../constants/constants.dart';
import '../http/http.dart';
import '../main.dart';

class WechatUtil {
  WechatUtil._();

  static Function(WeChatPaymentResponse)? onPayResultListener;
  static final _fluwx = Fluwx()
    ..addSubscriber((resp) async {
      if (resp is WeChatAuthResponse) {
        if (resp.isSuccessful) {
          switch (resp.errCode) {
            case 0:
              try {
                final code = resp.code;
                //todo 调用获取access_token 接口
                final appSecretResp = await HttpUtil.dio.get<String>(
                    "${HttpUtil.baseUrl}api/app/web-site/app-secret");
                    // 此方法可以让后端直接去调用
                final accessTokenResponse = await HttpUtil.dio.get(
                    "https://api.weixin.qq.com/sns/oauth2/access_token",
                    queryParameters: <String, dynamic>{
                      "appid": wechatId,
                      "secret": appSecretResp.data,
                      "code": code,
                      "grant_type": "authorization_code",
                    });

                if (accessTokenResponse.data != null) {
                  var json = jsonDecode(accessTokenResponse.data);
                  final model = WechatAccessTokenModel.fromJson(json);
                  // 调用自己服务端的微信登录接口完成信息获取,存储
                  AccountRequest.wechatLogin(
                      accessToken: model.access_token,
                      openId: model.openid,
                      onSucceedDeal: (m) {
                        if (m.newUser) {
                          Get.to(
                              () => WechatBindPhonePage(openId: model.openid));
                        } else {
                          storage.write(phoneNumber, m.phone.toString());
                          storage.write(token, m.accessToken);
                          Get.clearRouteTree();
                          Get.offAll(() => const MainPage());
                        }
                      });
                }
              } on Exception catch (_, e) {
                e.toString();
              }
              break;

            default:
              break;
          }
        }
      }
      if (resp is WeChatPaymentResponse) {
        onPayResultListener?.call(resp);
      }
    });

// 选择适当时机初始化
  static void registerApi() async {
    _fluwx.registerApi(
      appId: wechatId,
      //todo 添加IOS universalLink
      universalLink: "https://xxxx.com/目录/",
    );
  }
// 请求拉起登录
  static void authBy() async {
    final isInstall = await _fluwx.isWeChatInstalled;
    if (isInstall) {
      await _fluwx.authBy(
        which: NormalAuth(
          scope: 'snsapi_userinfo',
          state: 'wechat_sdk_demo_test',
        ),
      );
    } else {
      toast("微信未安装");
    }
  }
// 请求支付
  static void pay(
    PrePayIdModel m, {
    Function(WeChatPaymentResponse)? onPayResult,
  }) async {
    onPayResultListener = onPayResult;
    final isInstall = await _fluwx.isWeChatInstalled;
    if (isInstall) {
      await _fluwx.pay(
        which: Payment(
          appId: m.appId,
          partnerId: m.partnerId,
          prepayId: m.prepayId,
          packageValue: m.packageValue,
          nonceStr: m.nonceStr,
          timestamp: int.tryParse(m.timeStamp) ?? -1,
          sign: m.sign,
        ),
      );
    } else {
      toast("微信未安装");
    }
  }

  static Future<bool> get wechatIsInstalled async =>
      await _fluwx.isWeChatInstalled;
}
相关推荐
2401_8396339118 分钟前
鸿蒙flutter第三方库适配 - URL处理应用
flutter·华为·harmonyos
李李李勃谦2 小时前
Flutter 框架跨平台鸿蒙开发 - 星空日记
flutter·华为·harmonyos
2401_839633912 小时前
鸿蒙flutter第三方库适配 - 看板应用
flutter·华为·harmonyos
空中海3 小时前
5.1 HTTP 与网络请求
网络·网络协议·flutter·http
提子拌饭1334 小时前
生命组学架构下的细胞分化与基因突变生存模拟器:基于鸿蒙Flutter的情景树渲染与状态溢出防御
flutter·华为·架构·开源·harmonyos
空中海5 小时前
6.1 主题与暗色模式
运维·服务器·前端·flutter
疯狂的程序猴6 小时前
Flutter应用代码混淆完整指南:Android与iOS平台配置详解
后端·ios
你听得到116 小时前
周下载60w,但是作者删库!我从本地 pub 缓存里把它救出来,顺手备份到了自己的 GitHub
前端·flutter
SY.ZHOU6 小时前
移动端架构体系(五):终篇总结
flutter·ios·系统架构·安卓·鸿蒙
不爱吃糖的程序媛7 小时前
flutter build hap --release` 后版本号被改回 1.0.0
flutter