skuSelect.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { adminCartAdd, adminCartDel, adminCartNum } from "@/api/admin.js"
  2. export default {
  3. data() {
  4. return {
  5. attr: {
  6. cartAttr: false,
  7. productAttr: [],
  8. productSelect: {},
  9. deliveryType: [],
  10. },
  11. productValue: [],
  12. id: 0,
  13. };
  14. },
  15. created() {
  16. },
  17. methods: {
  18. /**
  19. * 默认选中属性
  20. *
  21. */
  22. DefaultSelect: function() {
  23. let productAttr = this.attr.productAttr;
  24. let valueobj = [];
  25. let value = [];
  26. for (var key in this.productValue) {
  27. if (this.productValue[key].stock > 0) {
  28. valueobj = this.attr.productAttr.length ? key.split(',') : [];
  29. break;
  30. }
  31. }
  32. // 处理已售罄时默认选中第一个
  33. if (!valueobj.length && this.attr.productAttr.length) {
  34. value = Object.keys(this.productValue)[0].split(',');
  35. } else {
  36. value = valueobj;
  37. }
  38. for (let i = 0; i < productAttr.length; i++) {
  39. this.$set(productAttr[i], 'index', value[i]);
  40. }
  41. //sort();排序函数:数字-英文-汉字;
  42. let productSelect = this.productValue[value.join(',')];
  43. this.$set(this.attr.productSelect, 'store_name', this.storeInfo.store_name);
  44. if (productSelect && productAttr.length) {
  45. this.$set(this.attr.productSelect, 'image', productSelect.image);
  46. this.$set(this.attr.productSelect, 'price', productSelect.price);
  47. this.$set(this.attr.productSelect, 'stock', productSelect.stock);
  48. this.$set(this.attr.productSelect, 'unique', productSelect.unique);
  49. this.$set(this.attr.productSelect, 'cart_num', 1);
  50. this.$set(this, 'attrValue', value.join(','));
  51. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  52. this.$set(this, 'attrTxt', '已选择');
  53. } else if (!productSelect && productAttr.length) {
  54. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  55. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  56. this.$set(this.attr.productSelect, 'stock', 0);
  57. this.$set(this.attr.productSelect, 'unique', '');
  58. this.$set(this.attr.productSelect, 'cart_num', 0);
  59. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  60. this.$set(this, 'attrValue', '');
  61. this.$set(this, 'attrTxt', '请选择');
  62. } else if (!productSelect && !productAttr.length) {
  63. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  64. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  65. this.$set(this.attr.productSelect, 'stock', this.storeInfo.stock);
  66. this.$set(this.attr.productSelect, 'unique', this.storeInfo.unique || '');
  67. this.$set(this.attr.productSelect, 'cart_num', 1);
  68. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  69. this.$set(this, 'attrValue', '');
  70. this.$set(this, 'attrTxt', '请选择');
  71. }
  72. },
  73. /**
  74. * 属性变动赋值
  75. *
  76. */
  77. ChangeAttr: function(res) {
  78. let productSelect = this.productValue[res];
  79. this.$set(this, "selectSku", productSelect);
  80. if (productSelect && productSelect.stock >= 0) {
  81. this.$set(this.attr.productSelect, 'image', productSelect.image);
  82. this.$set(this.attr.productSelect, 'price', productSelect.price);
  83. this.$set(this.attr.productSelect, 'stock', productSelect.stock);
  84. this.$set(this.attr.productSelect, 'unique', productSelect.unique);
  85. this.$set(this.attr.productSelect, 'cart_num', 1);
  86. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  87. this.$set(this, 'attrValue', res);
  88. this.$set(this, 'attrTxt', '已选择');
  89. } else {
  90. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  91. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  92. this.$set(this.attr.productSelect, 'stock', 0);
  93. this.$set(this.attr.productSelect, 'unique', '');
  94. this.$set(this.attr.productSelect, 'cart_num', 0);
  95. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  96. this.$set(this, 'attrValue', '');
  97. this.$set(this, 'attrTxt', '请选择');
  98. }
  99. },
  100. attrVal(val) {
  101. this.$set(this.attr.productAttr[val.indexw], 'index', this.attr.productAttr[val.indexw].attr_values[val
  102. .indexn]);
  103. },
  104. /**
  105. * 购物车手动填写
  106. *
  107. */
  108. iptCartNum: function(e) {
  109. this.$set(this.attr.productSelect, 'cart_num', e);
  110. },
  111. onMyEvent: function() {
  112. this.$set(this.attr, 'cartAttr', false);
  113. },
  114. // 改变多属性购物车
  115. ChangeCartNumDuo(changeValue) {
  116. if(changeValue){
  117. if(this.attr.productSelect.cart_num == this.attr.productSelect.stock) return this.$util.Tips({
  118. title: '该产品没有更多库存了'
  119. });
  120. this.attr.productSelect.cart_num++;
  121. }else{
  122. if(this.attr.productSelect.cart_num == 1) return
  123. this.attr.productSelect.cart_num--;
  124. }
  125. },
  126. // 多规格加入购物车;
  127. goCatNum(type) {
  128. this.goCartChange(true,null,type);
  129. },
  130. goCartChange(duo, productId,type){
  131. let data = {
  132. productId: duo ? this.id : productId,
  133. cartNum: duo ? this.attr.productSelect.cart_num : 1,
  134. uniqueId: duo ? this.attr.productSelect.unique : "",
  135. 'new':type,
  136. tourist_uid: this.touristId,
  137. };
  138. adminCartAdd(this.userId,data).then(res=>{
  139. if(duo){
  140. this.attr.cartAttr = false;
  141. }
  142. if(type){
  143. uni.navigateTo({
  144. url: `/pages/behalf/order_confirm/index?cartId=${res.data.cartId}&uid=${this.userId}&news=1&delivery_type=${this.delivery_type}&addressId=${this.addressId}&store_id=${this.store_id}`
  145. });
  146. }
  147. this.$util.Tips({
  148. title: '加入购物车成功'
  149. });
  150. this.getCartList(1);
  151. }).catch(err => {
  152. return this.$util.Tips({
  153. title: err
  154. });
  155. });
  156. },
  157. goCartDuo(item) {
  158. if (!this.isLogin) {
  159. toLogin();
  160. } else {
  161. this.storeName = item.store_name;
  162. this.getAttrs(item.id);
  163. this.$set(this, 'id', item.id);
  164. this.$set(this.attr, 'isType', item.type);
  165. this.$set(this.attr, 'deliveryType', item.delivery_type);
  166. this.$set(this.attr, 'cartAttr', true);
  167. }
  168. },
  169. // 点击默认单属性购物车
  170. goCartDan(item) {
  171. if (!this.isLogin) {
  172. this.getIsLogin();
  173. } else {
  174. this.goCartChange(false, item.id,item.cart_button == 1 ? 0 : 1);
  175. }
  176. },
  177. closeList(e) {
  178. this.$set(this.cartData, 'iScart', e);
  179. },
  180. selectitem(index){
  181. this.cartData.cartList[index].select = !this.cartData.cartList[index].select;
  182. let isSelect = this.cartData.cartList.filter(el=> el.select == true);
  183. if(isSelect.length == this.cartData.cartList.length){
  184. this.$refs.cartPopup.allSelect = true;
  185. }else{
  186. this.$refs.cartPopup.allSelect = false;
  187. }
  188. },
  189. selectAll(val){
  190. this.cartData.cartList.map(item=>{
  191. this.$set(item,'select',val ? false : true);
  192. })
  193. },
  194. cartDelChange(){
  195. let ids = [];
  196. this.cartData.cartList.forEach(item=>{
  197. if(item.select){
  198. ids.push(item.id);
  199. }
  200. })
  201. if(!ids.length) return this.$util.Tips({
  202. title: '请先选择商品'
  203. });
  204. adminCartDel(this.userId,{
  205. ids: ids.toString(),
  206. tourist_uid: this.touristId,
  207. }).then(res=>{
  208. this.getCartList(0);
  209. this.$util.Tips({
  210. title: err
  211. });
  212. }).catch(err => {
  213. return this.$util.Tips({
  214. title: err
  215. });
  216. });
  217. },
  218. cartNumChange(data){
  219. if(data.type){
  220. if(data.item.cart_num == data.item.productInfo.attrInfo.stock) return this.$util.Tips({
  221. title: '该产品没有更多库存了'
  222. });
  223. }else{
  224. if(data.item.cart_num == 1) return
  225. }
  226. adminCartNum(this.userId,{
  227. id: data.item.id,
  228. number: data.type ? data.item.cart_num + 1 : data.item.cart_num - 1,
  229. tourist_uid: this.touristId
  230. }).then(res=>{
  231. if(data.type){
  232. this.cartData.cartList[data.index].cart_num++;
  233. }else{
  234. this.cartData.cartList[data.index].cart_num--;
  235. }
  236. this.getTotalPrice();
  237. this.$util.Tips({
  238. title: res.msg
  239. });
  240. }).catch(err => {
  241. return this.$util.Tips({
  242. title: err
  243. });
  244. });
  245. }
  246. }
  247. };