index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="main">
  3. <uni-forms class="form" ref="form" :rules="rules" :modelValue="form" label-position="top" label-width='100'>
  4. <uni-forms-item label="建议描述(必填)" required name="content">
  5. <uni-easyinput type="textarea" v-model="form.content" placeholder="请输入建议描述" />
  6. </uni-forms-item>
  7. <uni-forms-item label="上传图片0/3(选填)" name="file">
  8. <upload class='upload' :list="form.file" name="file" :count="3" @uplSuc="uplSuc" @uplDel="uplDel">
  9. </upload>
  10. </uni-forms-item>
  11. <uni-forms-item label="联系方式(选填)" name="phone">
  12. <uni-easyinput v-model="form.phone" placeholder="请输入您的手机号" />
  13. </uni-forms-item>
  14. </uni-forms>
  15. <button class="button" type="primary" @click="submit('form')">提交</button>
  16. </view>
  17. </template>
  18. <script>
  19. import upload from '../../components/upload/index.vue';
  20. export default {
  21. components: {
  22. upload
  23. },
  24. data() {
  25. return {
  26. user: {},
  27. form: {
  28. file: []
  29. },
  30. // 校验规则
  31. rules: {
  32. content: {
  33. rules: [{
  34. required: true,
  35. errorMessage: '请输入建议描述'
  36. }]
  37. },
  38. },
  39. }
  40. },
  41. onLoad: function(e) {
  42. const that = this;
  43. that.searchToken();
  44. },
  45. methods: {
  46. searchToken() {
  47. const that = this;
  48. try {
  49. const res = uni.getStorageSync('token');
  50. if (res) {
  51. that.$set(that, `user`, res);
  52. }
  53. } catch (e) {
  54. uni.showToast({
  55. title: err.errmsg,
  56. icon: 'error',
  57. duration: 2000
  58. });
  59. }
  60. },
  61. // 图片上传
  62. uplSuc(e) {
  63. const that = this;
  64. that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
  65. },
  66. // 图片删除
  67. uplDel(e) {
  68. const that = this;
  69. let data = that.form[e.name];
  70. let arr = data.filter((i, index) => index != e.data.index);
  71. that.$set(that.form, `${e.name}`, arr)
  72. },
  73. submit(ref) {
  74. const that = this;
  75. that.$refs[ref].validate().then(async data => {
  76. data.user = that.user._id
  77. const res = await that.$api(`/opinion`, 'POST', data);
  78. if (res.errcode == '0') {
  79. uni.showToast({
  80. title: '意见反馈成功',
  81. icon: 'none'
  82. })
  83. uni.navigateBack({
  84. delta: 1
  85. })
  86. } else {
  87. uni.showToast({
  88. title: res.errmsg,
  89. icon: 'none'
  90. })
  91. }
  92. }).catch(err => {
  93. console.log('err', err);
  94. })
  95. },
  96. }
  97. }
  98. </script>
  99. <style lang="scss" scoped>
  100. .main {
  101. padding: 4vw;
  102. .form-item {
  103. display: flex;
  104. align-items: center;
  105. }
  106. .button {
  107. background-color: var(--f3CColor);
  108. }
  109. }
  110. </style>