微信小程序 生成小程序码
首先看下效果图:
微信小程序已经上线很久了。现在才开始把代码拷进来 因为真的很忙 …………
先看代码吧。首先是小程序端的内容:
怕麻烦 全部拷进去吧 这块的需求就是完成分享小程序码保存到本地的相册里面。再通过发朋友圈或者什么方式发展下级。小程序码里面带着邀请码。这个没法直接发朋友圈或者分享链接。微信小程序暂时没提供这样的功能。
1.js里面的内容 刚开始一进来的时候我就获取了用户的头像和昵称。所有的图片我都是在后台获取的
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = Page({
data: {
NAV_HEIGHT: wx.STATUS_BAR_HEIGHT + wx.DEFAULT_HEADER_HEIGHT + 'px',
fileContext: getApp().globalData.fileContext,
postersData: [],
swiperIndex: 0,
currentIndex: 0,
avatarUrl: '',
nickName: '',
show: false,
backgroundImage: '',
logoImage: '../../images/icon-logo.png',
qrCode: '', //小程序码的路径
path_code: '', //处理后的小程序码
path_logo: '', //左下角logo的路径
path_san: '', //三角图标的路径
path_invite: '', //海报背景
path_tx: '', //用户头像的路径,
tmpImgNum: 0,
canvasContext: null,
getImageInfoNum: 4,
localPostersPaths: [], // 海报本地缓存路径
invitecode: ''
},
onLoad: function onLoad(options) {
var that = this;
if (options) {
that.data.invitecode = options.invitecode;
}
},
navigateBack: function navigateBack() {
wx.navigateBack();
},
onReady: function onReady() {
var that = this;
that.loadPosters();
getApp().refreshUserInfo(function (userInfo) {
if (userInfo.ifauthorize != 1) {
wx.showModal({ title: '提示', showCancel: false,
content: '请先授权应用获取您的用户信息',
success: function success(res) {
if (res.confirm) {
wx.switchTab({ url: '/pages/user/user' });
}
}
});
return;
}
var tmpData = { nickName: userInfo.nickname, avatarUrl: userInfo.headimage, qrCode: userInfo.qrcodepath };
that.setData(tmpData);
// 加载要绘制的内容
if (userInfo.headimage) {
wx.getImageInfo({ //用户头像的路径处理
src: userInfo.headimage,
success: function success(res) {
that.data.path_tx = res.path;
},
fail: function fail(res) {
that.data.path_tx = '../../images/nopic.png';
}
});
}
wx.getImageInfo({ //用户小程序码处理
src: userInfo.qrcodepath,
success: function success(res) {
that.data.path_code = res.path;
},
fail: function fail(res) {
that.createQrcode();
}
});
});
},
loadPosters: function loadPosters() {
var that = this;
getApp().GET({
url: '/webapi/poster/getPage.json',
data: {},
success: function success(res) {
var data = res.data;
if (res.code == 0 && data && data.length > 0) {
that.setData({
postersData: data
});
var tmpPath = that.data.localPostersPaths = new Array(data.length);
for (var i = 0; i < data.length; i++) {
that.logoPostersImage(that.data.localPostersPaths, data[i].picAddress, i);
}
}
}
});
},
logoPostersImage: function logoPostersImage(tmpPath, url, index) {
wx.getImageInfo({ src: url,
success: function success(ires) {
tmpPath[index] = ires.path;
},
fail: function fail(res) {
console.log("logoPostersImage---fail", res);
}
});
},
handleTap: function handleTap(e) {
var that = this;
var num = e.currentTarget.dataset.num;
num = num ? parseInt(num) : 0;
var length = this.data.postersData.length;
var index = this.data.currentIndex;
if (num + index >= length) {
index = 0;
} else if (num + index < 0) {
index = length - 1;
} else {
index += num;
}
this.setData({
currentIndex: index,
swiperIndex: index
});
},
handleSwiperChange: function handleSwiperChange(e) {
var that = this;
var current = e.detail.current;
this.setData({
currentIndex: current
});
},
handleImgListTap: function handleImgListTap(e) {
var that = this;
var index = e.currentTarget.dataset.index;
this.setData({
currentIndex: index,
swiperIndex: index
});
},
//关闭邀请弹窗
closePop: function closePop() {
this.setData({
showPop: !this.data.showPop
});
},
//点击邀请好友
inviteFun: function inviteFun() {
wx.showLoading({
title: '加载中'
});
var that = this;
that.setData({
showPop: !that.data.showPop
});
that.formSubmit();
},
//生成小程序码的代码
createQrcode: function createQrcode() {
var that = this;
getApp().GET({
url: '/webapi/member/createQrCode.json',
data: {},
success: function success(res) {
that.setData({ qrCode: res.path });
that.data.tmpImgNum = 0;
wx.getImageInfo({ src: res.path,
success: function success(res) {
that.data.path_code = res.path;
}
});
}
});
},
getImageInfoCount: function getImageInfoCount(that) {
that.data.tmpImgNum++;
if (that.data.tmpImgNum >= that.data.getImageInfoNum) {
that.formSubmit();
}
},
//用canvas绘制海报
createNewImg: function createNewImg() {
this.data.tmpDrawImgNum = 0;
var that = this;
var canvasContext = wx.createCanvasContext('mycanvas'