Skip to content

Instantly share code, notes, and snippets.

@junlas
Last active January 25, 2018 07:58
Show Gist options
  • Save junlas/cde3558d001c00093da816336519c862 to your computer and use it in GitHub Desktop.
Save junlas/cde3558d001c00093da816336519c862 to your computer and use it in GitHub Desktop.
微信小游戏平台数据接口 - 基于Promise异步形式封装
class WxgamePlatform {
name = 'Cat';
_bgmAudio = null;
login() {
return new Promise((resolve, reject) => {
wx.login({
success:res=> {
resolve(res)
}
})
});
}
getUserInfo() {
return new Promise((resolve, reject) => {
wx.getUserInfo({
withCredentials: true,
success:res=>{
var userInfo = res.userInfo;
var nickName = userInfo.nickName;
var avatarUrl = userInfo.avatarUrl;
var gender = userInfo.gender; //性别 0:未知、1:男、2:女
var province = userInfo.province;
var city = userInfo.city;
var country = userInfo.country;
resolve(userInfo);
},
fail:res=>{
wx.showModal({
title: '友情提醒',
content: '请允许微信获得授权!',
confirmText:"授权",
showCancel: false,
success: res => {
resolve(null);
}
});
}
});
});
}
checkSettingOpen(){
return new Promise((resolve,reject)=>{
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
console.log("请求授权获取用户信息");
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
resolve(true);
}else {
wx.openSetting({
complete:res=>{
console.log("-->", res.authSetting['scope.userInfo']);
if (res.authSetting['scope.userInfo']) {
resolve(true);
}else {
resolve(false);
}
}
});
}
}
});
});
}
checkSession(){
return new Promise((resolve,reject)=>{
wx.checkSession({
success:res=> {//session 未过期,并且在本生命周期一直有效
resolve(res);
},
fail:res=> {//登录态过期
reject(res);
}
});
});
}
playAudio(src){
return new Promise((resolve,reject)=>{
var audio = wx.createInnerAudioContext();
audio.obeyMuteSwitch = false;
audio.src = src;
audio.play();
resolve(audio);
});
}
playBgmAudio(src){
return new Promise((resolve,reject)=>{
if (!this._bgmAudio) {
this._bgmAudio = wx.createInnerAudioContext();
this._bgmAudio.autoplay = true;
this._bgmAudio.loop = true;
this._bgmAudio.obeyMuteSwitch = false;
this._bgmAudio.src = src;
}
wx.onShow(()=>{
this._bgmAudio.play();
});
wx.onAudioInterruptionEnd(()=>{
this._bgmAudio.play()
});
this._bgmAudio.play();
resolve();
});
}
pauseBmgAudio(){
return new Promise((resolve,reject)=>{
if (this._bgmAudio) {
this._bgmAudio.pause();
}
resolve();
});
}
vibratePhone(){
let p = new Promise((resolve,reject)=>{
wx.vibrateLong({
success:res=>{
resolve(res);
}
});
});
return p;
}
shareAppMessage(fromTitle, fromImageUrl){
let p = new Promise((resolve,reject)=>{
wx.shareAppMessage({
title:fromTitle,
imageUrl:fromImageUrl,
success:res=>{
resolve(res);
},
fail:res=>{
reject(res);
}
});
});
return p;
}
showModal(fromTitle, fromContent, fromConfirmText,fromIsShowCancel){
let p = new Promise((resolve,reject)=>{
wx.showModal({
title: fromTitle,
content: fromContent,
confirmText: fromConfirmText,
showCancel: fromIsShowCancel,
success: res => {
resolve(res);
}
});
});
}
setStorageSync(key, value) {
return new Promise((resolve, reject) => {
wx.setStorageSync(key, value);
resolve();
});
}
getStorageSync(key) {
return new Promise((resolve, reject) => {
let value = wx.getStorageSync(key);
resolve(value);
});
}
}
window.platform = new WxgamePlatform();
@junlas
Copy link
Author

junlas commented Jan 23, 2018

微信授权接口相关调用逻辑(WeixinAuthor.ts),配合platform.js使用:

class WeixinAuthor {
private static _instance:WeixinAuthor;

public constructor(s:Single) {
}

public static get instance():WeixinAuthor {
	if(!WeixinAuthor._instance) {
		WeixinAuthor._instance = new WeixinAuthor(new Single());
	}
	return WeixinAuthor._instance;
}

public async initialize(){
	try {
        let res = await platform.checkSession();
		console.log("check session, yes sessionKey!");
		UserModel.instance.getUserinfo();
    } catch (error) {
		while(true) {
			let getUserinfo = await this.initUserinfoModel();
			if(!getUserinfo) {
				await platform.checkSettingOpen();
			}else {
				break;
			}
		}
		this.refreshLogin();
    }
}

private async initUserinfoModel() {
	const userInfo = await platform.getUserInfo();
	if(userInfo) {
		console.log(userInfo);
		UserModel.instance.initialize(userInfo);
	}	
	return userInfo;
}

private async refreshLogin(){
	let resLogin = await platform.login();
	console.log("用户登录成功,res code:",resLogin);
	UserModel.instance.getSessionKey(resLogin["code"]);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment