微信发起摄像头接口请求 上传头像 图片

var requestSign = require('../../../../utils/requestData.js');
var api = require('../../../../utils/api.js').open_api;
var util = require('../../../../utils/util.js');
var header = getApp().header;
var time = require('../../../../utils/time.js');
const app = getApp();

小程序发起头像请求 可以选择头像上传 或者 拍照 调用摄像头接口
// 微信头像上传

    <view class="wxcode-imgbox">
        <image src="{{tempFile1?tempFile1:'../../../imgs/zhifubaocode.png'}}" bindtap="zfbTakePhoto" style="vertical-align: middle;430rpx; height: 430rpx;"></image>
        <image src="../../../imgs/del.png" bindtap="delZfbPhoto" style="vertical-align: middle;44rpx; height: 44rpx;position: absolute;right:174rpx;top:12rpx;z-index:9"></image>
    </view>
  wxTakePhoto: function () {
    // console.log("wx微信头像上传")
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        // that.setData({
        //   tempFile: tempFilePaths
        // })

        // for (let path of res.tempFilePaths) {
        wx.uploadFile({
          url: api.UpdataWxCode,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'paytype': 1,
            "type": 'cacher'

          },
          header: {
            'Content-Type': 'multipart/form-data',
            'X-Requested-With': 'XMLHttpRequest',
            'user-token': wx.getStorageSync('user_token'),
          },
          responseType: 'text',
          success: (res) => {
            // console.log(res, '服务器返回的微信图片')
            let image_data = res.data;
            let image_src = JSON.parse(image_data);
            that.setData({
              // tempFile: image_src.data.src
              tempFile: image_src.data.src
            })
          }
        })
        // }
      }
    })
  },
原文地址:https://www.cnblogs.com/xiaoxiaoxun/p/12083336.html