add.vue 3.1 KB

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