fieldPermissions.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. ],
  131. };
  132. },
  133. computed: {
  134. currentRoleId() {
  135. if (this.roleList.length > 0) {
  136. const r = this.roleList[this.roleIndex];
  137. return r ? r.roleId : null;
  138. }
  139. const id = parseInt(this.formRoleId, 10);
  140. return isNaN(id) ? null : id;
  141. },
  142. currentRoleKey() {
  143. if (this.roleList.length > 0) {
  144. const r = this.roleList[this.roleIndex];
  145. return r ? r.roleKey || '' : '';
  146. }
  147. return this.formRoleKey || '';
  148. },
  149. currentRoleName() {
  150. if (this.roleList.length > 0) {
  151. const r = this.roleList[this.roleIndex];
  152. return r ? (r.roleName || '请选择角色') : '请选择角色';
  153. }
  154. return this.currentRoleId ? `角色ID: ${this.currentRoleId}` : '请填写角色ID并加载';
  155. },
  156. },
  157. onLoad() { },
  158. onShow() {
  159. this.fetchRoleList();
  160. },
  161. methods: {
  162. fieldLabel(name) {
  163. return FIELD_LABELS[name] || name;
  164. },
  165. fetchRoleList() {
  166. uni.$u.api.getRoleOptionSelectAll({ custom: { loading: false } })
  167. .then((res) => {
  168. const list = (res && res.data) ? (Array.isArray(res.data) ? res.data : []) : [];
  169. this.roleList = list.map((r) => ({ roleId: r.roleId, roleName: r.roleName || r.roleKey || '', roleKey: r.roleKey || '' }));
  170. this.roleIndex = 0;
  171. })
  172. .catch(() => {
  173. this.roleList = [];
  174. });
  175. },
  176. onRoleChange(e) {
  177. this.roleIndex = e.detail.value;
  178. this.permissionList = [];
  179. },
  180. loadConfig() {
  181. const roleId = this.currentRoleId;
  182. if (roleId == null) {
  183. uni.$u.toast('请选择或填写角色');
  184. return;
  185. }
  186. this.loadConfigLoading = true;
  187. Promise.all([
  188. uni.$u.api.wareHouseFieldPermissionsConfigGet({ roleId }),
  189. uni.$u.api.wareHouseButtonPermissionsConfigGet({ roleId }),
  190. ])
  191. .then(([fieldRes, buttonRes]) => {
  192. if (fieldRes && fieldRes.data && Array.isArray(fieldRes.data)) {
  193. this.permissionList = fieldRes.data.map((p) => ({
  194. fieldName: p.fieldName,
  195. read: !!p.read,
  196. edit: !!p.edit,
  197. }));
  198. } else {
  199. this.permissionList = [];
  200. }
  201. if (buttonRes && buttonRes.data && Array.isArray(buttonRes.data)) {
  202. const map = {};
  203. buttonRes.data.forEach((b) => { map[b.buttonKey] = !!b.enabled; });
  204. this.buttonPermissionList = [
  205. { buttonKey: 'edit', label: '编辑', enabled: !!map['edit'] },
  206. { buttonKey: 'delete', label: '删除', enabled: !!map['delete'] },
  207. ];
  208. }
  209. })
  210. .catch(() => {
  211. this.permissionList = [];
  212. uni.$u.toast('加载失败');
  213. })
  214. .finally(() => {
  215. this.loadConfigLoading = false;
  216. });
  217. },
  218. updateFieldPermissions:updateFieldPermissions,
  219. updateButtonPermissions:updateButtonPermissions,
  220. saveConfig() {
  221. const roleId = this.currentRoleId;
  222. const roleKey = this.currentRoleKey;
  223. if (roleId == null) {
  224. uni.$u.toast('请选择或填写角色');
  225. return;
  226. }
  227. if (!this.permissionList.length) {
  228. uni.$u.toast('请先加载配置');
  229. return;
  230. }
  231. this.saveLoading = true;
  232. uni.$u.api.wareHouseFieldPermissionsConfigSave({
  233. roleId,
  234. roleKey,
  235. permissions: this.permissionList.map((p) => ({ fieldName: p.fieldName, read: p.read, edit: p.edit })),
  236. })
  237. .then(() => {
  238. uni.$u.toast('保存成功');
  239. })
  240. .catch(() => {
  241. uni.$u.toast('保存失败');
  242. })
  243. .finally(() => {
  244. this.saveLoading = false;
  245. });
  246. this.updateFieldPermissions();
  247. },
  248. saveButtonConfig() {
  249. const roleId = this.currentRoleId;
  250. const roleKey = this.currentRoleKey;
  251. if (roleId == null) {
  252. uni.$u.toast('请选择或填写角色');
  253. return;
  254. }
  255. this.saveLoading = true;
  256. uni.$u.api.wareHouseButtonPermissionsConfigSave({
  257. roleId,
  258. roleKey,
  259. permissions: this.buttonPermissionList.map((p) => ({ buttonKey: p.buttonKey, enabled: p.enabled })),
  260. })
  261. .then(() => {
  262. uni.$u.toast('按钮权限保存成功');
  263. })
  264. .catch(() => {
  265. uni.$u.toast('保存失败');
  266. })
  267. .finally(() => {
  268. this.saveLoading = false;
  269. });
  270. this.updateButtonPermissions();
  271. },
  272. clearConfig() {
  273. if (!this.currentRoleId) return;
  274. uni.showModal({
  275. title: '确认清空',
  276. content: '将删除该角色下所有字段权限与按钮权限配置,确定吗?',
  277. success: (res) => {
  278. if (res.confirm) {
  279. uni.$u.api.wareHouseFieldPermissionsConfigDeleteByRole(this.currentRoleId).then(() => {
  280. uni.$u.toast('已清空');
  281. this.permissionList = [];
  282. this.buttonPermissionList = [
  283. { buttonKey: 'edit', label: '编辑', enabled: false },
  284. { buttonKey: 'delete', label: '删除', enabled: false },
  285. ];
  286. }).catch(() => uni.$u.toast('清空失败'));
  287. }
  288. },
  289. });
  290. },
  291. // 一键打开所有权限
  292. openAllPermissions() {
  293. this.permissionList.forEach(item => {
  294. item.read = true;
  295. item.edit = true;
  296. });
  297. uni.$u.toast('已打开所有权限');
  298. },
  299. // 一键关闭所有权限
  300. closeAllPermissions() {
  301. this.permissionList.forEach(item => {
  302. item.read = false;
  303. item.edit = false;
  304. });
  305. uni.$u.toast('已关闭所有权限');
  306. },
  307. },
  308. };
  309. </script>
  310. <style lang="scss" scoped>
  311. .page {
  312. min-height: 100vh;
  313. background: #f5f6f8;
  314. display: flex;
  315. flex-direction: column;
  316. }
  317. .nav-bar {
  318. border-bottom: 1px solid #eee;
  319. }
  320. .content {
  321. flex: 1;
  322. display: flex;
  323. flex-direction: column;
  324. padding: 24rpx;
  325. box-sizing: border-box;
  326. }
  327. .tip-card {
  328. display: flex;
  329. align-items: flex-start;
  330. gap: 12rpx;
  331. background: #e8f4ff;
  332. border-radius: 16rpx;
  333. padding: 20rpx 24rpx;
  334. margin-bottom: 24rpx;
  335. border: 1px solid rgba(16, 140, 255, 0.2);
  336. .tip-text {
  337. font-size: 26rpx;
  338. color: #333;
  339. line-height: 1.5;
  340. flex: 1;
  341. }
  342. }
  343. .section {
  344. background: #fff;
  345. border-radius: 16rpx;
  346. padding: 24rpx;
  347. margin-bottom: 24rpx;
  348. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  349. }
  350. .section-title {
  351. font-size: 30rpx;
  352. font-weight: 600;
  353. color: #333;
  354. margin-bottom: 20rpx;
  355. }
  356. .section-head {
  357. display: flex;
  358. align-items: center;
  359. justify-content: space-between;
  360. margin-bottom: 20rpx;
  361. .section-title {
  362. margin-bottom: 0;
  363. }
  364. .btn-group {
  365. display: flex;
  366. gap: 12rpx;
  367. .u-button {
  368. width: auto;
  369. margin: 0;
  370. }
  371. }
  372. }
  373. .role-section {
  374. .role-select-wrap {
  375. margin-bottom: 20rpx;
  376. }
  377. .picker-inner {
  378. display: flex;
  379. align-items: center;
  380. justify-content: space-between;
  381. padding: 20rpx 24rpx;
  382. background: #f9fafb;
  383. border-radius: 12rpx;
  384. border: 1px solid #eee;
  385. .picker-text {
  386. font-size: 28rpx;
  387. color: #333;
  388. }
  389. }
  390. .role-input-wrap {
  391. margin-bottom: 20rpx;
  392. .input-row {
  393. display: flex;
  394. align-items: center;
  395. margin-bottom: 16rpx;
  396. .input-label {
  397. width: 140rpx;
  398. font-size: 28rpx;
  399. color: #666;
  400. }
  401. }
  402. }
  403. .btn-row {
  404. display: flex;
  405. gap: 20rpx;
  406. // flex-wrap: wrap;
  407. }
  408. }
  409. .list-wrap {
  410. max-height: 30vh;
  411. }
  412. .loading-wrap {
  413. display: flex;
  414. flex-direction: column;
  415. align-items: center;
  416. justify-content: center;
  417. padding: 60rpx 0;
  418. .loading-text {
  419. margin-top: 16rpx;
  420. font-size: 26rpx;
  421. color: #999;
  422. }
  423. }
  424. .field-list {
  425. display: flex;
  426. flex-direction: column;
  427. gap: 12rpx;
  428. padding-bottom: 24rpx;
  429. }
  430. .field-item {
  431. display: flex;
  432. align-items: center;
  433. justify-content: space-between;
  434. padding: 20rpx 24rpx;
  435. background: #fafafa;
  436. border-radius: 12rpx;
  437. border: 1px solid #f0f0f0;
  438. }
  439. .field-name-wrap {
  440. flex: 1;
  441. min-width: 0;
  442. display: flex;
  443. flex-direction: column;
  444. gap: 4rpx;
  445. }
  446. .field-name {
  447. font-size: 28rpx;
  448. font-weight: 600;
  449. color: #333;
  450. }
  451. .field-key {
  452. font-size: 22rpx;
  453. color: #999;
  454. }
  455. .field-switches {
  456. display: flex;
  457. align-items: center;
  458. gap: 32rpx;
  459. flex-shrink: 0;
  460. }
  461. .switch-cell {
  462. display: flex;
  463. align-items: center;
  464. gap: 12rpx;
  465. .switch-label {
  466. font-size: 24rpx;
  467. color: #666;
  468. }
  469. }
  470. .empty-wrap {
  471. padding: 60rpx 0;
  472. }
  473. .button-list {
  474. display: flex;
  475. flex-direction: column;
  476. gap: 12rpx;
  477. }
  478. </style>