IndexSkuDialog.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <div class="check_sku">
  3. <el-dialog title="选择需要购买的课程" :visible.sync="skuModal" width="680px" class="appoint-modal"
  4. :close-on-click-modal="false" :close-on-press-escape="false" :before-close="cancel">
  5. <div v-loading="loading" class="contents">
  6. <div class="check_con">
  7. <div v-for="(item, index) in specList" :key="index" class="check_items">
  8. <div class="grades" v-if="specList.length > 1">{{ item.name }}</div>
  9. <div class="mains" v-if="index == 0 && specList.length > 1">
  10. <div class="grade_names">
  11. <div class="course_items" v-for="(child, c_index) in item.specAttrList" :key="c_index"
  12. :class="{ nactive: selectGoodIndex == c_index }" @click="selectGoodType(c_index)">
  13. {{ child.name }}
  14. </div>
  15. </div>
  16. </div>
  17. <div v-if="index == 1 || specList.length == 1" class="check_box">
  18. <el-checkbox-group v-model="checkedAttrs" @change="changeCheck">
  19. <el-checkbox v-for="spec in specAttrList" :label="spec.specAttributeId" :key="spec.specAttributeId">
  20. <div class="box_centen">{{ spec.name }}<span>{{ spec.standPrice
  21. === 0 ?
  22. '免费' : `¥${spec.standPrice}`
  23. }}</span></div>
  24. </el-checkbox>
  25. </el-checkbox-group>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. <div slot="footer" class="dialog-footer f_c_b">
  31. <div class="price">
  32. <div>
  33. <span>总价:</span><span><i>¥</i>{{ allPrice | toFixed }}</span>
  34. </div>
  35. <p>已选择{{ checkedAttrs.length }}个商品</p>
  36. </div>
  37. <el-button :disabled="
  38. checkedAttrs.length == 0" @click="confirms()" style="width: 136px" type="primary">{{ isCarOrBuy == 1 ?
  39. "加入购物车" : "立即购买"
  40. }}</el-button>
  41. </div>
  42. </el-dialog>
  43. </div>
  44. </template>
  45. <script>
  46. export default {
  47. name: "checkSku",
  48. props: {
  49. skuModal: {
  50. type: Boolean,
  51. default: false,
  52. },
  53. specTemplateId: {
  54. type: [String, Number],
  55. default: "",
  56. },
  57. isCarOrBuy: {
  58. type: Number,
  59. default: 1,
  60. },
  61. },
  62. data() {
  63. return {
  64. loading: false,
  65. specList: [], // 规格列表
  66. checkedAttrs: [],
  67. selectGoodIndex: 0,
  68. specAttrPriceList: []
  69. };
  70. },
  71. watch: {
  72. skuModal(newV, oldV) {
  73. if (newV) {
  74. this.checkedAttrs = []
  75. this.getSpecDetail();
  76. }
  77. },
  78. },
  79. computed: {
  80. selectId() {
  81. if (this.specList.length < 2) {
  82. return
  83. }
  84. return this.specList[0].specAttrList[this.selectGoodIndex].specAttributeId
  85. },
  86. specAttrList() {
  87. let arr = []
  88. let data = this.specList.slice(-1)
  89. data[0].specAttrList.forEach(ele => {
  90. let id = ele.specAttributeId
  91. if (this.specList.length > 1) {
  92. id = this.selectId + ',' + id
  93. }
  94. let item = this.specAttrPriceList.find(e => e.specAttrIds == id)
  95. if (item) {
  96. ele.goodsId = item.goodsId
  97. ele.standPrice = item.standPrice
  98. arr.push(ele)
  99. }
  100. });
  101. return arr
  102. },
  103. allPrice() {
  104. if (!this.checkedAttrs.length) {
  105. return 0
  106. }
  107. return this.specAttrList.reduce((a, b) => {
  108. if (this.checkedAttrs.includes(b.specAttributeId)) {
  109. return a + b.standPrice
  110. }
  111. return a
  112. }, 0)
  113. },
  114. isSingleChoice() {
  115. if (!this.specAttrPriceList.length) {
  116. return false
  117. }
  118. return this.specAttrPriceList[0].specialGoods
  119. }
  120. },
  121. methods: {
  122. changeCheck(val) {
  123. // 二建变单选
  124. if (this.isSingleChoice) {
  125. this.checkedAttrs = val.slice(-1)
  126. }
  127. },
  128. selectGoodType(index) {
  129. if (index == this.selectGoodIndex) {
  130. return
  131. }
  132. this.selectGoodIndex = index
  133. },
  134. cancel() {
  135. this.$emit("update:skuModal", false);
  136. },
  137. getSpecDetail() {
  138. this.loading = true;
  139. this.$axios({
  140. url: `/app/common/spec/${this.specTemplateId}`,
  141. method: "get",
  142. noToken: true,
  143. })
  144. .then((res) => {
  145. this.loading = false;
  146. if (res.code == 200) {
  147. let data = res.data;
  148. if (data) {
  149. this.specAttrPriceList = data.specAttrPriceList
  150. this.specList = data && (data.specList || []);
  151. }
  152. }
  153. })
  154. .catch(() => {
  155. this.loading = false;
  156. });
  157. },
  158. confirms() {
  159. let goodsIds = []
  160. let goodList = []
  161. this.specAttrList.forEach(e => {
  162. if (this.checkedAttrs.includes(e.specAttributeId) && !goodsIds.includes(e.goodsId)) {
  163. goodsIds.push(e.goodsId)
  164. goodList.push(this.specAttrPriceList.find(ele => ele.goodsId == e.goodsId))
  165. }
  166. })
  167. this.$request.checkGoodsStatus({ goodsIds }).then((res) => {
  168. if (this.isCarOrBuy == 1) {
  169. // 加入购物车
  170. this.$emit("toShopCart", goodsIds);
  171. } else {
  172. this.$emit("togoBuy", goodList);
  173. }
  174. this.cancel();
  175. }).catch((e) => {
  176. this.$message.warning(e.msg);
  177. })
  178. },
  179. },
  180. };
  181. </script>
  182. <style lang="scss" scoped>
  183. .appoint-modal {
  184. /deep/ {
  185. .el-dialog__header {
  186. padding: 20px 28px;
  187. border-bottom: 1px solid #d9d9d9;
  188. .el-dialog__title {
  189. color: #222222;
  190. font-size: 20px;
  191. }
  192. }
  193. .el-checkbox {
  194. width: 100%;
  195. background: #f8f8f8;
  196. margin-bottom: 16px;
  197. padding: 0 28px 0 16px;
  198. box-sizing: border-box;
  199. color: #222222;
  200. border-radius: 8px;
  201. display: flex;
  202. align-items: center;
  203. .el-checkbox__label {
  204. flex: 1;
  205. }
  206. &:nth-last-of-type(1) {
  207. margin-bottom: 0;
  208. }
  209. }
  210. .is-checked {
  211. background: #ebf2ff;
  212. }
  213. }
  214. .dialog-footer {
  215. padding: 0 8px;
  216. .price {
  217. p {
  218. font-size: 16px;
  219. color: #666666;
  220. text-align: left;
  221. }
  222. div {
  223. span {
  224. &:nth-of-type(1) {
  225. color: #222222;
  226. font-size: 16px;
  227. }
  228. &:nth-of-type(2) {
  229. i {
  230. font-size: 17px;
  231. font-style: normal;
  232. }
  233. font-weight: bold;
  234. color: #eb445a;
  235. font-size: 28px;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. .f_c_b {
  242. display: flex;
  243. align-items: center;
  244. justify-content: space-between;
  245. }
  246. }
  247. .contents {
  248. width: 100%;
  249. padding: 0 8px;
  250. .check_con {
  251. width: 100%;
  252. .check_items {
  253. margin-bottom: 32px;
  254. &:nth-last-of-type(1) {
  255. margin-bottom: 10px;
  256. }
  257. }
  258. .grades {
  259. font-size: 16px;
  260. color: #606266;
  261. margin-bottom: 16px;
  262. // margin-top: 32px;
  263. }
  264. .mains {
  265. width: 100%;
  266. max-height: 200px;
  267. overflow-y: auto;
  268. }
  269. .check_box {
  270. max-height: 400px;
  271. overflow-y: auto;
  272. &::-webkit-scrollbar {
  273. display: none;
  274. }
  275. .box_centen {
  276. height: 44px;
  277. line-height: 42px;
  278. display: flex;
  279. justify-content: space-between;
  280. span {
  281. font-weight: bold;
  282. color: #eb445a;
  283. font-size: 16px;
  284. font-family: DIN Alternate-Bold, DIN Alternate;
  285. }
  286. }
  287. }
  288. .grade_names {
  289. width: 100%;
  290. display: flex;
  291. flex-wrap: wrap;
  292. }
  293. .course_items {
  294. padding: 10px 16px;
  295. border-radius: 8px;
  296. margin-right: 16px;
  297. background: #f8f8f8;
  298. font-size: 14px;
  299. font-weight: 500;
  300. color: #222222;
  301. text-align: center;
  302. cursor: pointer;
  303. &.nactive {
  304. background: #ebf2ff;
  305. color: #3f8dfd;
  306. }
  307. }
  308. }
  309. }
  310. </style>