优化代码

This commit is contained in:
2026-01-08 17:41:46 +08:00
parent c994fcca33
commit 94bf924525
7 changed files with 954 additions and 446 deletions
+341 -135
View File
@@ -1,101 +1,352 @@
<script> <script>
import authUtil from '@/utils/auth.js'; // 引入工具类
import userUtil from '@/utils/user.js'; // 引入工具类
export default { export default {
globalData: { globalData: {
website: "https://www.lailab.cn/miniprogram", website: "https://www.lailab.cc:8080",
hasUserInfo: false, hasUserInfo: false,
userInfo: {}, userInfo: {},
openId: null openId: null,
defaultAuthCode: "D768ECAAB35C55ABBA071C5415D6520E",
method: {
getOpenId: "/api/wx/getOpenId",
getUserByOpenId: "/api/user/getUserByOpenId",
addUser: "/api/user/addUser",
getConfigAll: "/api/pic/getPicAll",
getAuthMd5Code: "/api/auth/getAuthMd5Code"
},
homePageData:{
tip: {
msg:"世上无难事,只要肯攀登。",
isShow:true,
level:2
},
swiper: {
background: [
{
type:"color",
background: "#0226",
text:"广告位1",
clickable:true,
action:"dialog",
content:"你点击了广告位1",
},
{
type:"image",
background: "",
mode:"scale",
text:"广告位2",
clickable:true,
action:"goto",
content:"/pages/utils/showImg/showImg"
},
{
type:"color",
background: "#A70",
text:"广告位3",
clickable:true,
action:"dialog",
content:"你点击了广告位3"
}
],
indicatorDots: true,
autoplay: true,
interval: 2000,
duration: 500,
vertical: false,
circular: true
},
pics: [
{
url:"https://img1.baidu.com/it/u=2172818577,3783888802&fm=253&app=138&f=JPEG?w=800&h=1422",
source:"LaiLab",
id:"1"
},
{
url:"https://hellorfimg.zcool.cn/provider_image/large/hi2241910533.jpg?x-image-process=image/format,webp",
source:"百度",
id:"2"
},
{
url:"https://c-ssl.dtstatic.com/uploads/blog/202403/13/0GSvde87c0G05zE.thumb.1000_0.jpg",
source:"百度",
id:"3"
},
{
url:"https://img0.baidu.com/it/u=3815152378,1770089461&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
source:"百度",
id:"4"
},
{
url:"https://img1.baidu.com/it/u=1046630760,1711311937&fm=253&app=138&f=JPEG?w=800&h=1428",
source:"百度",
id:"5"
},
{
url:"https://c-ssl.dtstatic.com/uploads/blog/202312/04/0GSvVw6Xt0exN0g.thumb.1000_0.jpg",
source:"百度",
id:"6"
},
{
url:"https://img0.baidu.com/it/u=41840629,336950107&fm=253&app=138&f=JPEG?w=500&h=1109",
source:"百度",
id:"7"
},
{
url:"https://hellorfimg.zcool.cn/provider_image/large/hi2241910533.jpg?x-image-process=image/format,webp",
source:"百度",
id:"8"
}
]
},
morePageData:{
tools: [
{
name:"Hello",
cards:[
{
title:"测试",
detail:"村上春树",
color:"#aaa",
icon:"../../static/icons/icon_qrcode.png",
url:""
}
]
},
],
}
}, },
onLaunch: function() { onLaunch: function() {
console.log("=== LaiLab小程序主页 ===");
this.login(); this.login();
}, },
methods: { methods: {
login() { async login() {
const that = this const that = this;
if (this.globalData.hasUserInfo) { const gData = this.globalData;
console.log("已获取用户信息");
} else { if (gData.hasUserInfo) {
//调用登录接口 console.log("已获取用户信息,跳过登录流程");
uni.login({ return;
success: function(res1) { }
// 获取个人 code
uni.getUserInfo({ console.log("未获取用户信息,开始获取");
success: res2 => {
// 获取 OpenId try {
wx.request({ // 步骤1: 获取微信code
url: that.globalData.website + const step1Result = await this.getWechatCode();
"/api/getOpenId.php",
method: "POST", // 步骤2: 获取用户信息
data: { const step2Result = await this.getUserInfo();
code: res1.code,
}, // 步骤3: 获取OpenId
header: { console.log(`3.调用方法 ${gData.method.getOpenId} 开始 入参: ${step1Result.code}`);
"Content-Type": "application/x-www-form-urlencoded" const openId = await userUtil.getOpenId(step1Result.code);
}, console.log(`3.调用方法 ${gData.method.getOpenId} 结束 返回: ${openId}`);
success: function(res3) { gData.openId = openId;
if (res3.data.result.openid) {
that.globalData.openId = res3.data // 获取授权码
.result.openid; var requestData_getAuth = {
console.log("获取OpenId成功"); openId: gData.openId
uni.request({ };
url: that.globalData console.log(`4.1 调用方法 ${gData.method.getAuthMd5Code} 开始 入参: ${JSON.stringify(requestData_getAuth)}`);
.website + var authCode = await authUtil.getAuthMd5Code(requestData_getAuth)
"/api/getUserInfo.php", console.log(`4.1 调用方法 ${gData.method.getAuthMd5Code} 结束 返回: ${authCode}`);
method: 'POST', // 若用户未注册,则以下步骤将都无法进行,所以填充默认授权码
data: { if(authCode===''||authCode===null){
openId: res3.data authCode = gData.defaultAuthCode;
.result.openid }
},
success(res4) { // 步骤4: 检查用户注册状态
if (res4.data.err == var requestData_checkUser = {
0) { openId: gData.openId,
console.log( authCode: authCode
"用户未注册"); };
uni.showModal({ console.log(`4.2 调用方法 ${gData.method.getUserByOpenId} 开始 入参: ${JSON.stringify(requestData_checkUser)}`);
title: '提示', const userData = await userUtil.checkUserStatus(requestData_checkUser);
content: '检测到您尚未登录,某些工具可能无权使用,请先去登录。', console.log(`4.2 调用根据 ${gData.method.getUserByOpenId} 结束 返回: ${isexist}`);
confirmText: '去登陆', const isexist = userData.data.code;
cancelText: '取消', switch (isexist) {
success(res) { case -1:
if (res.confirm) { console.log("用户未注册");
that.userLogin(); this.handleUserNotRegistered();
} break;
}
}) case -2:
} else if (res4.data console.error(`报错: ${userData.data.message}`);
.err == 1) { this.toast(userData.data.message || "查询用户信息失败", 'error');
console.log( break;
"用户已注册");
that.globalData case 0:
.userInfo = console.log(`用户已注册,自动登录!用户信息: ${JSON.stringify(userData.data.data)}`);
res4.data
.result[0]; gData.userInfo = userData.data.data;
that.globalData gData.hasUserInfo = true;
.hasUserInfo = this.toast("自动登录成功", 'success');
true; break;
}
} default:
}); console.warn(`4.未知状态码: ${isexist}`);
} else { break;
console.log("获取OpenId失败", res3); }
}
}, } catch (error) {
fail: function(res3) { console.error("登录流程失败:", error);
console.log("访问OpenId服务器失败", res); this.toast("登录失败,请重试", 'error');
}
})
},
fail: function() {
that.authorizeCheck("scope.userInfo");
}
})
},
fail(res) {
console.log("调用登录服务失败", res);
}
})
} }
}, },
// 步骤1: 获取微信code
getWechatCode() {
return new Promise((resolve, reject) => {
console.log("1.调用登录接口wx.login()开始");
uni.login({
success: function(res) {
console.log(`1.调用登录接口wx.login()结束 返回: ${JSON.stringify(res)}`);
console.log(`1.返回参数: code=${res.code}`);
resolve(res);
},
fail: function(res) {
console.error(`1.调用微信接口wx.login()失败 返回: ${JSON.stringify(res)}`);
reject(new Error("微信登录失败"));
}
});
});
},
// 步骤2: 获取用户信息
getUserInfo() {
return new Promise((resolve, reject) => {
console.log("2.调用用户信息接口wx.getUserInfo()开始");
uni.getUserInfo({
success: (res) => {
console.log("2.调用用户信息接口wx.getUserInfo()结束 返回:", res);
console.log("2.返回参数: userInfo=", res.userInfo);
resolve(res);
},
fail: (res) => {
console.error(`2.调用微信接口wx.getUserInfo()失败 返回: ${JSON.stringify(res)}`);
this.authorizeCheck("scope.userInfo");
reject(new Error("获取用户信息失败"));
}
});
});
},
// 处理用户未注册情况
handleUserNotRegistered() {
const that = this;
uni.showModal({
title: '提示',
content: '检测到您尚未登录,某些工具可能无权使用,请先去登录。',
confirmText: '去登陆',
cancelText: '取消',
success(res) {
if (res.confirm) {
console.log("用户选择去登录");
that.userLogin();
} else {
console.log("用户点击取消");
}
}
});
},
// 用户登录(手动)
async userLogin() {
const that = this;
const gData = this.globalData;
// 若用户未登录
if (!gData.hasUserInfo) {
this.toast("正在登录", 'loading');
try {
// 获取详细用户信息
const profile = await this.getUserProfileWithDesc();
gData.hasUserInfo = true;
gData.userInfo = profile.userInfo;
this.toast("登录成功", 'success');
// 注册用户
if (gData.openId) {
console.log(`4.3 调用 ${gData.method.addUser} 开始 入参: ${profile.userInfo}`);
const result = await userUtil.registerUser(profile.userInfo);
console.log(`4.3 调用 ${gData.method.addUser} 结束 返回: ${result}`);
} else {
console.warn("没有OpenId,无法注册用户");
this.toast("登录异常,请重新进入小程序", 'error');
return;
}
// 跳转到个人中心
uni.switchTab({
url: '/pages/mine/mine'
});
} catch (error) {
console.error("用户登录失败:", error);
this.toast(error.message || "登录失败", 'error');
}
} else {
this.toast("您已经登录", 'none');
}
},
// 获取用户详细信息(带描述)
getUserProfileWithDesc() {
return new Promise((resolve, reject) => {
uni.getUserProfile({
desc: "获取你的昵称、头像、地区及性别",
success: resolve,
fail: (res) => {
reject(new Error("您拒绝了授权请求"));
}
});
});
},
// 授权检查
authorizeCheck(scope) {
console.log(`检查授权: ${scope}`);
uni.authorize({
scope: scope,
success: () => {
console.log(`${scope} 授权成功`);
},
fail: () => {
console.log(`${scope} 授权失败`);
this.toast("需要授权才能使用完整功能", 'none');
}
});
},
// Toast提示
toast(title, icon = 'none', duration = 2000) {
uni.showToast({
title: title,
icon: icon,
duration: duration
});
},
// 分享相关方法(保留原有逻辑)
getRandomShareTitle: function() { getRandomShareTitle: function() {
var arr = []; var arr = [];
@@ -105,70 +356,25 @@
return arr[Math.floor(Math.random() * arr.length)]; return arr[Math.floor(Math.random() * arr.length)];
}, },
getMainAppShare: function() { getMainAppShare: function() {
return { return {
title: this.getRandomShareTitle(), title: this.getRandomShareTitle(),
path: '/pages/index/index', path: '/pages/index/index',
success: function(res) { success: function(res) {
// 转发成功 console.log("分享成功");
}, },
fail: function(res) { fail: function(res) {
// 转发失败 console.log("分享失败");
} }
} }
},
userLogin() {
const that = this;
// 若用户未登录
if (!that.globalData.hasUserInfo) {
that.toast("正在登录", 'loading');
uni.getUserProfile({
desc: "获取你的昵称、头像、地区及性别",
success: res => {
that.globalData.hasUserInfo = true;
that.globalData.userInfo = res.userInfo;
that.toast("登录成功", 'success');
uni.switchTab({
url: '/pages/mine/mine'
})
const openId = that.globalData.openId;
if (openId) {
res.userInfo.openId = openId;
uni.request({
url: that.globalData.website + "/api/login.php",
method: 'POST',
data: res.userInfo,
success(res) {
uni.showModal({
title: "提示",
content: res.msg
})
}
})
} else {
console.log("没有OpenId,请重新进入小程序");
}
},
fail: res => {
that.toast("您拒绝了请求", 'error');
}
})
} else {
that.toast("您已经登录",'none');
}
},
toast(title, icon) {
uni.showToast({
title: title,
icon: icon
})
} }
} }
} }
</script> </script>
<style> <style>
/*每个页面公共css */ /* 每个页面公共css */
uni-page-body, uni-page-body,
html, html,
body { body {
+1
View File
@@ -2,6 +2,7 @@ import App from './App'
// #ifndef VUE3 // #ifndef VUE3
import Vue from 'vue' import Vue from 'vue'
Vue.config.productionTip = false Vue.config.productionTip = false
App.mpType = 'app' App.mpType = 'app'
const app = new Vue({ const app = new Vue({
+2 -1
View File
@@ -108,7 +108,8 @@
"urlCheck" : false, "urlCheck" : false,
"minified" : true "minified" : true
}, },
"usingComponents" : true "usingComponents" : true,
"permission" : {}
}, },
"mp-alipay" : { "mp-alipay" : {
"usingComponents" : true "usingComponents" : true
+12
View File
@@ -96,6 +96,18 @@
"enablePullDownRefresh": true "enablePullDownRefresh": true
} }
},
{
"path": "pages/mine/mine/mine",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/test/test",
"style": {
"navigationBarTitleText": ""
}
} }
], ],
"globalStyle": { "globalStyle": {
+24 -8
View File
@@ -94,20 +94,34 @@
// 初始化左右盒子高度 // 初始化左右盒子高度
"leftH": 0, "leftH": 0,
"rightH": 0, "rightH": 0,
'tip': '', tip: {},
'swiper': '', swiper: {
'pics': '' background: [],
indicatorDots: true,
autoplay: true,
interval: 2000,
duration: 500,
vertical: false,
circular: true
},
pics:[]
} }
}, },
onLoad() { onLoad() {
const app = getApp();
const gData = app.globalData;
this.getDataByOnline(); this.getDataByOnline();
this.tip = gData.homePageData.tip;
this.swiper = gData.homePageData.swiper;
this.pics = gData.homePageData.pics;
this.showPics();
}, },
onPullDownRefresh() { onPullDownRefresh() {
const that = this; const that = this;
this.leftList = []; this.leftList = [];
this.rightList = []; this.rightList = [];
this.tip = {}; this.tip = {};
this.swiper = {}; //this.swiper = {};
this.pics = ''; this.pics = '';
setTimeout(function() { setTimeout(function() {
that.getDataByOnline(); that.getDataByOnline();
@@ -128,14 +142,15 @@
methods: { methods: {
getDataByOnline() { getDataByOnline() {
const that = this; const that = this;
uni.request({ //console.log("tip:",this.tip);
/*uni.request({
url: 'https://www.lailab.cn/miniprogram/data.php?type=all', url: 'https://www.lailab.cn/miniprogram/data.php?type=all',
success(rst) { success(rst) {
that.tip = rst.data.tip; that.tip = rst.data.tip;
that.swiper = rst.data.swiper; that.swiper = rst.data.swiper;
that.showPics(); that.showPics();
}, },
}); });*/
}, },
doList() { doList() {
const that = this; const that = this;
@@ -179,7 +194,7 @@
}, },
showPics() { showPics() {
const that = this; const that = this;
uni.request({ /*uni.request({
url: "https://www.lailab.cn/miniprogram/pics.php?page=" + that.picPage + "&size=" + that url: "https://www.lailab.cn/miniprogram/pics.php?page=" + that.picPage + "&size=" + that
.picSize, .picSize,
success(rst) { success(rst) {
@@ -187,7 +202,8 @@
that.pics = rst.data; that.pics = rst.data;
that.doList(); that.doList();
} }
}) })*/
that.doList()
}, },
showImg(pic) { showImg(pic) {
uni.navigateTo({ uni.navigateTo({
+558 -289
View File
@@ -1,169 +1,412 @@
<template> <template>
<view class="container"> <view class="mine-container">
<view class="user-info" open-type="getUserInfo" @tap="login()"> <!-- 用户信息卡片 -->
<view class="info-img"> <view class="user-card" @tap="handleUserCardClick">
<view class="img-bg"></view> <view class="user-avatar-container">
<image class="img-icon" :src="hasUserInfo?userInfo.avatarUrl:''" mode="cover"></image> <view class="avatar-background"></view>
<image
class="user-avatar"
:src="hasUserInfo ? userInfo.avatarUrl : '/static/icons/icon_default_avatar.png'"
mode="aspectFill"
></image>
</view> </view>
<view class="info-text"> <view class="user-info-container">
<text class="text-name">{{hasUserInfo?userInfo.nickName:"点击登录"}}</text> <text class="user-name">
{{ hasUserInfo ? userInfo.nickName : "点击登录" }}
</text>
<text class="user-status" :class="{ 'status-logged': hasUserInfo }">
{{ hasUserInfo ? "已登录" : "未登录" }}
</text>
</view> </view>
</view> </view>
<view class="items">
<button class="item" open-type="contact"> <!-- 功能列表 -->
<image class="i-icon" src="/static/icons/icon_author_message.png" mode="widthFix"></image> <view class="function-list">
<text class="i-name">联系作者</text> <!-- 联系作者 -->
<text class="i-more iconfont icon-arrow-right"></text> <button class="function-item" open-type="contact" hover-class="item-hover">
<view class="item-left">
<image class="item-icon" src="/static/icons/icon_author_message.png" mode="widthFix"></image>
<text class="item-text">联系作者</text>
</view>
<text class="iconfont icon-arrow-right item-arrow"></text>
</button> </button>
<button class="item feedback" @tap="feedback()">
<image class="i-icon" src="/static/icons/icon_feedback.png" mode="widthFix"></image> <!-- 反馈问题 -->
<text class="i-name">反馈问题</text> <button class="function-item" @tap="handleFeedback" hover-class="item-hover">
<text class="i-more iconfont icon-arrow-right"></text> <view class="item-left">
<image class="item-icon" src="/static/icons/icon_feedback.png" mode="widthFix"></image>
<text class="item-text">反馈问题</text>
</view>
<text class="iconfont icon-arrow-right item-arrow"></text>
</button> </button>
<button class="item clearcatch" @tap="clearStorage()">
<image class="i-icon" src="/static/icons/icon_clear_catch.png" mode="widthFix"></image> <!-- 清理缓存 -->
<text class="i-name">清理缓存</text> <button class="function-item" @tap="handleClearStorage" hover-class="item-hover">
<text class="i-more iconfont icon-arrow-right"></text> <view class="item-left">
<image class="item-icon" src="/static/icons/icon_clear_catch.png" mode="widthFix"></image>
<text class="item-text">清理缓存</text>
</view>
<text class="iconfont icon-arrow-right item-arrow"></text>
</button> </button>
<button class="item devlog" @tap="log()">
<image class="i-icon" src="/static/icons/icon_dev_log.png" mode="widthFix"></image> <!-- 开发日志 -->
<text class="i-name">开发日志</text> <button class="function-item" @tap="handleDevLog" hover-class="item-hover">
<text class="i-more iconfont icon-arrow-right"></text> <view class="item-left">
<image class="item-icon" src="/static/icons/icon_dev_log.png" mode="widthFix"></image>
<text class="item-text">开发日志</text>
</view>
<text class="iconfont icon-arrow-right item-arrow"></text>
</button> </button>
<!-- <button class="item love" @tap="admire()">
<image class="i-icon" src="/static/icons/icon_love.png" mode="widthFix"></image> <!-- 分享好友 -->
<text class="i-name">捐赠作者</text> <button class="function-item" open-type="share" hover-class="item-hover">
<text class="i-more iconfont icon-arrow-right"></text> <view class="item-left">
</button> --> <image class="item-icon" src="/static/icons/icon_share.png" mode="widthFix"></image>
<button class="item share" open-type="share"> <text class="item-text">分享好友</text>
<image class="i-icon" src="/static/icons/icon_share.png" mode="widthFix"></image> </view>
<text class="i-name">分享好友</text> <text class="iconfont icon-arrow-right item-arrow"></text>
<text class="i-more iconfont icon-arrow-right"></text>
</button> </button>
<button class="item" open-type="openSetting">
<image class="i-icon" src="/static/icons/icon_setting.png" mode="widthFix"></image> <!-- 捐赠作者 -->
<text class="i-name">功能设置</text> <button class="function-item" @tap="handleAdmire" hover-class="item-hover">
<text class="i-more iconfont icon-arrow-right"></text> <view class="item-left">
<image class="item-icon" src="/static/icons/icon_love.png" mode="widthFix"></image>
<text class="item-text">捐赠作者</text>
</view>
<text class="iconfont icon-arrow-right item-arrow"></text>
</button>
<!-- 功能设置 -->
<button class="function-item" open-type="openSetting" hover-class="item-hover">
<view class="item-left">
<image class="item-icon" src="/static/icons/icon_setting.png" mode="widthFix"></image>
<text class="item-text">功能设置</text>
</view>
<text class="iconfont icon-arrow-right item-arrow"></text>
</button>
<!-- 退出登录 -->
<button
v-if="hasUserInfo"
class="function-item logout-item"
@tap="handleLogout"
hover-class="item-hover"
>
<text class="logout-text">退出登录</text>
</button> </button>
<view v-if="hasUserInfo" class="item singout" @tap="logout()">
<text class="i-name singout-text">退出登录</text>
</view>
</view> </view>
</view> </view>
</template> </template>
<script> <script>
const app = getApp(); const app = getApp();
import authUtil from '@/utils/auth.js'; // 引入工具类
// 配置常量
const CONFIG = {
CONTACT: {
EMAIL: "lailab_mail@126.com",
QQ: "1694319867"
},
VERSION_LOG: `
<b>v2.2.5 2022-11-3</b><br />
1.优化部分代码<br />
2.去掉微信未开放功能<br />
<b>v2.2.4 2022-10-28</b><br />
1.更新部分代码<br />
<b>v2.2.3 2022-10-26</b><br />
1.实现根据人数自动开奖的功能<br />
<b>v2.2.2 2022-10-25</b><br />
1.实现抽奖页面创建加入界面<br />
2.实现抽奖页面创建后台逻辑<br />
<b>v2.2.1 2022-10-24</b><br />
1.完善抽奖助手子页面<br />
<b>v2.2 2022-10-23</b><br />
1.添加抽奖助手界面<br />
<b>v2.1 2022-10-22</b><br />
1.添加自动登录逻辑<br />
2.各个界面微调美化<br />
<b>v2.0 2022-10-19</b><br />
1.更新主页空内容显示<br />
2.实现工具智能抠图、证件照、摩斯电码、随机骰子等功能<br />
3.实现部分登录逻辑<br />
<b>v1.0 2022-10-7</b><br />
1.从微信端进行移植<br />
`
};
export default { export default {
data() { data() {
return { return {
hasUserInfo: false, hasUserInfo: false,
userInfo: {}, userInfo: {},
openid: "", openid: ""
} }
}, },
onLoad() { onLoad() {
const that = this; console.log("----------------------------- 加载我的页面 -----------------------------");
if (app.globalData.hasUserInfo) { this.initPageData();
this.hasUserInfo = app.globalData.hasUserInfo;
this.userInfo = app.globalData.userInfo;
}
}, },
onShow() {
console.log("----------------------------- 显示我的页面 -----------------------------");
this.updateUserInfo();
},
methods: { methods: {
login() { // 初始化页面数据
const that = this; initPageData() {
// 若用户未登录 this.updateUserInfo();
if (!app.globalData.hasUserInfo) { this.getStorageSize();
that.toast("正在登录", 'loading'); },
// 更新用户信息
updateUserInfo() {
if (app.globalData.hasUserInfo) {
this.hasUserInfo = true;
this.userInfo = app.globalData.userInfo;
console.log("用户信息已更新:", this.userInfo);
} else {
console.log("用户未登录");
}
},
// 处理用户卡片点击
async handleUserCardClick() {
console.log("1.开始检测是否存在用户信息");
if (!this.hasUserInfo) {
console.log("2.未查询到用户信息,触发登录流程");
await this.handleLogin();
} else {
console.log("2.查询到信息,提示用户已登录");
this.toast("您已经登录", 'none');
}
},
// 处理登录
async handleLogin() {
try {
this.toast("正在登录", 'loading');
console.log("3.1 调用wx.getUserProfile()获取信息开始");
const profile = await this.getUserProfile();
console.log("3.1 用wx.getUserProfile()获取信息结束 返回:", profile.userInfo);
// 更新全局和本地数据
app.globalData.hasUserInfo = true;
app.globalData.userInfo = profile.userInfo;
this.hasUserInfo = true;
this.userInfo = profile.userInfo;
console.log("3.2 填充用户信息到界面");
// 注册用户到后端
console.log("3.3 判断是否存在用户OopnId");
if (app.globalData.openId) {
console.log("3.3 获取到OopnId,开始注册");
await this.registerUser(profile.userInfo);
} else {
console.warn("3.3 没有OpenId,重新进入小程序获取");
this.toast("登录异常,请重新进入小程序", 'error');
return;
}
this.toast("登录成功", 'success');
console.log("登录流程完成");
} catch (error) {
console.error("登录失败:", error);
this.toast(error.message, 'error');
}
},
// 获取用户信息(Promise封装)
getUserProfile() {
return new Promise((resolve, reject) => {
uni.getUserProfile({ uni.getUserProfile({
desc: "获取你的昵称、头像、地区及性别", desc: "获取你的昵称、头像、地区及性别",
success: res => { success: resolve,
app.globalData.hasUserInfo = true; fail: () => {
app.globalData.userInfo = res.userInfo; reject(new Error("您拒绝了授权"));
that.hasUserInfo = true; }
that.userInfo = res.userInfo; });
that.toast("登录成功", 'success'); });
const openId = app.globalData.openId; },
if (openId) {
res.userInfo.openId = openId; // 注册用户到后端
// console.log("res",res); async registerUser(userInfo) {
uni.request({ try {
url: app.globalData.website + "/api/login.php", const registerData = {
method: 'POST', ...userInfo,
data: res.userInfo, openId: app.globalData.openId,
success(res) { //authCode:authCode,
uni.showModal({ // username:"admin",
title: "提示", // password:"1234",
content: res.msg // phone:"1234567",
}) // email:"1234567",
} creator:"admin",
}) source:0,
} else { status:0
console.log("没有OpenId,请重新进入小程序"); };
const result = await this.requestRegister(registerData);
} catch (error) {
console.error("3.4 用户注册失败 返回:", error);
throw error;
}
},
// 请求注册(Promise封装)
requestRegister(userInfo) {
console.log("3.4.2 调用:"+app.globalData.website +app.globalData.method.addUser+"()添加用户开始 入参:", userInfo);
return new Promise((resolve, reject) => {
uni.request({
url: app.globalData.website + app.globalData.method.addUser,
method: 'POST',
data: userInfo,
success: (res) => {
if (res.data.code == 0) {
console.log("3.4.2 调用:"+app.globalData.method.addUser+"()添加用户结束 返回:", res);
resolve(res.data);
this.showRegisterSuccess(res.data.msg);
}else{
console.log("3.4.2 调用:"+app.globalData.method.addUser+"()添加用户失败 返回:", res);
reject(new Error(res.data.msg || "注册失败"));
} }
}, },
fail: res => { fail: (res) => {
that.toast("您拒绝了请求", 'error'); console.error("3.4.2 调用:"+app.globalData.addUser+"()添加用户失败 返回:", res);
reject(new Error("网络请求失败"));
} }
}) });
} else { });
uni.showToast({ },
title: "您已经登录",
icon: 'none' // 显示注册成功提示
}) showRegisterSuccess(message) {
} uni.showModal({
}, title: "提示",
logout() { content: message || "注册成功",
const that = this; showCancel: false,
uni.showModal({ confirmText: "知道了"
title: "提示", });
content: "是否确定退出登录?", },
complete(res) {
if (res.confirm) { // 处理退出登录
app.globalData.hasUserInfo = false; async handleLogout() {
app.globalData.userInfo = {}; console.log("用户点击退出登录");
that.hasUserInfo = false;
that.userInfo = {}; const result = await this.confirmLogout();
} else if (res.cancel) {
that.toast("取消操作", 'success'); if (result) {
} this.performLogout();
} } else {
}) this.toast("取消操作", 'none');
}, }
feedback() { },
const mail = "lailab_mail@126.com";
const qq = "1694319867"; // 确认退出登录
uni.showModal({ confirmLogout() {
title: "提示", return new Promise((resolve) => {
content: "有问题可以通过联系客服进行反馈,也可以添加作者QQ[1694319867]或发送邮箱[lailab_mail@126.com]进行反馈,点击下面按钮可以进行一键复制。", uni.showModal({
confirmText: "邮箱", title: "提示",
cancelText: "QQ", content: "是否确定退出登录?",
confirmColor: "#000", confirmText: "确定",
success(res) { cancelText: "取消",
var msg = ""; success: (res) => {
if (res.confirm) { resolve(res.confirm);
msg = mail; }
} else if (res.cancel) { });
msg = qq; });
} },
uni.setClipboardData({
data: msg, // 执行退出登录
success(res) { performLogout() {
uni.getClipboardData({ // 清除全局数据
success(res) { app.globalData.hasUserInfo = false;
uni.showToast({ app.globalData.userInfo = {};
title: '复制成功',
icon: "success", // 清除本地数据
}); this.hasUserInfo = false;
} this.userInfo = {};
})
} console.log("用户已退出登录");
}) this.toast("已退出登录", 'success');
} },
// 处理反馈
async handleFeedback() {
console.log("用户点击反馈问题");
const result = await this.showFeedbackDialog();
if (result === 'email') {
await this.copyToClipboard(CONFIG.CONTACT.EMAIL, "邮箱");
} else if (result === 'qq') {
await this.copyToClipboard(CONFIG.CONTACT.QQ, "QQ号");
}
},
// 显示反馈对话框
showFeedbackDialog() {
return new Promise((resolve) => {
uni.showModal({
title: "反馈方式",
content: `有问题可以通过以下方式联系作者:
1. 邮箱:${CONFIG.CONTACT.EMAIL}
2. QQ${CONFIG.CONTACT.QQ}
请选择一种方式复制联系方式`,
confirmText: "复制邮箱",
cancelText: "复制QQ",
success: (res) => {
if (res.confirm) {
resolve('email');
} else if (res.cancel) {
resolve('qq');
}
}
});
});
},
// 复制到剪贴板
async copyToClipboard(text, type) {
try {
await this.setClipboardData(text);
await this.verifyClipboardData();
this.toast(`${type}复制成功`, 'success');
console.log(`${type}复制成功:`, text);
} catch (error) {
console.error("复制失败:", error);
this.toast("复制失败", 'error');
}
},
// 设置剪贴板数据
setClipboardData(text) {
return new Promise((resolve, reject) => {
uni.setClipboardData({
data: text,
success: resolve,
fail: reject
});
});
},
// 验证剪贴板数据
verifyClipboardData() {
return new Promise((resolve, reject) => {
uni.getClipboardData({
success: resolve,
fail: reject
});
}); });
}, },
share() {},
// 获取本地缓存大小 // 获取本地缓存大小
getStorageSize() { getStorageSize() {
let that = this; let that = this;
@@ -187,7 +430,8 @@
} }
}, },
// 清理缓存 // 清理缓存
clearStorage() { // 处理清理缓存
async handleClearStorage() {
let that = this; let that = this;
that.toast("正在清理", "loading"); that.toast("正在清理", "loading");
try { try {
@@ -228,193 +472,218 @@
return; return;
} }
}, },
log() { // 处理开发日志
const log = ` handleDevLog() {
<b>v1.0 2022-10-7</b><br /> console.log("用户查看开发日志");
1.从微信端进行移植<br /><br /> // encodeURIComponent(CONFIG.VERSION_LOG)
<b>v2.0 2022-10-19</b><br />
1.更新主页空内容显示<br />
2.实现工具智能抠图、证件照、摩斯电码、随机骰子等功能<br />
3.实现部分登录逻辑<br /><br />
<b>v2.1 2022-10-22</b><br />
1.添加自动登录逻辑<br />
<b>v2.1 2022-10-22</b><br />
1.添加自动登录逻辑<br />
2.各个界面微调美化<br />
<b>v2.2 2022-10-23</b><br />
1.添加抽奖助手界面<br />
<b>v2.2.1 2022-10-24</b><br />
1.完善抽奖助手子页面<br />
<b>v2.2.2 2022-10-25</b><br />
1.实现抽奖页面创建加入界面<br />
2.实现抽奖页面创建后台逻辑<br />
<b>v2.2.3 2022-10-26</b><br />
1.实现根据人数自动开奖的功能<br />
<b>v2.2.4 2022-10-28</b><br />
1.更新部分代码<br />
<b>v2.2.5 2022-11-3</b><br />
1.优化部分代码<br />
2.去掉微信未开放功能<br />
`;
uni.navigateTo({ uni.navigateTo({
url: `/pages/utils/showText/showText?nodes=${log}` url: `/pages/utils/showText/showText?nodes=${CONFIG.VERSION_LOG}&title=开发日志`
}); });
}, },
admire() {
const url = app.globalData.website + "/image/other/image_admire.jpg" // 捐赠作者(暂时注释)
handleAdmire() {
// const url = app.globalData.website + "/image/other/image_admire.jpg";
// uni.navigateTo({
// url: `/pages/utils/showImg/showImg?id=0&url=${url}&showBar=false&showMode=aspectFit&canLongTap=true`
// });
uni.navigateTo({ uni.navigateTo({
url: `/pages/utils/showImg/showImg?id=${0}&url=${url}&showBar=false&showMode=aspectFit&canLongTap=true` url: `/pages/test/test`
}); });
}, },
toast(title, icon) {
// Toast提示
toast(title, icon = 'none', duration = 2000) {
uni.showToast({ uni.showToast({
title: title, title: title,
icon: icon icon: icon,
}) duration: duration
});
}, },
},
onLoad() { // 分享配置
if (app.globalData.hasUserInfo) { onShareAppMessage() {
this.hasUserInfo = app.globalData.hasUserInfo; return {
this.userInfo = app.globalData.userInfo; title: 'LaiLab工具箱 - 我的工具库',
} path: '/pages/mine/mine',
this.getStorageSize(); imageUrl: '/static/share.png'
}, };
onShow() {
if (app.globalData.hasUserInfo) {
this.hasUserInfo = app.globalData.hasUserInfo;
this.userInfo = app.globalData.userInfo;
}
},
onShareAppMessage() {
return {
title: 'LaiLab工具箱',
path: 'pages/mine/mine'
} }
} }
} }
</script> </script>
<style> <style lang="scss">
@import url("../../static/iconfont/iconfont.css"); @import url("../../static/iconfont/iconfont.css");
.container { .mine-container {
width: 100%; min-height: 100vh;
/* background-color: red; */ background: white;//linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
background-color: #ffffff; padding: 20rpx 0;
padding: 20px 0 80px 0;
/* text-align: center; */
} }
.user-info { /* 用户卡片样式 */
width: 90%; .user-card {
height: 140px; display: flex;
background-color: #f6f7f9; align-items: center;
border-radius: 15px; background: #f6f7f9;//white;
margin: 0 auto; border-radius: 50rpx;
} padding: 40rpx 32rpx;
margin: 32rpx 32rpx;
.info-img { //box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
width: 30%; transition: all 0.3s ease;
height: 100%;
/* background-color: green; */
float: left;
text-align: center;
position: relative; position: relative;
&:active {
background: #f8f9fa;
transform: translateY(-2rpx);
}
} }
.img-bg { .user-avatar-container {
height: 80px; position: relative;
width: 80px; width: 140rpx;
border-radius: 15px; height: 140rpx;
background-color: #eeeeee; padding: 20rpx;
background: #eee;
border-radius: 28rpx;
flex-shrink: 0;
}
.avatar-background {
position: absolute; position: absolute;
top: 30px; top: 50%;
left: 20px; left: 50%;
transform: translate(-50%, -50%);
width: 140rpx;
height: 140rpx;
background: white;
//background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
//box-shadow: #07c160 solid;
border-radius: 28rpx;
opacity: 0.1;
} }
.img-icon { .user-avatar {
height: 60px; width: 140rpx;
width: 60px; height: 140rpx;
/* background-color: red; */ border-radius: 24rpx;
border-radius: 10px; background: white;
position: absolute; //border: 4rpx solid white;
top: 40px; //box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
left: 30px;
} }
.info-text { .user-info-container {
width: 70%; flex: 1;
height: 100%; margin-left: 32rpx;
/* background-color: skyblue; */
float: right;
} }
.text-name { .user-name {
font-size: 30px;
line-height: 140px;
font-weight: bold;
}
button::after {
border: none;
}
.items {
width: 90%;
background-color: #ffffff;
border-radius: 15px;
margin: 20px auto;
padding: 0;
}
.item {
height: 60px;
line-height: 60px;
text-align: left;
background-color: #f6f7f9;
border-radius: 0;
}
.item:first-child {
border-radius: 20px 20px 0 0;
}
.item:last-child {
border-radius: 0 0 20px 20px;
}
.i-icon {
height: 30px;
width: 30px;
/* background-color: red; */
float: left;
margin: 15px;
}
.i-name {
font-size: 18px;
padding-left: 5px;
}
.i-more {
display: block; display: block;
height: 30px; font-size: 55rpx;
width: 40px; font-weight: 600;
line-height: 40px; color: #333;
margin: 10px auto;
color: #888888;
font-size: 17px;
float: right;
} }
/* 退出登录字体 */ .user-status {
.singout-text { font-size: 24rpx;
width: 100%; color: #999;
display: inline-block;
&.status-logged {
color: #07c160;
}
}
.arrow-icon {
font-size: 28rpx;
color: #ccc;
}
/* 功能列表样式 */
.function-list {
background: #f6f7f9;//white;
border-radius: 50rpx;
margin: 0 32rpx;
overflow: hidden;
//box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
}
.function-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 32rpx 40rpx;
background: transparent;
border-bottom: 1rpx solid #f0f0f0;
position: relative;
text-align: left;
line-height: normal;
&:after {
border: none;
}
&:last-child {
border-bottom: none;
}
&.item-hover {
background: #f8f9fa;
}
}
.item-left {
display: flex;
align-items: center;
flex: 1;
}
.item-icon {
width: 60rpx;
height: 60rpx;
margin-right: 24rpx;
}
.item-text {
font-size: 34rpx;
color: #000;
margin-left: 10rpx;
flex: 1;
}
.item-arrow {
font-size: 32rpx;
color: #888;
margin-right: 30rpx;
}
.item-extra {
font-size: 28rpx;
color: #999;
}
/* 退出登录样式 */
.logout-item {
margin-top: 32rpx;
//background: #fff5f5;
border-radius: 24rpx;
justify-content: center;
&:active {
background: #ffeaea;
}
}
.logout-text {
color: #ff6b6b; color: #ff6b6b;
font-weight: bold; font-size: 32rpx;
text-align: center; font-weight: 600;
}
/* 分割线 */
.function-item + .logout-item {
margin-top: 0;
border-top: 1rpx solid #f0f0f0;
} }
</style> </style>
+4 -1
View File
@@ -21,11 +21,14 @@
}, },
data() { data() {
return { return {
tools: [] tools:[]
} }
}, },
onLoad() { onLoad() {
const app = getApp();
const gData = app.globalData;
this.getDataByOnline(); this.getDataByOnline();
this.tools = gData.morePageData.tools;
}, },
methods: { methods: {
getDataByOnline() { getDataByOnline() {