由于官方修改了头像昵称填写规则,本人菜鸟通过相关教程修改了头像和昵称获取规则,但是到头像获取后,上传到服务器上后,在保存头像路径的地方遇到了问题(138行处),在另外一个方法内无法调用服务器返回的头像路径,不知道该如何修改了,难在这里3天了,请教各位帮忙指导一下了,感谢了
var app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
userInfo: {
coupu: 0,
money: 0.00
},
islogin: false,
loginuserinfo: [],
count_list: {
user_fav_count: 0,
fav_count: 0,
footprint_count: 0,
},
user: {
money: 0.00
},
code: '',
avatarUrl: '',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let menuButtonObject = wx.getMenuButtonBoundingClientRect();
console.log(menuButtonObject)
wx.getSystemInfo({
success: (res) => {
console.log(res)
// console.log(res.windowHeight*2)
let statusBarHeight = res.statusBarHeight;
const navTop = menuButtonObject.top; //胶囊按钮与顶部的距离
const navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; //导航高度
let height = res.windowHeight * 2 - navHeight * 2 - 40 - 315 - 40;
const titleBarHeight = (menuButtonObject.bottom + menuButtonObject.top) - (res.statusBarHeight * 2)
console.log(titleBarHeight)
// console.log(navHeight)
// console.log(navTop)
// console.log(height)
// console.log(res.statusBarHeight)
this.setData({
statusBarHeight: statusBarHeight,
// navHeight: navHeight,
navHeight: navHeight * 2,
navTop: navTop * 2,
height
})
this.setData({}, () => {
// console.log(30+315+40+34+30+176)
})
}
})
if (wx.getStorageSync("userinfo")) {
console.log('用户信息', wx.getStorageSync("userinfo"))
this.setData({
loginuserinfo: wx.getStorageSync("userinfo"),
islogin: true
})
}
// this.getconfig();
},
link(e) {
// console.log("跑腿状态",this.data.userInfo.runerStatus.status)
console.log(this.data.userInfo.runerStatus)
if (this.data.userInfo.runerStatus == 0 || this.data.userInfo.runerStatus == 3) {
wx.navigateTo({
url: e.currentTarget.dataset.url
})
}
},
onChooseAvatar(e) {
const { avatarUrl } = e.detail
console.log(avatarUrl)
this.setData({
avatarUrl,
})
},
formSubmit(e) {
this.setData({
modalName: null,
islogin: true,
nickName: e.detail.value.nickname,
getUseInfo: true
})
wx.uploadFile({
filePath: this.data.avatarUrl,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
name: 'file',
url: app.util.url() + 'c=entry&a=wxapp&do=ImgPost&m=gc_market',
success: (result) => {
console.log("上传图片",result)
var data = result.data
data = JSON.parse(data).data
this.setData({
imgurl:data
})
console.log("imgurl",this.data.imgurl)
}
})
var self = this;
wx.login({
success: function (data) {
data.code && app.util.request({
url: "entry/wxapp/GetUid",
data: {
code: data.code
},
success: function (data) {
console.log("用户openid",data.data.data.userinfo.openid)
self.setData({
getUseInfo: false,
openid: data.data.data.userinfo.openid,
});
console.log("打印用户头像路径",this.data.imgurl)
app.util.request({
url: "entry/wxapp/UserInfoUpdate",
data: {
openid: data.data.data.userinfo.openid,
nickname: e.detail.value.nickname,
avatar:this.data.imgurl
},
success: function (data) {
console.log("保存用户信息进入数据库",data);
}
});
console.log(data);
console.log("这里的")
wx.aldPushSendOpenid(data.data.data.openid)
// setTimeout(function(){
// wx.aldPushSubscribeMessage({
// eventId: '6010dad790fcd68beee4d642',
// success(res) {
// // 成功后的回调函数
// console.log(res)
// },
// fail(res, e) {
// // 失败后的回调函数
// console.log(res)
// console.log(e)
// }
// });
// },3000)
wx.setStorageSync("openid", data.data.data.openid)
// wx.getUserProfile({
// desc:"用户获取用户信息",
// success: function (params) {
// console.log("扣扣",params)
// var userInfo = params.userInfo
wx.setStorageSync("userinfo", )
self.setData({
islogin: true
})
// },
// fail(err)
// {
// console.log(err)
// }
// })
return false;
}
});
}
});
},
getPhoneNumber(e) {
var that = this;
wx.checkSession({
success() {
//session_key 未过期,并且在本生命周期一直有效
},
fail() {
// session_key 已经失效,需要重新执行登录流程
wx.login({
success: res => {
that.setData({
code: res.code
})
}
})
},
complete() {
//将code,encryptedData,iv传给后台进行解密
app.util.request({
url: 'entry/wxapp/getPhone',
data: {
code: that.data.code,
iv: e.detail.iv,
encryptedData: e.detail.encryptedData,
openid: wx.getStorageSync('openid')
},
success(resu) {
}
})
}
})
console.log(e)
},
showModal(e) {
this.setData({
modalName: e.currentTarget.dataset.target
})
},
hideModal(e) {
this.setData({
modalName: null
})
},
phone() {
if (!this.data.user.phone) {
wx.navigateTo({
url: '/gc_market/pages/user/phone',
})
}
},
yue() {
console.log(this.data.sysparment.is_pay_open);
if (!this.data.islogin) {
wx.showToast({
title: '请先登录',
icon: 'none'
})
} else {
wx.navigateTo({
url: '/gc_market/pages/replay/index?type=' + this.data.sysparment.is_pay_open,
})
}
},
jump(e) {
if (!this.data.islogin) {
wx.showToast({
title: '请先登录',
icon: 'none'
})
} else {
wx.navigateTo({
url: e.currentTarget.dataset.url
})
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
//获取列表
getUser() {
const self = this
app.util.request({
url: 'entry/wxapp/goodsCount',
data: {
openid: wx.getStorageSync('openid'),
},
success(res) {
if (res.data.data.user.phone) {
wx.setStorageSync('phone', res.data.data.user.phone)
res.data.data.user.phone = res.data.data.user.phone.substring(0, 3) + '****' + res.data.data.user.phone.substring(7);
}
self.setData({
count_list: res.data.data.count,
user: res.data.data.user,
})
if (res.data.data.logo == '') {
self.setData({
logo: '/images/code/c17.png'
})
} else {
self.setData({
logo: res.data.data.logo
})
}
// self.getUserCoupu()
}
})
},
//获取用户信息
bindgetuserinfo(data) {
console.log("打印", data)
var t = this;
wx.getUserProfile({
desc: "用于获取用户信息",
success(res) {
console.log("dd", res)
"getUserProfile:ok" == res.errMsg ? (t.setData({
getUseInfo: true
}), wx.setStorage({
key: "useInfo",
data: "true",
userinfo:"true",
}), t.getGetUid(res.userInfo)) : t.setData({
getUseInfo: !0
});
}
})
},
//获取用户openid
getGetUid: function (a) {
var self = this;
wx.login({
success: function (data) {
data.code && app.util.request({
url: "entry/wxapp/GetUid",
data: {
code: data.code
},
success: function (data) {
self.setData({
getUseInfo: false
});
console.log(data);
console.log("这里的")
wx.aldPushSendOpenid(data.data.data.openid)
// setTimeout(function(){
// wx.aldPushSubscribeMessage({
// eventId: '6010dad790fcd68beee4d642',
// success(res) {
// // 成功后的回调函数
// console.log(res)
// },
// fail(res, e) {
// // 失败后的回调函数
// console.log(res)
// console.log(e)
// }
// });
// },3000)
wx.setStorageSync("openid", data.data.data.openid)
// wx.getUserProfile({
// desc:"用户获取用户信息",
// success: function (params) {
// console.log("扣扣",params)
// var userInfo = params.userInfo
wx.setStorageSync("userinfo", a)
self.setData({
loginuserinfo: a,
islogin: true
})
self.toUserInfo(data.data.data.openid, a);
self.getUser()
// },
// fail(err)
// {
// console.log(err)
// }
// })
return false;
}
});
}
});
},
toUserInfo: function (openid, userInfo) {
console.log(toUserInfo, userInfo)
var that = this;
app.util.request({
url: "entry/wxapp/UserInfoUpdate",
data: {
openid: openid,
nickname: nickName,
avatar: avatarUrl,
},
success: function (data) {
console.log(data.data);
}
});
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
if (wx.getStorageSync('openid')) {
this.getUser()
}
// else{
this.getconfig();
// }
console.log('1221221', wx.getStorageSync('userinfo'))
this.setData({
userInfo: wx.getStorageSync('userinfo')
})
},
getconfig() {
var t = this;
app.util.request({
url: 'entry/wxapp/Sysparment',
success(res) {
console.log(res.data.data.sys.logo)
t.setData({
sysparment: res.data.data.sys
})
if (res.data.data.sys.logo == '') {
t.setData({
logo: '/images/code/c17.png'
})
} else {
t.setData({
logo: res.data.data.sys.logo
})
}
// t.msg()
}
})
},
msg() {
console.log("弹出框")
var self = this;
// wx.showModal({
// title:"提示",
// content:'允许接收订阅消息',
// success(res){
// if(res.confirm){
wx.getSetting({
withSubscriptions: true,
success(res) {
console.log("设置", res)
console.log(res.subscriptionsSetting)
}
})
wx.requestSubscribeMessage({
tmplIds: [self.data.sysparment.template_id, self.data.sysparment.cancel_template_id], // 此处可填写多个模板 ID,但低版本微信不兼容只能授权一个
success(res) {
console.log('已授权接收订阅消息');
// app.util.request({
// url: 'entry/wxapp/sendSubscribeMessage',
// data: {
// ordersn:'3583202103171811593073'
// },
// })
// wx.navigateTo({
// url: '/schoolrun/pages/shop/index?id='+id+'&openid='+openid+'&name='+name+'&status='+status,
// })
}
})
// }
// else{
// }
// }
// })
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
你猜 wx.uploadfile 里面 this 指向哪里
1.获取生成头像临时文件 2.wx.uploadfile 将data的临时头像文件填写进去3.后端接收将其保存到一个文件夹,返回链接到前端,接着调用就是了
后端只要能返回链接了 赋值就好了呀
你可以将图片保存到文件夹 然后将链接存到数据库 然后接口根据openid 返回链接
参考:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html
头像昵称填写能力,官方案例