index.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. <template>
  2. <div class="app-container">
  3. <el-tabs v-model="goodsType" @tab-click="onChangeGoodsType">
  4. <el-tab-pane v-for="dict in dict.type.goods_type" :label="dict.label" :name="dict.value"></el-tab-pane>
  5. </el-tabs>
  6. <!-- 查询 -->
  7. <el-card class="search-card" shadow="never">
  8. <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="68px">
  9. <el-form-item prop="goodsName">
  10. <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
  11. </el-form-item>
  12. <el-form-item prop="brandId">
  13. <el-select v-model="queryParams.brandId" placeholder="请选择商品品牌" clearable>
  14. <el-option v-for="brand in brandList" :key="brand.id" :label="brand.brandName" :value="brand.id" />
  15. </el-select>
  16. </el-form-item>
  17. <el-form-item prop="goodsCategoryId">
  18. <el-select v-model="queryParams.goodsCategoryId" placeholder="请选择商品分类" clearable>
  19. <el-option v-for="category in categoryList" :key="category.id" :label="category.categoryName" :value="category.id" />
  20. </el-select>
  21. </el-form-item>
  22. <el-form-item prop="goodsCode">
  23. <el-input v-model="queryParams.goodsCode" placeholder="请输入商品编号" clearable @keyup.enter.native="handleQuery" />
  24. </el-form-item>
  25. <el-form-item>
  26. <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
  27. <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
  28. </el-form-item>
  29. </el-form>
  30. </el-card>
  31. <el-row :gutter="10" class="mb8">
  32. <el-col :span="1.5">
  33. <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPermi="['goods:commonGoods:add']"
  34. v-if="userInfoVO.userType == '00'">新增</el-button>
  35. </el-col>
  36. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  37. </el-row>
  38. <!-- 列表 -->
  39. <Page uri="/mapi/core/goods/list" :request-params="queryParams" ref="pagination">
  40. <el-table-column label="商品ID" align="center" prop="id" width="100" />
  41. <el-table-column label="商品图片" align="left" width="120">
  42. <template slot-scope="scope">
  43. <div class="goods-info">
  44. <el-image style="width: 90px; height: 90px; margin-right: 10px;" :src="scope.row.goodsImg" :preview-src-list="[scope.row.goodsImg]">
  45. </el-image>
  46. </div>
  47. </template>
  48. </el-table-column>
  49. <el-table-column label="商品编码" align="center" prop="goodsCode" width="120" />
  50. <el-table-column label="商品名称" align="center" prop="goodsName" min-width="180" />
  51. <el-table-column label="商品品牌" align="center" prop="brandName" width="150" />
  52. <el-table-column label="商品分类" align="center" prop="categoryName" width="150">
  53. <template slot-scope="scope">
  54. <el-tag size="mini" type="info" v-if="scope.row.goodsCategoryId" style="margin-left: 5px;">
  55. <span v-if="scope.row.goodsCategoryId == item.id" v-for="item in categoryList" :key="item.id">
  56. {{item.categoryName}}
  57. </span>
  58. </el-tag>
  59. </template>
  60. </el-table-column>
  61. <!-- <el-table-column label="价格/库存" align="center" width="200">
  62. <template slot-scope="scope">
  63. <div v-if="scope.row.skuList && scope.row.skuList.length > 0">
  64. <div>销售价:¥{{getMinMaxPrice(scope.row.skuList, 'salePrice')}}</div>
  65. <div>批发价:¥{{getMinMaxPrice(scope.row.skuList, 'purchasePrice')}}</div>
  66. <div>总库存:{{getTotalStock(scope.row.skuList)}}</div>
  67. </div>
  68. <div v-else>
  69. <div>暂无价格信息</div>
  70. </div>
  71. </template>
  72. </el-table-column> -->
  73. <el-table-column label="规格" align="center" width="120">
  74. <template slot-scope="scope">
  75. <el-button type="text" @click="openSkuDialog(scope.row)">
  76. <span>{{scope.row.goodsItemNum}}种规格</span>
  77. <i class="el-icon-arrow-right"></i>
  78. </el-button>
  79. </template>
  80. </el-table-column>
  81. <el-table-column label="支付方式" align="center" width="150">
  82. <template slot-scope="scope">
  83. <div>
  84. <el-tag v-if="scope.row.isBalancePay === 'Y'" size="mini" type="success">支持余额</el-tag>
  85. <el-tag v-else size="mini" type="info">不支持余额</el-tag>
  86. </div>
  87. <div style="margin-top: 5px;">
  88. <el-tag v-if="scope.row.isSell === 'Y'" size="mini" type="warning">促销标识</el-tag>
  89. </div>
  90. </template>
  91. </el-table-column>
  92. <el-table-column label="状态" align="center" width="120">
  93. <template slot-scope="scope">
  94. <div style="margin-top: 5px;">
  95. <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)">
  96. </el-switch>
  97. </div>
  98. </template>
  99. </el-table-column>
  100. <el-table-column label="排序" align="center" width="120">
  101. <template slot-scope="scope">
  102. <div>排序:{{scope.row.sort}}</div>
  103. </template>
  104. </el-table-column>
  105. <el-table-column label="创建时间" align="center" prop="createTime" width="150"></el-table-column>
  106. <el-table-column label="操作" align="center" width="200" fixed="right" v-if="userInfoVO.userType == '00'">
  107. <template slot-scope="scope">
  108. <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['goods:commonGoods:edit']"
  109. v-if="userInfoVO.userType == '00'">修改</el-button>
  110. <el-button type="text" icon="el-icon-view" v-if="goodsType != 1" @click="gotoComment(scope.row)">评论</el-button>
  111. <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['goods:commonGoods:remove']"
  112. v-if="userInfoVO.userType == '00'">删除</el-button>
  113. </template>
  114. </el-table-column>
  115. </Page>
  116. <!-- 分页 -->
  117. <!-- 添加或修改商品管理对话框 -->
  118. <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
  119. <el-form ref="form" :model="form" :rules="rules" label-width="120px">
  120. <el-tabs v-model="activeTab">
  121. <!-- 基本信息 tab -->
  122. <el-tab-pane label="基本信息" name="basic">
  123. <el-row :gutter="15">
  124. <el-col :span="12">
  125. <el-form-item label="商品名称" prop="goodsName">
  126. <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
  127. </el-form-item>
  128. </el-col>
  129. <el-col :span="12">
  130. <el-form-item label="商品编码" prop="goodsCode">
  131. <el-input v-model="form.goodsCode" placeholder="请输入商品编码" />
  132. </el-form-item>
  133. </el-col>
  134. </el-row>
  135. <el-row :gutter="15">
  136. <el-col :span="12">
  137. <el-form-item label="品牌" prop="brandId">
  138. <el-select v-model="form.brandId" placeholder="请选择品牌" clearable>
  139. <el-option v-for="brand in brandList" :key="brand.id" :label="brand.brandName" :value="brand.id" />
  140. </el-select>
  141. </el-form-item>
  142. </el-col>
  143. <el-col :span="12">
  144. <el-form-item label="商品分类" prop="goodsCategoryId">
  145. <el-select v-model="form.goodsCategoryId" placeholder="请选择商品分类" clearable>
  146. <el-option v-for="category in categoryList" :key="category.id" :label="category.categoryName" :value="category.id" />
  147. </el-select>
  148. </el-form-item>
  149. </el-col>
  150. </el-row>
  151. <el-form-item label="商品图片" prop="goodsImg">
  152. <image-upload v-model="form.goodsImg" />
  153. </el-form-item>
  154. <el-form-item label="商品图片集" prop="goodsImgs">
  155. <image-upload v-model="form.goodsImgs" :limit="limit" />
  156. </el-form-item>
  157. <el-form-item label="商品简介" prop="goodsSynopsis">
  158. <editor v-model="form.goodsSynopsis" :min-height="192" />
  159. </el-form-item>
  160. </el-tab-pane>
  161. <!-- 规格设置 tab -->
  162. <el-tab-pane label="规格设置" name="sku">
  163. <el-form-item label="规格类型">
  164. <el-radio-group v-model="form.skuType" @change="handleSkuTypeChange">
  165. <el-radio :label="0">单规格</el-radio>
  166. <el-radio :label="1">多规格</el-radio>
  167. </el-radio-group>
  168. </el-form-item>
  169. <!-- 单规格设置 -->
  170. <template v-if="form.skuType === 0">
  171. <el-form-item label="销售价格" prop="singleSkuPrice">
  172. <el-input-number v-model="form.singleSkuPrice" :precision="2" :step="0.1" :min="0" />
  173. </el-form-item>
  174. <el-form-item label="成本价格" prop="singleSkuCostPrice">
  175. <el-input-number v-model="form.singleSkuCostPrice" :precision="2" :step="0.1" :min="0" />
  176. </el-form-item>
  177. <el-form-item label="采购价格" prop="singleSkuPurchasePrice">
  178. <el-input-number v-model="form.singleSkuPurchasePrice" :precision="2" :step="0.1" :min="0" />
  179. </el-form-item>
  180. <el-form-item label="库存数量" prop="singleSkuStock">
  181. <el-input-number v-model="form.singleSkuStock" :min="0" :step="1" />
  182. </el-form-item>
  183. </template>
  184. <!-- 多规格设置 -->
  185. <template v-if="form.skuType === 1">
  186. <!-- 规格项设置 -->
  187. <div class="spec-group" v-for="(spec, index) in form.specList" :key="index" v-if="form.id==null">
  188. <el-row :gutter="10">
  189. <el-col :span="8">
  190. <el-form-item :label="'规格项' + (index + 1)">
  191. <el-input v-model="spec.name" placeholder="如: 颜色、尺码" />
  192. </el-form-item>
  193. </el-col>
  194. <el-col :span="14">
  195. <el-form-item label="规格值">
  196. <el-tag :key="tag" v-for="tag in spec.specValues" closable :disable-transitions="false"
  197. @close="handleRemoveSpecValue(spec.specValues, tag)">
  198. {{tag}}
  199. </el-tag>
  200. <el-input class="input-new-tag" v-if="spec.inputVisible" v-model="spec.inputValue" ref="tagInput" size="small"
  201. @keyup.enter.native="handleInputConfirm(spec, index)" @blur="handleInputConfirm(spec, index)">
  202. </el-input>
  203. <el-button v-else class="button-new-tag" size="small" @click="showInput(spec, index)">
  204. + 添加规格值
  205. </el-button>
  206. </el-form-item>
  207. </el-col>
  208. <el-col :span="2">
  209. <el-button type="text" @click="removeSpec(index)" icon="el-icon-delete">删除</el-button>
  210. </el-col>
  211. </el-row>
  212. </div>
  213. <el-button type="primary" plain @click="addSpec" v-if="form.id==null">添加规格项</el-button>
  214. <!-- 规格组合列表 -->
  215. <el-table :data="form.skuList" border style="margin-top: 20px">
  216. <el-table-column v-for="(spec, index) in form.specList" :key="index" :label="spec.name" align="center">
  217. <template slot-scope="scope">
  218. {{scope.row.specValLists[index]}}
  219. </template>
  220. </el-table-column>
  221. <el-table-column label="规格编码" align="center" width="150">
  222. <template slot-scope="scope">
  223. <el-input v-model="scope.row.skuCode" placeholder="请输入规格编码" size="mini" />
  224. </template>
  225. </el-table-column>
  226. <el-table-column label="销售价格" align="center" width="150">
  227. <template slot-scope="scope">
  228. <el-input-number v-model="scope.row.salePrice" :precision="2" :step="0.1" :min="0" controls-position="right" size="mini" />
  229. </template>
  230. </el-table-column>
  231. <el-table-column label="成本价格" align="center" width="150">
  232. <template slot-scope="scope">
  233. <el-input-number v-model="scope.row.costPrice" :precision="2" :step="0.1" :min="0" controls-position="right" size="mini" />
  234. </template>
  235. </el-table-column>
  236. <el-table-column label="采购价格" align="center" width="150">
  237. <template slot-scope="scope">
  238. <el-input-number v-model="scope.row.purchasePrice" :precision="2" :step="0.1" :min="0" controls-position="right" size="mini" />
  239. </template>
  240. </el-table-column>
  241. <el-table-column label="库存" align="center" width="150">
  242. <template slot-scope="scope">
  243. <el-input-number v-model="scope.row.stock" :min="0" :step="1" controls-position="right" size="mini" />
  244. </template>
  245. </el-table-column>
  246. </el-table>
  247. </template>
  248. </el-tab-pane>
  249. <!-- 其他设置 tab -->
  250. <el-tab-pane label="其他设置" name="other">
  251. <el-form-item label="促销标识" prop="isSell">
  252. <el-radio-group v-model="form.isSell">
  253. <el-radio-button v-for="dict in dict.type.sys_yes_no" :label="dict.value" :key="dict.code">
  254. {{dict.label}}
  255. </el-radio-button>
  256. </el-radio-group>
  257. </el-form-item>
  258. <el-form-item label="余额支付" prop="isBalancePay">
  259. <el-radio-group v-model="form.isBalancePay">
  260. <el-radio-button v-for="dict in dict.type.sys_yes_no" :label="dict.value" :key="dict.code">
  261. {{dict.label}}
  262. </el-radio-button>
  263. </el-radio-group>
  264. </el-form-item>
  265. <el-form-item label="显示顺序" prop="sort">
  266. <el-input-number v-model="form.sort" :min="0" :step="1" />
  267. </el-form-item>
  268. </el-tab-pane>
  269. </el-tabs>
  270. </el-form>
  271. <div slot="footer" class="dialog-footer">
  272. <el-button type="primary" @click="submitForm">确 定</el-button>
  273. <el-button @click="cancel">取 消</el-button>
  274. </div>
  275. </el-dialog>
  276. <!-- 添加规格弹窗组件 -->
  277. <goods-sku-dialog ref="skuDialog"></goods-sku-dialog>
  278. </div>
  279. </template>
  280. <script>
  281. import { listGoods, getGoods, delGoods, addGoods, updateGoods, updateGoodsStatus } from '@/api/core/goods'
  282. import { listCategory } from '@/api/core/category'
  283. import { listBrand } from '@/api/core/brand'
  284. import { status } from 'nprogress'
  285. import GoodsSkuDialog from '@/components/GoodsSkuDialog'
  286. export default {
  287. name: 'appGoods',
  288. components: {
  289. GoodsSkuDialog
  290. },
  291. dicts: ['goods_type', 'sys_yes_no'],
  292. data() {
  293. return {
  294. userInfoVO: null,
  295. uploadAction: `${process.env.VUE_APP_BASE_API}` + '/common/uploadOSS',
  296. imgsList: [],
  297. fileSize: 3,
  298. // 遮罩层
  299. loading: true,
  300. // 选中数组
  301. ids: [],
  302. // 非单个禁用
  303. single: true,
  304. // 非多个禁用
  305. multiple: true,
  306. // 显示搜索条件
  307. showSearch: true,
  308. // 总条数
  309. total: 0,
  310. // 商品管理表格数据
  311. goodsList: [],
  312. // 弹出层标题
  313. title: '',
  314. // 是否显示弹出层
  315. open: false,
  316. // 查询参数
  317. queryParams: {
  318. goodsName: null,
  319. goodsType: null,
  320. status: null,
  321. goodsCategoryId: null,
  322. goodsCode: null
  323. },
  324. queryParams2: {
  325. pageNum: 1,
  326. pageSize: 9999,
  327. status: '0',
  328. goodsType: null
  329. },
  330. // 表单参数
  331. form: {
  332. id: null,
  333. goodsName: null,
  334. goodsCategoryId: null,
  335. goodsSynopsis: null,
  336. brandId: null,
  337. goodsImg: null,
  338. goodsImgs: [],
  339. goodsCode: null,
  340. isSell: 'N',
  341. isCashPay: 'Y',
  342. cashMoney: null,
  343. isBalancePay: 'Y',
  344. createBy: null,
  345. createById: null,
  346. createTime: null,
  347. updateBy: null,
  348. updateById: null,
  349. updateTime: null,
  350. remark: null,
  351. skuType: 0,
  352. status: '0',
  353. specList: [],
  354. skuList: [],
  355. sort: '0',
  356. singleSkuPrice: 0,
  357. singleSkuCostPrice: 0,
  358. singleSkuPurchasePrice: 0,
  359. singleSkuStock: 0
  360. },
  361. // 表单校验
  362. rules: {
  363. goodsName: [
  364. {
  365. required: true,
  366. message: '商品名称不能为空',
  367. trigger: 'blur'
  368. }
  369. ],
  370. goodsCode: [
  371. {
  372. required: true,
  373. message: '商品编号不能为空',
  374. trigger: 'blur'
  375. }
  376. ],
  377. goodsSynopsis: [
  378. {
  379. required: true,
  380. message: '商品简介不能为空',
  381. trigger: 'blur'
  382. }
  383. ],
  384. goodsImg: [
  385. {
  386. required: true,
  387. message: '商品头图不能为空',
  388. trigger: 'blur'
  389. }
  390. ],
  391. goodsImgs: [
  392. {
  393. required: true,
  394. message: '商品图片集不能为空',
  395. trigger: 'blur'
  396. }
  397. ],
  398. isBalancePay: [
  399. {
  400. required: true,
  401. message: '是否余额支付不能为空',
  402. trigger: 'blur'
  403. }
  404. ],
  405. isCashPay: [
  406. {
  407. required: true,
  408. message: '是否现金支付不能为空',
  409. trigger: 'blur'
  410. }
  411. ],
  412. isSell: [
  413. {
  414. required: true,
  415. message: '是否促销不能为空',
  416. trigger: 'blur'
  417. }
  418. ],
  419. cashMoney: [
  420. {
  421. required: true,
  422. message: '现金金额不能为空',
  423. trigger: 'blur'
  424. }
  425. ]
  426. },
  427. categoryList: [],
  428. brandList: [],
  429. goodsType: '',
  430. limit: 9,
  431. activeTab: 'basic',
  432. skuType: 0
  433. }
  434. },
  435. created() {
  436. this.handleUser()
  437. this.goodsType = this.getUrlParam('goodsType')
  438. this.form.goodsType = this.goodsType
  439. this.getList()
  440. this.getCategoryList()
  441. this.getBrandList()
  442. },
  443. methods: {
  444. handleUser() {
  445. this.userInfoVO = this.getUserInfo()
  446. },
  447. onChangeGoodsType() {
  448. this.queryParams.goodsType = this.goodsType
  449. this.handleQuery()
  450. // 重新获取品牌和分类列表
  451. this.getBrandList()
  452. this.getCategoryList()
  453. },
  454. getBrandList() {
  455. listBrand({ ...this.queryParams2, goodsType: this.goodsType }).then(response => {
  456. this.brandList = response.rows || []
  457. })
  458. },
  459. getCategoryList() {
  460. listCategory({ ...this.queryParams2, goodsType: this.goodsType }).then(response => {
  461. this.categoryList = response.rows || []
  462. })
  463. },
  464. /** 查询商品管理列表 */
  465. getList() {
  466. this.queryParams.goodsType = this.goodsType
  467. this.$nextTick(() => {
  468. this.$refs.pagination.handleSearch(true)
  469. })
  470. },
  471. // 取消按钮
  472. cancel() {
  473. this.open = false
  474. this.reset()
  475. },
  476. // 表单重置
  477. reset() {
  478. this.form = {
  479. id: null,
  480. goodsType: this.goodsType,
  481. goodsName: null,
  482. goodsCategoryId: null,
  483. goodsSynopsis: null,
  484. brandId: null,
  485. goodsImg: null,
  486. goodsImgs: [],
  487. goodsCode: null,
  488. isSell: 'N',
  489. skuType: 0,
  490. isCashPay: 'Y',
  491. cashMoney: null,
  492. isBalancePay: 'Y',
  493. createBy: null,
  494. createById: null,
  495. createTime: null,
  496. updateBy: null,
  497. updateById: null,
  498. updateTime: null,
  499. remark: null,
  500. status: '0',
  501. sort: '0',
  502. specList: [],
  503. skuList: [],
  504. singleSkuPrice: 0,
  505. singleSkuCostPrice: 0,
  506. singleSkuPurchasePrice: 0,
  507. singleSkuStock: 0
  508. }
  509. this.resetForm('form')
  510. },
  511. /** 搜索按钮操作 */
  512. handleQuery() {
  513. this.getList()
  514. },
  515. /** 重置按钮操作 */
  516. resetQuery() {
  517. this.resetForm('queryForm')
  518. this.handleQuery()
  519. },
  520. // 多选框选中数据
  521. handleSelectionChange(selection) {
  522. this.ids = selection.map((item) => item.id)
  523. this.single = selection.length !== 1
  524. this.multiple = !selection.length
  525. },
  526. /** 新增按钮操作 */
  527. handleAdd() {
  528. this.reset()
  529. this.open = true
  530. this.title = '添加商品管理'
  531. },
  532. /** 修改按钮操作 */
  533. handleUpdate(row) {
  534. this.reset()
  535. this.imgsList = []
  536. const id = row.id || this.ids
  537. getGoods(id).then((response) => {
  538. this.form = response.data
  539. this.form.brandId = Number(this.form.brandId)
  540. this.form.goodsCategoryId = Number(this.form.goodsCategoryId)
  541. if (this.form.goodsImgs != null && this.form.goodsImgs != '') {
  542. this.form.goodsImgs.split(',').forEach((item) => {
  543. const vo = {
  544. name: '',
  545. url: item
  546. }
  547. this.imgsList.push(vo)
  548. })
  549. }
  550. this.open = true
  551. this.title = '修改商品管理'
  552. })
  553. },
  554. /** 提交按钮 */
  555. submitForm() {
  556. this.$refs['form'].validate(valid => {
  557. if (valid) {
  558. const formData = { ...this.form }
  559. // 处理SKU数据
  560. if (this.form.skuType === 0) {
  561. formData.skuList = [{
  562. specValLists: [],
  563. salePrice: this.form.singleSkuPrice,
  564. costPrice: this.form.singleSkuCostPrice,
  565. purchasePrice: this.form.singleSkuPurchasePrice,
  566. stock: this.form.singleSkuStock
  567. }]
  568. } else {
  569. formData.skuList = this.form.skuList
  570. }
  571. // 提交数据
  572. if (formData.id) {
  573. updateGoods(formData).then(response => {
  574. this.$modal.msgSuccess('修改成功')
  575. this.open = false
  576. this.getList()
  577. })
  578. } else {
  579. addGoods(formData).then(response => {
  580. this.$modal.msgSuccess('新增成功')
  581. this.open = false
  582. this.getList()
  583. })
  584. }
  585. }
  586. })
  587. },
  588. /** 删除按钮操作 */
  589. handleDelete(row) {
  590. const ids = row.id || this.ids
  591. this.$modal
  592. .confirm('是否确认删除商品管理编号为"' + ids + '"的数据项?')
  593. .then(function () {
  594. return delGoods(ids)
  595. })
  596. .then(() => {
  597. this.getList()
  598. this.$modal.msgSuccess('删除成功')
  599. })
  600. .catch(() => { })
  601. },
  602. /** 导出按钮操作 */
  603. handleExport() {
  604. this.download(
  605. 'commonGoods/commonGoods/export',
  606. {
  607. ...this.queryParams
  608. },
  609. `goods_${new Date().getTime()}.xlsx`
  610. )
  611. },
  612. checkClose(done) {
  613. this.$confirm('是否关闭表单,关闭后数据将丢失?')
  614. .then(function () {
  615. done()
  616. })
  617. .then(() => { })
  618. .catch(() => { })
  619. },
  620. handleStatusChange(row) {
  621. let text = row.status === '0' ? '启用' : '停用'
  622. this.$confirm('确认要' + text + '吗?')
  623. .then(function () {
  624. return updateGoodsStatus(row.id, row.status)
  625. })
  626. .then(() => {
  627. this.$modal.msgSuccess(text + '成功')
  628. })
  629. .catch(function () {
  630. row.status = row.status === '0' ? '1' : '0'
  631. })
  632. },
  633. /** 设置物料 */
  634. handleSkuUpdate(row) {
  635. this.openSkuDialog(row)
  636. },
  637. openSkuDialog(row) {
  638. this.$refs.skuDialog.show(row.id)
  639. },
  640. handleRemove(file, fileList) {
  641. console.log(fileList)
  642. this.imgsList = []
  643. fileList.forEach((item) => {
  644. const vo = {
  645. name: item.name,
  646. url: item.url
  647. }
  648. this.imgsList.push(vo)
  649. console.log(fileList)
  650. })
  651. },
  652. // 上传前校检格式和大小
  653. handleBeforeUpload(file) {
  654. // 校检文件大小
  655. if (this.fileSize) {
  656. const isLt = file.size / 1024 / 1024 < this.fileSize
  657. if (!isLt) {
  658. this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`)
  659. return false
  660. }
  661. }
  662. return true
  663. },
  664. handleUploadSuccessByImg(res, file) {
  665. // 获取富文本组件实例
  666. let quill = this.Quill
  667. // 如果上传成功
  668. if (res.code == 200) {
  669. // 获取光标所在位置
  670. this.form.goodsImg = res.data.src
  671. this.$forceUpdate()
  672. } else {
  673. this.$message.error('图片插入失败')
  674. }
  675. },
  676. handleUploadSuccessByImgs(res, file, fileList) {
  677. // 获取富文本组件实例
  678. let quill = this.Quill
  679. // 如果上传成功
  680. if (res.code == 200) {
  681. const vo = {
  682. name: '',
  683. url: res.data.src
  684. }
  685. this.imgsList.push(vo)
  686. this.$forceUpdate()
  687. } else {
  688. this.$message.error('图片插入失败')
  689. }
  690. },
  691. handleUploadError() {
  692. this.$message.error('图片插入失败')
  693. },
  694. handleExceed() {
  695. //提示最多只能上传9个
  696. this.$message.error('最多上传9个图片!')
  697. },
  698. gotoComment(row) {
  699. this.$router.push({ path: `/system/goodsComment/appGoodsComment?goodsType=${this.goodsType}&goodsId=${row.id}` })
  700. },
  701. handleSkuTypeChange(val) {
  702. if (val === 0) {
  703. this.form.specList = []
  704. this.form.skuList = []
  705. } else {
  706. // 如果已有规格数据,重新生成组合
  707. if (this.form.specList.length > 0) {
  708. this.generateSkuList()
  709. }
  710. }
  711. },
  712. addSpec() {
  713. if (!this.form.specList) {
  714. this.form.specList = []
  715. }
  716. this.form.specList.push({
  717. name: '',
  718. specValues: [],
  719. inputVisible: false,
  720. inputValue: ''
  721. })
  722. },
  723. removeSpec(index) {
  724. this.form.specList.splice(index, 1)
  725. this.$nextTick(() => {
  726. this.generateSkuList()
  727. })
  728. },
  729. handleRemoveSpecValue(values, tag) {
  730. values.splice(values.indexOf(tag), 1)
  731. this.$nextTick(() => {
  732. this.generateSkuList()
  733. })
  734. },
  735. showInput(spec, index) {
  736. // 先重置其他所有输入框的状态
  737. this.form.specList.forEach(item => {
  738. this.$set(item, 'inputVisible', false);
  739. });
  740. // 设置当前输入框可见
  741. this.$set(spec, 'inputVisible', true);
  742. // 强制更新视图
  743. this.$forceUpdate();
  744. // 等待 DOM 更新后设置焦点
  745. this.$nextTick(() => {
  746. if (this.$refs.tagInput && this.$refs.tagInput.length > 0) {
  747. const inputs = this.$refs.tagInput;
  748. // 找到最后一个输入框(应该是刚刚显示的那个)
  749. const input = Array.isArray(inputs) ? inputs[inputs.length - 1] : inputs;
  750. if (input && input.$el.querySelector('input')) {
  751. input.$el.querySelector('input').focus();
  752. }
  753. }
  754. });
  755. },
  756. handleInputConfirm(spec, index) {
  757. let inputValue = spec.inputValue
  758. if (inputValue && spec.specValues.indexOf(inputValue) === -1) {
  759. if (!spec.specValues) {
  760. spec.specValues = []
  761. }
  762. spec.specValues.push(inputValue)
  763. this.$nextTick(() => {
  764. this.generateSkuList()
  765. })
  766. }
  767. spec.inputVisible = false
  768. spec.inputValue = ''
  769. },
  770. generateSkuList() {
  771. const specs = this.form.specList
  772. if (specs.length === 0 || specs.some(spec => spec.specValues.length === 0)) {
  773. this.form.skuList = []
  774. return
  775. }
  776. // 获取所有规格值的组合
  777. const values = specs.map(spec => spec.specValues)
  778. const combinations = this.cartesianProduct(values)
  779. // 保存现有的价格和库存数据
  780. const existingSkus = this.form.skuList || []
  781. // 生成新的SKU列表,保留已存在的价格和库存
  782. this.form.skuList = combinations.map(specs => {
  783. // 查找是否存在相同规格的旧数据
  784. const existingSku = existingSkus.find(old =>
  785. JSON.stringify(old.specValLists) === JSON.stringify(specs)
  786. )
  787. return {
  788. specValLists: specs,
  789. skuCode: existingSku ? existingSku.skuCode : '',
  790. salePrice: existingSku ? existingSku.salePrice : 0,
  791. costPrice: existingSku ? existingSku.costPrice : 0,
  792. purchasePrice: existingSku ? existingSku.purchasePrice : 0,
  793. stock: existingSku ? existingSku.stock : 0
  794. }
  795. })
  796. },
  797. cartesianProduct(arrays) {
  798. return arrays.reduce((a, b) => {
  799. return a.map(x => {
  800. return b.map(y => {
  801. return Array.isArray(x) ? [...x, y] : [x, y]
  802. })
  803. }).flat()
  804. }, [[]])
  805. },
  806. /**
  807. * @description 添加规格值
  808. * @param {number} index 规格索引
  809. */
  810. addSpecValue(index) {
  811. // 确保规格值数组已初始化
  812. if (!this.form.specList[index].specValueList) {
  813. this.$set(this.form.specList[index], 'specValueList', [])
  814. }
  815. this.form.specList[index].specValueList.push({
  816. specValue: '',
  817. image: ''
  818. })
  819. },
  820. getMinMaxPrice(skuList, priceType) {
  821. if (!skuList || skuList.length === 0) return '0.00';
  822. const prices = skuList.map(sku => sku[priceType] || 0);
  823. const min = Math.min(...prices);
  824. const max = Math.max(...prices);
  825. return min === max ? min.toFixed(2) : `${min.toFixed(2)} ~ ${max.toFixed(2)}`;
  826. },
  827. getTotalStock(skuList) {
  828. if (!skuList || skuList.length === 0) return 0;
  829. return skuList.reduce((total, sku) => total + (sku.stock || 0), 0);
  830. }
  831. }
  832. }
  833. </script>
  834. <style lang="scss" scoped>
  835. .spec-group {
  836. padding: 10px;
  837. border: 1px solid #EBEEF5;
  838. margin-bottom: 10px;
  839. }
  840. .el-tag + .el-tag {
  841. margin-left: 10px;
  842. }
  843. .button-new-tag {
  844. margin-left: 10px;
  845. height: 32px;
  846. line-height: 30px;
  847. padding-top: 0;
  848. padding-bottom: 0;
  849. }
  850. .input-new-tag {
  851. width: 90px;
  852. margin-left: 10px;
  853. vertical-align: bottom;
  854. }
  855. .goods-info {
  856. display: flex;
  857. align-items: flex-start;
  858. padding: 10px 0;
  859. }
  860. .goods-detail {
  861. flex: 1;
  862. overflow: hidden;
  863. }
  864. .goods-name {
  865. font-size: 14px;
  866. font-weight: bold;
  867. color: #303133;
  868. margin-bottom: 5px;
  869. overflow: hidden;
  870. text-overflow: ellipsis;
  871. display: -webkit-box;
  872. -webkit-line-clamp: 2;
  873. -webkit-box-orient: vertical;
  874. }
  875. .goods-code {
  876. font-size: 12px;
  877. color: #909399;
  878. margin-bottom: 5px;
  879. }
  880. .goods-category {
  881. margin-top: 5px;
  882. }
  883. </style>