更新记录
1.0.7(2024-03-22)
- 添加getIdToken
1.0.6(2024-03-22)
- 兼容interface.uts
1.0.5(2024-03-13)
- 添加 isSupportGMS - 是否支持GMS
平台兼容性
Vue2 | Vue3 |
---|---|
× | × |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 4.03,Android:5.0,iOS:11,HarmonyNext:不确定 | × | × | × | × | × | × |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 |
---|---|---|---|
× | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × |
UTS环境兼容性
uni-app | uni-app x |
---|---|
√ | √ |
sn-signin-google使用文档 (更多插件及问题,请加微信:zhuzheVIP1)
说明
sn-signin-google是uts插件,用于google登录
使用前提
需要去firebase控制台创建项目
①添加授权供应商-google(firebase控制台项目概览左侧菜单-Authentication)
②配置android包名、sha1秘钥
之后,去项目设置下载google-services.json以及GoogleService-Info.plist
google service配置
在uni-app项目根目录添加如下:
nativeResources/
└── android
└── res
└── values
└── values.xml
└── ios
└── Resources
└── GoogleService-Info.plist
android
添加values.xml(由google-services.json转化,可以加wx:zhuzheVIP1)
values.xml
如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="default_web_client_id" translatable="false">1003328790123-92umscqostbbancemkpdth3ibu0q5dh1.apps.googleusercontent.com</string>
<string name="gcm_defaultSenderId" translatable="false">1003328790123</string>
<string name="google_api_key" translatable="false">AIzaSyAPereBMhKT2f9BaSeVmqGJAfudNYlDajo</string>
<string name="google_app_id" translatable="false">1:1003328790123:android:e969c449cc7453d3331b2e</string>
<string name="google_crash_reporting_api_key" translatable="false">AIzaSyAPereBMhKT2f9BaSeVmqGJAfudNYlDajo</string>
<string name="google_storage_bucket" translatable="false">unitest-c25d3.appspot.com</string>
<string name="project_id" translatable="false">unitest-c25d3</string>
</resources>
ios
- 添加GoogleService-Info.plist
- 配置url types
{
"app-plus": {
"distribute" : {
"ios": {
"urltypes" : [
{
"urlschemes" : [
// 取值来自GoogleService-Info.plist->REVERSED_CLIENT_ID
"com.googleusercontent.apps.<xxx>"
],
"documentrole" : "Editor"
}
]
}
}
}
}
接口
isSupportGMS - 是否支持GMS
var isSupport = isSupportGMS()
isLoggedIn - 是否已登录
var isLog = isLoggedIn()
getCurrentUser - 获取已登录的用户信息
var user = getCurrentUser()
// {}
signOut - 登出
signOut()
signIn
signIn({
// android就是xml文件中default_web_client_id的值
// ios就是GoogleService-Info.plist中CLIENT_ID的值
serverClientId: '<web_client_id>'
}, e => {
// {code:0,msg:'success',user:{displayName:'',email:'',uid:111}}
})
getIdToken
getIdToken((code, token) => {
if (code == 0) {
// token
}
})
示例
- uni-app
<template>
<view class="content">
<button type="default" @click="check">检查</button>
<button type="default" @click="googleSignIn">Google-SignIn</button>
<button type="default" @click="googleSignOut">Google-SignOut</button>
</view>
</template>
<script>
import * as GoogleSignIn from '@/uni_modules/sn-signin-google';
export default {
data() {
return {
title: 'Hello'
};
},
onLoad() {},
methods: {
check() {
if (GoogleSignIn.isLoggedIn()) {
uni.showToast({
title: '已登录'
});
console.log('user', GoogleSignIn.getCurrentUser());
} else {
uni.showToast({
title: '未登录'
});
}
},
googleSignIn() {
const isAndroid = uni.getSystemInfoSync().platform == 'android';
// 一定记得修改为自己项目的
const androidClientId = '1003328790123-92umscqostbbancemkpdth3ibu0q5dh1.apps.googleusercontent.com'
const iosClientId = '1003328790123-bpngtibsljd9ma0e0e2u3ocgt42b2bd1.apps.googleusercontent.com'
GoogleSignIn.signIn(
{
serverClientId: isAndroid ? androidClientId : iosClientId
},
(e) => {
console.log('signInWithGoogle', e);
if(e.code == 0) {
// 成功
} else {
// 失败
}
}
);
},
googleSignOut() {
GoogleSignIn.signOut();
}
}
};
</script>
- uni-app x
<template>
<view class="content">
<button type="default" @click="check">检查</button>
<button type="default" @click="googleSignIn">Google-SignIn</button>
<button type="default" @click="googleSignOut">Google-SignOut</button>
</view>
</template>
<script>
import * as GoogleSignIn from '@/uni_modules/sn-signin-google';
import { SignInParams, SignInResult } from '@/uni_modules/sn-signin-google';
export default {
data() {
return {
title: 'Hello'
};
},
onLoad() {},
methods: {
check() {
if (GoogleSignIn.isLoggedIn()) {
uni.showToast({
title: '已登录'
});
console.log('user', GoogleSignIn.getCurrentUser());
} else {
uni.showToast({
title: '未登录'
});
}
},
googleSignIn() {
const isAndroid = uni.getSystemInfoSync().platform == 'android';
// 一定记得修改为自己项目的
const androidClientId = '1003328790123-92umscqostbbancemkpdth3ibu0q5dh1.apps.googleusercontent.com'
const iosClientId = '1003328790123-bpngtibsljd9ma0e0e2u3ocgt42b2bd1.apps.googleusercontent.com'
GoogleSignIn.signIn(
{
serverClientId: isAndroid ? androidClientId : iosClientId
} as SignInParams,
(e: SignInResult) => {
console.log('signInWithGoogle', e);
if(e.code == 0) {
// 成功
} else {
// 失败
}
}
);
},
googleSignOut() {
GoogleSignIn.signOut();
}
}
};
</script>