add.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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">
  17. </upload>
  18. </uni-forms-item>
  19. <uni-forms-item label="是否使用" required name="is_use">
  20. <uni-data-select v-model="form.is_use" :localdata="is_useList" @change="is_usechange">
  21. </uni-data-select>
  22. </uni-forms-item>
  23. </uni-forms>
  24. <button class="button" type="primary" @click="submit('valiForm')">确定</button>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import upload from '../../components/upload/index.vue';
  30. export default {
  31. components: {
  32. upload
  33. },
  34. data() {
  35. return {
  36. id: '',
  37. form: {
  38. file: []
  39. },
  40. // 校验规则
  41. rules: {
  42. name: {
  43. rules: [{
  44. required: true,
  45. errorMessage: '名称不能为空'
  46. }]
  47. },
  48. type: {
  49. rules: [{
  50. required: true,
  51. errorMessage: '类型不能为空'
  52. }]
  53. },
  54. brief: {
  55. rules: [{
  56. required: true,
  57. errorMessage: '简介不能为空'
  58. }]
  59. },
  60. file: {
  61. rules: [{
  62. required: true,
  63. errorMessage: '图片不能为空'
  64. }]
  65. },
  66. is_use: {
  67. rules: [{
  68. required: true,
  69. errorMessage: '是否使用不能为空'
  70. }]
  71. },
  72. },
  73. // 字典表
  74. typeList: [],
  75. is_useList: [],
  76. }
  77. },
  78. onLoad: async function(e) {
  79. const that = this;
  80. that.$set(that, `id`, e && e.id || '');
  81. that.searchToken();
  82. await that.searchOther();
  83. await that.search();
  84. },
  85. methods: {
  86. searchToken() {
  87. const that = this;
  88. try {
  89. const res = uni.getStorageSync('token');
  90. if (res) that.$set(that.form, `supplier_id`, res._id);
  91. } catch (e) {
  92. uni.showToast({
  93. title: err.errmsg,
  94. icon: 'error',
  95. duration: 2000
  96. });
  97. }
  98. },
  99. async search() {
  100. const that = this;
  101. if (that.id) {
  102. const res = await that.$api(`/Good/${that.id}`, 'GET', {})
  103. if (res.errcode == '0') {
  104. that.$set(that, `form`, res.data)
  105. } else {
  106. uni.showToast({
  107. title: res.errmsg,
  108. });
  109. }
  110. }
  111. },
  112. // 类型选择
  113. typechange(type) {
  114. const that = this;
  115. that.$set(that.form, `type`, type);
  116. },
  117. // 是否使用
  118. is_usechange(is_use) {
  119. const that = this;
  120. that.$set(that.form, `is_use`, is_use);
  121. },
  122. // 图片上传
  123. uplSuc(e) {
  124. const that = this;
  125. that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
  126. },
  127. // 图片删除
  128. uplDel(e) {
  129. const that = this;
  130. let data = that.form[e.name];
  131. let arr = data.filter((i, index) => index != e.data.index);
  132. that.$set(that.form, `${e.name}`, arr)
  133. },
  134. // 提交
  135. submit(ref) {
  136. const that = this;
  137. that.$refs[ref].validate().then(async params => {
  138. let res;
  139. if (that.id) res = await that.$api(`/Good/${that.id}`, 'POST', that.form);
  140. else res = await that.$api(`/Good`, 'POST', that.form);
  141. if (res.errcode == '0') {
  142. uni.showToast({
  143. title: '维护信息成功',
  144. icon: 'none'
  145. })
  146. uni.navigateBack({
  147. delta: 1
  148. })
  149. } else {
  150. uni.showToast({
  151. title: res.errmsg,
  152. icon: 'none'
  153. })
  154. }
  155. }).catch(err => {
  156. console.log('err', err);
  157. })
  158. },
  159. async searchOther() {
  160. const that = this;
  161. let res;
  162. //类型
  163. res = await that.$api('/DictData', 'GET', {
  164. type: 'goods_type',
  165. is_use: '0'
  166. })
  167. if (res.errcode == '0') {
  168. let typeList = []
  169. for (let val of res.data) {
  170. typeList.push({
  171. text: val.label,
  172. value: val.value
  173. })
  174. }
  175. that.$set(that, `typeList`, typeList);
  176. }
  177. //是否使用
  178. res = await that.$api('/DictData', 'GET', {
  179. type: 'is_use',
  180. is_use: '0'
  181. })
  182. if (res.errcode == '0') {
  183. let is_useList = []
  184. for (let val of res.data) {
  185. is_useList.push({
  186. text: val.label,
  187. value: val.value
  188. })
  189. }
  190. that.$set(that, `is_useList`, is_useList);
  191. }
  192. },
  193. }
  194. }
  195. </script>
  196. <style lang="scss">
  197. .content {
  198. display: flex;
  199. flex-direction: column;
  200. .one {
  201. padding: 3vw;
  202. .button {
  203. margin: 2vw 0 0 0;
  204. background-color: var(--f3CColor);
  205. color: var(--mainColor);
  206. }
  207. }
  208. }
  209. </style>