index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <template>
  2. <div class="app-container">
  3. <doc-alert title="功能开启" url="https://doc.iocoder.cn/mall/build/" />
  4. <!-- 搜索工作栏 -->
  5. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  6. <el-form-item label="活动名称" prop="name">
  7. <el-input v-model="queryParams.name" placeholder="请输入活动名称" clearable @keyup.enter.native="handleQuery"/>
  8. </el-form-item>
  9. <el-form-item label="活动状态" prop="status">
  10. <el-select v-model="queryParams.status" placeholder="请选择活动状态" clearable size="small">
  11. <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PROMOTION_ACTIVITY_STATUS)"
  12. :key="dict.value" :label="dict.label" :value="dict.value"/>
  13. </el-select>
  14. </el-form-item>
  15. <el-form-item label="创建时间" prop="createTime">
  16. <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
  17. range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
  18. </el-form-item>
  19. <el-form-item>
  20. <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
  21. <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
  22. </el-form-item>
  23. </el-form>
  24. <!-- 操作工具栏 -->
  25. <el-row :gutter="10" class="mb8">
  26. <el-col :span="1.5">
  27. <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
  28. v-hasPermi="['promotion:discount-activity:create']">新增</el-button>
  29. </el-col>
  30. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  31. </el-row>
  32. <!-- 列表 -->
  33. <el-table v-loading="loading" :data="list">
  34. <el-table-column label="活动名称" align="center" prop="name" />
  35. <el-table-column label="活动时间" align="center" prop="startTime" width="240">
  36. <template v-slot="scope">
  37. <div>开始:{{ parseTime(scope.row.startTime) }}</div>
  38. <div>结束:{{ parseTime(scope.row.endTime) }}</div>
  39. </template>
  40. </el-table-column>
  41. <el-table-column label="状态" align="center" prop="status">
  42. <template v-slot="scope">
  43. <dict-tag :type="DICT_TYPE.PROMOTION_ACTIVITY_STATUS" :value="scope.row.status" />
  44. </template>
  45. </el-table-column>
  46. <el-table-column label="创建时间" align="center" prop="createTime" width="180">
  47. <template v-slot="scope">
  48. <span>{{ parseTime(scope.row.createTime) }}</span>
  49. </template>
  50. </el-table-column>
  51. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  52. <template v-slot="scope">
  53. <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
  54. v-if="scope.row.status !== PromotionActivityStatusEnum.CLOSE.type"
  55. v-hasPermi="['promotion:discount-activity:update']">修改</el-button>
  56. <el-button size="mini" type="text" icon="el-icon-delete" @click="handleClose(scope.row)"
  57. v-if="scope.row.status !== PromotionActivityStatusEnum.CLOSE.type &&
  58. scope.row.status !== PromotionActivityStatusEnum.END.type"
  59. v-hasPermi="['promotion:discount-activity:close']">关闭</el-button>
  60. <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
  61. v-if="scope.row.status === PromotionActivityStatusEnum.CLOSE.type"
  62. v-hasPermi="['promotion:discount-activity:delete']">删除</el-button>
  63. </template>
  64. </el-table-column>
  65. </el-table>
  66. <!-- 分页组件 -->
  67. <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
  68. @pagination="getList"/>
  69. <!-- 对话框(添加 / 修改) -->
  70. <el-dialog :title="title" :visible.sync="open" width="1000px" v-dialogDrag append-to-body>
  71. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  72. <el-form-item label="活动名称" prop="name">
  73. <el-input v-model="form.name" placeholder="请输入活动名称" />
  74. </el-form-item>
  75. <el-form-item label="备注" prop="remark">
  76. <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
  77. </el-form-item>
  78. <el-form-item label="活动时间" prop="startAndEndTime">
  79. <el-date-picker clearable v-model="form.startAndEndTime" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
  80. value-format="timestamp" placeholder="选择开始时间" style="width: 880px" />
  81. </el-form-item>
  82. <el-form-item label="商品选择">
  83. <el-select v-model="form.skuIds" placeholder="请选择活动商品" clearable size="small"
  84. multiple filterable style="width: 880px" @change="changeFormSku">
  85. <el-option v-for="item in productSkus" :key="item.id" :label="item.spuName + ' ' + item.name" :value="item.id">
  86. <span style="float: left">{{ item.spuName }} &nbsp; {{ item.name}}</span>
  87. <span style="float: right; color: #8492a6; font-size: 13px">¥{{ (item.price / 100.0).toFixed(2) }}</span>
  88. </el-option>
  89. </el-select>
  90. <el-table v-loading="loading" :data="form.products">
  91. <el-table-column label="商品名称" align="center" width="200">
  92. <template v-slot="scope">
  93. {{ scope.row.spuName }} &nbsp; {{ scope.row.name}}
  94. </template>
  95. </el-table-column>
  96. <el-table-column label="商品价格" align="center" prop="price">
  97. <template v-slot="scope">
  98. ¥{{ (scope.row.price / 100.0).toFixed(2) }}
  99. </template>
  100. </el-table-column>
  101. <el-table-column label="库存" align="center" prop="stock" />
  102. <el-table-column label="优惠类型" align="center" property="discountType">
  103. <template v-slot="scope">
  104. <el-select v-model="scope.row.discountType" placeholder="请选择优惠类型">
  105. <el-option v-for="dict in getDictDatas(DICT_TYPE.PROMOTION_DISCOUNT_TYPE)"
  106. :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
  107. </el-select>
  108. </template>
  109. </el-table-column>
  110. <el-table-column label="优惠" align="center" prop="startTime" width="250">
  111. <template v-slot="scope">
  112. <el-form-item v-if="scope.row.discountType === PromotionDiscountTypeEnum.PRICE.type" prop="discountPrice">
  113. 减 <el-input-number v-model="scope.row.discountPrice" placeholder="请输入优惠金额"
  114. style="width: 190px" :precision="2" :min="0" :max="scope.row.price / 100.0 - 0.01" /> 元
  115. </el-form-item>
  116. <el-form-item v-if="scope.row.discountType === PromotionDiscountTypeEnum.PERCENT.type" prop="discountPercent">
  117. 打 <el-input-number v-model="scope.row.discountPercent" placeholder="请输入优惠折扣"
  118. style="width: 190px" :precision="1" :min="1" :max="9.9" /> 折
  119. </el-form-item>
  120. </template>
  121. </el-table-column>
  122. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  123. <template v-slot="scope">
  124. <el-button size="mini" type="text" icon="el-icon-delete" @click="removeFormSku(scope.row.skuId)">删除</el-button>
  125. </template>
  126. </el-table-column>
  127. </el-table>
  128. </el-form-item>
  129. </el-form>
  130. <div slot="footer" class="dialog-footer">
  131. <el-button type="primary" @click="submitForm">确 定</el-button>
  132. <el-button @click="cancel">取 消</el-button>
  133. </div>
  134. </el-dialog>
  135. </div>
  136. </template>
  137. <script>
  138. import {
  139. createDiscountActivity,
  140. updateDiscountActivity,
  141. deleteDiscountActivity,
  142. getDiscountActivity,
  143. getDiscountActivityPage,
  144. closeDiscountActivity
  145. } from "@/api/mall/promotion/discountActivity";
  146. import {
  147. PromotionActivityStatusEnum, PromotionDiscountTypeEnum,
  148. PromotionProductScopeEnum
  149. } from "@/utils/constants";
  150. import { getSkuOptionList } from "@/api/mall/product/sku";
  151. import { deepClone } from "@/utils";
  152. export default {
  153. name: "DiscountActivity",
  154. components: {
  155. },
  156. data() {
  157. return {
  158. // 遮罩层
  159. loading: true,
  160. // 导出遮罩层
  161. exportLoading: false,
  162. // 显示搜索条件
  163. showSearch: true,
  164. // 总条数
  165. total: 0,
  166. // 限时折扣活动列表
  167. list: [],
  168. // 弹出层名称
  169. title: "",
  170. // 是否显示弹出层
  171. open: false,
  172. // 查询参数
  173. queryParams: {
  174. pageNo: 1,
  175. pageSize: 10,
  176. name: null,
  177. status: null,
  178. createTime: [],
  179. },
  180. // 表单参数
  181. form: {
  182. skuIds: [], // 选中的 SKU
  183. products: [], // 商品信息
  184. },
  185. // 表单校验
  186. rules: {
  187. name: [{ required: true, message: "活动名称不能为空", trigger: "blur" }],
  188. startAndEndTime: [{ required: true, message: "活动时间不能为空", trigger: "blur" }],
  189. skuIds: [{ required: true, message: "选择商品不能为空", trigger: "blur" }],
  190. },
  191. // 商品 SKU 列表
  192. productSkus: [],
  193. // 如下的变量,主要为了 v-if 判断可以使用到
  194. PromotionProductScopeEnum: PromotionProductScopeEnum,
  195. PromotionActivityStatusEnum: PromotionActivityStatusEnum,
  196. PromotionDiscountTypeEnum: PromotionDiscountTypeEnum,
  197. };
  198. },
  199. created() {
  200. this.getList();
  201. },
  202. methods: {
  203. /** 查询列表 */
  204. getList() {
  205. this.loading = true;
  206. // 执行查询
  207. getDiscountActivityPage(this.queryParams).then(response => {
  208. this.list = response.data.list;
  209. this.total = response.data.total;
  210. this.loading = false;
  211. });
  212. // 获得 SKU 商品列表
  213. getSkuOptionList().then(response => {
  214. this.productSkus = response.data;
  215. });
  216. },
  217. /** 取消按钮 */
  218. cancel() {
  219. this.open = false;
  220. this.reset();
  221. },
  222. /** 表单重置 */
  223. reset() {
  224. this.form = {
  225. id: undefined,
  226. name: undefined,
  227. startAndEndTime: undefined,
  228. startTime: undefined,
  229. endTime: undefined,
  230. remark: undefined,
  231. skuIds: [],
  232. products: [],
  233. };
  234. this.resetForm("form");
  235. },
  236. /** 搜索按钮操作 */
  237. handleQuery() {
  238. this.queryParams.pageNo = 1;
  239. this.getList();
  240. },
  241. /** 重置按钮操作 */
  242. resetQuery() {
  243. this.resetForm("queryForm");
  244. this.handleQuery();
  245. },
  246. /** 新增按钮操作 */
  247. handleAdd() {
  248. this.reset();
  249. this.open = true;
  250. this.title = "添加限时折扣活动";
  251. },
  252. /** 修改按钮操作 */
  253. handleUpdate(row) {
  254. this.reset();
  255. const id = row.id;
  256. getDiscountActivity(id).then(response => {
  257. this.form = response.data;
  258. // 修改数据
  259. this.form.startAndEndTime = [response.data.startTime, response.data.endTime];
  260. this.form.skuIds = response.data.products.map(item => item.skuId);
  261. this.form.products.forEach(product => {
  262. // 获得对应的 SKU 信息
  263. const sku = this.productSkus.find(item => item.id === product.skuId);
  264. if (!sku) {
  265. return;
  266. }
  267. // 设置商品信息
  268. product.name = sku.name;
  269. product.spuName = sku.spuName;
  270. product.price = sku.price;
  271. product.stock = sku.stock;
  272. product.discountPrice = product.discountPrice !== undefined ? product.discountPrice / 100.0 : undefined;
  273. product.discountPercent = product.discountPercent !== undefined ? product.discountPercent / 10.0 : undefined;
  274. });
  275. // 打开弹窗
  276. this.open = true;
  277. this.title = "修改限时折扣活动";
  278. });
  279. },
  280. /** 提交按钮 */
  281. submitForm() {
  282. this.$refs["form"].validate(valid => {
  283. if (!valid) {
  284. return;
  285. }
  286. // 处理数据
  287. const data = deepClone(this.form); // 必须深拷贝,不然后面的 products 操作会有影响
  288. data.startTime = this.form.startAndEndTime[0];
  289. data.endTime = this.form.startAndEndTime[1];
  290. data.products.forEach(product => {
  291. product.discountPrice = product.discountPrice !== undefined ? product.discountPrice * 100 : undefined;
  292. product.discountPercent = product.discountPercent !== undefined ? product.discountPercent * 10 : undefined;
  293. });
  294. if (!valid) {
  295. return;
  296. }
  297. // 修改的提交
  298. if (this.form.id != null) {
  299. updateDiscountActivity(data).then(response => {
  300. this.$modal.msgSuccess("修改成功");
  301. this.open = false;
  302. this.getList();
  303. });
  304. return;
  305. }
  306. // 添加的提交
  307. createDiscountActivity(data).then(response => {
  308. this.$modal.msgSuccess("新增成功");
  309. this.open = false;
  310. this.getList();
  311. });
  312. });
  313. },
  314. /** 删除按钮操作 */
  315. handleDelete(row) {
  316. const id = row.id;
  317. this.$modal.confirm('是否确认删除限时折扣活动编号为"' + id + '"的数据项?').then(function() {
  318. return deleteDiscountActivity(id);
  319. }).then(() => {
  320. this.getList();
  321. this.$modal.msgSuccess("删除成功");
  322. }).catch(() => {});
  323. },
  324. /** 关闭按钮操作 */
  325. handleClose(row) {
  326. const id = row.id;
  327. this.$modal.confirm('是否确认关闭限时折扣活动编号为"' + id + '"的数据项?').then(function() {
  328. return closeDiscountActivity(id);
  329. }).then(() => {
  330. this.getList();
  331. this.$modal.msgSuccess("关闭成功");
  332. }).catch(() => {});
  333. },
  334. /** 当 Form 的 SKU 发生变化时 */
  335. changeFormSku(skuIds) {
  336. // 处理【新增】
  337. skuIds.forEach(skuId => {
  338. // 获得对应的 SKU 信息
  339. const sku = this.productSkus.find(item => item.id === skuId);
  340. if (!sku) {
  341. return;
  342. }
  343. // 判断已存在,直接跳过
  344. const product = this.form.products.find(item => item.skuId === skuId);
  345. if (product) {
  346. return;
  347. }
  348. this.form.products.push({
  349. skuId: sku.id,
  350. name: sku.name,
  351. price: sku.price,
  352. stock: sku.stock,
  353. spuId: sku.spuId,
  354. spuName: sku.spuName,
  355. discountType: PromotionDiscountTypeEnum.PRICE.type,
  356. });
  357. });
  358. // 处理【移除】
  359. this.form.products.map((product, index) => {
  360. if (!skuIds.includes(product.skuId)) {
  361. this.form.products.splice(index, 1);
  362. }
  363. });
  364. },
  365. /** 移除 Form 的 SKU */
  366. removeFormSku(skuId) {
  367. this.form.skuIds.map((id, index) => {
  368. if (skuId === id) {
  369. this.form.skuIds.splice(index, 1);
  370. }
  371. });
  372. this.changeFormSku(this.form.skuIds);
  373. },
  374. }
  375. }
  376. </script>