Files
JayuCloud/YumiNail-Admin-Program/pages/profile/index.js
T

119 lines
3.0 KiB
JavaScript

const authApi = require('../../api/auth')
const navigation = require('../../utils/navigation')
const permission = require('../../utils/permission')
Page({
data: {
userInfo: null,
currentShop: null,
permissionCodes: [],
roleLabel: '',
shopScopeText: '',
currentShopText: ''
},
onShow() {
if (!navigation.ensureLogin('进入个人中心')) {
return
}
const app = getApp()
const userInfo = app.globalData.userInfo
const currentShop = app.globalData.currentShop
const shopScopeText = userInfo
? (Array.isArray(userInfo.shopNames) && userInfo.shopNames.length
? userInfo.shopNames.join(' / ')
: (userInfo.shopName || '--'))
: '--'
this.setData({
userInfo,
currentShop,
permissionCodes: app.globalData.permissionCodes || [],
roleLabel: userInfo ? permission.getRoleLabel(userInfo.type) : '--',
shopScopeText,
currentShopText: currentShop ? `当前店铺:${currentShop.shopName}` : '当前为平台总览模式'
})
},
handleOpenOrders() {
navigation.switchTab('/pages/orders/index').catch(() => undefined)
},
handleOpenManage() {
navigation.switchTab('/pages/manage/index').catch(() => undefined)
},
async handleSwitchShop() {
const userInfo = this.data.userInfo
if (!userInfo || !userInfo.account) {
return
}
try {
const result = await authApi.getLoginShops(userInfo.account)
const shops = Array.isArray(result && result.shops) ? result.shops : []
const allowShoplessLogin = Boolean(result && result.allowShoplessLogin)
if (allowShoplessLogin && permission.isPlatformAdmin(userInfo)) {
wx.showToast({
title: '平台管理员固定为总览模式',
icon: 'none'
})
return
}
const items = shops.map(shop => ({
label: shop.shopName,
value: shop.id
}))
if (!items.length) {
wx.showToast({
title: '当前账号没有可切换的店铺',
icon: 'none'
})
return
}
wx.showActionSheet({
itemList: items.map(item => item.label),
success: async action => {
const target = items[action.tapIndex]
if (!target) {
return
}
try {
await getApp().switchAdminShop(target.value || undefined)
wx.showToast({
title: '店铺已切换',
icon: 'none'
})
this.onShow()
} catch (error) {
wx.showToast({
title: error.message || '切换店铺失败',
icon: 'none'
})
}
}
})
} catch (error) {
wx.showToast({
title: error.message || '店铺列表加载失败',
icon: 'none'
})
}
},
async handleLogout() {
try {
await authApi.logout().catch(() => undefined)
} finally {
getApp().clearAdminSession()
wx.reLaunch({
url: '/pages/login/index'
})
}
}
})