|
用户的user表中,通常保存用户的图像如photo字段,此字段是File类型,在File表中保存对应的文件,user表中的photo字段表示摘要信息如对应File文件中的id、name、url等信息。利用MCM修改photo有两步工作:
1.上传一个新的文件到File中
2.修改User中对应的photo字段内容。
利用MCM实现如下:
1.使用getPicture获得客户端的图片链接
2.配置model
3.使用model.upFile上传文件,返回上传文件的信息
id,name,url等
4.利用model.updateByID更新对应的字段
代码如下:
api.getPicture({
sourceType: 'library',//;//'camera',
encodingType: 'jpg',
mediaValue: 'pic',
destinationType: 'url',
allowEdit: true,
quality: 50,
targetWidth:100,
targetHeight:100,
saveToPhotoAlbum: false
}, function(ret, err){
if (ret) {
var purl=ret.data;
var filename="AA.jpg";
var getUserInfoUrl = '/user/'+uid;
api.showProgress({
title: '正在上传...'
});
var model=api.require('model');
model.config({
appId: appId,
appKey: appKey
});
model.uploadFile({
report:false,
data:{
file:{
name:'2.png',
url:purl
},
values:{
key1:'value1',
key2:'value2'
}
}
},function(ret, err) {
if (ret) {
alert(JSON.stringify(ret));
var file={
id: ret.id,
name: ret.filename,
url: ret.url
}
alert("BBB");
alert(JSON.stringify(file));
model.updateById({
class: 'user',
id: uid,
value: {
photo:ret//JSON.stringify(file)
}}, function(ret, err){
if(ret){
alert(JSON.stringify(ret));
api.hideProgress();
}
});
}
|
|