add.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view class="uni-container">
  3. <uni-forms ref="form" :model="formData" validateTrigger="bind">
  4. <uni-forms-item name="app_status" label="">
  5. <uni-easyinput placeholder="app客户端" v-model="formData.app_status"></uni-easyinput>
  6. </uni-forms-item>
  7. <uni-forms-item name="user_name" label="">
  8. <uni-easyinput placeholder="用户名称" v-model="formData.user_name"></uni-easyinput>
  9. </uni-forms-item>
  10. <uni-forms-item name="user_brand" label="">
  11. <uni-easyinput placeholder="手机品牌" v-model="formData.user_brand"></uni-easyinput>
  12. </uni-forms-item>
  13. <uni-forms-item name="user_model" label="">
  14. <uni-easyinput placeholder="手机型号" v-model="formData.user_model"></uni-easyinput>
  15. </uni-forms-item>
  16. <uni-forms-item name="api_src" label="">
  17. <uni-easyinput placeholder="接口请求地址" v-model="formData.api_src"></uni-easyinput>
  18. </uni-forms-item>
  19. <uni-forms-item name="request_parameters" label="">
  20. <uni-easyinput placeholder="请求参数" v-model="formData.request_parameters"></uni-easyinput>
  21. </uni-forms-item>
  22. <uni-forms-item name="response_parameters" label="">
  23. <uni-easyinput placeholder="返回参数" v-model="formData.response_parameters"></uni-easyinput>
  24. </uni-forms-item>
  25. <uni-forms-item name="api_status" label="">
  26. <uni-easyinput placeholder="接口状态" v-model="formData.api_status"></uni-easyinput>
  27. </uni-forms-item>
  28. <uni-forms-item name="error_info" label="">
  29. <uni-easyinput placeholder="接口状态" v-model="formData.error_info"></uni-easyinput>
  30. </uni-forms-item>
  31. <view class="uni-button-group">
  32. <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
  33. <navigator open-type="navigateBack" style="margin-left: 15px;">
  34. <button class="uni-button" style="width: 100px;">返回</button>
  35. </navigator>
  36. </view>
  37. </uni-forms>
  38. </view>
  39. </template>
  40. <script>
  41. import { validator } from '../../js_sdk/validator/cyt-logs.js';
  42. const db = uniCloud.database();
  43. const dbCmd = db.command;
  44. const dbCollectionName = 'cyt-logs';
  45. function getValidator(fields) {
  46. let result = {}
  47. for (let key in validator) {
  48. if (fields.includes(key)) {
  49. result[key] = validator[key]
  50. }
  51. }
  52. return result
  53. }
  54. export default {
  55. data() {
  56. let formData = {
  57. "app_status": "",
  58. "user_name": "",
  59. "user_brand": "",
  60. "user_model": "",
  61. "api_src": "",
  62. "request_parameters": "",
  63. "response_parameters": "",
  64. "api_status": "",
  65. "error_info": ""
  66. }
  67. return {
  68. formData,
  69. formOptions: {},
  70. rules: {
  71. ...getValidator(Object.keys(formData))
  72. }
  73. }
  74. },
  75. onReady() {
  76. this.$refs.form.setRules(this.rules)
  77. },
  78. methods: {
  79. /**
  80. * 验证表单并提交
  81. */
  82. submit() {
  83. uni.showLoading({
  84. mask: true
  85. })
  86. this.$refs.form.validate().then((res) => {
  87. return this.submitForm(res)
  88. }).catch(() => {
  89. }).finally(() => {
  90. uni.hideLoading()
  91. })
  92. },
  93. /**
  94. * 提交表单
  95. */
  96. submitForm(value) {
  97. // 使用 clientDB 提交数据
  98. return db.collection(dbCollectionName).add(value).then((res) => {
  99. uni.showToast({
  100. title: '新增成功'
  101. })
  102. this.getOpenerEventChannel().emit('refreshData')
  103. setTimeout(() => uni.navigateBack(), 500)
  104. }).catch((err) => {
  105. uni.showModal({
  106. content: err.message || '请求服务失败',
  107. showCancel: false
  108. })
  109. })
  110. }
  111. }
  112. }
  113. </script>