函数写在util.js页面
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime,
}
使用函数
图片描述
图片描述
二、使用相同页面的函数
get_productInformation: function () {
。。。。
},
getZones:function(){
this.get_productInformation
},
三、使用app.js内定义的函数
app.js代码
//app.js
App({
onLaunch: function() {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
},
get_a_test:function(){
console.log('this is a test')
},
getUserInfo: function(cb) {
var that = this
if (this.globalData.userInfo) {
typeof cb == "function" &&cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.getUserInfo({
withCredentials: false,
success: function(res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" &&cb(that.globalData.userInfo)
}
})
}
},
globalData: {
userInfo: null,
college_change:false
}
})
在其他页面中使用
图片描述
多次拒绝登录和允许登录微信小程序的登录获取用户信息,是通过微信d出窗口,用户可点击允许 和拒绝两个按钮,点击允许,则获取用户信息登录成功,若点击拒绝,则获取失败,可通过二次请求调用d起获取用户信息窗口。
具体代码如下:
//app.js
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
console.log('logs',logs)
},
getUserInfo:function(cb){
var that = this
//调用登录接口
function getOpenid(backMsg){
that.globalData.userInfo = backMsg.userInfo
that.globalData.encryptedData = backMsg.encryptedData
that.globalData.iv = backMsg.iv
that.globalData.login = true
console.log(that.globalData.code)
wx.request({
url:that.localUrl+'Login/sendCodeLogin',
data:{
code:that.globalData.code,
encryptedData: backMsg.encryptedData,
iv:backMsg.iv
},
success:function(openData){
console.log('返回openid',openData,openData.data)
if(openData.data.code==1001){
that.globalData.openid = openData.data.data.openid
wx.setStorageSync('openid',that.globalData.openid)
wx.setStorageSync('userInfo',that.globalData.userInfo)
wx.setStorageSync('isManager', openData.data.data.isManager)
wx.showToast({
title: '登录成功',
icon: 'success',
duration: 500
})
typeof cb == "function" &&cb(that.globalData.userInfo)
}else{
wx.showLoading({
title: '登录失败'
})
setTimeout(function () {
wx.hideLoading()
}, 500)
}
}
})
}
wx.login({
success: function (msg) {
console.log('code',msg)
if(msg.code){
that.globalData.code = msg.code
if(that.globalData.login==false){
wx.openSetting({
success: function (data) {
if(data) {
if (data.authSetting["scope.userInfo"] == true) {
//loginStatus = true
wx.getUserInfo({
withCredentials: false,
success: function (res) {
console.log('第二次成功',res)
getOpenid(res)
},
fail: function (res) {
that.globalData.login = false
console.log('二次失败',res)
}
})
}else{
that.globalData.login = false
console.log('二次失败02')
}
}
},
fail: function () {
console.info("设置失败返回数据")
}
})
}else{
wx.getUserInfo({
success: function (res) {
console.log('第一次成功',res)
getOpenid(res)
},fail:function(msg){
that.globalData.login = false
console.log('第一次失败',msg)
}
})
}
}
},
fail:function(res){
console.log(res)
}
})
},
globalData:{
userInfo:null,
encryptedData:null,
iv:null,
openid:null,
code:null
}
})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)