fieldPermissions.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. <u-button type="primary" size="small" :loading="loadConfigLoading"
  30. @click="loadConfig">加载配置</u-button>
  31. </view>
  32. </view>
  33. <!-- 字段权限列表(可编辑) -->
  34. <view class="section">
  35. <view class="section-head">
  36. <text class="section-title">字段权限</text>
  37. <view class="btn-group">
  38. <u-button type="success" plain size="mini"
  39. :disabled="!currentRoleId || permissionList.length === 0" @click="openAllPermissions">一键全开</u-button>
  40. <u-button type="error" plain size="mini"
  41. :disabled="!currentRoleId || permissionList.length === 0" @click="closeAllPermissions">一键全关</u-button>
  42. <u-button type="primary" size="mini" :loading="saveLoading"
  43. :disabled="!currentRoleId || permissionList.length === 0" @click="saveConfig">保存配置</u-button>
  44. </view>
  45. </view>
  46. <view v-if="loadConfigLoading" class="loading-wrap">
  47. <u-loading-icon mode="circle" size="36"></u-loading-icon>
  48. <text class="loading-text">加载中...</text>
  49. </view>
  50. <scroll-view v-else class="list-wrap" scroll-y enable-back-to-top>
  51. <view class="field-list">
  52. <view v-for="(item, index) in permissionList" :key="item.fieldName" class="field-item">
  53. <view class="field-name-wrap">
  54. <text class="field-name">{{ fieldLabel(item.fieldName) }}</text>
  55. <!-- <text class="field-key">{{ item.fieldName }}</text> -->
  56. </view>
  57. <view class="field-switches">
  58. <view class="switch-cell">
  59. <text class="switch-label">查看</text>
  60. <u-switch v-model="item.read" size="20" active-color="#108cff"></u-switch>
  61. </view>
  62. <view class="switch-cell">
  63. <text class="switch-label">编辑</text>
  64. <u-switch v-model="item.edit" size="20" active-color="#09bb07"></u-switch>
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. <view v-if="permissionList.length === 0 && !loadConfigLoading" class="empty-wrap">
  70. <u-empty text="请先选择角色并点击「加载配置」" mode="list"></u-empty>
  71. </view>
  72. </scroll-view>
  73. </view>
  74. <!-- 按钮权限 -->
  75. <view class="section">
  76. <view class="section-head">
  77. <text class="section-title">按钮权限</text>
  78. <view class="btn-group">
  79. <u-button type="success" plain size="mini"
  80. :disabled="!currentRoleId || buttonPermissionList.length === 0" @click="openAllButtonPermissions">一键全开</u-button>
  81. <u-button type="error" plain size="mini"
  82. :disabled="!currentRoleId || buttonPermissionList.length === 0" @click="closeAllButtonPermissions">一键全关</u-button>
  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. if (this.roleList.length > 0) {
  176. this.loadConfig();
  177. }
  178. })
  179. .catch(() => {
  180. this.roleList = [];
  181. });
  182. },
  183. onRoleChange(e) {
  184. this.roleIndex = e.detail.value;
  185. this.permissionList = [];
  186. this.loadConfig()
  187. },
  188. loadConfig() {
  189. const roleId = this.currentRoleId;
  190. if (roleId == null) {
  191. uni.$u.toast('请选择或填写角色');
  192. return;
  193. }
  194. this.loadConfigLoading = true;
  195. Promise.all([
  196. uni.$u.api.wareHouseFieldPermissionsConfigGet({ roleId }),
  197. uni.$u.api.wareHouseButtonPermissionsConfigGet({ roleId }),
  198. ])
  199. .then(([fieldRes, buttonRes]) => {
  200. if (fieldRes && fieldRes.data && Array.isArray(fieldRes.data)) {
  201. this.permissionList = fieldRes.data.map((p) => ({
  202. fieldName: p.fieldName,
  203. read: !!p.read,
  204. edit: !!p.edit,
  205. }));
  206. } else {
  207. this.permissionList = [];
  208. }
  209. if (buttonRes && buttonRes.data && Array.isArray(buttonRes.data)) {
  210. const map = {};
  211. buttonRes.data.forEach((b) => { map[b.buttonKey] = !!b.enabled; });
  212. const defaultButtons = [
  213. { buttonKey: 'edit', label: '编辑', enabled: !!map['edit'] },
  214. { buttonKey: 'delete', label: '删除', enabled: !!map['delete'] },
  215. { buttonKey: 'shelf', label: '上/下架', enabled: !!map['shelf'] },
  216. { buttonKey: 'openOrder', label: '开单', enabled: !!map['openOrder'] },
  217. { buttonKey: 'viewLog', label: '查看日志', enabled: !!map['viewLog'] },
  218. { buttonKey: 'lockOrder', label: '解/锁单', enabled: !!map['lockOrder'] },
  219. ];
  220. this.buttonPermissionList = defaultButtons;
  221. }
  222. })
  223. .catch(() => {
  224. this.permissionList = [];
  225. uni.$u.toast('加载失败');
  226. })
  227. .finally(() => {
  228. this.loadConfigLoading = false;
  229. });
  230. },
  231. updateFieldPermissions:updateFieldPermissions,
  232. updateButtonPermissions:updateButtonPermissions,
  233. saveConfig() {
  234. const roleId = this.currentRoleId;
  235. const roleKey = this.currentRoleKey;
  236. if (roleId == null) {
  237. uni.$u.toast('请选择或填写角色');
  238. return;
  239. }
  240. if (!this.permissionList.length) {
  241. uni.$u.toast('请先加载配置');
  242. return;
  243. }
  244. this.saveLoading = true;
  245. uni.$u.api.wareHouseFieldPermissionsConfigSave({
  246. roleId,
  247. roleKey,
  248. permissions: this.permissionList.map((p) => ({ fieldName: p.fieldName, read: p.read, edit: p.edit })),
  249. })
  250. .then(() => {
  251. uni.$u.toast('保存成功');
  252. })
  253. .catch(() => {
  254. uni.$u.toast('保存失败');
  255. })
  256. .finally(() => {
  257. this.saveLoading = false;
  258. });
  259. this.updateFieldPermissions();
  260. },
  261. saveButtonConfig() {
  262. const roleId = this.currentRoleId;
  263. const roleKey = this.currentRoleKey;
  264. if (roleId == null) {
  265. uni.$u.toast('请选择或填写角色');
  266. return;
  267. }
  268. this.saveLoading = true;
  269. console.log(this.buttonPermissionList);
  270. uni.$u.api.wareHouseButtonPermissionsConfigSave({
  271. roleId,
  272. roleKey,
  273. permissions: this.buttonPermissionList.map((p) => ({ buttonKey: p.buttonKey, enabled: p.enabled })),
  274. })
  275. .then(() => {
  276. uni.$u.toast('按钮权限保存成功');
  277. })
  278. .catch(() => {
  279. uni.$u.toast('保存失败');
  280. })
  281. .finally(() => {
  282. this.saveLoading = false;
  283. });
  284. this.updateButtonPermissions();
  285. },
  286. clearConfig() {
  287. if (!this.currentRoleId) return;
  288. uni.showModal({
  289. title: '确认清空',
  290. content: '将删除该角色下所有字段权限与按钮权限配置,确定吗?',
  291. success: (res) => {
  292. if (res.confirm) {
  293. uni.$u.api.wareHouseFieldPermissionsConfigDeleteByRole(this.currentRoleId).then(() => {
  294. uni.$u.toast('已清空');
  295. this.permissionList = [];
  296. this.buttonPermissionList = [
  297. { buttonKey: 'edit', label: '编辑', enabled: false },
  298. { buttonKey: 'delete', label: '删除', enabled: false },
  299. { buttonKey: 'shelf', label: '上/下架', enabled: false },
  300. { buttonKey: 'openOrder', label: '开单', enabled: false },
  301. { buttonKey: 'viewLog', label: '查看日志', enabled: false },
  302. { buttonKey: 'lockOrder', label: '解/锁单', enabled: false },
  303. ];
  304. }).catch(() => uni.$u.toast('清空失败'));
  305. }
  306. },
  307. });
  308. },
  309. // 一键打开所有权限
  310. openAllPermissions() {
  311. this.permissionList.forEach(item => {
  312. item.read = true;
  313. item.edit = true;
  314. });
  315. uni.$u.toast('已打开所有权限');
  316. },
  317. // 一键关闭所有权限
  318. closeAllPermissions() {
  319. this.permissionList.forEach(item => {
  320. item.read = false;
  321. item.edit = false;
  322. });
  323. uni.$u.toast('已关闭所有权限');
  324. },
  325. // 一键打开所有按钮权限
  326. openAllButtonPermissions() {
  327. this.buttonPermissionList.forEach(item => {
  328. item.enabled = true;
  329. });
  330. uni.$u.toast('已打开所有按钮权限');
  331. },
  332. // 一键关闭所有按钮权限
  333. closeAllButtonPermissions() {
  334. this.buttonPermissionList.forEach(item => {
  335. item.enabled = false;
  336. });
  337. uni.$u.toast('已关闭所有按钮权限');
  338. },
  339. },
  340. };
  341. </script>
  342. <style lang="scss" scoped>
  343. .page {
  344. min-height: 100vh;
  345. background: #f5f6f8;
  346. display: flex;
  347. flex-direction: column;
  348. }
  349. .nav-bar {
  350. border-bottom: 1px solid #eee;
  351. }
  352. .content {
  353. flex: 1;
  354. display: flex;
  355. flex-direction: column;
  356. padding: 24rpx;
  357. box-sizing: border-box;
  358. }
  359. .tip-card {
  360. display: flex;
  361. align-items: flex-start;
  362. gap: 12rpx;
  363. background: #e8f4ff;
  364. border-radius: 16rpx;
  365. padding: 20rpx 24rpx;
  366. margin-bottom: 24rpx;
  367. border: 1px solid rgba(16, 140, 255, 0.2);
  368. .tip-text {
  369. font-size: 26rpx;
  370. color: #333;
  371. line-height: 1.5;
  372. flex: 1;
  373. }
  374. }
  375. .section {
  376. background: #fff;
  377. border-radius: 16rpx;
  378. padding: 24rpx;
  379. margin-bottom: 24rpx;
  380. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  381. }
  382. .section-title {
  383. font-size: 30rpx;
  384. font-weight: 600;
  385. color: #333;
  386. margin-bottom: 20rpx;
  387. }
  388. .section-head {
  389. display: flex;
  390. align-items: center;
  391. justify-content: space-between;
  392. margin-bottom: 20rpx;
  393. .section-title {
  394. margin-bottom: 0;
  395. }
  396. .btn-group {
  397. display: flex;
  398. gap: 12rpx;
  399. .u-button {
  400. width: auto;
  401. margin: 0;
  402. }
  403. }
  404. }
  405. .role-section {
  406. .role-select-wrap {
  407. margin-bottom: 20rpx;
  408. }
  409. .picker-inner {
  410. display: flex;
  411. align-items: center;
  412. justify-content: space-between;
  413. padding: 20rpx 24rpx;
  414. background: #f9fafb;
  415. border-radius: 12rpx;
  416. border: 1px solid #eee;
  417. .picker-text {
  418. font-size: 28rpx;
  419. color: #333;
  420. }
  421. }
  422. .role-input-wrap {
  423. margin-bottom: 20rpx;
  424. .input-row {
  425. display: flex;
  426. align-items: center;
  427. margin-bottom: 16rpx;
  428. .input-label {
  429. width: 140rpx;
  430. font-size: 28rpx;
  431. color: #666;
  432. }
  433. }
  434. }
  435. }
  436. .list-wrap {
  437. max-height: 35vh;
  438. }
  439. .loading-wrap {
  440. display: flex;
  441. flex-direction: column;
  442. align-items: center;
  443. justify-content: center;
  444. padding: 60rpx 0;
  445. .loading-text {
  446. margin-top: 16rpx;
  447. font-size: 26rpx;
  448. color: #999;
  449. }
  450. }
  451. .field-list {
  452. display: flex;
  453. flex-direction: column;
  454. gap: 12rpx;
  455. padding-bottom: 24rpx;
  456. }
  457. .field-item {
  458. display: flex;
  459. align-items: center;
  460. justify-content: space-between;
  461. padding: 20rpx 24rpx;
  462. background: #fafafa;
  463. border-radius: 12rpx;
  464. border: 1px solid #f0f0f0;
  465. }
  466. .field-name-wrap {
  467. flex: 1;
  468. min-width: 0;
  469. display: flex;
  470. flex-direction: column;
  471. gap: 4rpx;
  472. }
  473. .field-name {
  474. font-size: 28rpx;
  475. font-weight: 600;
  476. color: #333;
  477. }
  478. .field-key {
  479. font-size: 22rpx;
  480. color: #999;
  481. }
  482. .field-switches {
  483. display: flex;
  484. align-items: center;
  485. gap: 32rpx;
  486. flex-shrink: 0;
  487. }
  488. .switch-cell {
  489. display: flex;
  490. align-items: center;
  491. gap: 12rpx;
  492. .switch-label {
  493. font-size: 24rpx;
  494. color: #666;
  495. }
  496. }
  497. .empty-wrap {
  498. padding: 60rpx 0;
  499. }
  500. .button-list {
  501. display: flex;
  502. flex-direction: column;
  503. gap: 12rpx;
  504. }
  505. </style>