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;
}
相关推荐
茶底世界之下14 小时前
Metal 冷启动并发下,Pipeline Cache 为什么必须合并在途编译
ios·swift
weixin_4038101315 小时前
用 Python 调 EasyClick iOS 免越狱脚本:开放接口对接步骤与参数
ios·iphone
weixin_4038101318 小时前
EasyClick iOS 免越狱自动化脚本怎么写:从环境判断到跑通第一个脚本
运维·ios·自动化
weixin_4038101319 小时前
不用越狱也能批量控制 iPhone:EasyClick iOS 免越狱脚本实战
ios·iphone
恋猫de小郭19 小时前
Android CLI 支持 AI Agent 通过 Device Streaming 调试云真机
android·前端·flutter
weixin_4038101320 小时前
iOS 自动化脚本怎么输入文字:代理、输入法与快捷指令三级方案
运维·ios·自动化
m0_738185821 天前
Flutter 鸿蒙化实战:flutter_scankit 适配 OpenHarmony,华为 ScanKit 扫码
flutter·华为·harmonyos·鸿蒙
wechatbot8882 天前
企业微信HTTP协议接口完整接入教程|全量消息收发 API 实战
网络协议·http·微信·企业微信·ai编程
黑科技iOS上架2 天前
双引擎解决oc/swift开发引用加固难题
开发语言·ios·swift·审核·ios混淆·执行程序差异化
事圆则缓2 天前
Flutter 开发鸿蒙实战:从 OpenHarmony 适配版到 HAP 构建与插件接入
flutter·华为·harmonyos