add.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view class="content">
  3. <view class="one">
  4. <uni-forms ref="valiForm" :rules="rules" :modelValue="form" labelWidth="80px">
  5. <uni-forms-item label="名称" required name="name">
  6. <uni-easyinput v-model="form.name" placeholder="请输入名称" />
  7. </uni-forms-item>
  8. <uni-forms-item label="类型" required name="type">
  9. <uni-data-select v-model="form.type" :localdata="typeList" @change="typechange">
  10. </uni-data-select>
  11. </uni-forms-item>
  12. <uni-forms-item label="简介" required name="brief">
  13. <uni-easyinput type="textarea" autoHeight v-model="form.brief" placeholder="请输入简介"></uni-easyinput>
  14. </uni-forms-item>
  15. <uni-forms-item label="图片" required name="file">
  16. <upload class='upload' :list="form.file" name="file" :count="6" @uplSuc="uplSuc" @uplDel="uplDel"></upload>
  17. </uni-forms-item>
  18. </uni-forms>
  19. <button class="button" type="primary" @click="submit('valiForm')">修改</button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import upload from '../../components/upload/index.vue';
  25. export default {
  26. components: {
  27. upload
  28. },
  29. data() {
  30. return {
  31. id: '',
  32. form: {},
  33. // 校验规则
  34. rules: {
  35. name: {
  36. rules: [{
  37. required: true,
  38. errorMessage: '名称不能为空'
  39. }]
  40. },
  41. type: {
  42. rules: [{
  43. required: true,
  44. errorMessage: '类型不能为空'
  45. }]
  46. },
  47. brief: {
  48. rules: [{
  49. required: true,
  50. errorMessage: '简介不能为空'
  51. }]
  52. },
  53. file: {
  54. rules: [{
  55. required: true,
  56. errorMessage: '图片不能为空'
  57. }]
  58. },
  59. },
  60. // 字典表
  61. typeList: [],
  62. }
  63. },
  64. onLoad: async function(e) {
  65. const that = this;
  66. that.$set(that, `id`, e && e.id || '');
  67. await that.searchToken();
  68. await that.searchOther();
  69. },
  70. methods: {
  71. async searchToken() {
  72. const that = this;
  73. uni.getStorage({
  74. key: 'token',
  75. success: function(res) {
  76. that.$set(that.form, `supplier_id`, res.data._id);
  77. },
  78. fail: function(err) {
  79. uni.showToast({
  80. title: err.errmsg,
  81. icon: 'error',
  82. duration: 2000
  83. });
  84. }
  85. })
  86. },
  87. // 类型选择
  88. typechange(type) {
  89. const that = this;
  90. that.$set(that.form, `type`, type);
  91. },
  92. // 图片上传
  93. uplSuc(e) {
  94. const that = this;
  95. that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
  96. },
  97. // 图片删除
  98. uplDel(e) {
  99. const that = this;
  100. let data = that.form[e.name];
  101. let arr = data.filter((i, index) => index != e.data.index);
  102. that.$set(that.form, `${e.name}`, arr)
  103. },
  104. // 提交
  105. submit(ref) {
  106. const that = this;
  107. that.$refs[ref].validate().then(async params => {
  108. let res;
  109. if (that.id) res = await that.$api(`/goods/${that.id}`, 'POST', that.form);
  110. else res = await that.$api(`/goods`, 'POST', that.form);
  111. if (res.errcode == '0') {
  112. uni.showToast({
  113. title: '维护信息成功',
  114. icon: 'none'
  115. })
  116. uni.navigateBack({
  117. delta: 1
  118. })
  119. } else {
  120. uni.showToast({
  121. title: res.errmsg,
  122. icon: 'none'
  123. })
  124. }
  125. }).catch(err => {
  126. console.log('err', err);
  127. })
  128. },
  129. async searchOther() {
  130. const that = this;
  131. let res;
  132. //类型
  133. res = await that.$api('/DictData', 'GET', {
  134. type: 'goods_type',
  135. is_use: '0'
  136. })
  137. if (res.errcode == '0') {
  138. let typeList = []
  139. for (let val of res.data) {
  140. typeList.push({
  141. text: val.label,
  142. value: val.value
  143. })
  144. }
  145. that.$set(that, `typeList`, typeList);
  146. }
  147. },
  148. }
  149. }
  150. </script>
  151. <style lang="scss">
  152. .content {
  153. display: flex;
  154. flex-direction: column;
  155. .one {
  156. padding: 3vw;
  157. .button {
  158. margin: 2vw 0 0 0;
  159. background-color: var(--f3CColor);
  160. color: var(--mainColor);
  161. }
  162. }
  163. }
  164. </style>