小程序更改头像

 changeAvatar: function (e) {
    var _this = this
    wx.chooseImage({
      count: 1,// 默认9
      sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'],// 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        //这里是上传操作
        console.log( api._urlimg('a') )
        console.log( api._urlimg)
        wx.uploadFile({
          url: api._urlimg('a') + 'lps/oos/upLoadImageList', //里面填写你的上传图片服务器API接口的路径 
          filePath: tempFilePaths[0],//要上传文件资源的路径 String类型 
          name: 'image',//按个人情况修改,文件对应的 key,开发者在服务器端通过这个 key 可以获取到文件二进制内容,(后台接口规定的关于图片的请求参数)
          header: {
            //记得设置,
            "Content-Type": "multipart/form-data",
            'accept': 'application/json',
            "token": _this.data.token
          },
          formData: {
            //和服务器约定的token, 一般也可以放在header中
            'session_token': wx.getStorageSync('session_token')
          },
          success: function (res) {

            console.log(res)
            console.log(wx.getStorageSync('session_token'))
            //当调用uploadFile成功之后,再次调用后台修改的操作,这样才真正做了修改头像
            if (JSON.parse(res.data).code == 1) {
              _this.setData({
                imageUrl: JSON.parse(res.data).data[0]
              })
            }
          }
        })
      }
    })
  },

  

用uploadFile成功之后,再次调用后台修改的操作,这样才真正做了修改头像 if (JSON.parse(res.data).code == 1) { _this.setData({ imageUrl: JSON.parse(res.data).data[0] }) } } }) } }) },

原文地址:https://www.cnblogs.com/xzhce/p/13716317.html