Shenlong/library/imports/3c/3c9692f1-fd69-4a82-bd5c-ce5...

146 lines
5.1 KiB
JavaScript
Raw Normal View History

2024-06-25 09:30:41 +08:00
"use strict";
cc._RF.push(module, '3c969Lx/WlKgr1czlVvLIix', 'WechatManager');
// scripts/WechatManager.ts
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WechatManager = void 0;
// wxAPI: https://developers.weixin.qq.com/minigame/dev/api/
// import { _decorator, screen} from 'cc';
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var WechatManager = /** @class */ (function () {
function WechatManager() {
// 头像回调
this._avatarCallBack = null;
}
Object.defineProperty(WechatManager, "instance", {
get: function () {
if (this._instance) {
return this._instance;
}
this._instance = new WechatManager();
return this._instance;
},
enumerable: false,
configurable: true
});
// 获取标记权限
WechatManager.prototype.initAutoSetting = function (callBack) {
var _this = this;
this._avatarCallBack = callBack;
// 避开ts语法检测
// const wx = window['wx'];
// 获取请求过的权限标记
var object = {
// 成功回调
success: function (res) {
// 是否授权用户信息
var autoSetting = res.authSetting;
if (autoSetting["scope.userInfo"]) {
// 已授权
_this.getUserInfo();
}
else {
// 未授权
_this.creatUserInfoButton();
}
},
// 失败回调
fail: function () {
console.log("wx.getSetting获取用户配置失败");
},
// 结束回调(调用成功,失败都会执行)
complete: function () {
console.log("wx.getSetting获取用户配置结束");
}
};
wx.getSetting(object);
};
// 创建用户授权按钮(仅用于登录页面, 如果用户拒绝授权,则一直显示)
WechatManager.prototype.creatUserInfoButton = function (isFull) {
var _this = this;
if (isFull === void 0) { isFull = false; }
var sysInfo = wx.getSystemInfoSync();
var width = sysInfo.screenWidth;
var height = sysInfo.screenHeight;
// const wx = window['wx'];
var object = {
type: 'text',
text: '',
style: {
left: 0,
top: 0,
width: width,
height: height,
backgroundColor: '#00000000',
color: '#ffffff',
fontSize: 20,
textAlign: "center",
lineHeight: height,
}
};
// 创建用户授权按钮(仅用于登录页面, 如果用户拒绝授权,则一直显示)
// private creatUserInfoButton(isFull: boolean = false) {
// // const wx = window['wx'];
// let object: any = {
// // 按钮类型: text可设置背景色和文本 image仅能设置背景贴图
// type: "text",
// // 按钮文本仅对type为text有效
// text: "授权",
// // 按钮样式
// style: {
// left: 70,
// top: 60,
// width: 100,
// height: 40,
// backgroundColor: "#66CC00",
// color: "#FFFFFF",
// textAlign: 'center',
// lineHeight: 40,
// fontSize: 20,
// },
// };
var button = wx.createUserInfoButton(object);
// 监听用户信息按钮点击事件
button.onTap(function (res) {
if (res && res.userInfo) {
console.log("用户同意授权");
_this._userInfo = res.userInfo;
if (_this._avatarCallBack) {
_this._avatarCallBack(_this._userInfo.avatarUrl);
}
// 授权成功后,才销毁按钮
button.destroy();
}
else {
console.log("用户拒绝授权");
}
});
};
// 获取用户信息需要获取scope.userInfo的授权也就是getSettings的接口调用
WechatManager.prototype.getUserInfo = function () {
var _this = this;
var wx = window['wx'];
var object = {
success: function (res) {
console.log("通过 getUserInfo 获取的数据:", res);
if (res) {
_this._userInfo = res.userInfo;
if (_this._avatarCallBack) {
_this._avatarCallBack(_this._userInfo.avatarUrl);
}
}
},
fail: function () {
console.log("getUserInfo获取信息失败");
},
complete: function () { },
};
wx.getUserInfo(object);
};
WechatManager._instance = null;
return WechatManager;
}());
exports.WechatManager = WechatManager;
cc._RF.pop();