fieldPermissions.vue 15 KB

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