edit.vue 5.6 KB

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