Registration application

If the application developed in wechat open platform is registered successfully, the following information will be returned: the unique identifier of the application APPID and the application key AppSecret.(Currently, only corporate users can apply for registration permission, but individual users cannot apply for registration permission)

Click to enter the official wechat development platform

Import third-party packages and reference them

dependencies:
  fluwx: ^${latestVersion}
Copy the code

import 'package:fluwx/fluwx.dart';

reference

  1. The application is initially registered
_initFluwx() async {await fluwx. registerWxApi(appId: "appId ", // Pass in the registered application ID doOnAndroid: true, // Run doOnIOS on Android: True, // run on ios); }Copy the code
  1. Determine whether wechat is installed on your phone
var exist = await Fluwx.isWeChatInstalled; if (! ToastLength: toast.length_short, gravity: ToastGravity.CENTER, timeforiosWeb: 1, backgroundColor: color. red, textColor: color. white, fontSize: 16.0); }Copy the code
  1. Request CODE
String _result = "" , Fluwx.weChatResponseEventHandler.listen((data) { if (data.errCode == 0) { Fluwx.WeChatAuthResponse wa = data; _result += ' Code:' + wa.code; _result += ' country:' + wa.country; var code; setState((){ code = wa.code; }) printLog('code: $code'); getWeChatAccessToken(code); Access_token}});Copy the code
  1. Obtain the access_token by code
https://api.weixin.qq.com/sns/oauth2/access_tokenappid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
Copy the code
void getWeChatAccessToken(code) async { var url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="; url += " APPID & secret = AppSecret & code=" + code; url += "&grant_type=authorization_code"; // Error 40163 will be reported when you log out after the first successful login // 40163 refers to the same code for multiple requests to obtain token // solution: Try {Response Response = await Dio().get(url); _result = response.data.toString(); var data = jsonDecode(_result); assert(data is Map); var access_token = data['access_token']; var openid = data['openid']; getWeChatUserInfo(access_token, openid); } catch (e) {printLog("getWeChatAccessToken error: $e"); }}Copy the code
  1. Obtain wechat user information
String _userinfo = " ";

void getWeChatUserInfo(accessToken, openId) async {
      if (accessToken == null) return;

      var url = "https://api.weixin.qq.com/sns/userinfo?access_token=";
      url += accessToken + "&openid=" + openId.toString();

      try {
        Response response = await Dio().get(url);
        var dataStr = response.data.toString();

        _userinfo = dataStr;
        _saveUserInfo(_userinfo);
        var data = jsonDecode(dataStr);
        headImageUrl = data["headimgurl"];
        nickname = data["nickname"];

        if (nickname != null) {
          _startTimer(data);
        }
        setState(() {});

      } catch (e) {
        print("e>>>>>>>>$e");
      }
    }
Copy the code

Apk wechat login problem

Wechat login prompt: The signature is incorrect, please check whether the signature is consistent with that on the development platform

The solution