2.没有设置的合法域名去请求数据也会影响
//当时的app.js
import oData from './utils/network.js'
import Storage from './utils/storage.js'
const store = new Storage(wx);
const regeneratorRuntime = require('./utils/libs/regenerator-runtime/runtime-module.js')
App({
onLaunch: function() {
// 扩展js方法
this.globalExpansion()
// 判断设备是否为 iPhone X
this.checkIsIPhoneX()
const res = wx.getSystemInfoSync()
this.globalData.SystemInfo = res
console.log('onLaunch')
},
App_load: async function() {
let loaded = false
let user = await this.UserLogin()
if (user.data) {
this.globalData.partyId = user.data.partyId
this.globalData.userTenantId = user.data.userTenantId
await this.getStoreMessage()
loaded = true
} else {
loaded = false
}
return loaded
},
globalExpansion: function() {
Array.prototype.equals = function (arr) {
return this.sort().join() === arr.sort().join()
}
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
},
checkIsIPhoneX: function () {
const self = this
wx.getSystemInfo({
success: function (res) {
// 根据 model 进行判断
if (res.model.search('iPhone X') != -1) {
self.globalData.isIPX = true
}
// 或者根据 screenHeight 进行判断
// if (res.screenHeight == 812) {
// self.globalData.isIPX = true
// }
}
})
},
getStoreMessage: async function() {
let productStoreId = '10000'
let options = {
path: 'ProductStores',
parameters: {
$filter: `productStoreId eq '${productStoreId}'`,
}
}
await oData.read(options).then(res =>{
if (res.data.value && res.data.value instanceof Array && res.data.value.length > 0) {
this.globalData.storeObject = res.data.value[0]
}
})
},
getSessionKey: function () {
return new Promise((resolve, reject) => {
let webSiteId = this.globalData.webSiteId
// 检查Session是否过期
wx.checkSession({
//session_key 未过期,并且在本生命周期一直有效
success: () => {
let openid = store.get('openid')
if (openid) {
resolve(openid)
} else {
// 重新登录
wx.login({
success: (res) => {
if (res.code) {
//发起网络请求
let options = {
path: 'getWxMiniprogramUserId',
method: 'POST',
body: {
'code': `${res.code}`,
'webSiteId': `${webSiteId}`
}
}
oData.submit(options).then(res => {
if (res.value) {
let openid = res.value
store.set('openid', openid)
resolve(openid)
} else {
resolve(false)
}
})
} else {
resolve(false)
console.log('登录失败!' + res.errMsg)
}
}
})
}
},
fail: () => {
// session_key 已经失效,需要重新执行登录流程
//重新登录
wx.login({
success: (res) => {
if (res.code) {
//发起网络请求
let options = {
path: 'getWxMiniprogramUserId',
method: 'POST',
body: {
'code': `${res.code}`,
'webSiteId': `${webSiteId}`
}
}
oData.submit(options).then(res => {
if (res.data.value) {
let openid = res.data.value
store.set('openid', openid)
resolve(openid)
} else {
resolve(false)
}
})
} else {
resolve(false)
console.log('登录失败!' + res.errMsg)
}
}
})
}
})
})
},
GlobalRequest: async (option) => {
let res = await new Promise((resolve, reject) => {
wx.request({
url: option.url,
data: option.data || {},
method: option.method || 'GET',
success: (res) => {
resolve(res)
},
fail: (res) => {
reject(res)
},
complete: (res) => {
}
})
})
return res
},
UserLogin: async function() {
let openid = ''
await this.getSessionKey().then(res => {
openid = res
})
let user = ''
if (openid) {
let option = {
url: 'https://https://www.cloudlakenet.com?openId=' + openid
}
await this.GlobalRequest(option).then(res => {
if (res.statusCode === 200 || res.statusCode === 204) {
let cookies = res.header['Set-Cookie'].split(';')
let set_cookies = res.header['Set-Cookie']
store.set('x-cookie', set_cookies)
}
user = res
})
}
return user
},
globalData: {
regeneratorRuntime: regeneratorRuntime,
SystemInfo: '',
userInfo: null,
USERNAME: 'posm2',
PASSWORD: 'ofbiz',
prodCatalogId: 'MPTC',
productStoreId: '10000',
webSiteId: 'ECX-01',
isIPX: false, // 当前设备是否为 iPhone X
storeObject: null,
partyId: null,
UomObj: {
'CNY': '¥'
},
requestUri: 'TestAppOdataServlet',
timeOut: 120,
authorizedFlag: false,
baseColor: '#0078c8',
shoppingCartObject: null,
userTenantId: null,
}
})
另外可以使用onfire方法传值
通过onfire.js为来实现这个效果
onfire.js的下载地址https://github.com/hustcc/onfire.js
使用思路:
A 页面先订阅一个事件,并定义处理方法;
从 B 页面返回时,发送消息;
A 页面卸载时,解除订阅。
方式:
var onfire = require("../utils/onfire.js"); var that; var eventObj = onfire.on('key', function () { // 当消息被传递时,做具体的事 }); Page({ data: { }, onLoad: function(options) { // Do some initialize when page load. }, onReady: function() { // Do something when page ready. }, onUnload: function (e) { onfire.un('key'); onfire.un(eventObj);//移除 } var eventObj = onfire.on('key', function (data){ // 执行操作 }) })