2018-10-03 13:05:05 +08:00

123 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/main/index.js
import {
DBPost
} from '../../../db/DBpost.js';
var app = getApp();
var QR = require("../../../data/QRcode.js");
Page({
data: {
canvasHidden: false,
maskHidden: true,
imagePath: '',
placeholder: '1525873266' //默认二维码生成文本
},
onLoad: function(options) {
// 页面初始化 options为页面跳转所带来的参数
var dbPost = new DBPost();
var xuehao1 = dbPost.getxuehaoData();
var shijianchuo1 = xuehao1 + dbPost.getshijianchuoData();
this.setData({
placeholder: shijianchuo1.toString() //数字转字符串
});
console.log(this.data.placeholder)
var size = this.setCanvasSize(); //动态设置画布大小
var initUrl = this.data.placeholder;
this.createQrCode(initUrl, "mycanvas", size.w, size.h);
wx.setScreenBrightness({
value: 1
})
},
onReady: function() {},
onShow: function() {
// 页面显示
},
onHide: function() {
// 页面隐藏
},
onUnload: function() {
// 页面关闭
},
//适配不同屏幕大小的canvas
setCanvasSize: function() {
var size = {};
try {
var res = wx.getSystemInfoSync();
var scale = 750 / 686; //不同屏幕下canvas的适配比例设计稿是750宽
var width = res.windowWidth / scale;
var height = width; //canvas画布为正方形
size.w = width;
size.h = height;
} catch (e) {
// Do something when catch error
console.log("获取设备信息失败" + e);
}
return size;
},
createQrCode: function(url, canvasId, cavW, cavH) {
//调用插件中的draw方法绘制二维码图片
QR.api.draw(url, canvasId, cavW, cavH);
setTimeout(() => {
this.canvasToTempImage();
}, 1000);
},
//获取临时缓存照片路径存入data中
canvasToTempImage: function() {
var that = this;
wx.canvasToTempFilePath({
canvasId: 'mycanvas',
success: function(res) {
var tempFilePath = res.tempFilePath;
console.log(tempFilePath);
that.setData({
imagePath: tempFilePath,
// canvasHidden:true
});
},
fail: function(res) {
console.log(res);
}
});
},
//点击图片进行预览,长按保存分享图片
previewImg: function(e) {
var img = this.data.imagePath;
console.log(img);
wx.previewImage({
current: img, // 当前显示图片的http链接
urls: [img] // 需要预览的图片http链接列表
})
},
formSubmit: function(e) {
var that = this;
var url = e.detail.value.url;
that.setData({
maskHidden: false,
});
wx.showToast({
title: '生成中...',
icon: 'loading',
duration: 2000
});
var st = setTimeout(function() {
wx.hideToast()
var size = that.setCanvasSize();
//绘制二维码
that.createQrCode(url, "mycanvas", size.w, size.h);
that.setData({
maskHidden: true
});
clearTimeout(st);
}, 2000)
},
sumit: function() {
wx.switchTab({
url: '../post'
})
}
})