帖子
帖子
用户
博客
课程
显示全部楼层
89
帖子
0
勋章
1万+
Y币

[云开发] 利用MCM实现用户的图像修改

[复制链接]
发表于 2015-2-6 09:12:42
用户的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();
                                            }
                                        });       
                            }





89
帖子
0
勋章
1万+
Y币
注意:
1。每一步都可以用  alert(JSON.stringify(ret))返回查看内容
2.对user要设置好权限,如果没有设置为public,则必须先使用user模块的login方法。
8
帖子
0
勋章
915
Y币
论坛表情太少了。

非常赞的分享。
您需要登录后才可以回帖 登录

本版积分规则