123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- // wxAPI: https://developers.weixin.qq.com/minigame/dev/api/
- import { VibrateType, uploadType } from "./SDK";
- export class WechatManager {
- private static _instance: WechatManager = null;
- // 用户信息: 昵称,头像,语言等
- private _userInfo: wx.types.UserInfo;
- // 头像成功失败回调
- private _avatarCallBack: Function = null;
- private _Fail: Function = null;
- private systemInfo: wx.types.SystemInfo = null;
- static get instance() {
- if (this._instance) {
- return this._instance;
- }
- this._instance = new WechatManager();
- this._instance.init()
- return this._instance;
- }
- private videoAd: RewardedVideoAd = null;
- public OpenId: string = '';
- init() {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- this.systemInfo = wx.getSystemInfoSync();
- //显示转发按钮
- wx.showShareMenu({
- withShareTicket: true,
- menus: ['shareAppMessage', 'shareTimeline']
- });
- //视频广告
- if (compareVersions(this.systemInfo.SDKVersion, "2.0.4") == 2) {
- //self.refreshBannerAd();
- this.videoAd = wx.createRewardedVideoAd({
- adUnitId: ''
- });
- this.videoAd.onClose((res) => {
- if (!res) return console.error("Error:video ad closed with no res!");
- res.isEnded ? () => { cc.log('成功') }
- : () => { cc.log('失败') };
- });
- this.videoAd.onError((errMsg) => {
- console.error("errCode:", errMsg);
- })
- }
- }
- }
- // 获取标记权限
- public initAutoSetting(Succeed, Fail, btnNode: cc.Node) {
- this._avatarCallBack = Succeed;
- this._Fail = Fail;
- // 避开ts语法检测
- // const wx = window['wx'];
- // 获取请求过的权限标记
- let object: any = {
- // 成功回调
- success: (res) => {
- // 是否授权用户信息
- const autoSetting = res.authSetting;
- console.error('已经授权的权限');
- console.error(autoSetting);
-
- if (autoSetting["scope.userInfo"]) {
- // 已授权
- this.getUserInfo();
- } else {
- // 未授权
- this.creatUserInfoButton(btnNode);
- }
- },
- // 失败回调
- fail() {
- console.log("wx.getSetting获取用户配置失败");
- },
- // 结束回调(调用成功,失败都会执行)
- complete() {
- console.log("wx.getSetting获取用户配置结束");
- }
- }
- wx.getSetting(object);
- }
- // 创建用户授权按钮(仅用于登录页面, 如果用户拒绝授权,则一直显示)
- private creatUserInfoButton(btnNode: cc.Node) {
- let btnSize = cc.size(btnNode.width + 10, btnNode.height + 10);
- let frameSize = cc.view.getFrameSize();
- let winSize = cc.director.getWinSize();
- //适配不同机型来创建微信授权按钮
- let left = (winSize.width * 0.5 + btnNode.x - btnSize.width * 0.5) / winSize.width * frameSize.width;
- let top = (winSize.height * 0.5 - btnNode.y - btnSize.height * 0.5) / winSize.height * frameSize.height;
- let width = btnSize.width / winSize.width * frameSize.width;
- let height = btnSize.height / winSize.height * frameSize.height;
- // const wx = window['wx'];
- let object: any = {
- // 按钮类型: text可设置背景色和文本 image仅能设置背景贴图
- type: "text",
- // 按钮文本,仅对type为text有效
- text: "授----权",
- // 按钮样式
- style: {
- left: left,
- top: top,
- width: width,
- height: height,
- backgroundColor: '',
- color: "#FFFFFF",
- textAlign: 'center',
- lineHeight: 40,
- fontSize: 20,
- borderRadius: 4
- },
- };
- const button = wx.createUserInfoButton(object);
- // 监听用户信息按钮点击事件
- button.onTap((res) => {
- if (res && res.userInfo) {
- console.log("用户同意授权");
- this._userInfo = res.userInfo;
- if (this._avatarCallBack) {
- this._avatarCallBack(this._userInfo);
- }
- // 授权成功后,才销毁按钮
- button.destroy();
- } else {
- if (this._Fail) {
- this._Fail();
- }
- console.log("用户拒绝授权");
- button.destroy();
- }
- });
- }
- // 获取用户信息,需要获取scope.userInfo的授权,也就是getSettings的接口调用
- private getUserInfo() {
- // const wx = window['wx'];
- let object: any = {
- success: (res) => {
- console.log("通过 getUserInfo 获取的数据:", res);
- if (res) {
- this._userInfo = res.userInfo;
- if (this._avatarCallBack) {
- this._avatarCallBack(this._userInfo);
- }
- }
- },
- fail: () => {
- console.log("getUserInfo获取信息失败");
- },
- complete: () => { },
- };
- wx.getUserInfo(object);
- }
- public Login(Cb: Function) {
- wx.login({
- success(res) {
- if (res.code) {
- console.error('登陆成功-得到的数据');
- console.error(res);
- Cb?.(res)
- // 发起网络请求
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- }
- //分享
- share() {
- if (typeof (wx) == "undefined") return console.error("需要微信环境");
- var title = ["要来和我一起玩吗?", "是兄弟就来上班!"];
- var Uuurl = [
- "https://mmocgame.qpic.cn/wechatgame/IDZiaFvyAB0KnCXhS0wjES2ic9HIHWYqoA6kibQ1R9XPCINOAhYZRha0zHz7XibliaoIm/0",
- "https://mmocgame.qpic.cn/wechatgame/zK99jwpnFZJLNrv5icNa8Bg7PzGvVicv9A1L7HvCvxTHyCWFpof4GXXUfsJZBZYyiab/0"];
- let randomMMM = function getRandomTitle(titles: string[]): string {
- const randomIndex = Math.floor(Math.random() * titles.length);
- return titles[randomIndex];
- }
- return new Promise((resolve, reject) => {
- wx.shareAppMessage({
- title: randomMMM(title),
- imageUrl: randomMMM(Uuurl),
- // query: 'inviteID=66666&key2=val2'
- query: `inviteID=${WechatManager.instance.OpenId}`
- });
- resolve(0);
- })
- }
- uploadUserData(_value1, _value2) {
- if (typeof (wx) == "undefined") return console.error("需要微信环境");
- var KVList = [];
- var KVData: any = {};
- KVData.key = uploadType.上班次数;
- KVData.value = _value1
- KVList.push(KVData);
- var KVData1: any = {};
- KVData1.key = uploadType.职位等级;
- KVData1.value = _value1
- KVList.push(KVData1);
- console.log("uploadData:", JSON.stringify(KVData));
- console.log("uploadData:", JSON.stringify(KVData1));
- wx.setUserCloudStorage({
- KVDataList: KVList,
- success: function () {
- console.log("upload success!");
- }
- });
- }
- //震动
- vibrate(t: VibrateType, a: wx.types.Callbacks) {
- switch (t) {
- case VibrateType.Long:
- wx.vibrateLong(a)
- break;
- case VibrateType.Short:
- wx.vibrateShort(a)
- break;
- default:
- break;
- }
- }
- }
- //比较版本号1和版本号2的大小
- //返回值:0:等于,1:小于,2:大于
- export function compareVersions(v1: string, v2: string): number {
- if (v1 == v2) return 0;
- var grp1 = v1.split('.');
- var grp2 = v2.split('.');
- for (let i = 0; i < Math.max(grp1.length, grp2.length); i++) {
- var num1 = parseInt(grp1[i] || '0');
- var num2 = parseInt(grp2[i] || '0');
- if (num1 > num2) {
- return 2;
- } else if (num1 < num2) {
- return 1;
- }
- }
- return 0;
- }
|