add.vue 4.5 KB

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