cocos creator 小游戲登錄

buttonclick:function(event){

? ? ? ? let exportJson = {};

? ? ? ? let sysInfo = window.wx.getSystemInfoSync();

? ? ? ? //獲取微信界面大小

? ? ? ? let width = sysInfo.screenWidth;

? ? ? ? let height = sysInfo.screenHeight;


? ? ? ? window.wx.login({

? ? ? ? ? ? success: (res) => {

? ? ? ? ? ? ? ? if (res.code) {

? ? ? ? ? ? ? ? ? ? console.log("res.code:", res);

? ? ? ? ? ? ? ? ? ? exportJson.code = res.code;//向服務端傳遞code用于獲取微信小游戲的用戶唯一標識


? ? ? ? ? ? ? ? ? ? window.wx.getSetting({

? ? ? ? ? ? ? ? ? ? ? ? success (res) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log(res.authSetting);

? ? ? ? ? ? ? ? ? ? ? ? ? ? if (res.authSetting["scope.userInfo"]) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log("用戶已授權");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? window.wx.getUserInfo({

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? success(res){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log(res);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? exportJson.userInfo = res.userInfo;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此時可進行登錄操作

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log("用戶未授權");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? let button = window.wx.createUserInfoButton({

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? type: 'text',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: '',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? style: {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? left: 0,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? top: 0,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? width: width,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? height: height,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? backgroundColor: '#00000000',//最后兩位為透明度

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? color: '#ffffff',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fontSize: 20,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? textAlign: "center",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lineHeight: height,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? button.onTap((res) => {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (res.userInfo) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log("用戶授權:", res);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? exportJson.userInfo = res.userInfo;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此時可進行登錄操作

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? button.destroy();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log("用戶拒絕授權:", res);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? console.log('登錄失?。? + res.errMsg)

? ? ? ? ? ? ? ? }



? ? ? ? ? ? },

? ? ? ? });


? ? },

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容