skuSelect.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import {
  11. getCategoryList,
  12. getProductslist,
  13. getAttr,
  14. postCartNum
  15. } from '@/api/store.js';
  16. import {cartDel} from "@/api/order.js";
  17. import {toLogin} from '@/libs/login.js';
  18. export default {
  19. data() {
  20. return {
  21. attr: {
  22. cartAttr: false,
  23. productAttr: [],
  24. productSelect: {}
  25. },
  26. productValue: [],
  27. };
  28. },
  29. created() {
  30. },
  31. methods: {
  32. /**
  33. * 默认选中属性
  34. *
  35. */
  36. DefaultSelect: function() {
  37. let productAttr = this.attr.productAttr;
  38. let value = [];
  39. for (let key in this.productValue) {
  40. if (this.productValue[key].stock > 0) {
  41. value = this.attr.productAttr.length ? key.split(",") : [];
  42. break;
  43. }
  44. }
  45. for (let i = 0; i < productAttr.length; i++) {
  46. this.$set(productAttr[i], "index", value[i]);
  47. }
  48. //sort();排序函数:数字-英文-汉字;
  49. let productSelect = this.productValue[value.join(",")];
  50. this.$set(this.attr.productSelect,"store_name",this.storeName);
  51. if (productSelect && productAttr.length) {
  52. this.$set(this.attr.productSelect, "image", productSelect.image);
  53. this.$set(this.attr.productSelect, "price", productSelect.price);
  54. this.$set(this.attr.productSelect, "stock", productSelect.stock);
  55. this.$set(this.attr.productSelect, "unique", productSelect.unique);
  56. this.$set(this.attr.productSelect, "cart_num", 1);
  57. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  58. this.$set(this, "attrValue", value.join(","));
  59. } else if (!productSelect && productAttr.length) {
  60. this.$set(this.attr.productSelect, "image", this.storeInfo.image);
  61. this.$set(this.attr.productSelect, "price", this.storeInfo.price);
  62. this.$set(this.attr.productSelect, "stock", 0);
  63. this.$set(this.attr.productSelect, "unique", "");
  64. this.$set(this.attr.productSelect, "cart_num", 0);
  65. this.$set(this, "attrValue", "");
  66. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  67. } else if (!productSelect && !productAttr.length) {
  68. this.$set(this.attr.productSelect, "image", this.storeInfo.image);
  69. this.$set(this.attr.productSelect, "price", this.storeInfo.price);
  70. this.$set(this.attr.productSelect, "stock", this.storeInfo.stock);
  71. this.$set(this.attr.productSelect,"unique",this.storeInfo.unique || "");
  72. this.$set(this.attr.productSelect, "cart_num", 1);
  73. this.$set(this, "attrValue", "");
  74. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  75. }
  76. },
  77. /**
  78. * 属性变动赋值
  79. *
  80. */
  81. ChangeAttr: function(res) {
  82. let productSelect = this.productValue[res];
  83. if (productSelect && productSelect.stock >= 0) {
  84. this.$set(this.attr.productSelect, "image", productSelect.image);
  85. this.$set(this.attr.productSelect, "price", productSelect.price);
  86. this.$set(this.attr.productSelect, "stock", productSelect.stock);
  87. this.$set(this.attr.productSelect, "unique", productSelect.unique);
  88. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  89. this.$set(this.attr.productSelect, "cart_num", 1);
  90. this.$set(this, "attrValue", res);
  91. } else {
  92. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  93. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  94. this.$set(this.attr.productSelect, 'stock', 0);
  95. this.$set(this.attr.productSelect, 'unique', '');
  96. this.$set(this.attr.productSelect, 'cart_num', 0);
  97. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  98. this.$set(this, 'attrValue', '');
  99. }
  100. },
  101. attrVal(val) {
  102. this.$set(this.attr.productAttr[val.indexw], 'index', this.attr.productAttr[val.indexw].attr_values[val
  103. .indexn]);
  104. },
  105. /**
  106. * 购物车手动填写
  107. *
  108. */
  109. iptCartNum: function(e) {
  110. this.$set(this.attr.productSelect, 'cart_num', e);
  111. },
  112. onMyEvent: function() {
  113. this.$set(this.attr, 'cartAttr', false);
  114. },
  115. // 改变多属性购物车
  116. ChangeCartNumDuo(changeValue) {
  117. //获取当前变动属性
  118. let productSelect = this.productValue[this.attrValue];
  119. //如果没有属性,赋值给商品默认库存
  120. if (productSelect === undefined && !this.attr.productAttr.length)
  121. productSelect = this.attr.productSelect;
  122. //无属性值即库存为0;不存在加减;
  123. if (productSelect === undefined) return;
  124. let stock = productSelect.stock || 0;
  125. let num = this.attr.productSelect;
  126. this.ChangeCartNum(changeValue, num, stock, 1);
  127. },
  128. // 改变单属性购物车
  129. ChangeCartNumDan(changeValue, index, item) {
  130. let num = this.tempArr[index];
  131. let stock = this.tempArr[index].stock;
  132. this.ChangeCartNum(changeValue, num, stock, 0, item.id);
  133. },
  134. ChangeSubDel: function(event) {
  135. let that = this,
  136. list = that.cartData.cartList,
  137. ids = [];
  138. list.forEach(item => {
  139. ids.push(item.id)
  140. });
  141. cartDel(ids.join(",")).then(res => {
  142. that.$set(that.cartData, 'cartList', []);
  143. that.cartData.iScart = false;
  144. that.totalPrice = 0.00;
  145. that.page = 1;
  146. that.loadend = false;
  147. that.tempArr = [];
  148. that.productslist();
  149. that.getCartNum();
  150. })
  151. },
  152. ChangeOneDel: function(id, index) {
  153. let that = this,
  154. list = that.cartData.cartList;
  155. cartDel(id.toString()).then(res => {
  156. list.splice(index, 1);
  157. if (!list.length) {
  158. that.cartData.iScart = false;
  159. that.page = 1;
  160. that.loadend = false;
  161. that.tempArr = [];
  162. that.productslist();
  163. };
  164. that.getCartNum();
  165. })
  166. },
  167. // 多规格加入购物车;
  168. goCatNum() {
  169. this.goCat(1, this.id, 1);
  170. },
  171. closeList(e) {
  172. this.$set(this.cartData, 'iScart', e);
  173. },
  174. // 已经加入购物车时的购物加减;
  175. ChangeCartList(changeValue, index) {
  176. let list = this.cartData.cartList;
  177. let num = list[index];
  178. let stock = list[index].trueStock;
  179. this.ChangeCartNum(changeValue, num, stock, 0, num.product_id, index, 1);
  180. if (!list.length) {
  181. this.cartData.iScart = false;
  182. this.page = 1;
  183. this.loadend = false;
  184. this.tempArr = [];
  185. this.productslist();
  186. }
  187. },
  188. // 购物车加减计算函数
  189. ChangeCartNum(changeValue, num, stock, isDuo, id, index, cart) {
  190. if (changeValue) {
  191. num.cart_num++;
  192. if (num.cart_num > stock) {
  193. if (isDuo) {
  194. this.$set(this.attr.productSelect, "cart_num", stock ? stock : 1);
  195. this.$set(this, "cart_num", stock ? stock : 1);
  196. } else {
  197. num.cart_num = stock ? stock : 0;
  198. this.$set(this, 'tempArr', this.tempArr);
  199. this.$set(this.cartData, 'cartList', this.cartData.cartList);
  200. }
  201. return this.$util.Tips({
  202. title: "该产品没有更多库存了"
  203. });
  204. } else {
  205. if (!isDuo) {
  206. if (cart) {
  207. this.goCat(0, id, 1, 1, num.product_attr_unique);
  208. this.getTotalPrice();
  209. } else {
  210. this.goCat(0, id, 1);
  211. }
  212. }
  213. }
  214. } else {
  215. num.cart_num--;
  216. if (num.cart_num == 0) {
  217. this.cartData.cartList.splice(index, 1);
  218. if (isDuo) {
  219. this.$set(this.attr.productSelect, "cart_num", 1);
  220. this.$set(this, "cart_num", 1);
  221. }
  222. }
  223. if (num.cart_num < 0) {
  224. if (isDuo) {
  225. this.$set(this.attr.productSelect, "cart_num", 1);
  226. this.$set(this, "cart_num", 1);
  227. } else {
  228. num.cart_num = 0;
  229. this.$set(this, 'tempArr', this.tempArr);
  230. this.$set(this.cartData, 'cartList', this.cartData.cartList);
  231. }
  232. } else {
  233. if (!isDuo) {
  234. if (cart) {
  235. this.goCat(0, id, 0, 1, num.product_attr_unique);
  236. this.getTotalPrice();
  237. } else {
  238. this.goCat(0, id, 0);
  239. }
  240. }
  241. }
  242. }
  243. this.tempArr.forEach((item)=>{
  244. if(item.id == id){
  245. item.cart_num = num.cart_num;
  246. }
  247. })
  248. },
  249. /*
  250. * 加入购物车
  251. */
  252. goCat: function(duo, id, type, cart, unique) {
  253. let that = this;
  254. if (duo) {
  255. let productSelect = that.productValue[this.attrValue];
  256. //如果有属性,没有选择,提示用户选择
  257. console.log(productSelect);
  258. if (
  259. that.attr.productAttr.length &&
  260. productSelect === undefined
  261. ) {
  262. return that.$util.Tips({
  263. title: "产品库存不足,请选择其它属性"
  264. });
  265. }
  266. }
  267. let q = {
  268. product_id: id,
  269. num: duo ? that.attr.productSelect.cart_num : 1,
  270. type: type,
  271. unique: duo ? that.attr.productSelect.unique : cart ? unique : ""
  272. };
  273. postCartNum(q)
  274. .then(function(res) {
  275. if (duo) {
  276. that.attr.cartAttr = false;
  277. // that.page = 1;
  278. // that.loadend = false;
  279. that.tempArr.forEach((item, index) => {
  280. if (item.id == that.id) {
  281. let arrtStock = that.attr.productSelect.stock
  282. let objNum = parseInt(item.cart_num) + parseInt(that.attr.productSelect.cart_num);
  283. item.cart_num = objNum > arrtStock?arrtStock:objNum
  284. }
  285. })
  286. // that.productslist();
  287. }
  288. that.$util.Tips({
  289. title: res.msg
  290. });
  291. that.getCartNum();
  292. if (!cart) {
  293. that.getCartList(1);
  294. }
  295. })
  296. .catch(err => {
  297. return that.$util.Tips({
  298. title: err
  299. });
  300. });
  301. },
  302. goCartDuo(item) {
  303. if (!this.isLogin) {
  304. toLogin();
  305. } else {
  306. if(item.cart_button == 0){
  307. if(item.is_presale_product){
  308. uni.navigateTo({
  309. url: `/pages/activity/goods_details/index?id=${item.id}&type=6`
  310. })
  311. }else{
  312. uni.navigateTo({
  313. url: `/pages/goods_details/index?id=${item.id}&fromType=1`
  314. })
  315. }
  316. }else{
  317. this.storeName = item.store_name;
  318. this.getAttrs(item.id);
  319. this.$set(this, 'id', item.id);
  320. this.$set(this.attr, 'cartAttr', true);
  321. }
  322. }
  323. },
  324. // 点击默认单属性购物车
  325. goCartDan(item, index) {
  326. if (!this.isLogin) {
  327. toLogin();
  328. } else {
  329. if(item.cart_button == 0){
  330. if(item.is_presale_product){
  331. uni.navigateTo({
  332. url: `/pages/activity/goods_details/index?id=${item.id}&type=6`
  333. })
  334. }else{
  335. uni.navigateTo({
  336. url: `/pages/goods_details/index?id=${item.id}&fromType=1`
  337. })
  338. }
  339. }else{
  340. this.tempArr[index].cart_num = 1;
  341. this.$set(this, 'tempArr', this.tempArr);
  342. this.goCat(0, item.id, 1);
  343. }
  344. }
  345. },
  346. }
  347. };