detail.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="main">
  3. <uni-forms class="form" ref="form" :rules="rules" :modelValue="form" label-width='90' label-position="top">
  4. <uni-forms-item label="标题" required name="title">
  5. <uni-easyinput v-model="form.title" placeholder="请输入标题" />
  6. </uni-forms-item>
  7. <uni-forms-item label="文件" required name="file">
  8. <upload class='upload' :list="form.file" name="file" :count="1" @uplSuc="uplSuc" @uplDel="uplDel">
  9. </upload>
  10. </uni-forms-item>
  11. </uni-forms>
  12. <button class="button" type="primary" @click="submit('form')">提交保存</button>
  13. </view>
  14. </template>
  15. <script>
  16. import upload from '../../components/upload/index.vue';
  17. export default {
  18. components: {
  19. upload
  20. },
  21. data() {
  22. return {
  23. id: '',
  24. user: {},
  25. form: {
  26. file: []
  27. },
  28. // 校验规则
  29. rules: {
  30. title: {
  31. rules: [{
  32. required: true,
  33. errorMessage: '请输入文件标题'
  34. }]
  35. }
  36. }
  37. }
  38. },
  39. onLoad: async function(e) {
  40. const that = this;
  41. that.$set(that, `id`, e && e.id || '');
  42. that.searchToken();
  43. that.search();
  44. },
  45. methods: {
  46. searchToken() {
  47. const that = this;
  48. try {
  49. const res = uni.getStorageSync('token');
  50. if (res) {
  51. const user = that.$jwt(res);
  52. that.$set(that, `user`, user);
  53. } else {
  54. uni.navigateTo({
  55. url: `/pages/login/index`
  56. })
  57. }
  58. } catch (e) {
  59. uni.showToast({
  60. title: err.errmsg,
  61. icon: 'error',
  62. duration: 2000
  63. });
  64. }
  65. },
  66. async search() {
  67. const that = this;
  68. if (that.id) {
  69. const res = await that.$api(`/policyfile/${that.id}`, 'GET', {}, 'freeLabel')
  70. if (res.errcode == '0') {
  71. that.$set(that, `form`, res.data)
  72. } else {
  73. uni.showToast({
  74. title: res.errmsg,
  75. });
  76. }
  77. }
  78. },
  79. // 文件上传
  80. uplSuc(e) {
  81. const that = this;
  82. that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
  83. },
  84. // 文件删除
  85. uplDel(e) {
  86. const that = this;
  87. let data = that.form[e.name];
  88. let arr = data.filter((i, index) => index != e.data.index);
  89. that.$set(that.form, `${e.name}`, arr)
  90. },
  91. submit(ref) {
  92. const that = this;
  93. const form = that.form;
  94. that.$refs[ref].validate().then(async data => {
  95. let res;
  96. if (form._id) res = await that.$api(`/policyfile/${form._id}`, 'POST', data, 'freeLabel');
  97. else res = await that.$api(`/policyfile`, 'POST', data, 'freeLabel');
  98. if (res.errcode == '0') {
  99. uni.showToast({
  100. title: '添加数据成功',
  101. icon: 'none'
  102. })
  103. uni.navigateBack({
  104. delta: 1
  105. })
  106. } else {
  107. uni.showToast({
  108. title: res.errmsg,
  109. icon: 'none'
  110. })
  111. }
  112. }).catch(err => {
  113. console.log('err', err);
  114. })
  115. }
  116. }
  117. }
  118. </script>
  119. <style lang="scss" scoped>
  120. .main {
  121. padding: 4vw;
  122. .form-item {
  123. display: flex;
  124. align-items: center;
  125. }
  126. .button {
  127. background-color: var(--f07CColor);
  128. }
  129. }
  130. </style>