AddGood copy.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <div class="add">
  3. <el-card class="add-container">
  4. <el-form
  5. :model="state.goodForm"
  6. :rules="state.rules"
  7. ref="goodRef"
  8. label-width="140px"
  9. class="goodForm"
  10. >
  11. <el-form-item required label="商品所属店铺">
  12. <el-select
  13. style="width: 300px"
  14. v-model="state.goodForm.shopIds"
  15. filterable
  16. clearable
  17. placeholder="请选择店铺"
  18. >
  19. <el-option
  20. v-for="item in state.shopList"
  21. :key="item.id"
  22. :label="item.name"
  23. :value="item.id"
  24. />
  25. </el-select>
  26. </el-form-item>
  27. <el-form-item required label="商品分类">
  28. <el-select
  29. style="width: 300px"
  30. v-model="state.goodForm.goodsCategoryId"
  31. filterable
  32. clearable
  33. placeholder="请选择分类"
  34. >
  35. <el-option
  36. v-for="item in categoryList"
  37. :key="item.categoryId"
  38. :label="item.categoryName"
  39. :value="item.categoryId"
  40. />
  41. </el-select>
  42. </el-form-item>
  43. <el-form-item label="商品名称" prop="goodsName">
  44. <el-input
  45. style="width: 300px"
  46. v-model="state.goodForm.goodsName"
  47. placeholder="请输入商品名称"
  48. ></el-input>
  49. </el-form-item>
  50. <el-form-item label="商品简介" prop="goodsIntro">
  51. <el-input
  52. style="width: 300px"
  53. type="textarea"
  54. v-model="state.goodForm.goodsIntro"
  55. placeholder="请输入商品简介(100字)"
  56. ></el-input>
  57. </el-form-item>
  58. <el-form-item label="商品价格" prop="originalPrice">
  59. <el-input
  60. min="0"
  61. style="width: 300px"
  62. v-model="state.goodForm.originalPrice"
  63. placeholder="请输入商品价格"
  64. ></el-input>
  65. </el-form-item>
  66. <el-form-item label="商品售卖价" prop="sellingPrice">
  67. <el-input
  68. min="0"
  69. style="width: 300px"
  70. v-model="state.goodForm.sellingPrice"
  71. placeholder="请输入商品售价"
  72. ></el-input>
  73. </el-form-item>
  74. <el-form-item label="商品库存" prop="stockNum">
  75. <el-input
  76. type="number"
  77. min="0"
  78. style="width: 300px"
  79. v-model="state.goodForm.stockNum"
  80. placeholder="请输入商品库存"
  81. ></el-input>
  82. </el-form-item>
  83. <el-form-item label="商品标签" prop="tag">
  84. <el-input
  85. style="width: 300px"
  86. v-model="state.goodForm.tag"
  87. placeholder="请输入商品小标签"
  88. ></el-input>
  89. </el-form-item>
  90. <el-form-item label="上架状态" prop="goodsSellStatus">
  91. <el-radio-group v-model="state.goodForm.goodsSellStatus">
  92. <el-radio label="0">上架</el-radio>
  93. <el-radio label="1">下架</el-radio>
  94. </el-radio-group>
  95. </el-form-item>
  96. <el-form-item label="交易方式">
  97. <el-radio-group v-model="state.goodForm.tradeType">
  98. <el-radio :label="0">线下</el-radio>
  99. <el-radio :label="1">线上</el-radio>
  100. </el-radio-group>
  101. </el-form-item>
  102. <el-form-item required label="商品主图" prop="goodsCoverImg">
  103. <el-upload
  104. class="avatar-uploader"
  105. :show-file-list="false"
  106. :before-upload="beforeAvatarUpload"
  107. :http-request="simpleImgHttpUpload"
  108. >
  109. <img
  110. v-if="state.goodForm.goodsCoverImg"
  111. :src="state.goodForm.goodsCoverImg"
  112. class="avatar"
  113. />
  114. <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  115. </el-upload>
  116. </el-form-item>
  117. <el-form-item label="详情内容" prop="goodsDetailContent">
  118. <Editor v-model:value="state.goodForm.goodsDetailContent"></Editor>
  119. </el-form-item>
  120. <el-form-item>
  121. <el-button type="primary" @click="submitAdd()">{{
  122. state.id ? "立即修改" : "立即创建"
  123. }}</el-button>
  124. </el-form-item>
  125. </el-form>
  126. </el-card>
  127. </div>
  128. </template>
  129. <script lang="ts" setup>
  130. import {
  131. reactive,
  132. ref,
  133. onMounted,
  134. onBeforeUnmount,
  135. getCurrentInstance
  136. } from "vue";
  137. import { request, uploadToOSS } from "@/utils/index";
  138. import { ElMessage } from "element-plus";
  139. import type { UploadProps } from "element-plus";
  140. import { Plus } from "@element-plus/icons-vue";
  141. import Editor from "@/components/Editor.vue";
  142. import { useRoute, useRouter } from "vue-router";
  143. const { proxy } = getCurrentInstance();
  144. const editor = ref(null);
  145. const goodRef = ref(null);
  146. const route = useRoute();
  147. const router = useRouter();
  148. const { id } = route.query;
  149. const state = reactive({
  150. id: id,
  151. defaultCate: "",
  152. shopList: [],
  153. goodForm: {
  154. shopIds: undefined,
  155. goodsName: "",
  156. goodsCategoryId: undefined,
  157. goodsIntro: "",
  158. originalPrice: "",
  159. sellingPrice: "",
  160. stockNum: "",
  161. goodsSellStatus: "0",
  162. goodsCoverImg: "",
  163. goodsDetailContent: "",
  164. tradeType: 0,
  165. tag: ""
  166. },
  167. rules: {
  168. shopIds: [
  169. { required: "true", message: "请选择商品所属店铺", trigger: ["change"] }
  170. ],
  171. goodsName: [
  172. { required: "true", message: "请填写商品名称", trigger: ["change"] }
  173. ],
  174. originalPrice: [
  175. { required: "true", message: "请填写商品价格", trigger: ["change"] }
  176. ],
  177. sellingPrice: [
  178. { required: "true", message: "请填写商品售价", trigger: ["change"] }
  179. ],
  180. stockNum: [
  181. { required: "true", message: "请填写商品库存", trigger: ["change"] }
  182. ],
  183. // tag: [{ required: "true", message: "请填写商品标签", trigger: ["change"] }],
  184. goodsCoverImg: [
  185. { required: "true", message: "请上传商品图片", trigger: ["change"] }
  186. ],
  187. goodsDetailContent: [
  188. { required: "true", message: "请填写详情内容", trigger: ["change"] }
  189. ]
  190. },
  191. categoryId: "",
  192. category: {
  193. lazy: true,
  194. lazyLoad(node, resolve) {
  195. const { level = 0, value } = node;
  196. request
  197. .get("/mallService/mechanism/category", {
  198. params: {
  199. page: 1,
  200. pageSize: 1000,
  201. categoryLevel: level + 1,
  202. parentId: value || 0
  203. }
  204. })
  205. .then(res => {
  206. const list = res.data.list;
  207. const nodes = list.map(item => ({
  208. value: item.categoryId,
  209. label: item.categoryName,
  210. leaf: level > 1
  211. }));
  212. resolve(nodes);
  213. });
  214. }
  215. }
  216. });
  217. const categoryList = ref([]);
  218. let instance;
  219. const getShopList = () => {
  220. request
  221. .get("/articleService/serviceProvider/shop/paginate", {
  222. params: {
  223. page: 1,
  224. pageSize: 9999,
  225. type: -1,
  226. status: -1
  227. }
  228. })
  229. .then(resp => {
  230. state.shopList = resp.data.list;
  231. });
  232. };
  233. onMounted(async () => {
  234. console.log("加载成功");
  235. await initCategory();
  236. getShopList();
  237. if (id) {
  238. request.get(`/mallService/mechanism/goods/${id}`).then(res => {
  239. const {
  240. goods,
  241. firstCategory,
  242. secondCategory,
  243. thirdCategory,
  244. category,
  245. shopIds,
  246. tradeType
  247. } = res?.data;
  248. state.goodForm = {
  249. shopIds: shopIds?.[0] || undefined,
  250. goodsName: goods.goodsName,
  251. goodsIntro: goods.goodsIntro,
  252. originalPrice: goods.originalPrice / 100,
  253. sellingPrice: goods.sellingPrice / 100,
  254. stockNum: goods.stockNum,
  255. goodsSellStatus: String(goods.goodsSellStatus),
  256. goodsCoverImg: goods.goodsCoverImg,
  257. tag: goods.tag,
  258. goodsDetailContent: goods.goodsDetailContent,
  259. tradeType: tradeType,
  260. goodsCategoryId: category.categoryId
  261. };
  262. state.categoryId = goods.goodsCategoryId;
  263. state.defaultCate = `${firstCategory?.categoryName}/${secondCategory?.categoryName}/${thirdCategory.categoryName}`;
  264. if (instance) {
  265. // 初始化商品详情 html
  266. // instance.txt.html(goods.goodsDetailContent);
  267. }
  268. });
  269. }
  270. });
  271. onBeforeUnmount(() => {
  272. // instance.destroy();
  273. instance = null;
  274. });
  275. const initCategory = () => {
  276. return new Promise(resolve => {
  277. request
  278. .get("/mallService/mechanism/category", {
  279. params: {
  280. page: 1,
  281. pageSize: 1000
  282. }
  283. })
  284. .then(res => {
  285. const list = res.data.list;
  286. categoryList.value = list;
  287. resolve();
  288. });
  289. });
  290. };
  291. const submitAdd = () => {
  292. goodRef.value.validate(vaild => {
  293. if (vaild) {
  294. // 默认新增用 post 方法
  295. let params = {
  296. shopIds: [state.goodForm.shopIds],
  297. goodsCategoryId: Number(state.goodForm.goodsCategoryId),
  298. goodsCoverImg: state.goodForm.goodsCoverImg,
  299. goodsDetailContent: state.goodForm.goodsDetailContent,
  300. goodsIntro: state.goodForm.goodsIntro,
  301. goodsName: state.goodForm.goodsName,
  302. goodsSellStatus: Number(state.goodForm.goodsSellStatus),
  303. originalPrice: Number(state.goodForm.originalPrice) * 100,
  304. sellingPrice: Number(state.goodForm.sellingPrice) * 100,
  305. stockNum: Number(state.goodForm.stockNum),
  306. tag: state.goodForm.tag,
  307. tradeType: state.goodForm.tradeType
  308. };
  309. console.log("params", params);
  310. if (id) {
  311. params.goodsId = Number(id);
  312. // 修改商品使用 put 方法
  313. request.put("/mallService/mechanism/goods", params).then(resp => {
  314. ElMessage.success(resp.message);
  315. router.push({ path: "/shop/goods" });
  316. });
  317. } else {
  318. request.post("/mallService/mechanism/goods", params).then(resp => {
  319. ElMessage.success(resp.message);
  320. router.push({ path: "/shop/goods" });
  321. });
  322. }
  323. }
  324. });
  325. };
  326. const handleBeforeUpload = file => {
  327. const sufix = file.name.split(".")[1] || "";
  328. if (!["jpg", "jpeg", "png"].includes(sufix)) {
  329. ElMessage.error("请上传 jpg、jpeg、png 格式的图片");
  330. return false;
  331. }
  332. };
  333. const handleUrlSuccess = val => {
  334. state.goodForm.goodsCoverImg = val.data || "";
  335. };
  336. const handleChangeCate = val => {
  337. state.categoryId = val[2] || 0;
  338. };
  339. const beforeAvatarUpload: UploadProps["beforeUpload"] = rawFile => {
  340. if (!["image/png", "image/jpeg"].includes(rawFile.type)) {
  341. ElMessage.error("Avatar picture must be JPG format!");
  342. return false;
  343. } else if (rawFile.size / 1024 / 1024 > 2) {
  344. ElMessage.error("Avatar picture size can not exceed 2MB!");
  345. return false;
  346. }
  347. return true;
  348. };
  349. const simpleImgHttpUpload = options => {
  350. console.log("options", options);
  351. uploadToOSS(options.file, options.file.name).then(resp => {
  352. state.goodForm.goodsCoverImg = resp;
  353. });
  354. };
  355. </script>
  356. <style scoped>
  357. .add {
  358. display: flex;
  359. }
  360. .add-container {
  361. flex: 1;
  362. height: 100%;
  363. }
  364. .avatar-uploader {
  365. width: 160px;
  366. height: 160px;
  367. color: #ddd;
  368. font-size: 30px;
  369. }
  370. .avatar-uploader-icon {
  371. display: block;
  372. width: 160px;
  373. height: 160px;
  374. border: 1px solid #e9e9e9;
  375. padding: 32px 17px;
  376. display: flex;
  377. justify-content: center;
  378. }
  379. </style>