123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- const LoginApi = require('./LoginApi');
- const ShareAction = require('../utils/ShareAction');
- const AlertManager = require('../utils/AlertManager');
- // const SkillApi = require("../net/SkillApi");
- class WeChat {
- constructor() {
- this.wxWidth = 0;//微信小程序可用屏幕宽度
- this.wxHeight = 0;//微信小程序可用屏幕高度
- this.js_code = null;
- this.anonymousCode = null;
- this.shareArray = WeChat.shareObjcts;
- }
- /**
- * 检查登录态是否已失效
- * 回调函数会回传flag值(true:未失效, false:已失效)
- * @param {Function} cb 回调方法
- */
- checkLogin(cb) {
- if (window.tt != undefined) {
- tt.checkSession({
- success() {
- //session_key 未失效
- cb && cb()
- },
- fail() {
- //session_key 已失效
- cb && cb(true)
- }
- })
- } else if (CC_WECHATGAME) {
- wx.checkSession({
- success() {
- //session_key 未失效
- cb && cb()
- },
- fail() {
- //session_key 已失效
- cb && cb(true)
- }
- })
- }
- }
- /**
- * 使用存在localStorage中的登录信息进行静默登录
- * 不刷新 uid 和 token
- * @param {Function} cb 回调方法
- */
- loginStatic(cb) {
- let userStorage = cc.sys.localStorage.getItem('GlobalUser')
- if (userStorage) {
- GameGlobal.user = userStorage;
- cb && cb()
- } else {
- this.login(cb)
- }
- }
- /**
- * 调起一个新的微信登录
- * 刷新 token
- * @param {Function} cb 回调方法
- */
- login(cb) {
- this.cb = cb || function () { }
- if (!CC_WECHATGAME && window.tt == undefined) {
- GameGlobal.user = {
- uid: 1,
- token: 'lucifer_test_token',
- nick: 'lucifer',
- avatarUrl: 'http://mpic.tiankong.com/f94/72b/f9472be4691ab43e3f6132bb8b63d00a/640.jpg',
- gender: 1
- }
- this.cb();
- return;
- }
- if (window.tt != undefined) {
- var self = this;
- tt.login({
- success (res) {
- self.js_code = res.code;
- self.anonymousCode = res.anonymousCode;
- self._getUserInfo();
- },
- fail (res) {
- self.cb && self.cb(true);
- }
- });
- } else if (CC_WECHATGAME) {
- this.isSupportInfoBtn = wx.createUserInfoButton ? true : false;
- wx.login({
- success: ({ code }) => {
- this.js_code = code;
- //如果支持用户信息按钮
- if (this.isSupportInfoBtn) {
- wx.getSetting({
- success: (res) => {
- // 已经授权,可以直接调用 getUserInfo 获取头像昵称
- if (res.authSetting['scope.userInfo']) {
- this._getUserInfo();
- } else {
- this._createBtn();
- }
- }
- })
- } else {
- this._getUserInfo();
- }
- }
- })
- wx.getSystemInfo({
- success: (res) => {
- GameGlobal.os = res.platform == "android" ? 2 : 1;
- this.wxWidth = res.windowWidth;
- this.wxHeight = res.windowHeight;
- }
- });
- }
- }
- _getUserInfo() {
- if (window.tt != undefined) {
- var self = this;
- tt.getUserInfo({
- withCredentials: true,
- success (res) {
- self._login(res);
- },
- fail (res) {
- self.cb && self.cb(true);
- }
- });
- } else if (CC_WECHATGAME) {
- wx.getUserInfo({
- withCredentials: true,
- success: (res) => {
- this._login(res);
- }
- })
- }
- }
- _login(res) {
- if (window.tt != undefined) {
- LoginApi.ttLogin(this.js_code, this.anonymousCode, res, this.cb);
- } else if (CC_WECHATGAME) {
- LoginApi.login(this.js_code, res, this.cb);
- }
- }
- /**
- * 创建一个微信环境下,获取登录信息的原生按钮
- */
- _createBtn() {
- let width = 167;
- let height = 88.5;
- let left = (this.wxWidth - width) / 2;
- let top = this.wxHeight - 380;
- console.log('top: ', top);
- let button = wx.createUserInfoButton({
- type: 'image',
- // text: '点击授权',
- image: 'https://pub.dwstatic.com/wxgame/taptapstar/share/startgame.png',
- style: {
- left: left,
- top: top,
- width: width,
- height: height,
- backgroundColor: '#ffffff',
- borderColor: '#ffffff',
- borderWidth: 0,
- borderRadius: 5,
- color: '#ffffff',
- textAlign: 'center',
- fontSize: 16,
- lineHeight: 88.5,
- },
- withCredentials: true
- })
- button.onTap((res) => {
- if (res.errMsg == 'getUserInfo:ok') {
- button.destroy();
- this._login(res);
- }
- })
- return button;
- }
- shareAction(type, success, fail) {
- if (CC_WECHATGAME) {
- GameGlobal.isIgnoreShareStatus = false;
- GameGlobal.clickShare = true;
- GameGlobal.gameShareType = type;
- }
- let randomIndex = parseInt(Math.random()*(WeChat.shareObjcts.length),10);
- let shareObjct = WeChat.shareObjcts[randomIndex];
- if (window.tt != undefined) {
- tt.shareAppMessage({
- title: shareObjct.title,
- imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
- query: 'uid=' + GameGlobal.user.uid + '&shareType=' + ShareAction.INVITE_FRIEND,
- success: (res) => {
- console.log('分享成功');
- success && success();
- },
- fail: () => {
- console.log('分享失败或取消');
- fail && fail();
- GameEvent.fire(GameNotificationKey.ShowShareAction, GameGlobal.gameShareType, false);
- GameGlobal.clickShare = false;
- GameGlobal.gameShareType = WechatShareType.None;
- GameGlobal.isIgnoreShareStatus = false;
- }
- });
- } else if (CC_WECHATGAME) {
- wx.shareAppMessage({
- title: shareObjct.title,
- imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
- query: 'uid=' + GameGlobal.user.uid + '&shareType=' + ShareAction.INVITE_FRIEND,
- success: (res) => {
- console.log('分享成功');
- //判断是否分享到群
- if (res.hasOwnProperty('shareTickets')) {
- if (success) {
- success();
- }
- } else {
- // AlertManager.showShareFailAlert();
- console.log('分享的不是群');
- if (fail) {
- fail();
- }
- }
- },
- fail: () => {
- if (fail) {
- fail();
- }
- console.log('分享失败或取消');
- }
- });
- } else if (CC_QQPLAY) {
- BK.Share.share({
- qqImgUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/' + shareObjct.icon,
- summary: shareObjct.title,
- isToFriend: true,
- extendInfo: '',
- success: function(res) {
- BK.Console.log('分享成功', res.code, JSON.stringify(res.data));
- if(res.data.ret == 0) {
- success && success()
- }
- },
- fail: function(res) {
- BK.Console.log('分享失败', res.code, JSON.stringify(res.msg));
- fail && fail()
- },
- complete: function(res) {
- BK.Console.log('分享完成,不论成功失败');
- }
- });
- }
- }
- /// 跳转客服 是否为提现
- jumpCustomerServices(isDraw = false) {
- if (CC_QQPLAY || window.tt !== undefined) {
- AlertManager.showStoreQQaddGroup();
- } else if (CC_WECHATGAME) {
- if (isDraw) {
- wx.openCustomerServiceConversation({
- sessionFrom: 'draw',
- showMessageCard: false,
- });
- } else {
- wx.openCustomerServiceConversation({
- sessionFrom: 'shop',
- showMessageCard: true,
- sendMessageTitle: '商品',
- sendMessageImg: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_shop.png'
- });
- }
- }
- }
- static shareObjcts = [
- {'title': '猜猜他是谁?', 'icon': 'share_image_1.png'},
- {'title': '猜猜他是谁?', 'icon': 'share_image_2.png'},
- {'title': '看到这位明星了吗,签下这份合同,她就是你的人了~', 'icon': 'share_image_3.png'},
- {'title': '就、就算你是大明星,签约了你也是我的人!', 'icon': 'share_image_4.png'},
- {'title': '老板跟着小姨子跑路啦!本公司签约女团低价转让!', 'icon': 'share_image_5.png'},
- {'title': '你,还有你,我钦定你们两个。。。组CP!', 'icon': 'share_image_6.png'},
- {'title': '猜猜他是谁?', 'icon': 'share_image_7.png'},
- {'title': '猜猜他是谁?', 'icon': 'share_image_8.png'},
- {'title': '猜猜她是谁?', 'icon': 'share_image_9.png'}
- ]
- }
- module.exports = new WeChat();
|