小程序|基于 Serverless 架构的头像漫画风处理小程序( 三 )


.detailList)[0
)chosePhoto() { const that = this wx.chooseImage({ count: 1 sizeType: ['compressed'
sourceType: ['album' 'camera'
complete(res) { that.setData({ userChosePhoho: res.tempFilePaths[0
resultPhoto: undefined ))headimgHD(imageUrl) { imageUrl = imageUrl.split('/'); //把头像的路径切成数组 //把大小数值为 46 || 64 || 96 || 132 的转换为0 if (imageUrl[imageUrl.length - 1
(imageUrl[imageUrl.length - 1
== 46 || imageUrl[imageUrl.length - 1
== 64 || imageUrl[imageUrl.length - 1
== 96 || imageUrl[imageUrl.length - 1
== 132)) { imageUrl[imageUrl.length - 1
= 0;imageUrl = imageUrl.join('/'); //重新拼接为字符串 return imageUrl;getUserAvatar() { const that = this wx.getUserProfile({ desc: \"获取您的头像\" success(res) { const newAvatar = that.headimgHD(res.userInfo.avatarUrl) wx.getImageInfo({ src: newAvatar success(res) { that.setData({ userChosePhoho: res.path resultPhoto: undefined )))previewImage(e) { wx.previewImage({ urls: [e.currentTarget.dataset.image
)editImage() { const that = this wx.editImage({ src: this.data.userChosePhoho success(res) { that.setData({ userChosePhoho: res.tempFilePath ))getNewPhoto() { const that = this wx.showLoading({ title: '图片生成中' ) this.setData({ getPhotoStatus: true ) app.doRequest(this.data.styleList[this.data.currentStyle
.detailList[this.data.currentSubStyle
.uri { style: this.data.styleList[this.data.currentStyle
.detailList[this.data.currentSubStyle
.name image: wx.getFileSystemManager().readFileSync(this.data.userChosePhoho \"base64\")option = { method: \"POST\" ).then(function (result) { wx.hideLoading() that.setData({ resultPhoto: result.error ? \"error\" : result.photo getPhotoStatus: false ) )saveImage() { wx.saveImageToPhotosAlbum({ filePath: this.data.resultPhoto success(res) { wx.showToast({ title: \"保存成功\" )fail(res) { wx.showToast({ title: \"异常 , 稍后重试\" ))onShareAppMessage: function () { return { title: \"头头是道个性头像\"onShareTimeline() { return { title: \"头头是道个性头像\"showModal(e) { if(e.currentTarget.dataset.target==\"Image\"){ const previewSubStyle = e.currentTarget.dataset.substyle const previewSubStyleUrl = this.data.styleList[this.data.currentStyle
.detailList[previewSubStyle
.preview if(previewSubStyleUrl){ this.setData({ previewStyle: previewSubStyleUrl ) else{ wx.showToast({ title: \"暂无模板预览\" icon: \"error\" ) returnthis.setData({ modalName: e.currentTarget.dataset.target )hideModal(e) { this.setData({ modalName: null )copyData(e) { wx.setClipboardData({ data: e.currentTarget.dataset.data success(res) { wx.showModal({ title: '复制完成' content: `已将${e.currentTarget.dataset.data复制到了剪切板` )) ) 因为项目会请求比较多次的后台接口 , 所以 , 我将请求方法进行额外的抽象: // 统一请求接口 doRequest: async function (uri data option) { const that = this return new Promise((resolve reject) ={ wx.request({ url: that.url + uri data: data header: { \"Content-Type\": 'application/json'method: optionoption.method ? option.method : \"POST\" success: function (res) { resolve(res.data)fail: function (res) { reject(null)) )完成之后配置一下后台接口 , 发布审核即可 。本文作者刘宇(花名:江昱) 原文链接:https://developer.aliyun.com/article/882384?utm_content=g_1000334557 本文为阿里云原创内容 , 未经允许不得转载 。

相关经验推荐