You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.0 KiB
JavaScript

2 years ago
// pages/changeAvatar/index.js
let app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
files: [],
userInfo: {},
temporaryImg: "",
isSure: false,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
this.setData({
userInfo: app.globalData.user,
});
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {},
/**
* 用户点击右上角分享
*/
onShareAppMessage1() {},
/**
* 选择临时图片
*
*
*/
chooseImage() {
const that = this;
wx.chooseImage({
sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
success(res) {
// 返回选定照片的本地文件路径列表tempFilePath可以作为img标签的src属性显示图片
console.log(res);
that.setData({
files: that.data.files.concat(res.tempFilePaths),
temporaryImg: res.tempFilePaths[0],
isSure: true,
});
},
});
},
changeUserImgsrc: function () {
var that = this;
// wx.chooseImage({
// count: 1, // 默认9
// sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
// sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
// success: function (res) {
// // 返回选定照片的本地文件路径列表tempFilePath可以作为img标签的src属性显示图片
// var tempFilePaths = res.tempFilePaths;
// // var formData = {'userId': app.globalData.userId};
// },
// });
wx.uploadFile({
url: app.globalData.ip + "/user/changeUserImgsrc",
filePath: that.data.temporaryImg,
name: "uploadFile",
header: app.globalData.headers2,
// formData: formData, // HTTP 请求中其他额外的 form data
success: function (res1) {
console.log(res1);
if (res1.statusCode == 200) {
var result = JSON.parse(res1.data);
if (result.status == 200) {
app.globalData.user.imgSrc = result.data.url;
app.showTips(that, "更改成功");
setTimeout(() => {
that.setData({
userInfo: app.globalData.user,
isSure: false,
});
wx.navigateBack({
delta: 1,
});
}, 1000);
} else {
app.showTips(that, result.msg);
}
} else if (res.data.status == 9999) {
app.dialogNotLogin();
}
},
fail: function (res2) {},
});
},
cancelChoice() {
this.setData({
isSure: false,
temporaryImg: "",
});
},
choiceAvatar(e) {
console.log(e);
const { avatarUrl } = e.detail;
this.setData({
temporaryImg: avatarUrl,
isSure: true,
});
},
});