You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.1 KiB
141 lines
4.1 KiB
// pages/qrcode/index.js
|
|
const API = require('../../utils/api.js');
|
|
|
|
Page({
|
|
data: {
|
|
qrCodeUrl: '', // 二维码图片URL
|
|
userInfo: {
|
|
projectName: '未登录',
|
|
name: '未登录',
|
|
phoneNumber: '未登录'
|
|
},
|
|
imageLoading: false,
|
|
imageError: false
|
|
},
|
|
|
|
onLoad: function (options) {
|
|
// 页面加载时的初始化逻辑
|
|
console.log('二维码页面加载');
|
|
// 加载用户信息
|
|
this.loadUserInfo();
|
|
},
|
|
|
|
onShow: function () {
|
|
// 页面显示时的逻辑
|
|
// 重新加载用户信息,确保信息是最新的
|
|
this.loadUserInfo();
|
|
},
|
|
|
|
// 加载用户信息
|
|
loadUserInfo: function () {
|
|
// 从本地存储获取用户信息
|
|
const userInfo = wx.getStorageSync('userInfo') || {};
|
|
const phoneNumber = userInfo.phoneNumber || wx.getStorageSync('phoneNumber') || '';
|
|
|
|
if (phoneNumber) {
|
|
// 根据电话号码查询personnel表
|
|
API.request('/api/personnel/get', 'POST', { phone: phoneNumber }).then(res => {
|
|
console.log('查询personnel表结果:', res);
|
|
if (res && res.success && res.data && res.data.length > 0) {
|
|
const personnelInfo = res.data[0];
|
|
this.setData({
|
|
userInfo: {
|
|
projectName: personnelInfo.projectName || '未提供',
|
|
name: personnelInfo.name || '未提供',
|
|
phoneNumber: phoneNumber
|
|
}
|
|
});
|
|
} else {
|
|
// 如果查询失败,使用本地存储的用户信息
|
|
this.setData({
|
|
userInfo: {
|
|
projectName: '未提供',
|
|
name: userInfo.nickName || userInfo.name || '未提供',
|
|
phoneNumber: phoneNumber
|
|
}
|
|
});
|
|
}
|
|
}).catch(err => {
|
|
console.error('查询personnel表失败:', err);
|
|
// 如果查询失败,使用本地存储的用户信息
|
|
this.setData({
|
|
userInfo: {
|
|
projectName: '未提供',
|
|
name: userInfo.nickName || userInfo.name || '未提供',
|
|
phoneNumber: phoneNumber
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
// 如果没有电话号码,提示用户登录
|
|
wx.showToast({
|
|
title: '请先登录',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
},
|
|
|
|
// 生成二维码的函数
|
|
generateQRCode: function () {
|
|
// 设置图片加载状态
|
|
this.setData({
|
|
imageLoading: true,
|
|
imageError: false
|
|
});
|
|
|
|
// 生成包含会话ID的URL
|
|
const sessionId = 'session_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
|
|
|
|
// 获取用户信息
|
|
const { userInfo } = this.data;
|
|
let inviterName = userInfo.name || '';
|
|
let inviterPhone = userInfo.phoneNumber || '';
|
|
let inviterProjectName = userInfo.projectName || '';
|
|
|
|
// 构建邀请URL,包含会话ID和邀请者信息
|
|
const inviteUrl = `http://8.137.125.67:3008/certificate.html?sessionId=${encodeURIComponent(sessionId)}&inviter=${encodeURIComponent(inviterName)}&inviterPhone=${encodeURIComponent(inviterPhone)}&inviterProjectName=${encodeURIComponent(inviterProjectName)}`;
|
|
|
|
// 使用api.qrserver.com生成二维码
|
|
const qrCodeUrl = `https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=${encodeURIComponent(inviteUrl)}`;
|
|
|
|
console.log('生成的二维码URL:', qrCodeUrl);
|
|
|
|
// 更新二维码图片
|
|
this.setData({
|
|
qrCodeUrl: qrCodeUrl
|
|
}, () => {
|
|
console.log('二维码URL已更新:', this.data.qrCodeUrl);
|
|
// 显示成功提示
|
|
wx.showToast({
|
|
title: '邀请二维码生成成功!',
|
|
icon: 'success',
|
|
duration: 2000
|
|
});
|
|
});
|
|
},
|
|
|
|
// 图片加载成功事件
|
|
onImageLoad: function () {
|
|
console.log('二维码图片加载成功');
|
|
this.setData({
|
|
imageLoading: false,
|
|
imageError: false
|
|
});
|
|
},
|
|
|
|
// 图片加载失败事件
|
|
onImageError: function (e) {
|
|
console.error('二维码图片加载失败:', e);
|
|
this.setData({
|
|
imageLoading: false,
|
|
imageError: true
|
|
});
|
|
wx.showToast({
|
|
title: '二维码图片加载失败,请重试',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
|
|
});
|