fieldPermissions.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. const FIELD_LABELS = {
  103. goodPicFileList: '商品图片', desc: '商品描述', origin: '来源', type: '商品分类', dictLabel: '品牌',
  104. series: '系列', model: '型号', dialType: '机芯类型', caseMaterial: '表壳材质', dialDiameter: '表盘直径',
  105. material: '材质', size: '尺寸', yardage: '尺码', productCondition: '商品成色', detailPicFileList: '细节图',
  106. title: '商品标题', productNo: '商品货号', productAttribute: '商品属性', stock: '库存数量', price: '官方指导价',
  107. costPrice: '总成本价', peerPrice: '同行价格', actualPrice: '实价', agentPrice: '代理价格', salePrice: '销售价格',
  108. productPosition: '商品位置', watchYear: '手表年份', identifyingPerson: '鉴定人员', stockStatus: '是否入库',
  109. recycleType: '回收类型', recycleSituation: '回收情况', label: '标签', recyclePerson: '回收人员',
  110. recycleBottomDesc: '回收留底描述', recycleBottomFileList: '回收留底图', recycleTime: '回收时间',
  111. indentifyCode: '独立编码', targetAudience: '适用人群', productCard: '商品保卡', cardYear: '保卡年份',
  112. productCardPicFileList: '保卡图片或独立编码照片留底', productTag: '商品标签', productAttachment: '商品附件',
  113. productDesc: '备注', productDescPicFileList: '备注图片', payType: '付款方式',
  114. };
  115. export default {
  116. data() {
  117. return {
  118. roleList: [],
  119. roleIndex: 0,
  120. formRoleId: '',
  121. formRoleKey: '',
  122. loadConfigLoading: false,
  123. saveLoading: false,
  124. permissionList: [],
  125. // 按钮权限:编辑、删除,与后端 buttonKey 一致
  126. buttonPermissionList: [
  127. { buttonKey: 'edit', label: '编辑', enabled: false },
  128. { buttonKey: 'delete', label: '删除', enabled: false },
  129. ],
  130. };
  131. },
  132. computed: {
  133. currentRoleId() {
  134. if (this.roleList.length > 0) {
  135. const r = this.roleList[this.roleIndex];
  136. return r ? r.roleId : null;
  137. }
  138. const id = parseInt(this.formRoleId, 10);
  139. return isNaN(id) ? null : id;
  140. },
  141. currentRoleKey() {
  142. if (this.roleList.length > 0) {
  143. const r = this.roleList[this.roleIndex];
  144. return r ? r.roleKey || '' : '';
  145. }
  146. return this.formRoleKey || '';
  147. },
  148. currentRoleName() {
  149. if (this.roleList.length > 0) {
  150. const r = this.roleList[this.roleIndex];
  151. return r ? (r.roleName || '请选择角色') : '请选择角色';
  152. }
  153. return this.currentRoleId ? `角色ID: ${this.currentRoleId}` : '请填写角色ID并加载';
  154. },
  155. },
  156. onLoad() { },
  157. onShow() {
  158. this.fetchRoleList();
  159. },
  160. methods: {
  161. fieldLabel(name) {
  162. return FIELD_LABELS[name] || name;
  163. },
  164. fetchRoleList() {
  165. uni.$u.api.getRoleOptionSelectAll({ custom: { loading: false } })
  166. .then((res) => {
  167. const list = (res && res.data) ? (Array.isArray(res.data) ? res.data : []) : [];
  168. this.roleList = list.map((r) => ({ roleId: r.roleId, roleName: r.roleName || r.roleKey || '', roleKey: r.roleKey || '' }));
  169. this.roleIndex = 0;
  170. })
  171. .catch(() => {
  172. this.roleList = [];
  173. });
  174. },
  175. onRoleChange(e) {
  176. this.roleIndex = e.detail.value;
  177. this.permissionList = [];
  178. },
  179. loadConfig() {
  180. const roleId = this.currentRoleId;
  181. if (roleId == null) {
  182. uni.$u.toast('请选择或填写角色');
  183. return;
  184. }
  185. this.loadConfigLoading = true;
  186. Promise.all([
  187. uni.$u.api.wareHouseFieldPermissionsConfigGet({ roleId }),
  188. uni.$u.api.wareHouseButtonPermissionsConfigGet({ roleId }),
  189. ])
  190. .then(([fieldRes, buttonRes]) => {
  191. if (fieldRes && fieldRes.data && Array.isArray(fieldRes.data)) {
  192. this.permissionList = fieldRes.data.map((p) => ({
  193. fieldName: p.fieldName,
  194. read: !!p.read,
  195. edit: !!p.edit,
  196. }));
  197. } else {
  198. this.permissionList = [];
  199. }
  200. if (buttonRes && buttonRes.data && Array.isArray(buttonRes.data)) {
  201. const map = {};
  202. buttonRes.data.forEach((b) => { map[b.buttonKey] = !!b.enabled; });
  203. this.buttonPermissionList = [
  204. { buttonKey: 'edit', label: '编辑', enabled: !!map['edit'] },
  205. { buttonKey: 'delete', label: '删除', enabled: !!map['delete'] },
  206. ];
  207. }
  208. })
  209. .catch(() => {
  210. this.permissionList = [];
  211. uni.$u.toast('加载失败');
  212. })
  213. .finally(() => {
  214. this.loadConfigLoading = false;
  215. });
  216. },
  217. saveConfig() {
  218. const roleId = this.currentRoleId;
  219. const roleKey = this.currentRoleKey;
  220. if (roleId == null) {
  221. uni.$u.toast('请选择或填写角色');
  222. return;
  223. }
  224. if (!this.permissionList.length) {
  225. uni.$u.toast('请先加载配置');
  226. return;
  227. }
  228. this.saveLoading = true;
  229. uni.$u.api.wareHouseFieldPermissionsConfigSave({
  230. roleId,
  231. roleKey,
  232. permissions: this.permissionList.map((p) => ({ fieldName: p.fieldName, read: p.read, edit: p.edit })),
  233. })
  234. .then(() => {
  235. uni.$u.toast('保存成功');
  236. })
  237. .catch(() => {
  238. uni.$u.toast('保存失败');
  239. })
  240. .finally(() => {
  241. this.saveLoading = false;
  242. });
  243. },
  244. saveButtonConfig() {
  245. const roleId = this.currentRoleId;
  246. const roleKey = this.currentRoleKey;
  247. if (roleId == null) {
  248. uni.$u.toast('请选择或填写角色');
  249. return;
  250. }
  251. this.saveLoading = true;
  252. uni.$u.api.wareHouseButtonPermissionsConfigSave({
  253. roleId,
  254. roleKey,
  255. permissions: this.buttonPermissionList.map((p) => ({ buttonKey: p.buttonKey, enabled: p.enabled })),
  256. })
  257. .then(() => {
  258. uni.$u.toast('按钮权限保存成功');
  259. })
  260. .catch(() => {
  261. uni.$u.toast('保存失败');
  262. })
  263. .finally(() => {
  264. this.saveLoading = false;
  265. });
  266. },
  267. clearConfig() {
  268. if (!this.currentRoleId) return;
  269. uni.showModal({
  270. title: '确认清空',
  271. content: '将删除该角色下所有字段权限与按钮权限配置,确定吗?',
  272. success: (res) => {
  273. if (res.confirm) {
  274. uni.$u.api.wareHouseFieldPermissionsConfigDeleteByRole(this.currentRoleId).then(() => {
  275. uni.$u.toast('已清空');
  276. this.permissionList = [];
  277. this.buttonPermissionList = [
  278. { buttonKey: 'edit', label: '编辑', enabled: false },
  279. { buttonKey: 'delete', label: '删除', enabled: false },
  280. ];
  281. }).catch(() => uni.$u.toast('清空失败'));
  282. }
  283. },
  284. });
  285. },
  286. // 一键打开所有权限
  287. openAllPermissions() {
  288. this.permissionList.forEach(item => {
  289. item.read = true;
  290. item.edit = true;
  291. });
  292. uni.$u.toast('已打开所有权限');
  293. },
  294. // 一键关闭所有权限
  295. closeAllPermissions() {
  296. this.permissionList.forEach(item => {
  297. item.read = false;
  298. item.edit = false;
  299. });
  300. uni.$u.toast('已关闭所有权限');
  301. },
  302. },
  303. };
  304. </script>
  305. <style lang="scss" scoped>
  306. .page {
  307. min-height: 100vh;
  308. background: #f5f6f8;
  309. display: flex;
  310. flex-direction: column;
  311. }
  312. .nav-bar {
  313. border-bottom: 1px solid #eee;
  314. }
  315. .content {
  316. flex: 1;
  317. display: flex;
  318. flex-direction: column;
  319. padding: 24rpx;
  320. box-sizing: border-box;
  321. }
  322. .tip-card {
  323. display: flex;
  324. align-items: flex-start;
  325. gap: 12rpx;
  326. background: #e8f4ff;
  327. border-radius: 16rpx;
  328. padding: 20rpx 24rpx;
  329. margin-bottom: 24rpx;
  330. border: 1px solid rgba(16, 140, 255, 0.2);
  331. .tip-text {
  332. font-size: 26rpx;
  333. color: #333;
  334. line-height: 1.5;
  335. flex: 1;
  336. }
  337. }
  338. .section {
  339. background: #fff;
  340. border-radius: 16rpx;
  341. padding: 24rpx;
  342. margin-bottom: 24rpx;
  343. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  344. }
  345. .section-title {
  346. font-size: 30rpx;
  347. font-weight: 600;
  348. color: #333;
  349. margin-bottom: 20rpx;
  350. }
  351. .section-head {
  352. display: flex;
  353. align-items: center;
  354. justify-content: space-between;
  355. margin-bottom: 20rpx;
  356. .section-title {
  357. margin-bottom: 0;
  358. }
  359. .btn-group {
  360. display: flex;
  361. gap: 12rpx;
  362. .u-button {
  363. width: auto;
  364. margin: 0;
  365. }
  366. }
  367. }
  368. .role-section {
  369. .role-select-wrap {
  370. margin-bottom: 20rpx;
  371. }
  372. .picker-inner {
  373. display: flex;
  374. align-items: center;
  375. justify-content: space-between;
  376. padding: 20rpx 24rpx;
  377. background: #f9fafb;
  378. border-radius: 12rpx;
  379. border: 1px solid #eee;
  380. .picker-text {
  381. font-size: 28rpx;
  382. color: #333;
  383. }
  384. }
  385. .role-input-wrap {
  386. margin-bottom: 20rpx;
  387. .input-row {
  388. display: flex;
  389. align-items: center;
  390. margin-bottom: 16rpx;
  391. .input-label {
  392. width: 140rpx;
  393. font-size: 28rpx;
  394. color: #666;
  395. }
  396. }
  397. }
  398. .btn-row {
  399. display: flex;
  400. gap: 20rpx;
  401. // flex-wrap: wrap;
  402. }
  403. }
  404. .list-wrap {
  405. max-height: 30vh;
  406. }
  407. .loading-wrap {
  408. display: flex;
  409. flex-direction: column;
  410. align-items: center;
  411. justify-content: center;
  412. padding: 60rpx 0;
  413. .loading-text {
  414. margin-top: 16rpx;
  415. font-size: 26rpx;
  416. color: #999;
  417. }
  418. }
  419. .field-list {
  420. display: flex;
  421. flex-direction: column;
  422. gap: 12rpx;
  423. padding-bottom: 24rpx;
  424. }
  425. .field-item {
  426. display: flex;
  427. align-items: center;
  428. justify-content: space-between;
  429. padding: 20rpx 24rpx;
  430. background: #fafafa;
  431. border-radius: 12rpx;
  432. border: 1px solid #f0f0f0;
  433. }
  434. .field-name-wrap {
  435. flex: 1;
  436. min-width: 0;
  437. display: flex;
  438. flex-direction: column;
  439. gap: 4rpx;
  440. }
  441. .field-name {
  442. font-size: 28rpx;
  443. font-weight: 600;
  444. color: #333;
  445. }
  446. .field-key {
  447. font-size: 22rpx;
  448. color: #999;
  449. }
  450. .field-switches {
  451. display: flex;
  452. align-items: center;
  453. gap: 32rpx;
  454. flex-shrink: 0;
  455. }
  456. .switch-cell {
  457. display: flex;
  458. align-items: center;
  459. gap: 12rpx;
  460. .switch-label {
  461. font-size: 24rpx;
  462. color: #666;
  463. }
  464. }
  465. .empty-wrap {
  466. padding: 60rpx 0;
  467. }
  468. .button-list {
  469. display: flex;
  470. flex-direction: column;
  471. gap: 12rpx;
  472. }
  473. </style>