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.

127 lines
2.9 KiB
JavaScript

2 years ago
// pages/changeAva/index.js
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
avaSrc:''
},
chooseImg(){
var that = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success (res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths[0]);
that.setData({
avaSrc:tempFilePaths[0]
})
}
})
},
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: res.tempFilePaths[0],
name: 'uploadFile',
header: app.globalData.header2,
// 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;
that.setData({
avaSrc: app.globalData.user.imgSrc
});
} else {
app.showTips(that, result.msg);
}
} else if (res.data.status == 9999) {
app.dialogNotLogin();
}
},
fail: function (res2) {
}
})
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if (app.isNotEmptyCheck(app.globalData.user.imgSrc)) {
this.setData({
avaSrc: app.globalData.user.imgSrc
});
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})