前端代码
//profile settings
var avatarData = '';
upload.preview('avatar','preview-avatar',function(data){
avatarData = data;//base64
});
new Vue({
el:'#profile-form',
data:{
profile:{
nickname:'',
sex:profileSex,
company:'',
avatar:'',
description:'',
github:'',
weibo:''
}
},
methods:{
submit:function(e){
e.preventDefault();
loadin.show('load');
var profileData = this.$data.profile;
if(avatarData){
avatarData = avatarData.replace(/^data:image\/\w+;base64,/,'');
}
profileData.avatar = avatarData;
$.ajax({type:'POST',url:'/profile',data:profileData}).then(function(ret){
ret = JSON.parse(ret);
if(ret.status){
loadin.show('alert',ret.msg,'success');
setTimeout(function(){
location.reload();
},1000);
}
},function(){});
}
}
});
后端代码
//profile
user.profile = function* (){
var body = this.request.body,
nickname = body.nickname,
sex = body.sex,
company=body.company,
avatar = body.avatar,
description= body.description,
github=body.github,
weibo = body.weibo;
if(avatar){
var imgbuffer = new Buffer(avatar,'base64');
var imgName = yield model.get({email:this.session.user.email},'avatar');
if(/custom/.test(imgName.avatar)){
imgName = 'assets'+imgName.avatar;
}else{
imgName = 'assets/customavatar/petitspois:'+(+new Date)+(Math.random()*1000|0)+'.png';
}
yield fs.writeFile(imgName,imgbuffer);
}
var profile = {
nickname:nickname,
sex: sex,
company:company,
description:description,
github:github,
weibo:weibo
}
avatar && (profile.avatar = imgName.slice(6));
var update = yield model.update({email:this.session.user.email},{$set:profile});
if(update){
this.body = {
msg:'更新成功',
status:1
}
}
}
assets/customavatar/ //static assets
swig
<a href="" class="thumb-lg pull-left m-r">
<img src="{{user.avatar}}" class="img-circle">
</a>
默认头像为一个随机的图片url,当自定义后就写入新的url。但是需要重启node.怎么才可以解决呢?