product-info.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <el-form :model="form" ref="queryForm" size="small" :rules="rules" :inline="true" label-width="120px">
  3. <el-row>
  4. <el-col :span="24">
  5. <el-form-item prop="spuDesc">
  6. <span slot="label">商品介绍</span>
  7. <el-input
  8. style="width: 700px"
  9. type="textarea"
  10. :rows="3"
  11. placeholder="请输入商品介绍"
  12. v-model="form.spuDesc"
  13. @input="passValue"
  14. >
  15. </el-input>
  16. </el-form-item>
  17. </el-col>
  18. <el-col :span="24">
  19. <el-form-item label="商品详情图" prop="spuName">
  20. <span class="tips">最多可上传10张</span>
  21. <el-upload
  22. list-type="picture-card"
  23. :file-list="spuDetailList"
  24. :before-remove="requestRemove"
  25. :http-request="(data) => requestUpload(data, 'detail')"
  26. :show-file-list="true"
  27. :limit="10"
  28. action="#"
  29. :multiple="true"
  30. accept=".png,.jpg,.jpeg"
  31. ref="files"
  32. >
  33. <i class="el-icon-plus avatar-uploader-icon"></i>
  34. </el-upload>
  35. </el-form-item>
  36. </el-col>
  37. <el-col :span="24">
  38. <el-form-item label="特殊资质图" prop="spuName">
  39. <span class="tips">例如农残检测报告等,最多可上传10张</span>
  40. <el-upload
  41. list-type="picture-card"
  42. :file-list="specialList"
  43. :before-remove="requestRemove1"
  44. :http-request="(data) => requestUpload(data, 'zz')"
  45. :show-file-list="true"
  46. :limit="10"
  47. action="#"
  48. :multiple="true"
  49. accept=".png,.jpg,.jpeg"
  50. ref="files"
  51. >
  52. <i class="el-icon-plus avatar-uploader-icon"></i>
  53. </el-upload>
  54. </el-form-item>
  55. </el-col>
  56. <el-col :span="24">
  57. <el-form-item prop="spuStatus">
  58. <span slot="label"> 发布状态</span>
  59. <el-radio-group v-model="form.spuStatus">
  60. <el-radio :label="0" :value="0">上架</el-radio>
  61. <el-radio :label="1" :value="1">下架</el-radio>
  62. </el-radio-group>
  63. </el-form-item>
  64. </el-col>
  65. </el-row>
  66. </el-form>
  67. </template>
  68. <script>
  69. import { getToken } from "@/utils/auth";
  70. import axios from "axios";
  71. export default {
  72. props: ["data"],
  73. data() {
  74. return {
  75. form: {
  76. spuDetailList: [],
  77. specialList: [],
  78. spuDesc: "",
  79. spuStatus: 0,
  80. },
  81. headers: { Auth: "Bearer " + getToken(), "Content-type": "multipart/form-data" },
  82. spuDetailList: [],
  83. specialList: [],
  84. index: 0,
  85. index1: 0,
  86. rules: {
  87. spuDesc: [{ required: true, message: "商品介绍不能为空", trigger: "blur" }],
  88. spuStatus: [{ required: true, message: "发布状态不能为空", trigger: "blur" }],
  89. },
  90. };
  91. },
  92. watch: {
  93. //监听info对象
  94. data: {
  95. handler(newVal, oldVal) {
  96. //监听info对象变化
  97. if (newVal) {
  98. this.form.spuDesc = newVal.spuDesc;
  99. this.spuDetailList = [];
  100. this.specialList = [];
  101. if (newVal.spuDetailList) {
  102. newVal.spuDetailList.forEach((e) => {
  103. this.spuDetailList.push({
  104. url: e.fileUrl,
  105. id: e.id,
  106. sort: e.sort,
  107. });
  108. });
  109. }
  110. if (newVal.specialList) {
  111. newVal.specialList.forEach((e) => {
  112. this.specialList.push({
  113. url: e.fileUrl,
  114. id: e.id,
  115. sort: e.sort,
  116. });
  117. });
  118. }
  119. }
  120. },
  121. deep: true, //深度监听
  122. immediate: true,
  123. },
  124. },
  125. methods: {
  126. formvalidate() {
  127. let that=this
  128. return new Promise((resolve) => {
  129. that.$refs.queryForm.validate((valid) => {
  130. console.log(valid);
  131. if (valid) {
  132. resolve(valid);
  133. } else {
  134. resolve(valid);
  135. }
  136. });
  137. });
  138. },
  139. passValue() {
  140. if (this.specialList) {
  141. this.form.specialList = [];
  142. for (let index = 0; index < this.specialList.length; index++) {
  143. this.form.specialList.push({
  144. fileUrl: this.specialList[index].url,
  145. sort: this.specialList[index].name,
  146. });
  147. }
  148. }
  149. if (this.spuDetailList) {
  150. this.form.spuDetailList = [];
  151. for (let index = 0; index < this.spuDetailList.length; index++) {
  152. this.form.spuDetailList.push({
  153. fileUrl: this.spuDetailList[index].url,
  154. sort: this.spuDetailList[index].name,
  155. });
  156. }
  157. }
  158. this.$emit("updateValue", this.form);
  159. },
  160. requestRemove1(val) {
  161. this.specialList.forEach((e, index) => {
  162. if (e.uid == val.uid) {
  163. this.specialList.splice(index, 1);
  164. }
  165. });
  166. this.passValue();
  167. },
  168. requestRemove(val) {
  169. this.spuDetailList.forEach((e, index) => {
  170. if (e.uid == val.uid) {
  171. this.spuDetailList.splice(index, 1);
  172. }
  173. });
  174. this.passValue();
  175. },
  176. // 可以获取图片参数
  177. async requestUpload(data, type) {
  178. const formdata = new FormData();
  179. formdata.append("file", data.file);
  180. const res = await axios.post(
  181. `${process.env.VUE_APP_NSY_UPLOAD_API}/api/third/op/v1/third/uploadPic`,
  182. formdata,
  183. {
  184. headers: this.headers,
  185. }
  186. );
  187. if (res.data.data) {
  188. if (type == "detail") {
  189. this.spuDetailList.push({
  190. name: this.index,
  191. url: res.data.data,
  192. });
  193. this.index++;
  194. } else if (type == "zz") {
  195. this.specialList.push({
  196. name: this.index1,
  197. url: res.data.data,
  198. });
  199. this.index1++;
  200. }
  201. this.passValue();
  202. this.$forceUpdate();
  203. }
  204. },
  205. },
  206. };
  207. </script>
  208. <style></style>