GithubHelp home page GithubHelp logo

do-one-thing-to-well / flutter_wechat_login Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yechong/flutter_wechat_login

0.0 0.0 0.0 8.74 MB

Flutter集成微信登录

License: BSD 3-Clause "New" or "Revised" License

Ruby 2.29% Objective-C 72.10% Java 17.02% Dart 8.60%

flutter_wechat_login's Introduction

flutter_wechat_login

中文请移步此处

Flutter Wechat Login Plugin

Android iOS
Support YES YES

An animated image of the iOS Wechat Login Plugin UI      An animated image of the Android Wechat Login Plugin UI

Features

This plugin has integrated the function of WeChat login:

  • WeChat app authorized login
  • Exchange code for access_token, refresh_token and authorized scope /sns/oauth2/access_token
  • Refresh or renew access_token /sns/oauth2/refresh_token
  • Check access_token validity /sns/auth
  • Get user info /sns/userinfo

Getting Started

Before using this plugin, it is strongly recommended to read the official documentation in detail

Usage

import 'package:flutter_wechat_login/flutter_wechat_login.dart';

// Create FlutterWechatLogin
final flutterWechatLogin = FlutterWechatLogin();

// Initialization
await flutterQqLogin.init(appId: "Your AppID", secret: "Your AppSecret", universalLink: "Your Universal Links(iOS Required)");

// Determine whether the WeChat application is currently installed
bool isInstalled = await flutterWechatLogin.isInstalled();

// Call up WeChat login, and return code after successful login
Map<String, dynamic> wechatInfo = await flutterWechatLogin.login();

// Exchange code for access_token, refresh_token and authorized scope
Map<String, dynamic> accessTokenInfo = await flutterWechatLogin.getAccessToken(code: wechatInfo['code']);

// Refresh or renew access_token
Map<String, dynamic> refreshTokenInfo = await flutterWechatLogin.refreshToken(refreshToken: accessTokenInfo['refresh_token']);

// Check access_token validity
Map<String, dynamic> checkTokenInfo = await flutterWechatLogin.checkToken(accessToken: accessTokenInfo['access_token'], openid: accessTokenInfo['openid']);

// Get user information
Map<String, dynamic> userInfo = await flutterWechatLogin.getUserInfo(accessToken: accessTokenInfo['access_token'], openid: accessTokenInfo['openid']);

Configure Android version

  • 1)Create a package name wxapi under the project android directory /app/src/main/java/packageName, and then create a new WXEntryActivity under this package name, the code is as follows:
package packageName.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.SubscribeMessage;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessView;
import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessWebview;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("flutter_wechat_login", "onCreate");
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, "", false);
        try {
            Intent intent = getIntent();
            api.handleIntent(intent, this);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d("flutter_wechat_login", "onResp -> " + resp.errCode);

        Intent intent = new Intent("flutter_wechat_login");
        intent.putExtra("errCode", resp.errCode);
        intent.putExtra("errStr", resp.errStr);
        intent.putExtra("type", resp.getType());

        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            SendAuth.Resp authResp = (SendAuth.Resp) resp;
            Log.i("flutter_wechat_login", "COMMAND_SENDAUTH");
            intent.putExtra("code", authResp.code);
            intent.putExtra("state", authResp.state);
            intent.putExtra("lang", authResp.lang);
            intent.putExtra("country", authResp.country);
        }

        sendBroadcast(intent);
        finish();
    }
}
  • 2)Configure android/app/src/main/AndroidManifest.xml

WeChat needs to verify the package name, so the path of the Activity must be your package name.wxapi.WXEntryActivity, where your package name must be the package name filled in by the WeChat open platform registration application.

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
	<!-- new content start -->
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<!-- new content end -->
	<application>
		...
		<!-- new content start -->
		<activity
			android:name="Your package name.wxapi.WXEntryActivity"
			android:theme="@android:style/Theme.Translucent.NoTitleBar"
			android:exported="true"
			android:taskAffinity="Your package name"
			android:launchMode="singleTask">
		</activity>
		<!-- new content end -->
		...
	</application>
	<!-- new content start -->
	<queries>
		<package android:name="com.tencent.mm" />
	</queries>
	<!-- new content end -->
</manifest>

Configure iOS version

Configure URL Types

  • Use xcode to open your iOS project Runner.xcworkspace
  • In the info configuration tab under URL Types, add a new entry
    • identifier fills in weixin
    • URL Schemes fills in Your APPID
    • As shown below: xcode configuration example

Configure LSApplicationQueriesSchemes

  • Method 1, configure info in xcode

    • Open info configuration, add a LSApplicationQueriesSchemes, namely Queried URL Schemes
    • Add these items:
      • weixin
      • weixinULAPI
      • weixinURLParamsAPI
    • As shown below: xcode configuration example
  • Method 2, modify Info.plist directly

    • Use Android Studio to open ios/Runner/Info.plist under the project project
    • Add the following configuration under the dict node (refer to the configuration format in the file):
<key>LSApplicationQueriesSchemes</key>
<array>
	<string>weixin</string>
	<string>weixinULAPI</string>
	<string>weixinURLParamsAPI</string>
</array>

Donate

Buy the writer a cup of coffee

WeChat payment QR code      Alipay collection QR code

LICENSE


BSD 3-Clause License

Copyright 2017 German Saprykin
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.