//在onload方法获取url参数
onLoad(options) {
if (options) {
this.tab = parseInt(options.tab);
this.$apply(); //更新数据方式 wepy
}
}
//index.js
getData() {
let that = this;
let session = that.$parent.globalData.session;
that.$parent.wrequest('post', 'https://dev-gsd.gamdream.com/mini-game/personal-info', {
gsd_session: session
}, (res) => {
console.log(res)
//获取分享配置
that.$parent.getShareBase('gameTimeShareGetTime');
})
}
onload() {
let that = this
let session = that.$parent.globalData.session;
if (session) {
that.getData()
} else {
that.$parent.wlogin(that.getData())
}
}
//页面分享
onShareAppMessage(ops) {
return this.$parent.shareFn();
}
//app.js
constructor() {
super()
this.use('requestfix'); // 修复小程序请求并发问题。
this.use('promisify'); // 使用wepy.xxx的方式请求小程序原生API都将Promise化。
}
globalData = {
hostUrl: 'https://galan-sandbox-h5sdk.gamdream.com',
baseUrl: 'https://galan-sandbox-mini.gamdream.com',
session: null,
openid: null,
userInfo: null,
shareConfig: null,
}
//初始化
onLaunch(e) {
var that = this;
wx.request({
url: 'https://dl.gamdream.com/activity/galan/gametime1/baseJson.json',
data: '',
method: 'get',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
var data = res.data;
var session = wx.getStorageSync("sessionPlatform");
if (session) {
if (that.getBaseInfoCallBack) {
that.getBaseInfoCallBack(res)
}
} else {
that.wlogin(that.getBaseInfoCallBack);
}
}
})
}
wlogin(callback) {
let that = this;
wx.login({
success: res => {
if (res.code) {
//发起网络请求
let data = {
js_code: res.code,
mini_id: 'mini_game'
};
that.wrequest("POST", "https://dev-gsd.gamdream.com/base/mini-login", data, function (res) {
let oData = res.data;
wx.setStorageSync("sessionPlatform", oData.gsd_session);
wx.setStorageSync("openidPlatform", oData.open_id);
that.globalData.session = oData.gsd_session;
that.globalData.openid = oData.open_id;
callback && callback(res)
});
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
},
fail: res => {
console.log(res);
}
})
}
async wrequest(method, url, data, callback) {
let that = this;
let host = (req == 1) ? this.globalData.baseUrl : this.globalData.hostUrl;
let res = await wepy.request({
method: method,
url: host + url,
data: data,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
});
let oData = res.data;
let parent = arguments;
if (oData.error_code == 0) {
wx.hideLoading();
callback(oData);
} else if (oData.error_code == -1000) {
wx.clearStorageSync();
wx.hideLoading();
that.wlogin(function (lres) {
parent[2].gsd_session = lres.data.gsd_session
that.wrequest(...parent)
});
} else {
wx.hideLoading();
wx.showToast({
title: oData.msg,
icon: 'none'
});
}
}
//获得分享配置
getShareBase(key) {
let that = this;
let param = {
gsd_session: that.globalData.session,
key: key,
url: 'pages/index'
};
that.wxRequest(2, "POST", "/shi-tu/share-config", param, function (data) {
that.globalData.shareConfig = data.shareConfig;
});
}
//公共分享
shareFn(callback) {
let that = this;
return {
title: that.globalData.shareConfig.title,
path: that.globalData.shareConfig.share_url,
imageUrl: that.globalData.shareConfig.icon
};
}
背景图不会加载本地图片,要放在线上才行。或者用image标签引入本地图片