Last active
January 25, 2018 07:58
-
-
Save junlas/cde3558d001c00093da816336519c862 to your computer and use it in GitHub Desktop.
微信小游戏平台数据接口 - 基于Promise异步形式封装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
微信授权接口相关调用逻辑(WeixinAuthor.ts),配合platform.js使用:
class WeixinAuthor {
private static _instance:WeixinAuthor;
}