add.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view class="uni-container">
  3. <uni-forms labelWidth="80" ref="form" v-model="formData" :rules="rules" validateTrigger="bind" @submit="submit">
  4. <uni-forms-item name="menu_id" label="标识" required>
  5. <uni-easyinput v-model="formData.menu_id" :clearable="false" placeholder="请输入菜单项的ID,不可重复" />
  6. </uni-forms-item>
  7. <uni-forms-item name="name" label="显示名称" required>
  8. <uni-easyinput v-model="formData.name" :clearable="false" placeholder="请输入菜单名称" />
  9. </uni-forms-item>
  10. <uni-forms-item name="icon" label="图标class" style="margin-bottom: 10px;">
  11. <uni-easyinput v-model="formData.icon" :clearable="false" placeholder="请输入菜单图标css样式类名">
  12. <template v-slot:right>
  13. <span style="color: #007aff; cursor: pointer;padding-right: 10px;" @click="showIconPopup">内置图标</span>
  14. </template>
  15. </uni-easyinput>
  16. <uni-link font-size="12" href="https://uniapp.dcloud.net.cn/uniCloud/admin?id=icon-%e5%9b%be%e6%a0%87" text="如何使用自定义图标?"
  17. class="uni-form-item-tips"></uni-link>
  18. </uni-forms-item>
  19. <uni-forms-item name="url" label="页面URL">
  20. <uni-easyinput v-model="formData.url" :clearable="false" placeholder="URL为空代表是目录而不是叶子节点" />
  21. </uni-forms-item>
  22. <uni-forms-item name="sort" label="序号">
  23. <uni-easyinput v-model="formData.sort" :clearable="false" placeholder="请输入菜单序号(越大越靠后)" />
  24. </uni-forms-item>
  25. <uni-forms-item name="parent_id" label="父菜单标识">
  26. <uni-easyinput :disabled="true" v-model="formData.parent_id" :clearable="false" placeholder="新增菜单时自动填充, 一级菜单不需要填写" />
  27. </uni-forms-item>
  28. <uni-forms-item name="permission" label="权限列表" class="flex-center-x">
  29. <uni-data-checkbox :multiple="true" v-model="formData.permission" collection="uni-id-permissions" :page-size="500" field="permission_name as text, permission_id as value" />
  30. <view class="uni-form-item-tips">
  31. 当用户拥有以上被选中的权限时,可以访问此菜单。建议仅对子菜单配置权限,父菜单会自动包含。如不选择权限,意味着仅超级管理员可访问本菜单
  32. </view>
  33. </uni-forms-item>
  34. <uni-forms-item name="enable" label="是否启用">
  35. <switch @change="binddata('enable', $event.detail.value)" :checked="formData.enable" />
  36. </uni-forms-item>
  37. <view class="uni-button-group">
  38. <button type="primary" class="uni-button" @click="submitForm" style="width: 100px;">{{$t('common.button.submit')}}</button>
  39. <navigator open-type="navigateBack" style="margin-left: 15px;"><button class="uni-button" tyle="width: 100px;">{{$t('common.button.back')}}</button></navigator>
  40. </view>
  41. </uni-forms>
  42. <uni-popup class="icon-modal-box" ref="iconPopup" type="center">
  43. <view class="icon-modal icon-modal-pc">
  44. <Icons :tag="false" :fix-window="false"/>
  45. </view>
  46. </uni-popup>
  47. </view>
  48. </template>
  49. <script>
  50. import validator from '@/js_sdk/validator/opendb-admin-menus.js';
  51. import Icons from '@/pages/demo/icons/icons.vue'
  52. const db = uniCloud.database();
  53. const dbCmd = db.command;
  54. const dbCollectionName = 'opendb-admin-menus';
  55. function getValidator(fields) {
  56. let result = {}
  57. for (let key in validator) {
  58. if (fields.includes(key)) {
  59. result[key] = validator[key]
  60. }
  61. }
  62. return result
  63. }
  64. export default {
  65. components: {
  66. Icons
  67. },
  68. data() {
  69. return {
  70. formData: {
  71. "menu_id": "",
  72. "name": "",
  73. "icon": "",
  74. "url": "",
  75. "sort": null,
  76. "parent_id": "",
  77. "permission": [],
  78. "enable": true
  79. },
  80. rules: {
  81. ...getValidator(["menu_id", "name", "icon", "url", "sort", "parent_id", "permission", "enable"])
  82. }
  83. }
  84. },
  85. onLoad(e) {
  86. if (e.parent_id) {
  87. this.formData.parent_id = e.parent_id
  88. }
  89. },
  90. methods: {
  91. /**
  92. * 触发表单提交
  93. */
  94. submitForm() {
  95. this.$refs.form.submit();
  96. },
  97. /**
  98. * 表单提交
  99. * @param {Object} event 回调参数 Function(callback:{value,errors})
  100. */
  101. submit(event) {
  102. const {
  103. value,
  104. errors
  105. } = event.detail
  106. // 表单校验失败页面会提示报错 ,要停止表单提交逻辑
  107. if (errors) {
  108. return
  109. }
  110. uni.showLoading({
  111. title: '提交中...',
  112. mask: true
  113. })
  114. // 使用 uni-clientDB 提交数据
  115. db.collection(dbCollectionName).add(value).then((res) => {
  116. uni.showToast({
  117. title: '新增成功'
  118. })
  119. this.getOpenerEventChannel().emit('refreshData')
  120. setTimeout(() => uni.navigateBack(), 500)
  121. }).catch((err) => {
  122. uni.showModal({
  123. content: err.message || '请求服务失败',
  124. showCancel: false
  125. })
  126. }).finally(() => {
  127. uni.hideLoading()
  128. })
  129. },
  130. showIconPopup() {
  131. this.$refs.iconPopup.open()
  132. }
  133. }
  134. }
  135. </script>
  136. <style scoped>
  137. .icon-modal-box {
  138. padding-top: var(--top-window-height);
  139. }
  140. .icon-modal {
  141. width: 350px;
  142. background-color: #fff;
  143. height: 500px;
  144. overflow-y: scroll;
  145. }
  146. @media screen and (min-width: 768px) {
  147. .icon-modal-pc {
  148. width: 600px;
  149. }
  150. }
  151. ::v-deep .uni-forms-item__label {
  152. width: 90px !important;
  153. }
  154. </style>