496 lines
13 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/home/home.js
const api = require('../../lib/api')
const util = require('../../lib/util')
const app = getApp()
const g = app.globalData
Page({
/**
* 页面的初始数据
*/
data: {
CustomBar: g.CustomBar, //顶部到胶囊底部的高度
StatusBar: g.StatusBar, //安卓bar高度
src: g.bgs[g.bgIndex], //背景图片地址
yiyan: null, //一言
show: false, //控制utils页面的显示动画
display: { //显示页面状态控制
'createBox': true,
'store': false,
'safe': false,
'start': false,
'example': false,
'maked': false
},
animating: false, //动画进行时ture->禁止触发下个动画
codes: [], //已完成的用户二维码
codeSrc: null, //预览用图片地址
createCode: { //创建二维码所用数据
wxcode: '',
qqcode: '',
alcode: '',
username: '',
openId: wx.getStorageSync('openid'),
node: '',
},
showBest: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var config = wx.getStorageSync('config')
if(config.showBest == 'false') {
this.setData({
showBest: false
})
} else {
this.setData({
showBest: true
})
}
wx.hideShareMenu()
this.setData({
yiyan: wx.getStorageSync('yiyan'),
})
if (this.data.yiyan.hitokoto.length > 15) {
this.setData({
hitokotoStyle: "text-indent:2em;"
})
}
this.manageCodes()
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
// 各种界面展示的动画处理
showUtils: function (e) {
if (e) var type = e.currentTarget.dataset.type
else var is_pay_back = true
if (this.data.animating) return
if (type == 'maked' && this.data.codes.length == 0) {
wx.showToast({
title: '空空如也呢',
icon: 'none'
})
return
}
var that = this
this.data.animating = true
if (this.data.show) {
//拉起主页
// 如果是start页先弹窗问是否返回 是就清除数据
if (this.data.display['start']) {
if (is_pay_back) {
that.data.createCode = {
wxcode: '',
qqcode: '',
alcode: '',
username: '',
openId: wx.getStorageSync('openid'),
node: '',
}
that.setData({
createCode: that.data.createCode
})
that.data.codeSrc = null
let display = that.data.display
for (var i in display) {
if (i != 'createBox') display[i] = false
}
display['createBox'] = true
that.setData({
display: display,
show: false
})
setTimeout(function () {
that.data.animating = false
}, 500)
} else {
wx.showModal({
title: '警告',
content: '此时返回将清除已录入的数据',
success(res) {
if (res.confirm) {
that.data.createCode = {
wxcode: '',
qqcode: '',
alcode: '',
username: '',
openId: wx.getStorageSync('openid'),
node: '',
}
that.setData({
createCode: that.data.createCode
})
that.data.codeSrc = null
let display = that.data.display
for (var i in display) {
if (i != 'createBox') display[i] = false
}
display['createBox'] = true
that.setData({
display: display,
show: false
})
setTimeout(function () {
that.data.animating = false
}, 500)
} else if (res.cancel) {
console.log('用户点击取消')
that.data.animating = false
}
}
})
}
} else {
this.data.codeSrc = null
let display = this.data.display
for (var i in display) {
if (i != 'createBox') display[i] = false
}
display['createBox'] = true
this.setData({
display: display,
show: false
})
setTimeout(function () {
that.data.animating = false
}, 500)
}
} else {
//拉起utils页
//阻止点击返回键
if (!type) {
this.data.animating = false
return
}
this.setData({
show: true,
})
let display = this.data.display
for (var i in display) {
if (i != type) display[i] = false
}
display[type] = true
setTimeout(function () {
that.setData({
display: display
})
that.data.animating = false
if (type == 'example') that.rouseQRcode(true, null)
}, 500)
}
},
//处理数据
manageCodes: function (callBack) {
let codes = wx.getStorageSync('codes')
this.setData({
codes: codes
})
callBack && callBack()
},
//删除二维码
delCode: function (e) {
var id = e.currentTarget.dataset.id
var that = this
wx.showModal({
title: '警告',
content: '此操作不可恢复',
success(res) {
if (res.confirm) {
wx.showLoading({
title: '操作中',
mask: true
})
api.delCode(id, function () {
//刷新并处理数据
api.reflash(wx.getStorageSync('openid'), function (info) {
wx.setStorageSync('codes', info);
that.manageCodes(function () {
wx.hideLoading()
wx.showToast({
title: "删除成功"
})
})
})
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
},
//从已完成跳转二维码显示
bindToRouse: function (e) {
var data = e.currentTarget.dataset.data
this.rouseQRcode(false, data)
},
//唤起二维码(参数制作)
rouseQRcode: function (isExample, data) {
//isExample == true 是从示例进来的
//isExample == false 是从创建完或者已创建进来的
wx.showLoading({
title: '绘制中',
mask: true
})
this.data.animating = true
if (isExample) {
var status = {
wx: true,
qq: true,
al: true
}
var text = '小例子~欢迎测试~'
var id = '5de3cc411e83c77332ded8a5'
}
else {
// 在这里还要完成页面的切换操作
// 只改display
var display = this.data.display
for (var i in display) {
if (i != 'example') display[i] = false
}
display['example'] = true
this.setData({
display: display
})
// data 的内容是一条记录的row
var id = data._id['$oid']
var text = data.username
var status = {
wx: data.wxcode ? true : false,
qq: data.qqcode ? true : false,
al: data.alcode ? true : false
}
}
this.drawQRcode(status, text, id)
},
//绘制二维码
drawQRcode: function (status, text, id) {
//传入的信息
// var status = {
// wx: true,
// qq: true,
// al: true
// }
// var text = '英博'
// var id = '5dc6d61074d52b3774ab3e25'
//基本的信息
var canvasimgbg = null
var rpx = g.rpx
var fontSet1 = 'bold ' + parseInt(50 * rpx) + 'px sans-serif'
var fontSet2 = parseInt(35 * rpx) + 'px sans-serif'
var that = this
var app = []
if (status.wx) app.push(25)
if (status.qq) app.push(279)
if (status.al) app.push(153)
wx.downloadFile({
url: that.data.src,
success: function (res) {
canvasimgbg = res.tempFilePath
var canvas = wx.createCanvasContext('qrcode');
canvas.drawImage(canvasimgbg, 0, 0, 632 * rpx, 1054 * rpx);
wx.downloadFile({
url: 'https://qr.lacus.site/generate?text=https%3A%2F%2Fqr.lacus.site%2Fqr%2Fqrcode.html%3Fid%3D' + id,
success: function (res) {
canvasimgbg = res.tempFilePath
canvas.drawImage(canvasimgbg, 66 * rpx, 66 * rpx, 500 * rpx, 500 * rpx);
canvas.font = fontSet1;
canvas.textAlign = 'center';
canvas.setFillStyle('white')
canvas.fillText(text, 316 * rpx, 660 * rpx)
canvas.font = fontSet2;
canvas.fillText('本二维码支持以下APP向我付款', 316 * rpx, 760 * rpx)
wx.downloadFile({
url: 'https://qr.lacus.site/qr/img/sprite1.png',
success: function (res) {
canvasimgbg = res.tempFilePath
//先画左右,再画中间
canvas.drawImage(canvasimgbg, app[0], 0, 103, 100, 66 * rpx, 830 * rpx, 100 * rpx, 100 * rpx);
canvas.drawImage(canvasimgbg, app[1], 0, 103, 100, 463 * rpx, 830 * rpx, 100 * rpx, 100 * rpx);
if (app[2])
canvas.drawImage(canvasimgbg, app[2], 0, 103, 100, 264 * rpx, 830 * rpx, 100 * rpx, 100 * rpx);
canvas.draw(true, () => { });
// 导出为临时图片
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: 632 * rpx,
height: 1054 * rpx,
destWidth: 1264,
destHeight: 2108,
canvasId: 'qrcode',
success: function (res) {
that.data.codeSrc = res.tempFilePath
wx.hideLoading()
that.data.animating = false
wx.showModal({
title: '提示',
content: '单击图片即可预览,预览界面长按即可保存',
showCancel: false
})
}
})
}
})
}
})
}
})
},
preview: function () {
wx.previewImage({
urls: [this.data.codeSrc],
})
},
//上传支付宝二维码
scanCode: function (e) {
var type = e.currentTarget.dataset.type
var that = this
wx.scanCode({
success(res) {
//utils里边写个校验返回true false
//写入
var check = util.checkInput(type, res.result)
if (check) {
that.data.createCode[type] = check
that.setData({
createCode: that.data.createCode
})
wx.showToast({
title: '录入成功',
icon: 'success',
})
} else {
return
}
},
fail(err) {
wx.showToast({
title: '扫码失败',
icon: 'none',
})
}
})
},
//输入用户名,备注
inputChange: function (e) {
var type = e.currentTarget.dataset.type
var value = e.detail.value
var check = util.checkInput(type, value)
if (check) {
this.data.createCode[type] = check
this.setData({
createCode: this.data.createCode
})
}
return check
},
//提交
submit: function () {
var that = this;
var res = util.checkSubmit(this.data.createCode)
if (!res) return
console.log(res)
wx.showLoading({
title: '疯狂上传中',
mask: true
})
api.createCode(res,function (oid) {
//重置createCode
var data = that.data.createCode
data._id = {}
data._id['$oid'] = oid
that.data.createCode = {
wxcode: '',
qqcode: '',
alcode: '',
username: '',
openId: wx.getStorageSync('openid'),
node: '',
}
that.setData({
createCode: that.data.createCode,
})
//调起绘制二维码的界面
//刷新并处理数据
api.reflash(wx.getStorageSync('openid'), function (info) {
wx.setStorageSync('codes', info);
that.manageCodes()
})
wx.hideLoading()
that.rouseQRcode(false,data)
})
},
showQrcode() {
wx.previewImage({
urls: ['cloud://cloud-83621f.636c-cloud-83621f/cherryStore/money.png'],
current: 'cloud://cloud-83621f.636c-cloud-83621f/cherryStore/money.png' // 当前显示图片的http链接
})
},
})