wms_mp_weixin/src/pages/mine/user-info.vue
2026-10-17 18:05:39 +08:00

771 lines
23 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="page">
<!-- 未登录:页内引导(**不自动跳转** —— 微信审核要求登录由用户主动触发) -->
<view v-if="!userStore.isLogin" class="card">
<empty-state text="登录后可查看个人信息" icon="account" :show-retry="false">
<template #action>
<up-button
type="primary"
shape="circle"
size="small"
text="去登录"
@click="goLogin"
></up-button>
</template>
</empty-state>
</view>
<template v-else>
<!-- ① 用户信息:身份信息来自 user.info 的 employees[](不改数据库、无额外接口) -->
<view class="card">
<view class="card__title">用户信息</view>
<view class="me">
<view class="me__avatar">
<image
v-if="headimgurl"
class="me__avatar-img"
:src="headimgurl"
mode="aspectFill"
></image>
<up-icon v-else name="account" color="#ffffff" size="36"></up-icon>
</view>
<view class="me__info">
<view class="me__name">{{ displayName }}</view>
<view class="me__desc">{{ identityDesc }}</view>
</view>
</view>
<up-cell-group v-if="identity" :border="false">
<up-cell title="姓名" :value="identity?.real_name || displayName" :border="false"></up-cell>
<up-cell title="身份类型" :value="userStore.roleTypeLabel || '-'" :border="false"></up-cell>
<up-cell title="所属组织" :value="identity?.store_name || '-'" :border="false"></up-cell>
<up-cell title="员工 ID" :value="identity ? String(identity.id) : '-'" :border="false"></up-cell>
<up-cell title="授权到期" :value="authEndText" :border="false"></up-cell>
<!-- 仅多身份时出现:身份切换入口 -->
<up-cell
v-if="userStore.hasMultipleEmployees"
title="切换身份"
:value="`${userStore.employees.length} 个可选`"
is-link
:border="false"
@click="onSwitchIdentity"
></up-cell>
</up-cell-group>
<view v-else class="card__note" @click="onRetryInfo">未获取到身份信息,点此重试</view>
</view>
<!-- ② 待绑定 WMS 账号(微信已登录但未绑定;绑定可跳过,跳过仍是免登录态) -->
<view v-if="userStore.isWechatUnbound" class="card">
<view class="card__title">待绑定 WMS 账号</view>
<view class="card__note">
当前微信尚未绑定 WMS 账号,绑定后即可使用入库、出库、盘点等全部功能。
</view>
<view class="card__action">
<up-button
type="primary"
shape="circle"
text="绑定 WMS 账号"
@click="bindVisible = true"
></up-button>
</view>
</view>
<!-- ③ 微信资料(后端 wechat.updateInfo 仅允许未绑定账号修改昵称) -->
<view v-if="userStore.canEditWechatProfile" class="card">
<view class="card__title">微信资料</view>
<up-cell-group :border="false">
<up-cell title="头像" is-link :border="false" @click="openProfileEditor">
<template #value>
<image
v-if="headimgurl"
class="cell-avatar"
:src="headimgurl"
mode="aspectFill"
></image>
<text v-else class="cell-avatar__empty">未设置</text>
</template>
</up-cell>
<up-cell
title="昵称"
:value="userStore.wechatNickname || '未设置'"
is-link
:border="false"
@click="openProfileEditor"
></up-cell>
</up-cell-group>
<view class="card__note">绑定 WMS 账号后,昵称与头像改由账号体系管理</view>
</view>
<!-- ④ 电脑端登录码(需微信登录态;账号密码登录的 token 后端不认) -->
<view v-if="userStore.canGetMyLoginCode" class="card">
<view class="card__title">电脑端登录码</view>
<up-cell
title="我的登录码"
value="点击查看"
is-link
:border="false"
@click="onShowLoginCode"
></up-cell>
<view class="card__note">在电脑端登录页凭此登录码登录本账号</view>
</view>
<!--
用户须知 / 隐私保护指引:已移到「我的」页(一级页常驻,**未登录也能看**)——
本页在未登录时不可达,放这里会导致未登录用户根本找不到隐私入口。
-->
<!-- ⑤ 退出登录(从「我的」页移来:细项统一收在本页) -->
<view class="footer">
<up-button type="info" :plain="true" text="退出登录" @click="onLogout"></up-button>
</view>
</template>
<!-- 绑定账号弹窗:与登录页共用 wechat-bind-form(这里不提供「跳过」,是用户主动点的) -->
<up-popup :show="bindVisible" mode="center" :round="16" @close="bindVisible = false">
<view class="bind-popup">
<view class="bind-popup__title">绑定 WMS 账号</view>
<view class="bind-popup__desc">输入 WMS 账号密码完成绑定,绑定后可使用全部功能</view>
<wechat-bind-form submit-text="绑定" @success="onBindSuccess"></wechat-bind-form>
<view class="bind-popup__cancel" @click="bindVisible = false">取消</view>
</view>
</up-popup>
<!-- 编辑微信资料:头像走微信「头像昵称填写能力」,昵称走 wechat.updateInfo -->
<up-popup :show="profileVisible" mode="bottom" :round="16" @close="closeProfileEditor">
<view class="profile-popup">
<view class="profile-popup__title">编辑微信资料</view>
<view class="profile-popup__field">
<view class="profile-popup__label">头像</view>
<!-- ⚠️ 微信已回收 getUserProfile,头像必须由用户主动选(open-type=chooseAvatar) -->
<button
class="avatar-picker"
open-type="chooseAvatar"
@chooseavatar="onChooseAvatar"
>
<image
v-if="headimgurl"
class="avatar-picker__img"
:src="headimgurl"
mode="aspectFill"
></image>
<view v-else class="avatar-picker__empty">
<up-icon name="camera-fill" color="#909399" size="30"></up-icon>
</view>
</button>
<text class="profile-popup__hint">点击头像从微信选择,自动裁剪为 PNG 后上传</text>
</view>
<view class="profile-popup__field">
<view class="profile-popup__label">昵称</view>
<!-- type=nickname:点击时由微信提供昵称填写面板 -->
<input
v-model="nicknameDraft"
class="nickname-input"
type="nickname"
placeholder="点击输入框填写微信昵称"
placeholder-style="color: #c0c4cc"
/>
</view>
<up-button
type="primary"
shape="circle"
text="保存昵称"
:loading="profileSaving"
@click="onSaveNickname"
></up-button>
<view class="profile-popup__cancel" @click="closeProfileEditor">关闭</view>
</view>
</up-popup>
<!-- 登录码弹窗:后端语义是「已有则返回」,码稳定不变 ⇒ 取一次即可反复展示 / 复制 -->
<up-popup
:show="loginCodeVisible"
mode="center"
:round="16"
@close="loginCodeVisible = false"
>
<view class="code-popup">
<view class="code-popup__title">我的登录码</view>
<view class="code-popup__desc">在电脑端登录页粘贴此登录码即可登录本账号</view>
<view class="code-popup__value">
<text v-if="loginCodeLoading" class="code-popup__value--tip">获取中…</text>
<text v-else-if="loginCode" user-select>{{ loginCode }}</text>
<text v-else class="code-popup__value--tip">获取失败,请关闭后重试</text>
</view>
<up-button
type="primary"
shape="circle"
text="复制登录码"
:disabled="!loginCode"
@click="onCopyLoginCode"
></up-button>
<view class="code-popup__cancel" @click="loginCodeVisible = false">关闭</view>
</view>
</up-popup>
<!--
登录抽屉(登录入口)
⚠️ 硬约束:`usePageAuth.goLogin()` 是「**就地弹抽屉**」而不是跳页(见 `composables/usePageAuth.ts`),
它只改 store 的 `authView`;**每个会调 `goLogin()` / `requireLogin()` 的页面都必须自己挂一份
`<auth-modal />`**,否则会出现「点了去登录没反应、回其他页面反而弹出登录框」(因为抽屉内容没了)。
同款挂载点:`pages/index/index.vue`、`pages/mine/mine.vue`、`pages/login/confirm-login.vue` 等。
-->
<auth-modal></auth-modal>
<!-- 头像规格化用的画布:旧版 canvas-id API 取不到未渲染的节点,故移到屏外(不能用 v-if) -->
<canvas :canvas-id="AVATAR_CANVAS_ID" class="avatar-canvas"></canvas>
</view>
</template>
<script setup lang="ts">
/**
* 用户信息(二级页)
*
* 从「我的」页拆出来:那里只保留菜单入口,细项全部收进本页,避免首页式堆叠。
* 本页按顺序包含四块:
* ① 用户信息(身份信息 + 切换身份)
* ② 待绑定 WMS 账号(未绑定时)
* ③ 微信资料(未绑定时可改昵称 / 头像)
* ④ 电脑端登录码(微信登录态可查看 / 复制)
*
* 登录策略:`optional` —— 未登录也能进(页内引导登录,不自动跳转)。
*/
import { computed, getCurrentInstance, ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { useUserStore } from '@/store/modules/user'
import { usePageAuth } from '@/composables/usePageAuth'
import { getToken } from '@/utils/auth'
import { AVATAR_CANVAS_ID, avatarFileToPngBase64 } from '@/utils/avatar'
import WechatBindForm from '@/components/wechat-bind-form/wechat-bind-form.vue'
import EmptyState from '@/components/empty-state/empty-state.vue'
import AuthModal from '@/components/auth-modal/auth-modal.vue'
const userStore = useUserStore()
/** 登录入口统一走 usePageAuth(本页是 optional:登录**就地弹抽屉**,不跳页) */
const { goLogin } = usePageAuth('optional')
/** 旧版画布 / canvasToTempFilePath 需要页面实例,否则取不到画布 */
const instance = getCurrentInstance()
/* ───────────── ① 用户信息 ───────────── */
const headimgurl = computed(() => userStore.headimgurl)
const identity = computed(() => userStore.currentEmployee)
/** 展示名:未绑定微信态优先显示用户设置的微信昵称 */
const displayName = computed(() => {
if (userStore.isWechatUnbound) return userStore.wechatNickname || '微信用户'
return userStore.userName || '已登录'
})
/** 身份摘要:身份类型 · 所属组织 */
const identityDesc = computed(() => {
const parts = [userStore.roleTypeLabel, identity.value?.store_name].filter(Boolean)
return parts.length ? parts.join(' · ') : '已登录'
})
/** 授权到期展示:后端 null / 空 表示长期有效 */
const authEndText = computed(() => {
const end = identity.value?.auth_end
if (!end) return '长期有效'
return String(end).slice(0, 10)
})
/**
* 手动重试:强制重拉 `user.info`
*
* 正常路径下页面**不主动请求** —— 数据由 `App.vue` 启动时(或登录 / 绑定 / 切身份时)
* 统一拉好存在 store,本页只负责渲染。只有拉取失败时用户才会看到重试入口。
*/
const onRetryInfo = () => {
userStore.getInfo(true).catch(() => {
// 失败提示由 request 层统一 toast
})
}
/** 选择要切换到的身份(入口仅多身份时出现;用 actionSheet 避免额外页面) */
const onSwitchIdentity = () => {
const list = userStore.employees
if (list.length < 2) return
// 微信 showActionSheet 的 itemList 最多 6 项:超出时保证「当前身份」仍在列表里
const MAX_ITEMS = 6
let visible = list
if (list.length > MAX_ITEMS) {
const current = list.find((item) => item.id === userStore.currentEmployeeId)
const rest = list.filter((item) => item.id !== userStore.currentEmployeeId)
visible = (current ? [current, ...rest] : rest).slice(0, MAX_ITEMS)
uni.$u.toast(`身份较多,仅显示前 ${MAX_ITEMS} 个`)
}
const itemList = visible.map((item) => {
const label =
[item.store_name, item.real_name].filter(Boolean).join(' · ') || `身份 ${item.id}`
return item.id === userStore.currentEmployeeId ? `${label}(当前)` : label
})
uni.showActionSheet({
itemList,
success: (res) => {
const target = visible[res.tapIndex]
if (!target || target.id === userStore.currentEmployeeId) return
doChangeRole(target.id)
}
})
}
/** 执行切换:换发 token → 重拉 user.info(失败提示由 request 层统一处理) */
const doChangeRole = async (roleId: number) => {
uni.showLoading({ title: '切换中', mask: true })
try {
await userStore.changeRole(roleId)
uni.hideLoading()
uni.$u.toast('身份已切换')
} catch (err) {
uni.hideLoading()
// store 内已回滚 / 以服务端为准;提示优先用 store 给出的具体原因
// (如「身份切换未生效」—— 比笼统的「切换失败」好定位得多)
uni.$u.toast(err instanceof Error && err.message ? err.message : '切换失败,请稍后再试')
}
}
/* ───────────── ② 待绑定 WMS 账号 ───────────── */
/** 绑定弹窗可见性 */
const bindVisible = ref(false)
/** 绑定成功:`store.wechatBind` 内部已重拉 user.info,这里只需关弹窗并提示 */
const onBindSuccess = () => {
bindVisible.value = false
uni.$u.toast('绑定成功')
}
/* ───────────── ③ 微信资料(昵称 / 头像)───────────── */
/** 编辑弹窗可见性 */
const profileVisible = ref(false)
/** 昵称提交中(防重复点击) */
const profileSaving = ref(false)
/** 昵称输入草稿(打开弹窗时从 store 回填;取消 / 关闭即丢弃) */
const nicknameDraft = ref('')
const openProfileEditor = () => {
nicknameDraft.value = userStore.wechatNickname
profileVisible.value = true
}
const closeProfileEditor = () => {
profileVisible.value = false
}
/**
* 选择头像(微信「头像昵称填写能力」)
*
* `chooseAvatar` 只给临时文件路径,且不保证是 PNG / 体积达标,
* 故先本地裁剪转码为 PNG base64 再上传(后端只收 PNG 且 < 500K)。
*/
const onChooseAvatar = async (event: { detail?: { avatarUrl?: string } }) => {
const tempPath = event?.detail?.avatarUrl || ''
if (!tempPath) {
// 取消选择不会触发本事件;能走到这里多半是**未在《隐私保护指引》声明「头像昵称」**
//(平台会直接禁用 chooseAvatar,返回空路径)→ 给一个可执行的提示,不要静默失败
uni.$u.toast('未获取到头像,如无法选择请在「…」→设置中同意隐私协议')
return
}
uni.showLoading({ title: '处理中', mask: true })
let image = ''
try {
image = await avatarFileToPngBase64(tempPath, instance?.proxy)
} catch (err) {
uni.hideLoading()
// 本地裁剪 / 转码失败(图片读不出、压到最小仍超 500K)才在这里提示
uni.$u.toast(err instanceof Error ? err.message : '头像处理失败')
return
}
try {
await userStore.uploadWechatHeadimg(image)
} catch {
// 接口失败提示由 request 层统一 toast,这里只负责关 loading
return
} finally {
uni.hideLoading()
}
uni.$u.toast('头像已更新')
}
/**
* 保存昵称(`wechat.updateInfo`,字段可部分更新)
*
* 只在**内容真的变了**时发请求:后端对未绑定账号会直接覆写,重复提交无意义。
*/
const onSaveNickname = async () => {
const nickname = nicknameDraft.value.trim()
if (!nickname) {
uni.$u.toast('请先填写昵称')
return
}
if (nickname === userStore.wechatNickname) {
uni.$u.toast('昵称未变化')
return
}
profileSaving.value = true
try {
await userStore.updateWechatProfile({ nickname })
profileVisible.value = false
uni.$u.toast('昵称已更新')
} catch {
// 失败提示由 request 层统一 toast(如已绑定账号不允许修改)
} finally {
profileSaving.value = false
}
}
/* ───────────── ④ 电脑端登录码 ───────────── */
/** 登录码弹窗可见性 */
const loginCodeVisible = ref(false)
/** 登录码请求中 */
const loginCodeLoading = ref(false)
const loginCode = computed(() => userStore.myLoginCode)
/**
* 查看「我的登录码」
*
* 后端语义是「没有才生成、已有则返回」⇒ 同一个微信账号的码**稳定不变**,
* state 里已有就直接展示,不再重复请求。
*/
const onShowLoginCode = async () => {
loginCodeVisible.value = true
if (loginCode.value) return
loginCodeLoading.value = true
try {
await userStore.getMyLoginCode()
} catch {
// 失败提示由 request 层统一 toast(非微信登录态 / 登录态失效时后端返回 401)
} finally {
loginCodeLoading.value = false
}
}
/**
* 复制登录码(微信自带「内容已复制」提示,这里不再重复 toast)
*
* ⚠️ 复制属于隐私接口(官方清单里「读取你的剪切板」= wx.setClipboardData):
* 后台未声明或用户拒绝了隐私授权时这里会直接失败,必须给提示。
*/
const onCopyLoginCode = () => {
if (!loginCode.value) return
uni.setClipboardData({
data: loginCode.value,
fail: () => uni.$u.toast('复制失败,请确认已同意《隐私保护指引》')
})
}
/* ───────────── ⑤ 退出登录 ───────────── */
/**
* 退出登录
*
* 退出后**停留在本页**(页面自动切回未登录引导态),不强制跳登录页
* —— 与「登录由用户主动触发」的约定保持一致。
*/
const onLogout = () => {
uni.showModal({
title: '提示',
content: '确定要退出登录吗?',
success: async (res) => {
if (!res.confirm) return
// logout 内部已做兜底:后端不可用也会清理本地登录态
await userStore.logout()
// 退出后本地登录态已清(含登录渠道 / 登录码),本页会自动切回「未登录」引导态
uni.$u.toast('已退出登录')
}
})
}
// onShow:登录态变化(如从登录页返回)后与本地存储对齐;
// 数据由 App / 登录 / 绑定 / 切身份统一灌入 store,故本页不在此处拉 user.info
onShow(() => {
userStore.token = getToken()
})
</script>
<style lang="scss" scoped>
.page {
min-height: 100vh;
background-color: #f5f6fa;
}
.card {
margin: 24rpx;
background-color: #ffffff;
border-radius: 16rpx;
overflow: hidden;
&__title {
padding: 24rpx 32rpx 8rpx;
font-size: 28rpx;
font-weight: 600;
color: #303133;
}
&__note {
padding: 0 32rpx 24rpx;
font-size: 24rpx;
color: #909399;
text-align: center;
}
&__action {
margin: 0 32rpx 32rpx;
}
}
/* 顶部「我是谁」概要:与「我的」页头部同款蓝底样式,保持视觉一致 */
.me {
display: flex;
align-items: center;
padding: 24rpx 32rpx;
background-color: #2979ff;
&__avatar {
width: 96rpx;
height: 96rpx;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.25);
display: flex;
align-items: center;
justify-content: center;
&-img {
width: 96rpx;
height: 96rpx;
border-radius: 50%;
}
}
&__info {
flex: 1;
margin-left: 24rpx;
}
&__name {
font-size: 34rpx;
font-weight: 600;
color: #ffffff;
}
&__desc {
margin-top: 8rpx;
font-size: 24rpx;
color: rgba(255, 255, 255, 0.8);
}
}
.cell-avatar {
width: 72rpx;
height: 72rpx;
border-radius: 50%;
background-color: #f2f3f5;
&__empty {
font-size: 26rpx;
color: #909399;
}
}
.footer {
padding: 24rpx 32rpx 48rpx;
}
.avatar-canvas {
/* 旧版 canvas-id API 取不到未渲染的节点,故移到屏幕外而不是 v-if / display:none */
position: fixed;
left: -9999px;
top: -9999px;
width: 400px;
height: 400px;
}
.bind-popup {
width: 620rpx;
padding: 48rpx 40rpx 32rpx;
box-sizing: border-box;
&__title {
font-size: 34rpx;
font-weight: 600;
color: #303133;
text-align: center;
}
&__desc {
margin-top: 12rpx;
font-size: 24rpx;
color: #909399;
text-align: center;
}
&__cancel {
display: flex;
align-items: center;
justify-content: center;
margin-top: 28rpx;
padding: 16rpx 0;
font-size: 28rpx;
color: #909399;
}
}
.profile-popup {
padding: 40rpx 40rpx 32rpx;
&__title {
font-size: 34rpx;
font-weight: 600;
color: #303133;
text-align: center;
}
&__field {
margin-top: 32rpx;
text-align: center;
}
&__label {
font-size: 26rpx;
color: #909399;
}
&__hint {
display: block;
margin-top: 12rpx;
font-size: 22rpx;
color: #c0c4cc;
}
&__cancel {
display: flex;
align-items: center;
justify-content: center;
margin-top: 24rpx;
padding: 16rpx 0;
font-size: 28rpx;
color: #909399;
}
}
.avatar-picker {
display: block;
width: 176rpx;
height: 176rpx;
margin: 16rpx auto 0;
padding: 0;
line-height: 1;
background-color: transparent;
border: none;
border-radius: 50%;
overflow: hidden;
/* 去掉小程序 button 的默认边框 */
&::after {
border: none;
}
&__img {
display: block;
width: 176rpx;
height: 176rpx;
border-radius: 50%;
background-color: #f2f3f5;
}
&__empty {
width: 176rpx;
height: 176rpx;
border-radius: 50%;
background-color: #f2f3f5;
display: flex;
align-items: center;
justify-content: center;
}
}
.nickname-input {
margin-top: 16rpx;
padding: 0 24rpx;
height: 88rpx;
box-sizing: border-box;
font-size: 30rpx;
/* 显式指定文字色:不依赖主题变量(小程序夜间模式下主题色会变浅) */
color: #303133;
background-color: #f5f6fa;
border-radius: 12rpx;
text-align: center;
}
.code-popup {
width: 620rpx;
padding: 48rpx 40rpx 32rpx;
box-sizing: border-box;
&__title {
font-size: 34rpx;
font-weight: 600;
color: #303133;
text-align: center;
}
&__desc {
margin-top: 12rpx;
font-size: 24rpx;
color: #909399;
text-align: center;
}
&__value {
margin: 32rpx 0;
padding: 24rpx;
max-height: 260rpx;
overflow: hidden;
font-size: 24rpx;
line-height: 1.6;
color: #303133;
/* 登录码是长 base64,必须允许任意位置断行,否则会撑破弹窗 */
word-break: break-all;
text-align: center;
background-color: #f5f6fa;
border-radius: 12rpx;
&--tip {
color: #909399;
}
}
&__cancel {
display: flex;
align-items: center;
justify-content: center;
margin-top: 24rpx;
padding: 16rpx 0;
font-size: 28rpx;
color: #909399;
}
}
</style>