功能:新增微信小程序界面
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
const authApi = require('../../api/auth')
|
||||
const couponApi = require('../../api/coupon')
|
||||
const orderApi = require('../../api/order')
|
||||
const userApi = require('../../api/user')
|
||||
const wishlistApi = require('../../api/wishlist')
|
||||
const auth = require('../../utils/auth')
|
||||
const navigation = require('../../utils/navigation')
|
||||
const { DEFAULT_AVATAR } = require('../../utils/config')
|
||||
const { runWithLoading } = require('../../utils/loading')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
isLoggedIn: false,
|
||||
loading: false,
|
||||
needSetPassword: false,
|
||||
userInfo: null,
|
||||
orderCount: 0,
|
||||
couponCount: 0,
|
||||
wishlistCount: 0,
|
||||
defaultAvatar: DEFAULT_AVATAR
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.loadProfile()
|
||||
},
|
||||
|
||||
async loadProfile() {
|
||||
const isLoggedIn = auth.isLoggedIn()
|
||||
this.setData({
|
||||
isLoggedIn,
|
||||
needSetPassword: auth.getNeedSetPassword(),
|
||||
loading: isLoggedIn
|
||||
})
|
||||
|
||||
if (!isLoggedIn) {
|
||||
this.setData({
|
||||
userInfo: null,
|
||||
orderCount: 0,
|
||||
couponCount: 0,
|
||||
wishlistCount: 0,
|
||||
loading: false
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const [userInfo, orders, coupons, wishlist] = await Promise.all([
|
||||
userApi.getUserInfo(),
|
||||
orderApi.getOrderList(),
|
||||
couponApi.listUserCoupons(),
|
||||
wishlistApi.getWishlist()
|
||||
])
|
||||
|
||||
getApp().refreshUserInfo(userInfo)
|
||||
this.setData({
|
||||
userInfo,
|
||||
orderCount: Array.isArray(orders) ? orders.length : 0,
|
||||
couponCount: Array.isArray(coupons) ? coupons.length : 0,
|
||||
wishlistCount: Array.isArray(wishlist) ? wishlist.length : 0
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '个人中心加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.setData({
|
||||
loading: false
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
handleOpenAuth() {
|
||||
navigation.openAuth('/pages/profile/index')
|
||||
},
|
||||
|
||||
handleOpenSettings() {
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.handleOpenAuth()
|
||||
return
|
||||
}
|
||||
navigation.navigate('/pages/settings/index')
|
||||
},
|
||||
|
||||
handleOpenOrders() {
|
||||
if (!navigation.ensureLogin('查看订单')) {
|
||||
return
|
||||
}
|
||||
navigation.navigate('/pages/orders/index')
|
||||
},
|
||||
|
||||
handleOpenWishlist() {
|
||||
if (!navigation.ensureLogin('查看收藏')) {
|
||||
return
|
||||
}
|
||||
navigation.navigate('/pages/wishlist/index')
|
||||
},
|
||||
|
||||
handleOpenCoupons() {
|
||||
if (!navigation.ensureLogin('查看优惠券')) {
|
||||
return
|
||||
}
|
||||
navigation.navigate('/pages/coupons/index')
|
||||
},
|
||||
|
||||
async handleSwitchShop() {
|
||||
if (!navigation.ensureLogin('切换门店')) {
|
||||
return
|
||||
}
|
||||
|
||||
const userInfo = this.data.userInfo
|
||||
const shopIds = userInfo && Array.isArray(userInfo.shopIds) ? userInfo.shopIds : []
|
||||
const shopNames = userInfo && Array.isArray(userInfo.shopNames) ? userInfo.shopNames : []
|
||||
|
||||
if (!shopIds.length) {
|
||||
wx.showToast({
|
||||
title: '当前账号还没有可用门店',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (shopIds.length === 1) {
|
||||
navigation.navigate('/pages/shop-detail/index', {
|
||||
id: shopIds[0],
|
||||
returnTo: navigation.getCurrentPageUrl()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const selection = await new Promise(resolve => {
|
||||
wx.showActionSheet({
|
||||
itemList: shopNames.map((name, index) => name || `门店 ${shopIds[index]}`),
|
||||
success: res => resolve(res.tapIndex),
|
||||
fail: () => resolve(-1)
|
||||
})
|
||||
})
|
||||
|
||||
if (selection < 0 || selection >= shopIds.length) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = await runWithLoading(() => authApi.switchShop({
|
||||
shopId: shopIds[selection]
|
||||
}))
|
||||
getApp().setUserSession(payload)
|
||||
wx.showToast({
|
||||
title: '门店已切换',
|
||||
icon: 'none'
|
||||
})
|
||||
this.loadProfile()
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '门店切换失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
async handleLogout() {
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.handleOpenAuth()
|
||||
return
|
||||
}
|
||||
|
||||
const confirmed = await new Promise(resolve => {
|
||||
wx.showModal({
|
||||
title: '退出登录',
|
||||
content: '退出后需要重新登录才能查看订单和继续下单。',
|
||||
success: res => resolve(Boolean(res.confirm))
|
||||
})
|
||||
})
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await runWithLoading(() => authApi.logout())
|
||||
} catch (error) {}
|
||||
|
||||
getApp().clearUserSession()
|
||||
await this.loadProfile()
|
||||
wx.showToast({
|
||||
title: '已退出登录',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user