fieldPermissions.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <template>
  2. <view class="page">
  3. <u-navbar class="nav-bar" title="仓库字段权限配置" :autoBack="true" :placeholder="true" v-hideNav></u-navbar>
  4. <view class="content">
  5. <view class="tip-card">
  6. <u-icon name="setting" size="18" color="#108cff"></u-icon>
  7. <text class="tip-text">为指定角色配置仓库各字段的「可查看」「可编辑」权限,以及「编辑」「删除」按钮权限。保存后该角色用户将按此配置在列表/详情中看到并可编辑对应字段、使用对应按钮。</text>
  8. </view>
  9. <!-- 选择角色 -->
  10. <view class="section role-section">
  11. <view class="section-title">选择角色</view>
  12. <view v-if="roleList.length > 0" class="role-select-wrap">
  13. <picker :value="roleIndex" :range="roleList" range-key="roleName" @change="onRoleChange">
  14. <view class="picker-inner">
  15. <text class="picker-text">{{ currentRoleName }}</text>
  16. <u-icon name="arrow-down" size="14" color="#999"></u-icon>
  17. </view>
  18. </picker>
  19. </view>
  20. <view v-else class="role-input-wrap">
  21. <view class="input-row">
  22. <text class="input-label">角色ID</text>
  23. <u--input v-model="formRoleId" type="number" placeholder="请输入角色ID" border="surround"></u--input>
  24. </view>
  25. <view class="input-row">
  26. <text class="input-label">角色Key</text>
  27. <u--input v-model="formRoleKey" placeholder="如 WAREHOUSER" border="surround"></u--input>
  28. </view>
  29. </view>
  30. <view class="btn-row">
  31. <u-button type="error" plain size="small" :disabled="!currentRoleId"
  32. @click="clearConfig">清空该角色配置</u-button>
  33. <u-button type="primary" size="small" :loading="loadConfigLoading"
  34. @click="loadConfig">加载配置</u-button>
  35. </view>
  36. </view>
  37. <!-- 字段权限列表(可编辑) -->
  38. <view class="section">
  39. <view class="section-head">
  40. <text class="section-title">字段权限</text>
  41. <view class="btn-group">
  42. <u-button type="success" plain size="mini"
  43. :disabled="!currentRoleId || permissionList.length === 0" @click="openAllPermissions">一键全开</u-button>
  44. <u-button type="error" plain size="mini"
  45. :disabled="!currentRoleId || permissionList.length === 0" @click="closeAllPermissions">一键全关</u-button>
  46. <u-button type="primary" size="mini" :loading="saveLoading"
  47. :disabled="!currentRoleId || permissionList.length === 0" @click="saveConfig">保存配置</u-button>
  48. </view>
  49. </view>
  50. <view v-if="loadConfigLoading" class="loading-wrap">
  51. <u-loading-icon mode="circle" size="36"></u-loading-icon>
  52. <text class="loading-text">加载中...</text>
  53. </view>
  54. <scroll-view v-else class="list-wrap" scroll-y enable-back-to-top>
  55. <view class="field-list">
  56. <view v-for="(item, index) in permissionList" :key="item.fieldName" class="field-item">
  57. <view class="field-name-wrap">
  58. <text class="field-name">{{ fieldLabel(item.fieldName) }}</text>
  59. <!-- <text class="field-key">{{ item.fieldName }}</text> -->
  60. </view>
  61. <view class="field-switches">
  62. <view class="switch-cell">
  63. <text class="switch-label">查看</text>
  64. <u-switch v-model="item.read" size="20" active-color="#108cff"></u-switch>
  65. </view>
  66. <view class="switch-cell">
  67. <text class="switch-label">编辑</text>
  68. <u-switch v-model="item.edit" size="20" active-color="#09bb07"></u-switch>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. <view v-if="permissionList.length === 0 && !loadConfigLoading" class="empty-wrap">
  74. <u-empty text="请先选择角色并点击「加载配置」" mode="list"></u-empty>
  75. </view>
  76. </scroll-view>
  77. </view>
  78. <!-- 按钮权限 -->
  79. <view class="section">
  80. <view class="section-head">
  81. <text class="section-title">按钮权限</text>
  82. <view class="btn-group">
  83. <u-button type="primary" size="mini" :loading="saveLoading"
  84. :disabled="!currentRoleId || !buttonPermissionList.length" @click="saveButtonConfig">保存配置</u-button>
  85. </view>
  86. </view>
  87. <view class="button-list">
  88. <view class="field-item" v-for="item in buttonPermissionList" :key="item.buttonKey">
  89. <view class="field-name-wrap">
  90. <text class="field-name">{{ item.label }}</text>
  91. </view>
  92. <view class="field-switches">
  93. <u-switch v-model="item.enabled" size="20" active-color="#09bb07"></u-switch>
  94. </view>
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. </view>
  100. </template>
  101. <script>
  102. import {updateFieldPermissions,updateButtonPermissions} from "@/utils/util.js";
  103. const FIELD_LABELS = {
  104. goodPicFileList: '商品图片', desc: '商品描述', origin: '来源', type: '商品分类', dictLabel: '品牌',
  105. series: '系列', model: '型号', dialType: '机芯类型', caseMaterial: '表壳材质', dialDiameter: '表盘直径',
  106. material: '材质', size: '尺寸', yardage: '尺码', productCondition: '商品成色', detailPicFileList: '细节图',
  107. title: '商品标题', productNo: '商品货号', productAttribute: '商品属性', stock: '库存数量', price: '官方指导价',
  108. costPrice: '总成本价', peerPrice: '同行价格', actualPrice: '实价', agentPrice: '代理价格', salePrice: '销售价格',
  109. productPosition: '商品位置', watchYear: '手表年份', identifyingPerson: '鉴定人员', stockStatus: '是否入库',
  110. recycleType: '回收类型', recycleSituation: '回收情况', label: '标签', recyclePerson: '回收人员',
  111. recycleBottomDesc: '回收留底描述', recycleBottomFileList: '回收留底图', recycleTime: '回收时间',
  112. indentifyCode: '独立编码', targetAudience: '适用人群', productCard: '商品保卡', cardYear: '保卡年份',
  113. productCardPicFileList: '保卡图片或独立编码照片留底', productTag: '商品标签', productAttachment: '商品附件',
  114. productDesc: '备注', productDescPicFileList: '备注图片', payType: '付款方式',
  115. };
  116. export default {
  117. data() {
  118. return {
  119. roleList: [],
  120. roleIndex: 0,
  121. formRoleId: '',
  122. formRoleKey: '',
  123. loadConfigLoading: false,
  124. saveLoading: false,
  125. permissionList: [],
  126. // 按钮权限:编辑、删除、上/下架、开单、查看日志、解/锁单,与后端 buttonKey 一致
  127. buttonPermissionList: [
  128. { buttonKey: 'edit', label: '编辑', enabled: false },
  129. { buttonKey: 'delete', label: '删除', enabled: false },
  130. { buttonKey: 'shelf', label: '上/下架', enabled: false },
  131. { buttonKey: 'openOrder', label: '开单', enabled: false },
  132. { buttonKey: 'viewLog', label: '查看日志', enabled: false },
  133. { buttonKey: 'lockOrder', label: '解/锁单', enabled: false },
  134. ],
  135. };
  136. },
  137. computed: {
  138. currentRoleId() {
  139. if (this.roleList.length > 0) {
  140. const r = this.roleList[this.roleIndex];
  141. return r ? r.roleId : null;
  142. }
  143. const id = parseInt(this.formRoleId, 10);
  144. return isNaN(id) ? null : id;
  145. },
  146. currentRoleKey() {
  147. if (this.roleList.length > 0) {
  148. const r = this.roleList[this.roleIndex];
  149. return r ? r.roleKey || '' : '';
  150. }
  151. return this.formRoleKey || '';
  152. },
  153. currentRoleName() {
  154. if (this.roleList.length > 0) {
  155. const r = this.roleList[this.roleIndex];
  156. return r ? (r.roleName || '请选择角色') : '请选择角色';
  157. }
  158. return this.currentRoleId ? `角色ID: ${this.currentRoleId}` : '请填写角色ID并加载';
  159. },
  160. },
  161. onLoad() { },
  162. onShow() {
  163. this.fetchRoleList();
  164. },
  165. methods: {
  166. fieldLabel(name) {
  167. return FIELD_LABELS[name] || name;
  168. },
  169. fetchRoleList() {
  170. uni.$u.api.getRoleOptionSelectAll({ custom: { loading: false } })
  171. .then((res) => {
  172. const list = (res && res.data) ? (Array.isArray(res.data) ? res.data : []) : [];
  173. this.roleList = list.map((r) => ({ roleId: r.roleId, roleName: r.roleName || r.roleKey || '', roleKey: r.roleKey || '' }));
  174. this.roleIndex = 0;
  175. })
  176. .catch(() => {
  177. this.roleList = [];
  178. });
  179. },
  180. onRoleChange(e) {
  181. this.roleIndex = e.detail.value;
  182. this.permissionList = [];
  183. },
  184. loadConfig() {
  185. const roleId = this.currentRoleId;
  186. if (roleId == null) {
  187. uni.$u.toast('请选择或填写角色');
  188. return;
  189. }
  190. this.loadConfigLoading = true;
  191. Promise.all([
  192. uni.$u.api.wareHouseFieldPermissionsConfigGet({ roleId }),
  193. uni.$u.api.wareHouseButtonPermissionsConfigGet({ roleId }),
  194. ])
  195. .then(([fieldRes, buttonRes]) => {
  196. if (fieldRes && fieldRes.data && Array.isArray(fieldRes.data)) {
  197. this.permissionList = fieldRes.data.map((p) => ({
  198. fieldName: p.fieldName,
  199. read: !!p.read,
  200. edit: !!p.edit,
  201. }));
  202. } else {
  203. this.permissionList = [];
  204. }
  205. if (buttonRes && buttonRes.data && Array.isArray(buttonRes.data)) {
  206. const map = {};
  207. buttonRes.data.forEach((b) => { map[b.buttonKey] = !!b.enabled; });
  208. const defaultButtons = [
  209. { buttonKey: 'edit', label: '编辑', enabled: !!map['edit'] },
  210. { buttonKey: 'delete', label: '删除', enabled: !!map['delete'] },
  211. { buttonKey: 'shelf', label: '上/下架', enabled: !!map['shelf'] },
  212. { buttonKey: 'openOrder', label: '开单', enabled: !!map['openOrder'] },
  213. { buttonKey: 'viewLog', label: '查看日志', enabled: !!map['viewLog'] },
  214. { buttonKey: 'lockOrder', label: '解/锁单', enabled: !!map['lockOrder'] },
  215. ];
  216. this.buttonPermissionList = defaultButtons;
  217. }
  218. })
  219. .catch(() => {
  220. this.permissionList = [];
  221. uni.$u.toast('加载失败');
  222. })
  223. .finally(() => {
  224. this.loadConfigLoading = false;
  225. });
  226. },
  227. updateFieldPermissions:updateFieldPermissions,
  228. updateButtonPermissions:updateButtonPermissions,
  229. saveConfig() {
  230. const roleId = this.currentRoleId;
  231. const roleKey = this.currentRoleKey;
  232. if (roleId == null) {
  233. uni.$u.toast('请选择或填写角色');
  234. return;
  235. }
  236. if (!this.permissionList.length) {
  237. uni.$u.toast('请先加载配置');
  238. return;
  239. }
  240. this.saveLoading = true;
  241. uni.$u.api.wareHouseFieldPermissionsConfigSave({
  242. roleId,
  243. roleKey,
  244. permissions: this.permissionList.map((p) => ({ fieldName: p.fieldName, read: p.read, edit: p.edit })),
  245. })
  246. .then(() => {
  247. uni.$u.toast('保存成功');
  248. })
  249. .catch(() => {
  250. uni.$u.toast('保存失败');
  251. })
  252. .finally(() => {
  253. this.saveLoading = false;
  254. });
  255. this.updateFieldPermissions();
  256. },
  257. saveButtonConfig() {
  258. const roleId = this.currentRoleId;
  259. const roleKey = this.currentRoleKey;
  260. if (roleId == null) {
  261. uni.$u.toast('请选择或填写角色');
  262. return;
  263. }
  264. this.saveLoading = true;
  265. uni.$u.api.wareHouseButtonPermissionsConfigSave({
  266. roleId,
  267. roleKey,
  268. permissions: this.buttonPermissionList.map((p) => ({ buttonKey: p.buttonKey, enabled: p.enabled })),
  269. })
  270. .then(() => {
  271. uni.$u.toast('按钮权限保存成功');
  272. })
  273. .catch(() => {
  274. uni.$u.toast('保存失败');
  275. })
  276. .finally(() => {
  277. this.saveLoading = false;
  278. });
  279. this.updateButtonPermissions();
  280. },
  281. clearConfig() {
  282. if (!this.currentRoleId) return;
  283. uni.showModal({
  284. title: '确认清空',
  285. content: '将删除该角色下所有字段权限与按钮权限配置,确定吗?',
  286. success: (res) => {
  287. if (res.confirm) {
  288. uni.$u.api.wareHouseFieldPermissionsConfigDeleteByRole(this.currentRoleId).then(() => {
  289. uni.$u.toast('已清空');
  290. this.permissionList = [];
  291. this.buttonPermissionList = [
  292. { buttonKey: 'edit', label: '编辑', enabled: false },
  293. { buttonKey: 'delete', label: '删除', enabled: false },
  294. { buttonKey: 'shelf', label: '上/下架', enabled: false },
  295. { buttonKey: 'openOrder', label: '开单', enabled: false },
  296. { buttonKey: 'viewLog', label: '查看日志', enabled: false },
  297. { buttonKey: 'lockOrder', label: '解/锁单', enabled: false },
  298. ];
  299. }).catch(() => uni.$u.toast('清空失败'));
  300. }
  301. },
  302. });
  303. },
  304. // 一键打开所有权限
  305. openAllPermissions() {
  306. this.permissionList.forEach(item => {
  307. item.read = true;
  308. item.edit = true;
  309. });
  310. uni.$u.toast('已打开所有权限');
  311. },
  312. // 一键关闭所有权限
  313. closeAllPermissions() {
  314. this.permissionList.forEach(item => {
  315. item.read = false;
  316. item.edit = false;
  317. });
  318. uni.$u.toast('已关闭所有权限');
  319. },
  320. },
  321. };
  322. </script>
  323. <style lang="scss" scoped>
  324. .page {
  325. min-height: 100vh;
  326. background: #f5f6f8;
  327. display: flex;
  328. flex-direction: column;
  329. }
  330. .nav-bar {
  331. border-bottom: 1px solid #eee;
  332. }
  333. .content {
  334. flex: 1;
  335. display: flex;
  336. flex-direction: column;
  337. padding: 24rpx;
  338. box-sizing: border-box;
  339. }
  340. .tip-card {
  341. display: flex;
  342. align-items: flex-start;
  343. gap: 12rpx;
  344. background: #e8f4ff;
  345. border-radius: 16rpx;
  346. padding: 20rpx 24rpx;
  347. margin-bottom: 24rpx;
  348. border: 1px solid rgba(16, 140, 255, 0.2);
  349. .tip-text {
  350. font-size: 26rpx;
  351. color: #333;
  352. line-height: 1.5;
  353. flex: 1;
  354. }
  355. }
  356. .section {
  357. background: #fff;
  358. border-radius: 16rpx;
  359. padding: 24rpx;
  360. margin-bottom: 24rpx;
  361. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  362. }
  363. .section-title {
  364. font-size: 30rpx;
  365. font-weight: 600;
  366. color: #333;
  367. margin-bottom: 20rpx;
  368. }
  369. .section-head {
  370. display: flex;
  371. align-items: center;
  372. justify-content: space-between;
  373. margin-bottom: 20rpx;
  374. .section-title {
  375. margin-bottom: 0;
  376. }
  377. .btn-group {
  378. display: flex;
  379. gap: 12rpx;
  380. .u-button {
  381. width: auto;
  382. margin: 0;
  383. }
  384. }
  385. }
  386. .role-section {
  387. .role-select-wrap {
  388. margin-bottom: 20rpx;
  389. }
  390. .picker-inner {
  391. display: flex;
  392. align-items: center;
  393. justify-content: space-between;
  394. padding: 20rpx 24rpx;
  395. background: #f9fafb;
  396. border-radius: 12rpx;
  397. border: 1px solid #eee;
  398. .picker-text {
  399. font-size: 28rpx;
  400. color: #333;
  401. }
  402. }
  403. .role-input-wrap {
  404. margin-bottom: 20rpx;
  405. .input-row {
  406. display: flex;
  407. align-items: center;
  408. margin-bottom: 16rpx;
  409. .input-label {
  410. width: 140rpx;
  411. font-size: 28rpx;
  412. color: #666;
  413. }
  414. }
  415. }
  416. .btn-row {
  417. display: flex;
  418. gap: 20rpx;
  419. // flex-wrap: wrap;
  420. }
  421. }
  422. .list-wrap {
  423. max-height: 30vh;
  424. }
  425. .loading-wrap {
  426. display: flex;
  427. flex-direction: column;
  428. align-items: center;
  429. justify-content: center;
  430. padding: 60rpx 0;
  431. .loading-text {
  432. margin-top: 16rpx;
  433. font-size: 26rpx;
  434. color: #999;
  435. }
  436. }
  437. .field-list {
  438. display: flex;
  439. flex-direction: column;
  440. gap: 12rpx;
  441. padding-bottom: 24rpx;
  442. }
  443. .field-item {
  444. display: flex;
  445. align-items: center;
  446. justify-content: space-between;
  447. padding: 20rpx 24rpx;
  448. background: #fafafa;
  449. border-radius: 12rpx;
  450. border: 1px solid #f0f0f0;
  451. }
  452. .field-name-wrap {
  453. flex: 1;
  454. min-width: 0;
  455. display: flex;
  456. flex-direction: column;
  457. gap: 4rpx;
  458. }
  459. .field-name {
  460. font-size: 28rpx;
  461. font-weight: 600;
  462. color: #333;
  463. }
  464. .field-key {
  465. font-size: 22rpx;
  466. color: #999;
  467. }
  468. .field-switches {
  469. display: flex;
  470. align-items: center;
  471. gap: 32rpx;
  472. flex-shrink: 0;
  473. }
  474. .switch-cell {
  475. display: flex;
  476. align-items: center;
  477. gap: 12rpx;
  478. .switch-label {
  479. font-size: 24rpx;
  480. color: #666;
  481. }
  482. }
  483. .empty-wrap {
  484. padding: 60rpx 0;
  485. }
  486. .button-list {
  487. display: flex;
  488. flex-direction: column;
  489. gap: 12rpx;
  490. }
  491. </style>