Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// app.js - 小程序全局入口
const api = require('./utils/api.js')
const storage = require('./utils/storage.js')
const auth = require('./utils/auth.js')

App({
globalData: {
userInfo: null,
token: '',
role: 'user', // user | merchant | admin
location: null,
systemInfo: null,
apiBase: 'https://api.campus.example.com',
version: '1.0.0',
// 购物车 { shopId: { shopName, goods: [{id,name,price,spec,count}], selected: true } }
cart: {}
},

onLaunch(options) {
// 系统信息
try {
this.globalData.systemInfo = wx.getSystemInfoSync()
} catch (e) {}

// 恢复登录状态
const token = storage.get('token')
const userInfo = storage.get('userInfo')
if (token && userInfo) {
this.globalData.token = token
this.globalData.userInfo = userInfo
this.globalData.role = userInfo.role || 'user'
}

// 恢复购物车
const cart = storage.get('cart')
if (cart) this.globalData.cart = cart

// 获取定位
this.getLocation()

// 检查登录状态
this.checkLoginStatus()

// 场景值处理
if (options.scene) {
console.log('场景值:', options.scene)
}
},

onShow(options) {
// 页面显示时刷新用户信息
if (this.globalData.token) {
// 可选:刷新用户信息
}
},

onError(msg) {
console.error('小程序错误:', msg)
},

// 获取定位
getLocation() {
wx.getLocation({
type: 'gcj02',
success: (res) => {
this.globalData.location = {
latitude: res.latitude,
longitude: res.longitude
}
},
fail: () => {
// 默认位置(校园中心)
this.globalData.location = {
latitude: 39.908823,
longitude: 116.397470
}
}
})
},

// 检查登录状态
checkLoginStatus() {
if (!this.globalData.token) return
// 可调用后端校验token有效性
},

// 登录
login(cb) {
wx.login({
success: (res) => {
if (res.code) {
// 模拟登录(实际应发送到后端换取token)
auth.mockLogin(res.code).then((user) => {
this.globalData.token = user.token
this.globalData.userInfo = user.info
this.globalData.role = user.info.role
storage.set('token', user.token)
storage.set('userInfo', user.info)
cb && cb(user)
})
}
}
})
},

// 登出
logout() {
this.globalData.token = ''
this.globalData.userInfo = null
this.globalData.role = 'user'
storage.remove('token')
storage.remove('userInfo')
},

// 需要登录的操作
requireLogin(cb) {
if (this.globalData.token) {
cb && cb()
} else {
wx.showModal({
title: '提示',
content: '请先登录',
success: (res) => {
if (res.confirm) {
wx.navigateTo({ url: '/pages/user/login/login' })
}
}
})
}
},

// 保存购物车
saveCart() {
storage.set('cart', this.globalData.cart)
}
})
74 changes: 74 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"pages": [
"pages/index/index",
"pages/food/index/index",
"pages/food/shop/shop",
"pages/food/checkout/checkout",
"pages/food/order-detail/order-detail",
"pages/community/list/list",
"pages/community/detail/detail",
"pages/community/create/create",
"pages/community/activity/activity",
"pages/forum/list/list",
"pages/forum/detail/detail",
"pages/forum/publish/publish",
"pages/confession/list/list",
"pages/confession/publish/publish",
"pages/confession/detail/detail",
"pages/market/list/list",
"pages/market/publish/publish",
"pages/market/detail/detail",
"pages/market/my-goods/my-goods",
"pages/merchant/list/list",
"pages/merchant/detail/detail",
"pages/merchant/apply/apply",
"pages/merchant/my-shop/my-shop",
"pages/merchant/goods-manage/goods-manage",
"pages/merchant/goods-edit/goods-edit",
"pages/merchant/order-manage/order-manage",
"pages/errand/list/list",
"pages/errand/publish/publish",
"pages/errand/detail/detail",
"pages/custom/diy/diy",
"pages/custom/h5/h5",
"pages/user/profile/profile",
"pages/user/login/login",
"pages/user/order/my-order",
"pages/user/coupon/coupon",
"pages/user/favorite/favorite",
"pages/user/settings/settings"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTitleText": "校园综合服务",
"navigationBarTextStyle": "black",
"backgroundColor": "#f5f5f5"
},
"tabBar": {
"color": "#999999",
"selectedColor": "#1890ff",
"backgroundColor": "#ffffff",
"borderStyle": "black",
"list": [
{ "pagePath": "pages/index/index", "text": "首页" },
{ "pagePath": "pages/food/index/index", "text": "外卖" },
{ "pagePath": "pages/community/list/list", "text": "社区" },
{ "pagePath": "pages/forum/list/list", "text": "论坛" },
{ "pagePath": "pages/user/profile/profile", "text": "我的" }
]
},
"permission": {
"scope.userLocation": {
"desc": "用于显示附近商户、活动定位和骑手位置追踪"
}
},
"requiredPrivateInfos": [
"getLocation",
"chooseLocation"
],
"lazyCodeLoading": "requiredComponents",
"style": "v2",
"sitemapLocation": "sitemap.json",
"usingComponents": {}
}
Loading