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;
}
相关推荐
●VON7 小时前
鸿蒙Flutter实战:分类管理页BottomSheet CRUD
数据库·flutter·华为·harmonyos·鸿蒙
woodWu11 小时前
Flutter 复杂拖拽排序实战:同源排序 + 跨容器拖拽完整落地
flutter
小小小小小鹿11 小时前
Vibe Coding 实战:Flutter 自定义路径布局
flutter·vibecoding
择势13 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室 —— 常见问题汇总 & 解决方案手册
ios
择势13 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室(进阶封装)
ios
择势13 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室——从零到可跑的指南
ios
程序员老刘14 小时前
Dart 3.12 更新要点:乏善可陈
flutter·ai编程·dart
白玉cfc14 小时前
【iOS】底层原理:类的加载
ios·objective-c·xcode
●VON15 小时前
鸿蒙Flutter实战:水平滑动分类标签筛选栏
flutter·华为·harmonyos
光电的一只菜鸡16 小时前
shell脚本开发技巧
开发语言·ios·swift