refactor: 重构微信小程序从云开发迁移至后端API
将云数据库操作改为调用后端API接口 提取登录失败处理逻辑到单独方法 添加环境配置和API请求工具类 移除云开发相关配置
This commit is contained in:
@@ -1,16 +1,8 @@
|
||||
// app.js
|
||||
const { BASE_URL, API } = require('./utils/config')
|
||||
|
||||
App({
|
||||
onLaunch() {
|
||||
if (!wx.cloud) {
|
||||
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
|
||||
} else {
|
||||
wx.cloud.init({
|
||||
traceUser: true
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 自动静默登录:wx.login 获取 code,再请求后端接口换取 openid
|
||||
this.silentLogin()
|
||||
},
|
||||
@@ -22,66 +14,55 @@ App({
|
||||
this.loginWithCode(loginRes.code)
|
||||
} else {
|
||||
console.error('wx.login 失败', loginRes.errMsg)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
this.handleLoginFail()
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('wx.login 调用失败', err)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
this.handleLoginFail()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
loginWithCode(code) {
|
||||
wx.request({
|
||||
url: 'https://xcx.yun.588580.xyz/api/wx-mini/login',
|
||||
url: BASE_URL + API.LOGIN,
|
||||
method: 'GET',
|
||||
data: { code },
|
||||
success: (res) => {
|
||||
const result = res.data
|
||||
if (result && result.code === 0 && result.data) {
|
||||
const openid = result.data.openid
|
||||
const userInfo = {
|
||||
openid,
|
||||
openid: result.data.openid,
|
||||
sessionKey: result.data.session_key,
|
||||
unionid: result.data.unionid || ''
|
||||
}
|
||||
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = true
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(userInfo)
|
||||
}
|
||||
} else {
|
||||
console.error('后端登录失败', result)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
this.handleLoginFail()
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('请求后端登录接口失败', err)
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
this.handleLoginFail()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
handleLoginFail() {
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
},
|
||||
|
||||
globalData: {
|
||||
userInfo: null,
|
||||
isLoggedIn: false,
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
// 云函数入口文件
|
||||
const cloud = require('wx-server-sdk')
|
||||
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
|
||||
const db = cloud.database()
|
||||
|
||||
// 云函数入口函数
|
||||
exports.main = async (event, context) => {
|
||||
const wxContext = cloud.getWXContext()
|
||||
const openid = wxContext.OPENID
|
||||
|
||||
// 查找是否已存在用户
|
||||
const userRes = await db.collection('users').where({ _openid: openid }).get()
|
||||
|
||||
let user
|
||||
if (userRes.data.length > 0) {
|
||||
// 已存在,更新登录时间(如果传了头像昵称也一并更新)
|
||||
user = userRes.data[0]
|
||||
const updateData = {
|
||||
lastLoginTime: db.serverDate()
|
||||
}
|
||||
if (event.avatarUrl) updateData.avatarUrl = event.avatarUrl
|
||||
if (event.nickName) updateData.nickName = event.nickName
|
||||
await db.collection('users').doc(user._id).update({ data: updateData })
|
||||
// 合并最新数据返回
|
||||
if (event.avatarUrl) user.avatarUrl = event.avatarUrl
|
||||
if (event.nickName) user.nickName = event.nickName
|
||||
} else {
|
||||
// 新用户,创建记录
|
||||
const addRes = await db.collection('users').add({
|
||||
data: {
|
||||
_openid: openid,
|
||||
avatarUrl: event.avatarUrl || '',
|
||||
nickName: event.nickName || '',
|
||||
createTime: db.serverDate(),
|
||||
lastLoginTime: db.serverDate()
|
||||
}
|
||||
})
|
||||
user = {
|
||||
_id: addRes._id,
|
||||
_openid: openid,
|
||||
avatarUrl: event.avatarUrl || '',
|
||||
nickName: event.nickName || ''
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
openid: openid,
|
||||
appid: wxContext.APPID,
|
||||
unionid: wxContext.UNIONID,
|
||||
userInfo: user
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"name": "login",
|
||||
"version": "1.0.0",
|
||||
"description": "登录云函数",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"wx-server-sdk": "~2.6.3"
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// appointment.js
|
||||
const { formatDate } = require('../../utils/util')
|
||||
const { appointmentDB } = require('../../utils/cloud')
|
||||
const { appointmentDB } = require('../../utils/api')
|
||||
const app = getApp()
|
||||
|
||||
Page({
|
||||
@@ -109,8 +109,9 @@ Page({
|
||||
this.setData({ submitting: true })
|
||||
|
||||
try {
|
||||
// 写入云数据库
|
||||
await appointmentDB.create(this.data.form)
|
||||
// 通过后端API创建预约
|
||||
const openid = app.globalData.userInfo.openid
|
||||
await appointmentDB.create({ ...this.data.form, openid })
|
||||
|
||||
this.setData({ submitting: false })
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// index.js
|
||||
const { appointmentDB, formatRecord } = require('../../utils/cloud')
|
||||
const { appointmentDB, formatRecord } = require('../../utils/api')
|
||||
const app = getApp()
|
||||
|
||||
Page({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// records.js
|
||||
const { appointmentDB, formatRecord } = require('../../utils/cloud')
|
||||
const { appointmentDB, formatRecord } = require('../../utils/api')
|
||||
const app = getApp()
|
||||
|
||||
Page({
|
||||
|
||||
+1
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"compileType": "miniprogram",
|
||||
"libVersion": "trial",
|
||||
"libVersion": "3.15.2",
|
||||
"packOptions": {
|
||||
"ignore": [],
|
||||
"include": []
|
||||
@@ -37,6 +37,5 @@
|
||||
"tabSize": 2
|
||||
},
|
||||
"appid": "wx50fe0c5c28dd3060",
|
||||
"cloudfunctionRoot": "cloudfunctions/",
|
||||
"simulatorPluginLibVersion": {}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
|
||||
"projectname": "p1",
|
||||
"projectname": "miniwx",
|
||||
"setting": {
|
||||
"compileHotReLoad": true,
|
||||
"urlCheck": true,
|
||||
@@ -18,5 +18,6 @@
|
||||
"checkInvalidKey": true,
|
||||
"ignoreDevUnusedFiles": true
|
||||
},
|
||||
"libVersion": "3.15.2"
|
||||
"libVersion": "3.15.2",
|
||||
"condition": {}
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
// 后端 API 请求工具库
|
||||
const { BASE_URL, API } = require('./config')
|
||||
|
||||
/**
|
||||
* 封装 wx.request 为 Promise
|
||||
* @param {object} options - wx.request 参数
|
||||
* @returns {Promise<object>} 返回 result.data
|
||||
*/
|
||||
function request(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
...options,
|
||||
success: (res) => {
|
||||
const result = res.data
|
||||
if (result && result.code === 0) {
|
||||
resolve(result.data)
|
||||
} else {
|
||||
reject(new Error(result ? result.message : '请求失败'))
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('请求失败', options.url, err)
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化记录中的 createTime 字段
|
||||
* @param {object} record - 含 createTime 的记录
|
||||
* @returns {object} 格式化后的记录
|
||||
*/
|
||||
function formatRecord(record) {
|
||||
if (!record || !record.createTime) return record
|
||||
return {
|
||||
...record,
|
||||
createTime: new Date(record.createTime).toLocaleString('zh-CN')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将后端 API 返回的预约记录映射为前端模板字段
|
||||
* 后端: id, visitDate, visitTime → 前端: _id, date, time
|
||||
*/
|
||||
function mapApiRecord(record) {
|
||||
if (!record) return null
|
||||
return {
|
||||
...record,
|
||||
_id: record.id,
|
||||
date: record.visitDate,
|
||||
time: record.visitTime
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预约相关 API
|
||||
*/
|
||||
const appointmentDB = {
|
||||
/**
|
||||
* 创建预约
|
||||
* @param {object} data - 预约表单数据(需包含 openid)
|
||||
* @returns {Promise<string>} 新记录 id
|
||||
*/
|
||||
async create(data) {
|
||||
const result = await request({
|
||||
url: BASE_URL + API.APPOINTMENT_CREATE,
|
||||
method: 'POST',
|
||||
header: { 'content-type': 'application/json' },
|
||||
data: {
|
||||
name: data.name,
|
||||
phone: data.phone,
|
||||
company: data.company,
|
||||
reason: data.reason,
|
||||
visitDate: data.date,
|
||||
visitTime: data.time,
|
||||
hostName: data.hostName,
|
||||
area: data.area,
|
||||
openid: data.openid
|
||||
}
|
||||
})
|
||||
return result.id
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取当前用户的预约列表(按创建时间倒序)
|
||||
* @param {string} openid
|
||||
* @returns {Promise<Array>}
|
||||
*/
|
||||
async getList(openid) {
|
||||
const list = await request({
|
||||
url: BASE_URL + API.APPOINTMENT_LIST,
|
||||
method: 'GET',
|
||||
data: { openid }
|
||||
})
|
||||
return (list || []).map(item => mapApiRecord(item))
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取当前用户最新一条预约
|
||||
* @param {string} openid
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
async getLatest(openid) {
|
||||
const data = await request({
|
||||
url: BASE_URL + API.APPOINTMENT_LATEST,
|
||||
method: 'GET',
|
||||
data: { openid }
|
||||
})
|
||||
return mapApiRecord(data) || null
|
||||
},
|
||||
|
||||
/**
|
||||
* 取消预约
|
||||
* @param {string} id - 预约记录 id
|
||||
* @param {string} openid - 当前用户 openid
|
||||
* @returns {Promise<boolean>} 是否成功
|
||||
*/
|
||||
async cancel(id, openid) {
|
||||
const result = await request({
|
||||
url: BASE_URL + API.APPOINTMENT_CANCEL + '?id=' + encodeURIComponent(id) + '&openid=' + encodeURIComponent(openid),
|
||||
method: 'PUT'
|
||||
})
|
||||
return result === true
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatRecord,
|
||||
appointmentDB
|
||||
}
|
||||
-196
@@ -1,196 +0,0 @@
|
||||
// 云数据库操作工具库
|
||||
|
||||
/**
|
||||
* 延迟获取 db 实例,确保 cloud.init 已完成
|
||||
*/
|
||||
function getDb() {
|
||||
return wx.cloud.database()
|
||||
}
|
||||
|
||||
function getCmd() {
|
||||
return getDb().command
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化云数据库时间字段
|
||||
* @param {object} record - 含 createTime 的记录
|
||||
* @returns {object} 格式化后的记录
|
||||
*/
|
||||
function formatRecord(record) {
|
||||
if (!record) return record
|
||||
const date = record.createTime
|
||||
let createTimeStr = ''
|
||||
if (date) {
|
||||
if (typeof date === 'string') {
|
||||
// 后端API返回的时间字符串,直接格式化
|
||||
createTimeStr = new Date(date).toLocaleString('zh-CN')
|
||||
} else if (typeof date === 'object' && date.$date) {
|
||||
createTimeStr = new Date(date.$date).toLocaleString('zh-CN')
|
||||
} else if (typeof date === 'number') {
|
||||
createTimeStr = new Date(date).toLocaleString('zh-CN')
|
||||
} else {
|
||||
createTimeStr = new Date(date).toLocaleString('zh-CN')
|
||||
}
|
||||
}
|
||||
return { ...record, createTime: createTimeStr }
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户相关操作
|
||||
*/
|
||||
const userDB = {
|
||||
/**
|
||||
* 通过 openId 查找用户,不存在则创建
|
||||
* @param {string} openid
|
||||
* @returns {Promise<object>} 用户记录
|
||||
*/
|
||||
async loginOrCreate(openid) {
|
||||
const db = getDb()
|
||||
const res = await db.collection('users').where({ _openid: openid }).get()
|
||||
if (res.data.length > 0) {
|
||||
const existing = res.data[0]
|
||||
await db.collection('users').doc(existing._id).update({
|
||||
data: {
|
||||
lastLoginTime: db.serverDate()
|
||||
}
|
||||
})
|
||||
return existing
|
||||
} else {
|
||||
const addRes = await db.collection('users').add({
|
||||
data: {
|
||||
_openid: openid,
|
||||
createTime: db.serverDate(),
|
||||
lastLoginTime: db.serverDate()
|
||||
}
|
||||
})
|
||||
return {
|
||||
_id: addRes._id,
|
||||
_openid: openid
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预约相关操作
|
||||
*/
|
||||
const appointmentDB = {
|
||||
/**
|
||||
* 创建预约
|
||||
* @param {object} data - 预约表单数据
|
||||
* @returns {Promise<string>} 新记录 _id
|
||||
*/
|
||||
async create(data) {
|
||||
const db = getDb()
|
||||
const res = await db.collection('appointments').add({
|
||||
data: {
|
||||
...data,
|
||||
status: 'pending',
|
||||
statusText: '待审核',
|
||||
createTime: db.serverDate()
|
||||
}
|
||||
})
|
||||
return res._id
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取当前用户的预约列表(按创建时间倒序,自动分页取全部)
|
||||
* @param {string} openid
|
||||
* @returns {Promise<Array>}
|
||||
*/
|
||||
async getList(openid) {
|
||||
const db = getDb()
|
||||
const MAX_LIMIT = 100
|
||||
let allData = []
|
||||
let countResult = await db.collection('appointments').where({ _openid: openid }).count()
|
||||
const total = countResult.total
|
||||
const batchTimes = Math.ceil(total / MAX_LIMIT)
|
||||
|
||||
for (let i = 0; i < batchTimes; i++) {
|
||||
const res = await db.collection('appointments')
|
||||
.where({ _openid: openid })
|
||||
.orderBy('createTime', 'desc')
|
||||
.skip(i * MAX_LIMIT)
|
||||
.limit(MAX_LIMIT)
|
||||
.get()
|
||||
allData = allData.concat(res.data)
|
||||
}
|
||||
|
||||
return allData
|
||||
},
|
||||
|
||||
/**
|
||||
* 将后端API返回的预约记录映射为前端模板兼容的字段
|
||||
* 后端字段: id, visitDate, visitTime, createTime, ...
|
||||
* 前端字段: _id, date, time, createTime(格式化后), ...
|
||||
*/
|
||||
_mapApiRecord(record) {
|
||||
if (!record) return null
|
||||
return {
|
||||
...record,
|
||||
_id: record.id,
|
||||
date: record.visitDate,
|
||||
time: record.visitTime
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取当前用户最新一条预约(通过后端API)
|
||||
* @param {string} openid
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
getLatest(openid) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: 'https://xcx.yun.588580.xyz/api/wx-mini/appointment/latest',
|
||||
method: 'GET',
|
||||
data: { openid },
|
||||
success: (res) => {
|
||||
const result = res.data
|
||||
if (result && result.code === 0) {
|
||||
resolve(this._mapApiRecord(result.data) || null)
|
||||
} else {
|
||||
console.error('获取最新预约失败', result)
|
||||
reject(new Error(result ? result.message : '请求失败'))
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('请求后端获取最新预约失败', err)
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 取消预约
|
||||
* @param {string} id - 预约记录 _id
|
||||
* @param {string} openid - 当前用户 openid,用于权限校验
|
||||
* @returns {Promise<boolean>} 是否成功
|
||||
*/
|
||||
async cancel(id, openid) {
|
||||
const db = getDb()
|
||||
const res = await db.collection('appointments').doc(id).get()
|
||||
if (res.data._openid !== openid) {
|
||||
return false
|
||||
}
|
||||
if (res.data.status !== 'pending') {
|
||||
return false
|
||||
}
|
||||
await db.collection('appointments').doc(id).update({
|
||||
data: {
|
||||
status: 'cancelled',
|
||||
statusText: '已取消'
|
||||
}
|
||||
})
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getDb,
|
||||
getCmd,
|
||||
formatRecord,
|
||||
userDB,
|
||||
appointmentDB
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 接口基础配置
|
||||
|
||||
// 环境地址配置
|
||||
const ENV_CONFIG = {
|
||||
// 正式版
|
||||
release: 'https://xcx.yun.588580.xyz',
|
||||
// 开发版 & 体验版
|
||||
develop: 'http://172.16.60.235:8080'
|
||||
}
|
||||
|
||||
// 自动判断当前运行环境
|
||||
function getBaseUrl() {
|
||||
const accountInfo = wx.getAccountInfoSync()
|
||||
const envVersion = accountInfo.miniProgram.envVersion
|
||||
// release = 正式版, develop = 开发版, trial = 体验版
|
||||
return envVersion === 'release' ? ENV_CONFIG.release : ENV_CONFIG.develop
|
||||
}
|
||||
|
||||
const BASE_URL = getBaseUrl()
|
||||
|
||||
// API 路径配置
|
||||
const API = {
|
||||
LOGIN: '/api/wx-mini/login',
|
||||
APPOINTMENT_LATEST: '/api/wx-mini/appointment/latest',
|
||||
APPOINTMENT_LIST: '/api/wx-mini/appointment/list',
|
||||
APPOINTMENT_CREATE: '/api/wx-mini/appointment/create',
|
||||
APPOINTMENT_CANCEL: '/api/wx-mini/appointment/cancel'
|
||||
}
|
||||
|
||||
console.log('[config] 当前环境:', wx.getAccountInfoSync().miniProgram.envVersion, 'BASE_URL:', BASE_URL)
|
||||
|
||||
module.exports = {
|
||||
BASE_URL,
|
||||
API
|
||||
}
|
||||
Reference in New Issue
Block a user