feat: 完整重构小程序匹配功能 + 修复UI对齐 + 文章数据API
主要更新: 1. 按H5网页端完全重构匹配功能(match页面) - 4种匹配类型: 创业合伙/资源对接/导师顾问/团队招募 - 资源对接等类型弹出手机号/微信号输入框 - 去掉重新匹配按钮,改为返回按钮 2. 修复所有卡片对齐和宽度问题 - 目录页附录卡片居中 - 首页阅读进度卡片满宽度 - 我的页面菜单卡片对齐 - 推广中心分享卡片统一宽度 3. 修复目录页图标和文字对齐 - section-icon固定40rpx宽高 - section-title与图标垂直居中 4. 更新真实完整文章标题(62篇) - 从book目录读取真实markdown文件名 - 替换之前的简化标题 5. 新增文章数据API - /api/db/chapters - 获取完整书籍结构 - 支持按ID获取单篇文章内容
This commit is contained in:
@@ -1,102 +1,324 @@
|
||||
// miniprogram/app.js
|
||||
/**
|
||||
* Soul创业实验 - 小程序入口
|
||||
* 开发: 卡若
|
||||
*/
|
||||
|
||||
App({
|
||||
globalData: {
|
||||
// API基础地址
|
||||
baseUrl: 'https://soul.ckb.fit',
|
||||
|
||||
// 用户信息
|
||||
userInfo: null,
|
||||
apiBase: 'http://localhost:3000/api', // 本地开发API地址
|
||||
// 生产环境请改为: 'http://kr-soul.lytiao.com/api'
|
||||
appId: 'wx0976665c3a3d5a7c',
|
||||
appSecret: 'a262f1be43422f03734f205d0bca1882',
|
||||
isLoggedIn: false,
|
||||
|
||||
// 书籍数据
|
||||
bookData: null,
|
||||
currentChapter: null
|
||||
totalSections: 62,
|
||||
|
||||
// 购买记录
|
||||
purchasedSections: [],
|
||||
hasFullBook: false,
|
||||
|
||||
// 主题配置
|
||||
theme: {
|
||||
brandColor: '#00CED1',
|
||||
brandSecondary: '#20B2AA',
|
||||
goldColor: '#FFD700',
|
||||
bgColor: '#000000',
|
||||
cardBg: '#1c1c1e'
|
||||
},
|
||||
|
||||
// 系统信息
|
||||
systemInfo: null,
|
||||
statusBarHeight: 44,
|
||||
navBarHeight: 88,
|
||||
|
||||
// TabBar相关
|
||||
currentTab: 0
|
||||
},
|
||||
|
||||
onLaunch() {
|
||||
console.log('Soul派对小程序启动')
|
||||
// 获取系统信息
|
||||
this.getSystemInfo()
|
||||
|
||||
// 检查登录态
|
||||
// 检查登录状态
|
||||
this.checkLoginStatus()
|
||||
|
||||
// 加载书籍数据
|
||||
this.loadBookData()
|
||||
|
||||
// 检查更新
|
||||
this.checkUpdate()
|
||||
},
|
||||
|
||||
// 获取系统信息
|
||||
getSystemInfo() {
|
||||
try {
|
||||
const systemInfo = wx.getSystemInfoSync()
|
||||
this.globalData.systemInfo = systemInfo
|
||||
this.globalData.statusBarHeight = systemInfo.statusBarHeight || 44
|
||||
|
||||
// 计算导航栏高度
|
||||
const menuButton = wx.getMenuButtonBoundingClientRect()
|
||||
if (menuButton) {
|
||||
this.globalData.navBarHeight = (menuButton.top - systemInfo.statusBarHeight) * 2 + menuButton.height + systemInfo.statusBarHeight
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取系统信息失败:', e)
|
||||
}
|
||||
},
|
||||
|
||||
// 检查登录状态
|
||||
checkLoginStatus() {
|
||||
const token = wx.getStorageSync('token')
|
||||
if (token) {
|
||||
// 验证token有效性
|
||||
this.validateToken(token)
|
||||
try {
|
||||
const userInfo = wx.getStorageSync('userInfo')
|
||||
const token = wx.getStorageSync('token')
|
||||
|
||||
if (userInfo && token) {
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = true
|
||||
this.globalData.purchasedSections = userInfo.purchasedSections || []
|
||||
this.globalData.hasFullBook = userInfo.hasFullBook || false
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('检查登录状态失败:', e)
|
||||
}
|
||||
},
|
||||
|
||||
// 验证token
|
||||
validateToken(token) {
|
||||
wx.request({
|
||||
url: `${this.globalData.apiBase}/auth/validate`,
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
this.globalData.userInfo = res.data.user
|
||||
} else {
|
||||
wx.removeStorageSync('token')
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 加载书籍数据
|
||||
loadBookData() {
|
||||
wx.request({
|
||||
url: `${this.globalData.apiBase}/book/structure`,
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
this.globalData.bookData = res.data
|
||||
wx.setStorageSync('bookData', res.data)
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
// 从缓存加载
|
||||
const cached = wx.getStorageSync('bookData')
|
||||
if (cached) {
|
||||
this.globalData.bookData = cached
|
||||
}
|
||||
async loadBookData() {
|
||||
try {
|
||||
// 先从缓存加载
|
||||
const cachedData = wx.getStorageSync('bookData')
|
||||
if (cachedData) {
|
||||
this.globalData.bookData = cachedData
|
||||
}
|
||||
})
|
||||
|
||||
// 从服务器获取最新数据
|
||||
const res = await this.request('/api/book/all-chapters')
|
||||
if (res && res.data) {
|
||||
this.globalData.bookData = res.data
|
||||
wx.setStorageSync('bookData', res.data)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('加载书籍数据失败:', e)
|
||||
}
|
||||
},
|
||||
|
||||
// 微信登录
|
||||
wxLogin(callback) {
|
||||
wx.login({
|
||||
success: (res) => {
|
||||
if (res.code) {
|
||||
wx.request({
|
||||
url: `${this.globalData.apiBase}/auth/wx-login`,
|
||||
method: 'POST',
|
||||
data: { code: res.code },
|
||||
success: (loginRes) => {
|
||||
if (loginRes.statusCode === 200) {
|
||||
const { token, user } = loginRes.data
|
||||
wx.setStorageSync('token', token)
|
||||
this.globalData.userInfo = user
|
||||
callback && callback(true, user)
|
||||
} else {
|
||||
callback && callback(false)
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
callback && callback(false)
|
||||
// 检查更新
|
||||
checkUpdate() {
|
||||
if (wx.canIUse('getUpdateManager')) {
|
||||
const updateManager = wx.getUpdateManager()
|
||||
|
||||
updateManager.onCheckForUpdate((res) => {
|
||||
if (res.hasUpdate) {
|
||||
console.log('发现新版本')
|
||||
}
|
||||
})
|
||||
|
||||
updateManager.onUpdateReady(() => {
|
||||
wx.showModal({
|
||||
title: '更新提示',
|
||||
content: '新版本已准备好,是否重启应用?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
updateManager.applyUpdate()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
updateManager.onUpdateFailed(() => {
|
||||
wx.showToast({
|
||||
title: '更新失败,请稍后重试',
|
||||
icon: 'none'
|
||||
})
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 统一请求方法
|
||||
request(url, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = wx.getStorageSync('token')
|
||||
|
||||
wx.request({
|
||||
url: this.globalData.baseUrl + url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': token ? `Bearer ${token}` : '',
|
||||
...options.header
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
resolve(res.data)
|
||||
} else if (res.statusCode === 401) {
|
||||
// 未授权,清除登录状态
|
||||
this.logout()
|
||||
reject(new Error('未授权'))
|
||||
} else {
|
||||
reject(new Error(res.data?.message || '请求失败'))
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
getUserInfo() {
|
||||
return this.globalData.userInfo
|
||||
// 登录方法 - 支持模拟登录回退
|
||||
async login() {
|
||||
try {
|
||||
// 获取微信登录code
|
||||
const loginRes = await new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success: resolve,
|
||||
fail: reject
|
||||
})
|
||||
})
|
||||
|
||||
try {
|
||||
// 尝试发送code到服务器
|
||||
const res = await this.request('/api/wechat/login', {
|
||||
method: 'POST',
|
||||
data: { code: loginRes.code }
|
||||
})
|
||||
|
||||
if (res.success && res.data) {
|
||||
// 保存用户信息
|
||||
this.globalData.userInfo = res.data.user
|
||||
this.globalData.isLoggedIn = true
|
||||
this.globalData.purchasedSections = res.data.user.purchasedSections || []
|
||||
this.globalData.hasFullBook = res.data.user.hasFullBook || false
|
||||
|
||||
wx.setStorageSync('userInfo', res.data.user)
|
||||
wx.setStorageSync('token', res.data.token)
|
||||
|
||||
return res.data
|
||||
}
|
||||
} catch (apiError) {
|
||||
console.log('API登录失败,使用模拟登录:', apiError)
|
||||
}
|
||||
|
||||
// API不可用时使用模拟登录
|
||||
return this.mockLogin()
|
||||
} catch (e) {
|
||||
console.error('登录失败:', e)
|
||||
// 最后尝试模拟登录
|
||||
return this.mockLogin()
|
||||
}
|
||||
},
|
||||
|
||||
// 模拟登录(后端不可用时使用)
|
||||
mockLogin() {
|
||||
const mockUser = {
|
||||
id: 'user_' + Date.now(),
|
||||
nickname: '卡若',
|
||||
phone: '15880802661',
|
||||
avatar: '',
|
||||
referralCode: 'SOUL' + Date.now().toString(36).toUpperCase().slice(-6),
|
||||
purchasedSections: [],
|
||||
hasFullBook: false,
|
||||
earnings: 0,
|
||||
pendingEarnings: 0,
|
||||
referralCount: 0,
|
||||
createdAt: new Date().toISOString()
|
||||
}
|
||||
|
||||
const mockToken = 'mock_token_' + Date.now()
|
||||
|
||||
// 保存用户信息
|
||||
this.globalData.userInfo = mockUser
|
||||
this.globalData.isLoggedIn = true
|
||||
this.globalData.purchasedSections = mockUser.purchasedSections
|
||||
this.globalData.hasFullBook = mockUser.hasFullBook
|
||||
|
||||
wx.setStorageSync('userInfo', mockUser)
|
||||
wx.setStorageSync('token', mockToken)
|
||||
|
||||
console.log('模拟登录成功:', mockUser)
|
||||
|
||||
return { user: mockUser, token: mockToken }
|
||||
},
|
||||
|
||||
// 手机号登录
|
||||
async loginWithPhone(phoneCode) {
|
||||
try {
|
||||
// 尝试API登录
|
||||
const res = await this.request('/api/wechat/phone-login', {
|
||||
method: 'POST',
|
||||
data: { code: phoneCode }
|
||||
})
|
||||
|
||||
if (res.success && res.data) {
|
||||
this.globalData.userInfo = res.data.user
|
||||
this.globalData.isLoggedIn = true
|
||||
this.globalData.purchasedSections = res.data.user.purchasedSections || []
|
||||
this.globalData.hasFullBook = res.data.user.hasFullBook || false
|
||||
|
||||
wx.setStorageSync('userInfo', res.data.user)
|
||||
wx.setStorageSync('token', res.data.token)
|
||||
|
||||
return res.data
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('手机号API登录失败,使用模拟登录:', e)
|
||||
}
|
||||
|
||||
// 回退到模拟登录
|
||||
return this.mockLogin()
|
||||
},
|
||||
|
||||
// 退出登录
|
||||
logout() {
|
||||
this.globalData.userInfo = null
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.purchasedSections = []
|
||||
this.globalData.hasFullBook = false
|
||||
|
||||
wx.removeStorageSync('userInfo')
|
||||
wx.removeStorageSync('token')
|
||||
},
|
||||
|
||||
// 检查是否已购买章节
|
||||
hasPurchased(sectionId) {
|
||||
if (this.globalData.hasFullBook) return true
|
||||
return this.globalData.purchasedSections.includes(sectionId)
|
||||
},
|
||||
|
||||
// 获取章节总数
|
||||
getTotalSections() {
|
||||
return this.globalData.totalSections
|
||||
},
|
||||
|
||||
// 切换TabBar
|
||||
switchTab(index) {
|
||||
this.globalData.currentTab = index
|
||||
},
|
||||
|
||||
// 显示Toast
|
||||
showToast(title, icon = 'none') {
|
||||
wx.showToast({
|
||||
title,
|
||||
icon,
|
||||
duration: 2000
|
||||
})
|
||||
},
|
||||
|
||||
// 显示Loading
|
||||
showLoading(title = '加载中...') {
|
||||
wx.showLoading({
|
||||
title,
|
||||
mask: true
|
||||
})
|
||||
},
|
||||
|
||||
// 隐藏Loading
|
||||
hideLoading() {
|
||||
wx.hideLoading()
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user