123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- const LoginApi = require('./LoginApi');
- const ShareAction = require('../utils/ShareAction');
- const artistInvite = [
- {
- title: '脱口秀主持人只能有一个...那就是你!',
- image: 'talent_1.png'
- },
- {
- title: '舞王争霸还差你,快来决一高下!',
- image: 'talent_2.png'
- },
- {
- title: '的演唱会等你来献唱~',
- image: 'talent_3.png'
- },
- {
- title: '新片开等你来主演!',
- image: 'talent_4.png'
- },
- {
- title: '的电竞战队需要你来carry!',
- image: 'talent_5.png'
- },
- ];
- class WeChat {
- constructor() {
- this.wxWidth = 0;
- this.wxHeight = 0;
- this.js_code = null;
- }
-
- checkLogin(cb) {
- CC_WECHATGAME && wx.checkSession({
- success() {
-
- cb && cb(true)
- },
- fail() {
-
- cb && cb(false)
- }
- })
- }
-
- loginStatic(cb) {
- let userStorage = cc.sys.localStorage.getItem('GlobalUser')
- if (userStorage) {
- Global.user = userStorage;
- cb && cb()
- } else {
- this.login(cb)
- }
- }
-
- login(cb) {
- this.cb = cb || function () { }
- if (!CC_WECHATGAME) {
- Global.user = {
- uid: 1,
- token: 'lucifer_test_token',
- nick: 'lucifer',
- avatarUrl: 'http://mpic.tiankong.com/f94/72b/f9472be4691ab43e3f6132bb8b63d00a/640.jpg',
- gender: 1
- }
- this.cb();
- return;
- }
- this.isSupportInfoBtn = wx.createUserInfoButton ? true : false;
- wx.login({
- success: ({ code }) => {
-
- this.js_code = code;
-
- if (this.isSupportInfoBtn) {
- wx.getSetting({
- success: (res) => {
-
- if (res.authSetting['scope.userInfo']) {
- this._getUserInfo()
- } else {
- this._createBtn();
- }
- }
- })
- } else {
- this._getUserInfo()
- }
- }
- })
- wx.getSystemInfo({
- success: (res) => {
- Global.os = res.platform == "android" ? 2 : 1;
- this.wxWidth = res.windowWidth;
- this.wxHeight = res.windowHeight;
- }
- });
- }
- _getUserInfo() {
- wx.getUserInfo({
- withCredentials: true,
- success: (res) => {
- this._login(res);
- }
- })
- }
- _login(res) {
-
- 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',
-
- image: 'https://pub.dwstatic.com/wxgame/allstar/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;
- }
- inviteFriendPromise() {
- return this._invite(ShareAction.ADD_FRIEND, '确认过眼神,你就是我要的C位', 'share1.jpg');
- }
- inviteArtistPromise() {
- let content = artistInvite[Math.floor(Math.random() * 5)];
- return this._invite(ShareAction.BECOME_ARTIST, content.title, content.image);
- }
- _invite(action, title, image) {
- return new Promise((resolve, reject) => {
- if (!CC_WECHATGAME) {
- reject();
- }
- wx.shareAppMessage({
- title: Global.user.nick + title,
- imageUrl: 'https://pub.dwstatic.com/wxgame/allstar/share/' + image,
- query: 'uid=' + Global.user.uid + '&shareType=' + action,
- success: resolve,
- fail: reject
- });
- });
- }
- }
- module.exports = new WeChat();
|