Files
SupplierSystem/miniprogram/pages/mine/mine.js
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

157 lines
4.9 KiB
JavaScript

const { ROLE_LABELS } = require('../../utils/constants');
const { getSession, setSession, clearSession } = require('../../utils/auth');
const { fetchMe } = require('../../utils/request');
const { buildUnionModules, resolveDisplayMobileRoles, normalizeRoleList } = require('../../modules/registry');
const { buildMergedFeaturePatch } = require('../../utils/roleFeatures');
const { goLogin } = require('../../utils/nav');
const { clearUnitDictionaryCache } = require('../../utils/units');
function mapRoleOptions(roles) {
return (roles || []).map((item) => {
if (typeof item === 'string') {
return { value: item, label: ROLE_LABELS[item] || item };
}
return {
value: item.role_type,
label: item.label || ROLE_LABELS[item.role_type] || item.role_type,
};
});
}
function buildCapabilityHint(roleOptions) {
if (!roleOptions || roleOptions.length <= 1) return '';
const labels = roleOptions.map((item) => item.label || ROLE_LABELS[item.value] || item.value);
return `可用功能覆盖:${labels.join('、')}`;
}
function avatarFromName(name) {
const text = String(name || '用').trim();
return text ? text.slice(0, 1) : '用';
}
function buildSupplierCategoryDisplay(profile, roleType) {
if (roleType !== 'supplier') {
return { supplierCategoryLabels: [] };
}
const raw = profile?.supplier?.categories;
const list = Array.isArray(raw) ? raw : [];
const supplierCategoryLabels = list
.map((row) => {
const name = String(row?.category_name || row?.category_code || '').trim();
if (!name) return null;
return {
name,
primary: row?.is_primary === true || row?.is_primary === 1 || row?.is_primary === 't',
};
})
.filter(Boolean);
return { supplierCategoryLabels };
}
function resolveHeroBranchText(profile, roleType) {
if (roleType === 'supplier') {
const companyName = String(profile?.supplier?.company_name || '').trim();
if (companyName) return companyName;
}
if (profile?.branch_name) return profile.branch_name;
if (profile?.branch_code) return '已绑定分公司';
return '已登录';
}
Page({
data: {
needLogin: true,
roleType: '',
roleLabel: '',
displayName: '',
avatarChar: '用',
branchText: '',
supplierCategoryLabels: [],
availableRoles: [],
availableRoleValues: [],
canSwitchRole: false,
roleSwitchHint: '',
menuModules: [],
supplierStatusEnabled: false,
photoInspectionEnabled: false,
pickingEnabled: false,
deliveryEnabled: true,
bidEnabled: false,
},
onShow() {
const session = getSession();
if (!session?.access_token) {
this.setData({ needLogin: true });
return;
}
this.refreshFromSession(session);
this.refreshSupplierProfileIfNeeded(session);
},
refreshFromSession(session) {
const profile = session.profile || {};
const displayName = profile.full_name || profile.username || '用户';
const roleType = session.role_type;
const display = resolveDisplayMobileRoles(session.available_roles, roleType, session);
const roleValues = display.roles;
const roles = mapRoleOptions(roleValues);
const patch = {
needLogin: false,
roleType: display.roleType,
roleLabel: ROLE_LABELS[display.roleType] || roleType,
displayName,
avatarChar: avatarFromName(displayName),
branchText: resolveHeroBranchText(profile, display.roleType),
...buildSupplierCategoryDisplay(profile, display.roleType),
availableRoles: roles,
availableRoleValues: roleValues,
canSwitchRole: false,
roleSwitchHint: buildCapabilityHint(roles),
permissions: session.permissions || [],
isSuperAdmin: session.is_super_admin === true || session.isSuperAdmin === true,
...buildMergedFeaturePatch(roleValues, session, display.roleType),
};
this.setData(patch, () => {
this.setData({
menuModules: buildUnionModules(this.data, roleValues),
});
});
},
async refreshSupplierProfileIfNeeded(session) {
const display = resolveDisplayMobileRoles(session.available_roles, session.role_type, session);
if (display.roleType !== 'supplier') return;
try {
const me = await fetchMe();
if (!me?.profile) return;
const nextSession = {
...(getSession() || session),
profile: me.profile,
role_type: me.role_type || session.role_type,
};
if (Array.isArray(me.available_roles)) {
nextSession.available_roles = normalizeRoleList(
me.available_roles,
nextSession.role_type
);
}
setSession(nextSession);
this.refreshFromSession(nextSession);
} catch {
// 保留本地 session 档案
}
},
onLogout() {
clearUnitDictionaryCache();
clearSession();
wx.showToast({ title: '已退出', icon: 'success' });
setTimeout(() => goLogin(), 300);
},
goLogin() {
wx.navigateTo({ url: '/pages/login/login' });
},
});