add.vue 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. <input placeholder="权限唯一标识,不可修改,不允许重复" @input="binddata('permission_id', $event.detail.value)" class="uni-input-border" v-model="formData.permission_id" trim="both" />
  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" />
  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. onReady() {
  52. this.$refs.form.setRules(this.rules)
  53. },
  54. methods: {
  55. /**
  56. * 触发表单提交
  57. */
  58. submit() {
  59. uni.showLoading({
  60. mask: true
  61. })
  62. this.$refs.form.validate().then((res) => {
  63. this.submitForm(res)
  64. }).catch(() => {
  65. uni.hideLoading()
  66. })
  67. },
  68. submitForm(value) {
  69. // 使用 clientDB 提交数据
  70. db.collection(dbCollectionName).add(value).then((res) => {
  71. uni.showToast({
  72. title: '新增成功'
  73. })
  74. this.getOpenerEventChannel().emit('refreshData')
  75. setTimeout(() => uni.navigateBack(), 500)
  76. }).catch((err) => {
  77. uni.showModal({
  78. content: err.message || '请求服务失败',
  79. showCancel: false
  80. })
  81. }).finally(() => {
  82. uni.hideLoading()
  83. })
  84. }
  85. }
  86. }
  87. </script>
  88. <style>
  89. ::v-deep .uni-forms-item__label {
  90. width: 90px !important;
  91. }
  92. </style>