/** * Purpose: permissionApi supports index.js by holding or executing the permission api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const permissionApi = require('../../api/permission') /** * Purpose: navigation supports index.js by holding or executing the navigation page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const navigation = require('../../utils/navigation') /** * Purpose: permissionUtils supports index.js by holding or executing the permission utils page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const permissionUtils = require('../../utils/permission') const { getTempRecord, clearTempRecord } = require('../../utils/storage') /** * Purpose: buildPermissionGroups supports index.js by holding or executing the build permission groups page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function buildPermissionGroups(list, checkedIds, assignableIds) { const groups = permissionUtils.groupPermissionsByGroup(list) return Object.keys(groups).map(group => ({ group, items: groups[group].map(item => ({ ...item, valueId: String(item.id), checked: checkedIds.includes(String(item.id)), disabled: assignableIds.length ? !assignableIds.includes(String(item.id)) : false })) })) } Page({ /** * Purpose: data supports index.js by holding or executing the data page or API role. * Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow. */ data: { /** * Purpose: mode supports index.js by holding or executing the mode page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ mode: 'definition-create', /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: '', /** * Purpose: userId supports index.js by holding or executing the user id page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ userId: '', /** * Purpose: currentUser supports index.js by holding or executing the current user page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentUser: null, /** * Purpose: permissionGroups supports index.js by holding or executing the permission groups page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionGroups: [], /** * Purpose: form supports index.js by holding or executing the form page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ form: { /** * Purpose: permissionCode supports index.js by holding or executing the permission code page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionCode: '', /** * Purpose: permissionName supports index.js by holding or executing the permission name page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionName: '', /** * Purpose: permissionGroup supports index.js by holding or executing the permission group page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionGroup: '', /** * Purpose: description supports index.js by holding or executing the description page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ description: '' } }, /** * Purpose: onLoad supports index.js by holding or executing the on load page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ onLoad(options) { const mode = options && options.mode ? options.mode : 'definition-create' this.setData({ mode, /** * Purpose: userId supports index.js by holding or executing the user id page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ userId: options && options.userId ? options.userId : '', /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: mode === 'assign' ? '分配权限' : (mode === 'definition-edit' ? '编辑权限' : '新增权限') }) if (mode === 'assign') { this.loadAssignData() return } const currentPermission = getTempRecord('currentPermission', null) if (mode === 'definition-edit' && currentPermission) { this.setData({ /** * Purpose: form supports index.js by holding or executing the form page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ form: { /** * Purpose: permissionCode supports index.js by holding or executing the permission code page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionCode: currentPermission.permissionCode || '', /** * Purpose: permissionName supports index.js by holding or executing the permission name page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionName: currentPermission.permissionName || '', /** * Purpose: permissionGroup supports index.js by holding or executing the permission group page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionGroup: currentPermission.permissionGroup || '', /** * Purpose: description supports index.js by holding or executing the description page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ description: currentPermission.description || '' } }) } }, /** * Purpose: onUnload supports index.js by holding or executing the on unload page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ onUnload() { clearTempRecord('currentPermission') }, async loadAssignData() { const currentUser = getTempRecord('currentUser', null) this.setData({ currentUser }) try { const [allPermissions, currentPermissions, assignableIds] = await Promise.all([ permissionApi.listAllPermissions(), permissionApi.getUserPermissions(this.data.userId), permissionApi.getAssignablePermissionIds(this.data.userId) ]) const checkedIds = (Array.isArray(currentPermissions) ? currentPermissions : []).map(item => String(item.id)) const normalizedAssignableIds = (Array.isArray(assignableIds) ? assignableIds : []).map(item => String(item)) this.setData({ /** * Purpose: permissionGroups supports index.js by holding or executing the permission groups page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionGroups: buildPermissionGroups(allPermissions, checkedIds, normalizedAssignableIds) }) } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '权限数据加载失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } }, /** * Purpose: handleInputChange supports index.js by holding or executing the handle input change page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ handleInputChange(event) { const field = event.currentTarget.dataset.field this.setData({ [`form.${field}`]: event.detail.value }) }, /** * Purpose: handlePermissionCheck supports index.js by holding or executing the handle permission check page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ handlePermissionCheck(event) { const selected = event.detail.value || [] this.setData({ /** * Purpose: permissionGroups supports index.js by holding or executing the permission groups page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ permissionGroups: this.data.permissionGroups.map(group => ({ ...group, /** * Purpose: items supports index.js by holding or executing the items page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ items: group.items.map(item => ({ ...item, /** * Purpose: checked supports index.js by holding or executing the checked page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ checked: item.disabled ? item.checked : selected.includes(item.valueId) })) })) }) }, async handleSubmit() { if (this.data.mode === 'assign') { const permissionIds = [] this.data.permissionGroups.forEach(group => { group.items.forEach(item => { if (item.checked) { permissionIds.push(Number(item.id)) } }) }) try { await permissionApi.assignUserPermissions(this.data.userId, permissionIds) wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: '权限已保存', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) navigation.goBack('/pages/permissions/index') } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '权限保存失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } return } if (!this.data.form.permissionCode || !this.data.form.permissionName) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: '请补全权限编码和名称', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) return } try { if (this.data.mode === 'definition-edit') { const currentPermission = getTempRecord('currentPermission', null) await permissionApi.updatePermission(currentPermission.id, this.data.form) } else { await permissionApi.createPermission(this.data.form) } wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: '权限定义已保存', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) navigation.goBack('/pages/permissions/index') } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '保存失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } } })