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;
}
相关推荐
思盛iOS签名上架5 小时前
为什么 iOS 需要签名?未签名 App 无法安装的底层逻辑
ios
Chengbei1114 小时前
DSH渗透测试插件dsh-pentest全新升级!适配DeepSeek Harness,可视化探索链路,一键搭建轻量化AI渗透测试环境。
人工智能·web安全·网络安全·微信·小程序·系统安全·安全架构
iFlyCai16 小时前
Flutter原理深度解析:从架构到渲染的全面剖析(二)
flutter·flutter 三棵树
里欧跑得慢18 小时前
CSS 模块化架构的演进:BEM、CSS Modules 到 CSS-in-JS 的反思
前端·css·flutter·web·css-in-js
Data_Journal1 天前
Golang 中解析 HTML 的指南
ios·iphone
jike_20261 天前
销售拜访记录APP推荐:客户沟通内容怎么快速整理?
ios·语音识别·iphone
iFlyCai1 天前
深入理解Flutter:StatefulWidget生命周期全解析(一)
flutter·dart·状态管理·statefulwidget
xiangxiongfly9151 天前
Flutter video_player总结
flutter·video_player·chewie
00后程序员张1 天前
iOS 开发全流程需要哪些工具?编码、调试、构建、发布阶段选择
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程