1、公众号开发集成微信JSSDK
- 登录公众平台
- 下载TXT文件放到域名根目录下
- 加入js安全域名
2、配置wxsdk集成到项目中
import wx from 'weixin-js-sdk';
import { getWxSignature } from './../../api/member/commonApi';
const wxsdk = {
otherInit(type, callback, value) {
let url = window.location.href;
let appid = 'appid';
let link = window.location.href;
link = window.location.href.split('#')[0];
let params = {
url: link,
};
getWxSignature(params).then(res => {
if (res && res.data && !res.data.error) {
wx.config({
debug: false,
appId: appid,
timestamp: res.data.timestamp,
nonceStr: res.data.nonceStr,
signature: res.data.signature,
jsApiList: [
'chooseImage',
'uploadImage',
'downloadImage',
'getLocalImgData',
],
});
wx.ready(function() {
if (type == 'chooseImage') {
wxsdk.chooseImage(callback, value);
}
});
wx.error(function() {});
}
});
},
chooseImage(callback, type) {
wx.checkJsApi({
jsApiList: ['chooseImage'],
success: function(res) {
if (res.checkResult.chooseImage) {
if (type == '1') {
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album'],
success: function(res) {
var localIds = res.localIds[0];
wxsdk.wxBaseImage(localIds, callback);
},
cancel: function(res) {
if (callback) {
callback('');
}
},
});
} else if (type == '2') {
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['camera'],
success: function(res) {
var localIds = res.localIds[0];
wxsdk.wxBaseImage(localIds, callback);
},
cancel: function(res) {
if (callback) {
callback('');
}
},
});
} else if (type == '3') {
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function(res) {
var localIds = res.localIds[0];
wxsdk.wxBaseImage(localIds, callback);
},
cancel: function(res) {
if (callback) {
callback('');
}
},
});
}
}
},
});
},
wxBaseImage(localIds, callback) {
wx.uploadImage({
localId: localIds,
isShowProgressTips: 1,
success: function(res) {
var serverId = res.serverId;
wx.downloadImage({
serverId: serverId,
isShowProgressTips: 1,
success: function(res) {
var localId = res.localId;
wx.getLocalImgData({
localId: localId,
success: function(res) {
var localData = res.localData;
if (callback) {
callback(res);
}
},
});
},
});
},
});
},
}
export wxsdk
3、项目中使用拍照上传功能
import wxsdk from './../../../../common/js/wxsdk';
chooseImage = value => {
wxsdk.otherInit('chooseImage', this.triggerChooseImage, value);
};
triggerChooseImage = res => {
let baseImage = res.localData;
if (baseImage.indexOf('data:image') != 0) {
baseImage = 'data:image/jpeg;base64,' + baseImage;
}
baseImage = baseImage
.replace(/\r|\n/g, '')
.replace('data:image/jgp', 'data:image/jpeg');
var blob = this.dataURLtoBlob(baseImage);
var file = this.blobToFile(blob, 'shangchauan.jpg');
console.log(file);
const params = new FormData();
params.append('file', file);
uploadimg(params).then(res => {
if (res.status == 200) {
console.log(res.data.url);
this.setState({
wxImage: res.data.url,
});
}
});
};
dataURLtoBlob = dataurl => {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
};
blobToFile = (theBlob, fileName) => {
let options = {
lastModifiedDate: new Date(),
lastModified: new Date().getTime(),
type: 'image/png',
uid: this.getUuid(),
webkitRelativePath: '',
};
let file = new window.File([theBlob], fileName, options);
return file;
};
getUuid = () => {
var s = [];
var hexDigits = '0123456789abcdef';
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = '4';
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
s[8] = s[13] = s[18] = s[23] = '-';
var uuid = s.join('');
return uuid;
};
坑点总结
- 1、调用wx.getLocalImgData()失败
原因:上传的是单张图片在wx.chooseImage()取得var localIds = res.localIds------>需要替换成var localIds = res.localIds[0];
- 2、不清楚ios与安卓的兼容性问题
- 3、base64转换为Blob对象
- 4、调用后台接口需要上传File类型,需要将Blob转换为File对象(ps:网上有很多不正确的方法)
建议用这个:
blobToFile = (theBlob, fileName) => {
let options = {
lastModifiedDate: new Date(),
lastModified: new Date().getTime(),
type: 'image/png',
uid: this.getUuid(),
webkitRelativePath: '',
};
let file = new window.File([theBlob], fileName, options);
return file;
};