index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body>
  3. <div class="app-container">
  4. <el-row :gutter="10" class="mb8">
  5. <!-- 移除新增按钮 -->
  6. </el-row>
  7. <!-- 列表 -->
  8. <el-table v-loading="loading" :data="skuList">
  9. <!-- <el-table-column type="selection" width="55" align="center" /> -->
  10. <!-- <el-table-column label="规格编号" align="center" prop="id" /> -->
  11. <el-table-column label="规格详情" align="center" width="200" :show-overflow-tooltip="true">
  12. <template slot-scope="scope">
  13. <template v-if="!scope.row.specValLists || scope.row.specValLists.length === 0">
  14. 默认
  15. </template>
  16. <template v-else>
  17. {{ scope.row.specValLists.join(',') }}
  18. </template>
  19. </template>
  20. </el-table-column>
  21. <el-table-column label="零售价(元)" align="center" width="180">
  22. <template slot-scope="scope">
  23. <el-input-number
  24. v-model="scope.row.salePrice"
  25. :precision="2"
  26. :step="0.1"
  27. :min="0"
  28. controls-position="right"
  29. :disabled="userInfoVO.userType != '00'"
  30. size="mini"
  31. @change="handlePriceStockChange(scope.row)" />
  32. </template>
  33. </el-table-column>
  34. <el-table-column label="成本价(元)" align="center" width="180" v-if="userInfoVO.userType != '02'">
  35. <template slot-scope="scope">
  36. <el-input-number
  37. v-model="scope.row.costPrice"
  38. :precision="2"
  39. :step="0.1"
  40. :min="0"
  41. controls-position="right"
  42. size="mini"
  43. @change="handlePriceStockChange(scope.row)" />
  44. </template>
  45. </el-table-column>
  46. <el-table-column :label="userInfoVO.userType == '00' ? '批发价(元)' : '采购价(元)'" align="center" width="180">
  47. <template slot-scope="scope">
  48. <el-input-number
  49. v-model="scope.row.purchasePrice"
  50. :precision="2"
  51. :step="0.1"
  52. :min="0"
  53. controls-position="right"
  54. size="mini"
  55. :disabled="userInfoVO.userType != '00'"
  56. @change="handlePriceStockChange(scope.row)" />
  57. </template>
  58. </el-table-column>
  59. <el-table-column label="库存" align="center" width="180">
  60. <template slot-scope="scope">
  61. <el-input-number
  62. v-model="scope.row.stock"
  63. :min="0"
  64. :step="1"
  65. controls-position="right"
  66. size="mini"
  67. :disabled="userInfoVO.userType != '00'"
  68. @change="handlePriceStockChange(scope.row)" />
  69. </template>
  70. </el-table-column>
  71. <el-table-column label="启用状态" align="center" width="120">
  72. <template slot-scope="scope">
  73. <el-switch
  74. v-model="scope.row.status"
  75. active-value="0"
  76. inactive-value="1"
  77. @change="handleStatusChange(scope.row)">
  78. </el-switch>
  79. </template>
  80. </el-table-column>
  81. </el-table>
  82. <!-- 分页 -->
  83. <pagination
  84. v-show="total > 0"
  85. :total="total"
  86. :page.sync="queryParams.pageNum"
  87. :limit.sync="queryParams.pageSize"
  88. @pagination="getList"
  89. />
  90. </div>
  91. <!-- SKU编辑弹窗 -->
  92. <el-dialog
  93. :title="editTitle"
  94. :visible.sync="editDialogVisible"
  95. width="50%"
  96. append-to-body
  97. >
  98. <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-if="Object.keys(form).length > 0">
  99. <div class="specvo_content">
  100. <div class="list_specvo">
  101. <div class="title">物料规格</div>
  102. <div v-for="(item, index) in specVoList" :key="index">
  103. <el-row :gutter="15">
  104. <el-col :span="10">
  105. <el-form-item label="规格名称">
  106. <el-input v-model="item.specName" disabled />
  107. </el-form-item>
  108. </el-col>
  109. <el-col :span="10">
  110. <el-form-item label="规格内容">
  111. <el-input v-model="item.specValue" disabled />
  112. </el-form-item>
  113. </el-col>
  114. </el-row>
  115. </div>
  116. </div>
  117. <el-row :gutter="20">
  118. <el-col :span="12">
  119. <el-form-item label="零售价(元)" prop="salePrice">
  120. <el-input-number v-model="form.salePrice" placeholder="请输入零售价" :precision="2" :step="0.1" :min="0" :max="9999999" style="width: 100%" />
  121. </el-form-item>
  122. </el-col>
  123. <el-col :span="12">
  124. <el-form-item label="成本价(元)" prop="costPrice" v-if="userInfoVO.userType != '02'">
  125. <el-input-number v-model="form.costPrice" placeholder="请输入成本价" :precision="2" :step="0.1" :min="0" :max="9999999" style="width: 100%" />
  126. </el-form-item>
  127. </el-col>
  128. <el-col :span="12">
  129. <el-form-item label="批发价(元)" prop="purchasePrice">
  130. <el-input-number v-model="form.purchasePrice" placeholder="请输入批发价" :precision="2" :step="0.1" :min="0" :max="9999999" :disabled="userInfoVO.userType != '00'" style="width: 100%" />
  131. </el-form-item>
  132. </el-col>
  133. <el-col :span="12">
  134. <el-form-item label="库存" prop="stock">
  135. <el-input-number v-model="form.stock" placeholder="请输入库存" :precision="0" :step="1" :min="0" :max="9999999" :disabled="userInfoVO.userType != '00'" style="width: 100%" />
  136. </el-form-item>
  137. </el-col>
  138. </el-row>
  139. </div>
  140. </el-form>
  141. <div slot="footer" class="dialog-footer">
  142. <el-button type="primary" @click="submitForm">确 定</el-button>
  143. <el-button @click="cancelEdit">取 消</el-button>
  144. </div>
  145. </el-dialog>
  146. </el-dialog>
  147. </template>
  148. <script>
  149. import { listSku, getSku, delSku, addSku, updateSku, updateGoodsStatus } from '@/api/core/sku'
  150. export default {
  151. name: 'GoodsSkuDialog',
  152. props: {
  153. title: {
  154. type: String,
  155. default: '规格管理'
  156. }
  157. },
  158. data() {
  159. return {
  160. dialogVisible: false,
  161. editDialogVisible: false,
  162. editTitle: '',
  163. loading: false,
  164. userInfoVO: null,
  165. goodsId: null,
  166. // 查询参数
  167. queryParams: {
  168. pageNum: 1,
  169. pageSize: 10,
  170. goodsId: null
  171. },
  172. // 总条数
  173. total: 0,
  174. // SKU列表数据
  175. skuList: [],
  176. // 表单参数
  177. form: {
  178. salePrice: 0,
  179. costPrice: 0,
  180. purchasePrice: 0
  181. },
  182. // 表单校验
  183. rules: {
  184. salePrice: [{ required: true, message: '零售价不能为空', trigger: 'blur' }],
  185. costPrice: [{ required: true, message: '成本价不能为空', trigger: 'blur' }],
  186. purchasePrice: [{ required: true, message: '批发价不能为空', trigger: 'blur' }]
  187. },
  188. specVoList: [
  189. {
  190. specName: '',
  191. specValue: ''
  192. }
  193. ]
  194. }
  195. },
  196. created() {
  197. this.userInfoVO = this.getUserInfo()
  198. },
  199. methods: {
  200. // 显示弹窗
  201. show(goodsId) {
  202. this.goodsId = goodsId
  203. this.queryParams.goodsId = goodsId
  204. this.dialogVisible = true
  205. this.getList()
  206. },
  207. // 获取SKU列表
  208. async getList() {
  209. this.loading = true
  210. try {
  211. const response = await listSku(this.queryParams)
  212. this.skuList = response.rows
  213. this.total = response.total
  214. } catch (error) {
  215. console.error('获取SKU列表失败:', error)
  216. } finally {
  217. this.loading = false
  218. }
  219. },
  220. // 新增SKU
  221. handleAdd() {
  222. this.reset()
  223. this.editDialogVisible = true
  224. this.editTitle = '添加规格'
  225. },
  226. // 修改SKU
  227. handleUpdate(row) {
  228. this.reset()
  229. getSku(row.id).then(response => {
  230. this.form = response.data
  231. this.specVoList = this.form.specVoList
  232. this.editDialogVisible = true
  233. this.editTitle = '修改价格和库存'
  234. })
  235. },
  236. // 删除SKU
  237. handleDelete(row) {
  238. this.$modal.confirm('是否确认删除该规格?').then(() => {
  239. return delSku(row.id)
  240. }).then(() => {
  241. this.getList()
  242. this.$modal.msgSuccess('删除成功')
  243. })
  244. },
  245. // 处理价格或库存变更
  246. handlePriceStockChange(row) {
  247. updateSku(this.handleUpdateParams(row)).then(() => {
  248. this.$modal.msgSuccess('更新成功')
  249. }).catch(() => {
  250. this.getList() // 更新失败时刷新数据
  251. })
  252. },
  253. // 状态变更
  254. handleStatusChange(row) {
  255. let text = row.status === '0' ? '启用' : '停用'
  256. this.$confirm('确认要' + text + '吗?')
  257. .then(function () {
  258. // 这里应该调用 updateSku 而不是 updateGoodsStatus
  259. return updateSku({
  260. id: row.id,
  261. status: row.status
  262. })
  263. })
  264. .then(() => {
  265. this.$modal.msgSuccess(text + '成功')
  266. // 可以选择是否刷新列表
  267. // this.getList()
  268. })
  269. .catch(() => {
  270. // 如果失败,恢复状态
  271. row.status = row.status === '0' ? '1' : '0'
  272. })
  273. },
  274. // 提交表单
  275. submitForm() {
  276. this.$refs.form.validate(valid => {
  277. if (valid) {
  278. updateSku(this.handleUpdateParams(this.form)).then(() => {
  279. this.$modal.msgSuccess('修改成功')
  280. this.editDialogVisible = false
  281. this.getList()
  282. })
  283. }
  284. })
  285. },
  286. // 重置表单
  287. reset() {
  288. this.form = {
  289. id: null,
  290. sort: null,
  291. status: null,
  292. salePrice: null,
  293. costPrice: null,
  294. purchasePrice: null,
  295. stock: null,
  296. specVoList: []
  297. }
  298. this.specVoList = []
  299. if (this.$refs.form) {
  300. this.$refs.form.resetFields()
  301. }
  302. },
  303. // 取消编辑
  304. cancelEdit() {
  305. this.editDialogVisible = false
  306. this.reset()
  307. },
  308. // 添加规格
  309. addSpecList() {
  310. if (this.specVoList.length < 2) {
  311. this.specVoList.push({
  312. specName: '',
  313. specValue: ''
  314. })
  315. } else {
  316. this.$modal.msgSuccess('最多添加2种规格')
  317. }
  318. },
  319. // 删除规格
  320. deleteSpecList(index) {
  321. this.specVoList.splice(index, 1)
  322. },
  323. // 处理更新参数
  324. handleUpdateParams(data) {
  325. if (this.userInfoVO.userType == '00') {
  326. return data
  327. }
  328. if (this.userInfoVO.userType == '01') {
  329. data.purchasePrice = null
  330. return data
  331. }
  332. if (this.userInfoVO.userType == '02') {
  333. data.costPrice = null
  334. data.purchasePrice = null
  335. return data
  336. }
  337. }
  338. }
  339. }
  340. </script>
  341. <style lang="scss" scoped>
  342. .specvo_content {
  343. padding: 5px 20px 20px;
  344. .list_specvo {
  345. padding: 0px 0px 10px;
  346. margin-bottom: 40px;
  347. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  348. border-radius: 5px;
  349. .title {
  350. color: #606266;
  351. padding: 10px 20px;
  352. border-bottom: 1px #f4f4f4 solid;
  353. font-size: 14px;
  354. margin-bottom: 20px;
  355. }
  356. }
  357. .remark {
  358. margin-top: 30px;
  359. background-color: rgba(255, 228, 74, 0.1);
  360. border: 1px dashed #ffe44a;
  361. font-size: 14px;
  362. color: #e6a23c;
  363. width: 100%;
  364. padding: 20px;
  365. border-radius: 5px;
  366. }
  367. }
  368. </style>