edit.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="uni-container">
  3. <uni-forms ref="form" :value="formData" validateTrigger="bind">
  4. <uni-forms-item name="permission_id" label="权限ID" required>
  5. <uni-easyinput placeholder="权限唯一标识,不可修改,不允许重复" @input="binddata('permission_id', $event.detail.value)" v-model="formData.permission_id" trim="both" disabled></uni-easyinput>
  6. </uni-forms-item>
  7. <uni-forms-item name="permission_name" label="权限名称" required>
  8. <input placeholder="权限名称" @input="binddata('permission_name', $event.detail.value)" class="uni-input-border" v-model="formData.permission_name" trim="both" />
  9. </uni-forms-item>
  10. <uni-forms-item name="comment" label="备注">
  11. <textarea placeholder="备注" @input="binddata('comment', $event.detail.value)" class="uni-textarea-border" v-model="formData.comment" trim="both"></textarea>
  12. </uni-forms-item>
  13. <view class="uni-button-group">
  14. <button type="primary" class="uni-button" style="width: 100px;" @click="submit">{{$t('common.button.submit')}}</button>
  15. <navigator open-type="navigateBack" style="margin-left: 15px;">
  16. <button class="uni-button" style="width: 100px;">{{$t('common.button.back')}}</button>
  17. </navigator>
  18. </view>
  19. </uni-forms>
  20. </view>
  21. </template>
  22. <script>
  23. import { validator } from '@/js_sdk/validator/uni-id-permissions.js';
  24. const db = uniCloud.database();
  25. const dbCmd = db.command;
  26. const dbCollectionName = 'uni-id-permissions';
  27. function getValidator(fields) {
  28. let result = {}
  29. for (let key in validator) {
  30. if (fields.includes(key)) {
  31. result[key] = validator[key]
  32. }
  33. }
  34. return result
  35. }
  36. export default {
  37. data() {
  38. let formData = {
  39. "permission_id": "",
  40. "permission_name": "",
  41. "comment": ""
  42. }
  43. return {
  44. formData,
  45. formOptions: {},
  46. rules: {
  47. ...getValidator(Object.keys(formData))
  48. }
  49. }
  50. },
  51. onLoad(e) {
  52. if (e.id) {
  53. const id = e.id
  54. this.formDataId = id
  55. this.getDetail(id)
  56. }
  57. },
  58. onReady() {
  59. this.$refs.form.setRules(this.rules)
  60. },
  61. methods: {
  62. /**
  63. * 触发表单提交
  64. */
  65. submit() {
  66. uni.showLoading({
  67. mask: true
  68. })
  69. this.$refs.form.validate().then((res) => {
  70. this.submitForm(res)
  71. }).catch(() => {
  72. uni.hideLoading()
  73. })
  74. },
  75. submitForm(value) {
  76. // 使用 clientDB 提交数据
  77. db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
  78. uni.showToast({
  79. title: '修改成功'
  80. })
  81. this.getOpenerEventChannel().emit('refreshData')
  82. setTimeout(() => uni.navigateBack(), 500)
  83. }).catch((err) => {
  84. uni.showModal({
  85. content: err.message || '请求服务失败',
  86. showCancel: false
  87. })
  88. }).finally(() => {
  89. uni.hideLoading()
  90. })
  91. },
  92. /**
  93. * 获取表单数据
  94. * @param {Object} id
  95. */
  96. getDetail(id) {
  97. uni.showLoading({
  98. mask: true
  99. })
  100. db.collection(dbCollectionName).doc(id).field("permission_id,permission_name,comment").get().then((res) => {
  101. const data = res.result.data[0]
  102. if (data) {
  103. this.formData = data
  104. }
  105. }).catch((err) => {
  106. uni.showModal({
  107. content: err.message || '请求服务失败',
  108. showCancel: false
  109. })
  110. }).finally(() => {
  111. uni.hideLoading()
  112. })
  113. }
  114. }
  115. }
  116. </script>
  117. <style>
  118. ::v-deep .uni-forms-item__label {
  119. width: 90px !important;
  120. }
  121. </style>