1.微信小程序API之图片选择与调用微信拍照

wx.chooseImage选择图片(以下为主体代码)

<view bindtap="bindViewTap" class="userinfo">
<image class="userinfo-avatar" src="{{avatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</view>

data: {
motto: 'Hello World',
userInfo: {},
avatarUrl:'/image/logo.png'
},
//事件处理函数
bindViewTap: 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;
console.log(tempFilePaths);
that.setData({avatarUrl:tempFilePaths[0]});
}
})
},

 小程序调用

wx.login({
  success (res) {
    if (res.code) {
      //发起网络请求
      wx.request({
        url: 'https://example.com/onLogin',
        data: {
          code: res.code
        }
      })
    } else {
      console.log('登录失败!' + res.errMsg)
    }
  }
})

  

感觉调用麻烦,封装下

/*promisify.js*/
module.exports = api => {
    return (options, ...params) => {
        return new Promise((resolve, reject) => {
            api(Object.assign({}, options, {success: resolve, fail: reject}), ...params);
        });
    };
};
const login = promisify(wx.login);
const getSetting = promisify(wx.getSetting);

login().then(function (res) {
                getSetting().then(function (r) {
                    let userInfo = r;
                    userInfo.code = res.code;
                    if (r.authSetting['scope.userInfo']) {
                        let app = getApp();
                        app.globalData.hasAuth = true;
                        getUserInfo().then(function (r) {
                            let userInfo = r;
                            userInfo.code = res.code;
                            try {
                                wx.setStorageSync('userInfo', userInfo);
                            } catch (e) {

                            }
                            if (userInfo && userInfo.code && userInfo.iv) {
                                resolve(userInfo);
                            }
                            else {
                                reject('wx login fail');
                            }
                        }).catch(function (errer) {
                            reject(errer);
                            var app = getApp();
                            app.globalData.hasAuth = false;
                        });
                    } else {
                        let app = getApp();
                        app.globalData.hasAuth = false;
                        wx.navigateTo({
                            url: '/pages/auth/auth',
                        });
                    }
                }).catch(function (err) {
                    let app = getApp();
                    app.globalData.hasAuth = false;
                    wx.navigateTo({
                        url: '/pages/auth/auth',
                    });
                });

            }).catch(function (error) {
                reject(error);
            });

 

是不是更简单好用了  

posted on 2017-11-08 17:45  执候  阅读(191)  评论(0编辑  收藏  举报