// persondata.js
var gainData = require("../../utils/getRequestData.js");
var uploadFile = require("../../utils/uploadFile.js");
var imgUrl = require("../../config/urls.js");
var app = getApp();
Page({
data: {
imgurl: app.globalData.imgurlb,
uploadImgUrl: '/api2/file/upload',
url: '/api2/user/edit',
info_url: '/api2/user/info',
img_url: imgUrl.IMGURL,
icon_url: imgUrl.ICONURL,
logoUrl: '',
personInfo: null,
sure_clicked: false,
msg: ''
},
/**
* 修改个人LOGO
* @return null
*/
showImage: function () {
var that = this;
wx.showActionSheet({
itemList: ['拍照', '我的相册'],
success: function (res) {
var change_type = [];
if (res.tapIndex == 0) {
change_type = ['camera'];
} else if (res.tapIndex == 1) {
change_type = ['album'];
} else {
return;
}
wx.chooseImage({
count: 1, //默认9
sizeType: ['original', 'compressed'],
sourceType: change_type,
success: function (arg) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = arg.tempFilePaths[0];
wx.showLoading({
title: '图片上传中',
});
that.setData({
logoUrl: tempFilePaths
});
uploadFile.uploadFile(tempFilePaths, that.data.uploadImgUrl, that.uploadImgSuccess, that.uploadImgError);
}
});
}
});
},
/**
* 将上传的图片发向后台
* @return null
*/
changeLogo: function () {
gainData.getRequestData('POST', this.data.url + "?head=" + this.data.logoUrl, "", this.setUserList, this.errorData);
},
/**
* 请求成功返回的操作
* @param object arg 请求成功返回的数据
* @return null
*/
setUserList: function (arg) {
var that = this;
wx.hideLoading();
var data = arg.data;
if (data.status == 1) {
that.setData({
sure_clicked: true,
msg: data.msg
});
setTimeout(function () {
that.setData({
sure_clicked: false,
msg: ''
});
that.getData();
}, 1500);
} else {
wx.hideLoading();
that.setData({
sure_clicked: true,
msg: '图片保存失败'
});
}
},
/**
* 获取数据失败是的操作
* @param object arg 错误是返回的对象
* @return null
*/
errorData: function (arg) {
wx.hideLoading();
this.setData({
sure_clicked: true,
msg: '获取数据失败'
});
},
/**
* 图片上传成功
* @return null
*/
uploadImgSuccess: function (arg) {
wx.hideLoading();
var ret = JSON.parse(arg.data);
if (ret.status == 1) {
var net_img_url = ret.data[0].normal;
console.log(net_img_url)
this.setData({
logoUrl: net_img_url
});
wx.showLoading({
title: '图片保存中'
});
this.changeLogo();
} else {
this.setData({
sure_clicked: true,
msg: '图片上传失败'
});
}
},
/**
* 图片上传失败
* @return null
*/
uploadImgError: function (arg) {
wx.hideLoading();
this.setData({
sure_clicked: true,
msg: '图片上传失败'
});
},
/**
* 通过全局变量获取公司信息并设置
* @return null
*/
setDataFromGlobal: function () {
var that = this;
that.setData({
personInfo: app.globalData.userinfo1
});
},
/**
* 获取我的数据
* @return null
*/
getData: function () {
wx.showLoading({
title: '数据更新中',
mask: true
});
gainData.getRequestData('GET', this.data.info_url, "", this.successData, this.errorData);
},
/**
请求成功时候的操作
@param object arg 请求成功返回的数据
@return null
*/
successData: function (arg) {
wx.hideLoading();
var data = arg.data;
if (data.status == 1) {
app.globalData.userinfo1 = data.data;
console.log(app.globalData.userinfo1)
} else {
this.setData({
sure_clicked: true,
msg: '数据更新失败'
});
}
},
/**
* 页面加载完成立即执行
* @return null
*/
onLoad: function () {
var that = this;
that.setData({
personInfo: app.globalData.userinfo1,
logoUrl: app.globalData.userinfo1.userInfo.head
});
console.log(this.data.logoUrl);
this.setDataFromGlobal();
//this.getData();
},
/**
* 下拉刷新
* @return null
*/
onPullDownRefresh: function () {
wx.stopPullDownRefresh();
this.onLoad();
}
});
以上是上传logo代码 上传
这个页面
<view class="box" bindtap="showImage" >
<view class="box-1 mt-40">
<view class="ml-25">头像</view>
</view>
<image mode="" class="one" src="{{logoUrl?img_url+logoUrl:'../images/person.png'}}"></image>
<image mode="" src="{{imgurl}}right-icon.png" class="two"></image>
</view>
以下是个人中心一级页面 logo头像
//my.js
var imgUrl = require('../../config/urls.js');
var gainData = require("../../utils/getRequestData.js");
var app = getApp();
Page({
data: {
imgurl: app.globalData.imgurlb,
url: '/api2/user/logout?device=1',
img_url: imgUrl.IMGURL,
icon_url: imgUrl.ICONURL,
personInfo: '',
sure_clicked: false,
msg: '',
logoUrl:''
},
/**
通过全局变量获取公司信息并设置
@return {[type]} [description]
*/
setDataFromGlobal: function () {
var that = this;
that.setData({
personInfo: app.globalData.userinfo1,
logoUrl: app.globalData.userinfo1.userInfo.head
})
console.log(this.data.personInfo)
},
/**
页面加载完成后立即执行
@return null
*/
onLoad: function () {
this.setDataFromGlobal();
},
/**
下拉刷新
@return null
*/
onPullDownRefresh: function (e) {
var that = this;
wx.stopPullDownRefresh();
this.onLoad();
}
});
这个是wxml代码
<navigator url="/pages/my/persondata">
<view class="box">
<image mode="aspectFill" src="{{logoUrl?img_url+logoUrl:'../images/person.png'}}" class="one"></image>
<view class="box-1 mt-40">
<view>{{personInfo.userInfo.realname}}</view>
<view class="mt-10 ft-30">市场部</view>
</view>
<image src="{{imgurl}}right-icon.png" class="two"></image>
</view>
</navigator>
就是上传后返回不能自动刷新新的图片
没有同步 我加了手动刷新才行 有什么方法可以解决的
小程序里只能用setData更新页面,页面不会自动更新的,所以你只能在onShow的钩子里更新。
当然你也可以在当前页面更新上一个页面。