getGoodsImgPath() {
return new Promise((success, fail) => {
if (this.imgSrc != '') {
success(this.imgUrl);
} else {
wx.getImageInfo({
src: this.imgUrl,
success: res => {
this.imgSrc = res.path
success(res.path);
},
fail: res => {
this.imgSrc = res.path
}
})
}
});
},
获取 access_token
getAccessToken() {
this.$http({
url: this.$url.appInfo,
methods: 'post',
data: {
token: uni.getStorageSync('token')
}
}).then(res => {
uni.request({
method: "GET",
url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${res.data.appid}&secret=${res.data.secret}`,
success: (result) => {
let access_token = result.data.access_token
this.getQrCode(access_token)
}
})
})
},
getQrCode(token) {
let path = 'pagesB/register/register'
let mobile = uni.getStorageSync('mobile')
uni.request({
method: "POST",
responseType: 'arraybuffer',
url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`,
data: {
page: path,
scene: mobile
},
success: (result) => {
this.bufferImg = "data:image/png;base64," + wx.arrayBufferToBase64(result.data);
this.getGoodsImgPath().then(res => {
this.saveAlbum(res)
})
},
})
},
saveAlbum(res) {
var base64url = this.bufferImg;
console.log(base64url, '二维码')
var imgPath = wx.env.USER_DATA_PATH + '/invite' + 'dalaba' + '.png';
var imageData = base64url.replace(/^data:image\/\w+;base64,/, "");
var fs = wx.getFileSystemManager();
fs.writeFileSync(imgPath, imageData, "base64");
let backurl = '../../static/images/1.png'
uni.getSystemInfo({
success: (res) => {
console.log(backurl, imgPath, '地址')
var windowWidth = res.windowWidth;
var windowHeight = res.windowHeight;
const ctx = wx.createCanvasContext('myCanvas', this);
ctx.drawImage(backurl, 0, 0, windowWidth, windowHeight);
ctx.drawImage(imgPath, 15, 300, 65, 65);
ctx.draw(false, setTimeout(() => {
wx.canvasToTempFilePath({
width: this.windowWidth * 1.22,
height: this.windowHeight * 1.22,
destWidth: this.windowWidth * 1.22 * 3,
destHeight: this.windowHeight * 1.22 * 3,
fileType: 'png',
quality: 1,
canvasId: 'myCanvas',
success: (res) => {
this.tempFilePath = res.tempFilePath
},
fail() {
uni.showToast({
title: '保存失败,稍后再试',
duration: 2000,
icon: 'none'
})
}
})
}, 2000))
}
})
},
saveImage() {
uni.getSetting({
success: (res) => {
if (res.authSetting['scope.writePhotosAlbum']) {
this.share();
} else {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
this.share();
},
fail: () => {
uni.showToast({
title: "请打开保存相册权限,再点击保存相册分享",
icon: "none",
duration: 3000
});
setTimeout(() => {
uni.openSetting({
success: (res2) => {
}
});
}, 3000);
}
})
}
}
})
},
share() {
wx.showShareImageMenu({
path: this.tempFilePath,
success: (res) => {
uni.showToast({
title: '图片保存成功~',
duration: 2000
})
},
fail: (err) => {
uni.showToast({
title: '图片保存失败~',
icon: 'error',
duration: 2000
})
},
})
},
小程序内不能直接请求 api.weixin.qq.com ,这个得在服务端请求。
----
1、是开启了vConsole调试吧?(ios都可以)
2、api.weixin.qq.com 是无法配置服务器域名的,你是如何配置的呢?(后台域名都配置了)