add-project.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view class="u-page">
  3. <!-- 注意,如果需要兼容微信小程序,最好通过setRules方法设置rules规则 -->
  4. <u--form labelPosition="left" :model="model1" ref="form1">
  5. <u-form-item labelWidth="110" label="网关ID" prop="addProject.gatewayDeviceId" borderBottom ref="item1">
  6. <u--input v-model="model1.addProject.gatewayDeviceId" border="none" placeholder="网关ID"></u--input>
  7. </u-form-item>
  8. <u-form-item label="测试项目说明" prop="addProject.description" borderBottom ref="item3" labelWidth="110">
  9. <u--textarea placeholder="不低于3个字" v-model="model1.addProject.description" count></u--textarea>
  10. </u-form-item>
  11. </u--form>
  12. <view class="" style="display: flex; margin-top: 100rpx;">
  13. <u-button type="primary" text="提交" @click="submit"></u-button>
  14. <u-button type="error" text="重置" @click="reset"></u-button>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. type: 0, //0、增加 1、更改
  23. value: '',
  24. model1: {
  25. addProject: {
  26. gatewayDeviceId: '',
  27. description: '',
  28. userId: uni.getStorageSync('userId')
  29. }
  30. },
  31. rules: {
  32. 'addProject.gatewayDeviceId': {
  33. type: 'string',
  34. required: true,
  35. message: '网关ID不能为空',
  36. trigger: ['change']
  37. },
  38. 'addProject.description': {
  39. type: 'string',
  40. min: 3,
  41. required: true,
  42. message: '不低于3个字',
  43. trigger: ['change']
  44. },
  45. },
  46. }
  47. },
  48. onReady() {
  49. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  50. this.$refs.form1.setRules(this.rules)
  51. },
  52. onLoad(option) {
  53. if (option.item) {
  54. this.type = 1
  55. let item = JSON.parse(option.item)
  56. this.model1.addProject.description = item.description
  57. this.model1.addProject.gatewayDeviceId = item.gatewayDeviceId
  58. // this.projectDetail=option.item
  59. } else {
  60. this.type = 0
  61. }
  62. },
  63. methods: {
  64. navigateBack() {
  65. uni.navigateBack()
  66. },
  67. sexSelect(e) {
  68. this.model1.userInfo.sex = e.name
  69. this.$refs.form1.validateField('userInfo.sex')
  70. },
  71. submit() {
  72. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  73. this.$refs.form1.validate().then(async res => {
  74. if (this.type == 1) {
  75. console.log('更改');
  76. } else {
  77. console.log('增加', this.model1
  78. .addProject);
  79. const {
  80. data: result
  81. } = await uni.$http.post("/api/v1/test/testProject", this.model1
  82. .addProject)
  83. console.log(result);
  84. if (result.code === 200) {
  85. uni.showToast({
  86. title: result.data,
  87. duration: 1500,
  88. icon: 'none'
  89. })
  90. } else {
  91. uni.showToast({
  92. title: result.msg,
  93. duration: 1500,
  94. icon: 'none'
  95. })
  96. }
  97. }
  98. }).catch(errors => {
  99. // uni.$u.toast('校验失败')
  100. })
  101. },
  102. reset() {
  103. const validateList = ['userInfo.name', 'intro']
  104. this.$refs.form1.resetFields()
  105. this.$refs.form1.clearValidate()
  106. this.model1.addProject = {
  107. gatewayDeviceId: '',
  108. description: '',
  109. userId: uni.getStorageSync('userId')
  110. }
  111. setTimeout(() => {
  112. this.$refs.form1.clearValidate(validateList)
  113. // 或者使用 this.$refs.form1.clearValidate()
  114. }, 10)
  115. },
  116. },
  117. }
  118. </script>
  119. <style lang="scss">
  120. .u-page {
  121. width: calc(100vw - 60rpx);
  122. margin: 20rpx 30rpx;
  123. }
  124. </style>