微信小程序值找不到 传参没有数据undefined 使用onfire

1.data-questionId的驼峰式命名是不行的必须小写

2.没有设置的合法域名去请求数据也会影响

//当时的app.js

importoDatafrom'./utils/network.js'

importStoragefrom'./utils/storage.js'

conststore=newStorage(wx);

constregeneratorRuntime=require('./utils/libs/regenerator-runtime/runtime-module.js')

App({

onLaunch:function(){

//扩展js方法

this.globalExpansion()

//判断设备是否为iPhoneX

this.checkIsIPhoneX()

constres=wx.getSystemInfoSync()

this.globalData.SystemInfo=res

console.log('onLaunch')

},

App_load:asyncfunction(){

letloaded=false

letuser=awaitthis.UserLogin()

if(user.data){

this.globalData.partyId=user.data.partyId

this.globalData.userTenantId=user.data.userTenantId

awaitthis.getStoreMessage()

loaded=true

}else{

loaded=false

}

returnloaded

},

globalExpansion:function(){

Array.prototype.equals=function(arr){

returnthis.sort().join()===arr.sort().join()

}

Date.prototype.Format=function(fmt){//author:meizz

varo={

"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(varkino)

if(newRegExp("("+k+")").test(fmt))fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));

returnfmt;

}

},

checkIsIPhoneX:function(){

constself=this

wx.getSystemInfo({

success:function(res){

//根据model进行判断

if(res.model.search('iPhoneX')!=-1){

self.globalData.isIPX=true

}

//或者根据screenHeight进行判断

//if(res.screenHeight==812){

//self.globalData.isIPX=true

//}

}

})

},

getStoreMessage:asyncfunction(){

letproductStoreId='10000'

letoptions={

path:'ProductStores',

parameters:{

$filter:`productStoreIdeq'${productStoreId}'`,

}

}

awaitoData.read(options).then(res=>{

if(res.data.value&&res.data.valueinstanceofArray&&res.data.value.length>0){

this.globalData.storeObject=res.data.value[0]

}

})

},

getSessionKey:function(){

returnnewPromise((resolve,reject)=>{

letwebSiteId=this.globalData.webSiteId

//检查Session是否过期

wx.checkSession({

//session_key未过期,并且在本生命周期一直有效

success:()=>{

letopenid=store.get('openid')

if(openid){

resolve(openid)

}else{

//重新登录

wx.login({

success:(res)=>{

if(res.code){

//发起网络请求

letoptions={

path:'getWxMiniprogramUserId',

method:'POST',

body:{

'code':`${res.code}`,

'webSiteId':`${webSiteId}`

}

}

oData.submit(options).then(res=>{

if(res.value){

letopenid=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){

//发起网络请求

letoptions={

path:'getWxMiniprogramUserId',

method:'POST',

body:{

'code':`${res.code}`,

'webSiteId':`${webSiteId}`

}

}

oData.submit(options).then(res=>{

if(res.data.value){

letopenid=res.data.value

store.set('openid',openid)

resolve(openid)

}else{

resolve(false)

}

})

}else{

resolve(false)

console.log('登录失败!'+res.errMsg)

}

}

})

}

})

})

},

GlobalRequest:async(option)=>{

letres=awaitnewPromise((resolve,reject)=>{

wx.request({

url:option.url,

data:option.data||{},

method:option.method||'GET',

success:(res)=>{

resolve(res)

},

fail:(res)=>{

reject(res)

},

complete:(res)=>{

}

})

})

returnres

},

UserLogin:asyncfunction(){

letopenid=''

awaitthis.getSessionKey().then(res=>{

openid=res

})

letuser=''

if(openid){

letoption={

url:'https://https://www.cloudlakenet.com?openId='+openid

}

awaitthis.GlobalRequest(option).then(res=>{

if(res.statusCode===200||res.statusCode===204){

letcookies=res.header['Set-Cookie'].split(';')

letset_cookies=res.header['Set-Cookie']

store.set('x-cookie',set_cookies)

}

user=res

})

}

returnuser

},

globalData:{

regeneratorRuntime:regeneratorRuntime,

SystemInfo:'',

userInfo:null,

USERNAME:'posm2',

PASSWORD:'ofbiz',

prodCatalogId:'MPTC',

productStoreId:'10000',

webSiteId:'ECX-01',

isIPX:false,//当前设备是否为iPhoneX

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){
  // 执行操作
})
})

相关推荐