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;
}
相关推荐
一起养小猫2 小时前
Flutter for OpenHarmony 实战_魔方应用UI设计与交互优化
flutter·ui·交互·harmonyos
hudawei9963 小时前
flutter和Android动画的对比
android·flutter·动画
一只大侠的侠3 小时前
Flutter开源鸿蒙跨平台训练营 Day7Flutter+ArkTS双方案实现轮播图+搜索框+导航组件
flutter·开源·harmonyos
锐意无限4 小时前
Swift 扩展归纳--- UIView
开发语言·ios·swift
符哥20084 小时前
用Apollo + RxSwift + RxCocoa搭建一套网络请求框架
网络·ios·rxswift
一只大侠的侠4 小时前
Flutter开源鸿蒙跨平台训练营 Day9分类数据的获取与渲染实现
flutter·开源·harmonyos
一只大侠的侠4 小时前
Flutter开源鸿蒙跨平台训练营 Day 5Flutter开发鸿蒙电商应用
flutter·开源·harmonyos
陈思杰系统思考Jason5 小时前
系统思考:业务创新与组织重构
百度·微信·微信公众平台·新浪微博·微信开放平台
羊群智妍5 小时前
2026AI搜索时代:9成企业遭AI“隐身”,教你精准破局
百度·微信·微信公众平台·facebook·新浪微博
ZH15455891315 小时前
Flutter for OpenHarmony Python学习助手实战:GUI桌面应用开发的实现
python·学习·flutter