skuSelect.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. updateFun(e,num){
  33. if(e.cartNum){
  34. this.tempArr.forEach((item)=>{
  35. if(item.id == e.id){
  36. item.cart_num = e.cartNum
  37. }
  38. })
  39. // 只有门店页面过来时调用
  40. if(num){
  41. this.getCartNum();
  42. }
  43. }
  44. },
  45. /**
  46. * 默认选中属性
  47. *
  48. */
  49. DefaultSelect: function() {
  50. let productAttr = this.attr.productAttr;
  51. let value = [];
  52. for (let key in this.productValue) {
  53. if (this.productValue[key].stock > 0) {
  54. value = this.attr.productAttr.length ? key.split(",") : [];
  55. break;
  56. }
  57. }
  58. for (let i = 0; i < productAttr.length; i++) {
  59. this.$set(productAttr[i], "index", value[i]);
  60. }
  61. //sort();排序函数:数字-英文-汉字;
  62. let productSelect = this.productValue[value.join(",")];
  63. this.$set(this.attr.productSelect,"store_name",this.storeName);
  64. if (productSelect && productAttr.length) {
  65. this.$set(this.attr.productSelect, "image", productSelect.image);
  66. this.$set(this.attr.productSelect, "price", productSelect.price);
  67. this.$set(this.attr.productSelect, "stock", productSelect.stock);
  68. this.$set(this.attr.productSelect, "unique", productSelect.unique);
  69. this.$set(this.attr.productSelect, "cart_num", this.storeInfo.min_qty);
  70. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  71. this.$set(this, "attrValue", value.join(","));
  72. } else if (!productSelect && productAttr.length) {
  73. this.$set(this.attr.productSelect, "image", this.storeInfo.image);
  74. this.$set(this.attr.productSelect, "price", this.storeInfo.price);
  75. this.$set(this.attr.productSelect, "stock", 0);
  76. this.$set(this.attr.productSelect, "unique", "");
  77. this.$set(this.attr.productSelect, "cart_num", 0);
  78. this.$set(this, "attrValue", "");
  79. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  80. } else if (!productSelect && !productAttr.length) {
  81. this.$set(this.attr.productSelect, "image", this.storeInfo.image);
  82. this.$set(this.attr.productSelect, "price", this.storeInfo.price);
  83. this.$set(this.attr.productSelect, "stock", this.storeInfo.stock);
  84. this.$set(this.attr.productSelect,"unique",this.storeInfo.unique || "");
  85. this.$set(this.attr.productSelect, "cart_num", this.storeInfo.min_qty);
  86. this.$set(this, "attrValue", "");
  87. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  88. }
  89. },
  90. /**
  91. * 属性变动赋值
  92. *
  93. */
  94. ChangeAttr: function(res) {
  95. let productSelect = this.productValue[res];
  96. if (productSelect && productSelect.stock >= 0) {
  97. this.$set(this.attr.productSelect, "image", productSelect.image);
  98. this.$set(this.attr.productSelect, "price", productSelect.price);
  99. this.$set(this.attr.productSelect, "stock", productSelect.stock);
  100. this.$set(this.attr.productSelect, "unique", productSelect.unique);
  101. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  102. this.$set(this.attr.productSelect, "cart_num", this.storeInfo.min_qty);
  103. this.$set(this, "attrValue", res);
  104. } else {
  105. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  106. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  107. this.$set(this.attr.productSelect, 'stock', 0);
  108. this.$set(this.attr.productSelect, 'unique', '');
  109. this.$set(this.attr.productSelect, 'cart_num', 0);
  110. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  111. this.$set(this, 'attrValue', '');
  112. }
  113. },
  114. attrVal(val) {
  115. this.$set(this.attr.productAttr[val.indexw], 'index', this.attr.productAttr[val.indexw].attr_values[val
  116. .indexn]);
  117. },
  118. /**
  119. * 购物车手动填写
  120. *
  121. */
  122. iptCartNum: function(e) {
  123. // this.$set(this.attr.productSelect, 'cart_num', e);
  124. if (e) {
  125. let number = this.storeInfo.min_qty;
  126. if (Number.isInteger(parseInt(e)) && parseInt(e) >= this.storeInfo.min_qty) {
  127. number = parseInt(e);
  128. }
  129. this.$nextTick((e) => {
  130. this.$set(this.attr.productSelect, 'cart_num', e < 0 ? this.storeInfo.min_qty : number);
  131. });
  132. }
  133. },
  134. onMyEvent: function() {
  135. this.$set(this.attr, 'cartAttr', false);
  136. },
  137. // 改变多属性购物车
  138. ChangeCartNumDuo(changeValue) {
  139. //获取当前变动属性
  140. let productSelect = this.productValue[this.attrValue];
  141. //如果没有属性,赋值给商品默认库存
  142. if (productSelect === undefined && !this.attr.productAttr.length)
  143. productSelect = this.attr.productSelect;
  144. //无属性值即库存为0;不存在加减;
  145. if (productSelect === undefined) return;
  146. let stock = productSelect.stock || 0;
  147. let num = this.attr.productSelect;
  148. this.ChangeCartNum(changeValue, num, stock, 1);
  149. },
  150. // 改变单属性购物车
  151. ChangeCartNumDan(changeValue, index, item) {
  152. let num = this.tempArr[index];
  153. let stock = this.tempArr[index].stock;
  154. this.ChangeCartNum(changeValue, num, stock, 0, item.id);
  155. },
  156. ChangeSubDel: function(event) {
  157. let that = this,
  158. list = that.cartData.cartList,
  159. ids = [];
  160. list.forEach(item => {
  161. ids.push(item.id)
  162. });
  163. cartDel(ids.join(",")).then(res => {
  164. that.$set(that.cartData, 'cartList', []);
  165. that.cartData.iScart = false;
  166. that.totalPrice = 0.00;
  167. that.page = 1;
  168. that.loadend = false;
  169. that.tempArr = [];
  170. that.productslist();
  171. that.getCartNum();
  172. })
  173. },
  174. ChangeOneDel: function(id, index) {
  175. let that = this,
  176. list = that.cartData.cartList;
  177. cartDel(id.toString()).then(res => {
  178. list.splice(index, 1);
  179. if (!list.length) {
  180. that.cartData.iScart = false;
  181. that.page = 1;
  182. that.loadend = false;
  183. that.tempArr = [];
  184. that.productslist();
  185. };
  186. that.getCartNum();
  187. })
  188. },
  189. // 多规格加入购物车;
  190. goCatNum() {
  191. this.goCat(1, this.id, 1);
  192. },
  193. closeList(e) {
  194. this.$set(this.cartData, 'iScart', e);
  195. },
  196. // 已经加入购物车时的购物加减;
  197. ChangeCartList(changeValue, index) {
  198. let list = this.cartData.cartList;
  199. let num = list[index];
  200. let stock = list[index].trueStock;
  201. this.ChangeCartNum(changeValue, num, stock, 0, num.product_id, index, 1);
  202. if (!list.length) {
  203. this.cartData.iScart = false;
  204. this.page = 1;
  205. this.loadend = false;
  206. this.tempArr = [];
  207. this.productslist();
  208. }
  209. },
  210. // 购物车加减计算函数
  211. ChangeCartNum(changeValue, num, stock, isDuo, id, index, cart) {
  212. if (changeValue) {
  213. num.cart_num++;
  214. if (num.cart_num > stock) {
  215. if (isDuo) {
  216. this.$set(this.attr.productSelect, 'cart_num', stock ? stock : this.storeInfo.min_qty);
  217. this.$set(this, 'cart_num', stock ? stock : 1);
  218. } else {
  219. num.cart_num = stock ? stock : 0;
  220. this.$set(this, 'tempArr', this.tempArr);
  221. this.$set(this.cartData, 'cartList', this.cartData.cartList);
  222. }
  223. return this.$util.Tips({
  224. title: "该产品没有更多库存了"
  225. });
  226. } else {
  227. if (!isDuo) {
  228. if (cart) {
  229. this.goCat(0, id, 1, 1, num.product_attr_unique);
  230. this.getTotalPrice();
  231. } else {
  232. this.goCat(0, id, 1);
  233. }
  234. }
  235. }
  236. } else {
  237. num.cart_num--;
  238. if (num.cart_num == 0) {
  239. this.cartData.cartList.splice(index, 1);
  240. if (isDuo) {
  241. this.$set(this.attr.productSelect, 'cart_num', this.storeInfo.min_qty);
  242. this.$set(this, 'cart_num', 1);
  243. }
  244. }
  245. if (num.cart_num < 0) {
  246. if (isDuo) {
  247. this.$set(this.attr.productSelect, 'cart_num', this.storeInfo.min_qty);
  248. this.$set(this, 'cart_num', 1);
  249. } else {
  250. num.cart_num = 0;
  251. this.$set(this, 'tempArr', this.tempArr);
  252. this.$set(this.cartData, 'cartList', this.cartData.cartList);
  253. }
  254. } else {
  255. if (!isDuo) {
  256. if (cart) {
  257. this.goCat(0, id, 0, 1, num.product_attr_unique);
  258. this.getTotalPrice();
  259. } else {
  260. this.goCat(0, id, 0);
  261. }
  262. }
  263. }
  264. }
  265. this.tempArr.forEach((item)=>{
  266. if(item.id == id){
  267. item.cart_num = num.cart_num;
  268. }
  269. })
  270. },
  271. /*
  272. * 加入购物车
  273. */
  274. goCat: function(duo, id, type, cart, unique) {
  275. let that = this;
  276. if (duo) {
  277. let productSelect = that.productValue[this.attrValue];
  278. //如果有属性,没有选择,提示用户选择
  279. if (
  280. that.attr.productAttr.length &&
  281. productSelect === undefined
  282. ) {
  283. return that.$util.Tips({
  284. title: "产品库存不足,请选择其它属性"
  285. });
  286. }
  287. }
  288. let q = {
  289. product_id: id,
  290. num: duo ? that.attr.productSelect.cart_num : 1,
  291. type: type,
  292. unique: duo ? that.attr.productSelect.unique : cart ? unique : ""
  293. };
  294. postCartNum(q)
  295. .then(function(res) {
  296. if (duo) {
  297. that.attr.cartAttr = false;
  298. // that.page = 1;
  299. // that.loadend = false;
  300. that.tempArr.forEach((item, index) => {
  301. if (item.id == that.id) {
  302. let arrtStock = that.attr.productSelect.stock
  303. let objNum = parseInt(item.cart_num) + parseInt(that.attr.productSelect.cart_num);
  304. item.cart_num = objNum > arrtStock?arrtStock:objNum
  305. }
  306. })
  307. // that.productslist();
  308. }
  309. that.$util.Tips({
  310. title: res.msg
  311. });
  312. that.getCartNum();
  313. if (!cart) {
  314. that.getCartList(1);
  315. }
  316. })
  317. .catch(err => {
  318. return that.$util.Tips({
  319. title: err
  320. });
  321. });
  322. },
  323. goCartDuo(item,num) {
  324. if (!this.isLogin) {
  325. toLogin();
  326. } else {
  327. if(item.cart_button == 0){
  328. if(item.is_presale_product){
  329. uni.navigateTo({
  330. url: `/pages/activity/goods_details/index?id=${item.id}&type=6`
  331. })
  332. }else{
  333. //num:表示从首页点击进来的
  334. let page1 = `/pages/goods_details/index?id=${item.id}&fromType=1`;
  335. let page2 = `/pages/goods_details/index?id=${item.id}`;
  336. uni.navigateTo({
  337. url: num?page2:page1
  338. })
  339. }
  340. }else{
  341. this.storeName = item.store_name;
  342. this.getAttrs(item.id);
  343. this.$set(this, 'id', item.id);
  344. this.$set(this.attr, 'cartAttr', true);
  345. }
  346. }
  347. },
  348. // 点击默认单属性购物车
  349. goCartDan(item, index, num) {
  350. if (!this.isLogin) {
  351. toLogin();
  352. } else {
  353. if(item.cart_button == 0){
  354. if(item.is_presale_product){
  355. uni.navigateTo({
  356. url: `/pages/activity/goods_details/index?id=${item.id}&type=6`
  357. })
  358. }else{
  359. uni.navigateTo({
  360. url: `/pages/goods_details/index?id=${item.id}&fromType=1`
  361. })
  362. }
  363. }else{
  364. this.tempArr[index].cart_num = 1;
  365. // num表示从门店过来的;
  366. // if(num){
  367. // this.$store.commit('indexData/setCartNum', parseInt(this.cartNum)+1)
  368. // }
  369. this.$set(this, 'tempArr', this.tempArr);
  370. this.goCat(0, item.id, 1);
  371. }
  372. }
  373. },
  374. }
  375. };