skuSelect.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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", 1);
  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", 1);
  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", 1);
  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. },
  125. onMyEvent: function() {
  126. this.$set(this.attr, 'cartAttr', false);
  127. },
  128. // 改变多属性购物车
  129. ChangeCartNumDuo(changeValue) {
  130. //获取当前变动属性
  131. let productSelect = this.productValue[this.attrValue];
  132. //如果没有属性,赋值给商品默认库存
  133. if (productSelect === undefined && !this.attr.productAttr.length)
  134. productSelect = this.attr.productSelect;
  135. //无属性值即库存为0;不存在加减;
  136. if (productSelect === undefined) return;
  137. let stock = productSelect.stock || 0;
  138. let num = this.attr.productSelect;
  139. this.ChangeCartNum(changeValue, num, stock, 1);
  140. },
  141. // 改变单属性购物车
  142. ChangeCartNumDan(changeValue, index, item) {
  143. let num = this.tempArr[index];
  144. let stock = this.tempArr[index].stock;
  145. this.ChangeCartNum(changeValue, num, stock, 0, item.id);
  146. },
  147. ChangeSubDel: function(event) {
  148. let that = this,
  149. list = that.cartData.cartList,
  150. ids = [];
  151. list.forEach(item => {
  152. ids.push(item.id)
  153. });
  154. cartDel(ids.join(",")).then(res => {
  155. that.$set(that.cartData, 'cartList', []);
  156. that.cartData.iScart = false;
  157. that.totalPrice = 0.00;
  158. that.page = 1;
  159. that.loadend = false;
  160. that.tempArr = [];
  161. that.productslist();
  162. that.getCartNum();
  163. })
  164. },
  165. ChangeOneDel: function(id, index) {
  166. let that = this,
  167. list = that.cartData.cartList;
  168. cartDel(id.toString()).then(res => {
  169. list.splice(index, 1);
  170. if (!list.length) {
  171. that.cartData.iScart = false;
  172. that.page = 1;
  173. that.loadend = false;
  174. that.tempArr = [];
  175. that.productslist();
  176. };
  177. that.getCartNum();
  178. })
  179. },
  180. // 多规格加入购物车;
  181. goCatNum() {
  182. this.goCat(1, this.id, 1);
  183. },
  184. closeList(e) {
  185. this.$set(this.cartData, 'iScart', e);
  186. },
  187. // 已经加入购物车时的购物加减;
  188. ChangeCartList(changeValue, index) {
  189. let list = this.cartData.cartList;
  190. let num = list[index];
  191. let stock = list[index].trueStock;
  192. this.ChangeCartNum(changeValue, num, stock, 0, num.product_id, index, 1);
  193. if (!list.length) {
  194. this.cartData.iScart = false;
  195. this.page = 1;
  196. this.loadend = false;
  197. this.tempArr = [];
  198. this.productslist();
  199. }
  200. },
  201. // 购物车加减计算函数
  202. ChangeCartNum(changeValue, num, stock, isDuo, id, index, cart) {
  203. if (changeValue) {
  204. num.cart_num++;
  205. if (num.cart_num > stock) {
  206. if (isDuo) {
  207. this.$set(this.attr.productSelect, "cart_num", stock ? stock : 1);
  208. this.$set(this, "cart_num", stock ? stock : 1);
  209. } else {
  210. num.cart_num = stock ? stock : 0;
  211. this.$set(this, 'tempArr', this.tempArr);
  212. this.$set(this.cartData, 'cartList', this.cartData.cartList);
  213. }
  214. return this.$util.Tips({
  215. title: "该产品没有更多库存了"
  216. });
  217. } else {
  218. if (!isDuo) {
  219. if (cart) {
  220. this.goCat(0, id, 1, 1, num.product_attr_unique);
  221. this.getTotalPrice();
  222. } else {
  223. this.goCat(0, id, 1);
  224. }
  225. }
  226. }
  227. } else {
  228. num.cart_num--;
  229. if (num.cart_num == 0) {
  230. this.cartData.cartList.splice(index, 1);
  231. if (isDuo) {
  232. this.$set(this.attr.productSelect, "cart_num", 1);
  233. this.$set(this, "cart_num", 1);
  234. }
  235. }
  236. if (num.cart_num < 0) {
  237. if (isDuo) {
  238. this.$set(this.attr.productSelect, "cart_num", 1);
  239. this.$set(this, "cart_num", 1);
  240. } else {
  241. num.cart_num = 0;
  242. this.$set(this, 'tempArr', this.tempArr);
  243. this.$set(this.cartData, 'cartList', this.cartData.cartList);
  244. }
  245. } else {
  246. if (!isDuo) {
  247. if (cart) {
  248. this.goCat(0, id, 0, 1, num.product_attr_unique);
  249. this.getTotalPrice();
  250. } else {
  251. this.goCat(0, id, 0);
  252. }
  253. }
  254. }
  255. }
  256. this.tempArr.forEach((item)=>{
  257. if(item.id == id){
  258. item.cart_num = num.cart_num;
  259. }
  260. })
  261. },
  262. /*
  263. * 加入购物车
  264. */
  265. goCat: function(duo, id, type, cart, unique) {
  266. let that = this;
  267. if (duo) {
  268. let productSelect = that.productValue[this.attrValue];
  269. //如果有属性,没有选择,提示用户选择
  270. console.log(productSelect);
  271. if (
  272. that.attr.productAttr.length &&
  273. productSelect === undefined
  274. ) {
  275. return that.$util.Tips({
  276. title: "产品库存不足,请选择其它属性"
  277. });
  278. }
  279. }
  280. let q = {
  281. product_id: id,
  282. num: duo ? that.attr.productSelect.cart_num : 1,
  283. type: type,
  284. unique: duo ? that.attr.productSelect.unique : cart ? unique : ""
  285. };
  286. postCartNum(q)
  287. .then(function(res) {
  288. if (duo) {
  289. that.attr.cartAttr = false;
  290. // that.page = 1;
  291. // that.loadend = false;
  292. that.tempArr.forEach((item, index) => {
  293. if (item.id == that.id) {
  294. let arrtStock = that.attr.productSelect.stock
  295. let objNum = parseInt(item.cart_num) + parseInt(that.attr.productSelect.cart_num);
  296. item.cart_num = objNum > arrtStock?arrtStock:objNum
  297. }
  298. })
  299. // that.productslist();
  300. }
  301. that.$util.Tips({
  302. title: res.msg
  303. });
  304. that.getCartNum();
  305. if (!cart) {
  306. that.getCartList(1);
  307. }
  308. })
  309. .catch(err => {
  310. return that.$util.Tips({
  311. title: err
  312. });
  313. });
  314. },
  315. goCartDuo(item,num) {
  316. if (!this.isLogin) {
  317. toLogin();
  318. } else {
  319. if(item.cart_button == 0){
  320. if(item.is_presale_product){
  321. uni.navigateTo({
  322. url: `/pages/activity/goods_details/index?id=${item.id}&type=6`
  323. })
  324. }else{
  325. //num:表示从首页点击进来的
  326. let page1 = `/pages/goods_details/index?id=${item.id}&fromType=1`;
  327. let page2 = `/pages/goods_details/index?id=${item.id}`;
  328. uni.navigateTo({
  329. url: num?page2:page1
  330. })
  331. }
  332. }else{
  333. this.storeName = item.store_name;
  334. this.getAttrs(item.id);
  335. this.$set(this, 'id', item.id);
  336. this.$set(this.attr, 'cartAttr', true);
  337. }
  338. }
  339. },
  340. // 点击默认单属性购物车
  341. goCartDan(item, index, num) {
  342. if (!this.isLogin) {
  343. toLogin();
  344. } else {
  345. if(item.cart_button == 0){
  346. if(item.is_presale_product){
  347. uni.navigateTo({
  348. url: `/pages/activity/goods_details/index?id=${item.id}&type=6`
  349. })
  350. }else{
  351. uni.navigateTo({
  352. url: `/pages/goods_details/index?id=${item.id}&fromType=1`
  353. })
  354. }
  355. }else{
  356. this.tempArr[index].cart_num = 1;
  357. // num表示从门店过来的;
  358. // if(num){
  359. // this.$store.commit('indexData/setCartNum', parseInt(this.cartNum)+1)
  360. // }
  361. this.$set(this, 'tempArr', this.tempArr);
  362. this.goCat(0, item.id, 1);
  363. }
  364. }
  365. },
  366. }
  367. };