compoentCartMixin.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. var a = require("../utils/public");
  2. var app = getApp();
  3. var status = require('../utils/index.js');
  4. var util = require('../utils/util.js');
  5. module.exports = {
  6. data: {
  7. visible: false,
  8. stopClick: false,
  9. updateCart: 0
  10. },
  11. authModal: function (t=false) {
  12. t.detail && this.setData({needAuth: true});
  13. if (this.data.needAuth) {
  14. this.setData({ showAuthModal: !this.data.showAuthModal });
  15. return false;
  16. }
  17. return true;
  18. },
  19. vipModal: function(t) {
  20. this.setData(t.detail)
  21. },
  22. openSku: function (t) {
  23. if (!this.authModal()) return;
  24. var that = this,
  25. e = t.detail;
  26. var goods_id = e.actId;
  27. var options = e.skuList;
  28. that.setData({
  29. addCar_goodsid: goods_id
  30. })
  31. let list = options.list || [];
  32. let arr = [];
  33. if (list.length > 0) {
  34. for (let i = 0; i < list.length; i++) {
  35. let sku = list[i]['option_value'][0];
  36. let temp = {
  37. name: sku['name'],
  38. id: sku['option_value_id'],
  39. index: i,
  40. idx: 0
  41. };
  42. arr.push(temp);
  43. }
  44. //把单价剔除出来begin
  45. var id = '';
  46. for (let i = 0; i < arr.length; i++) {
  47. if (i == arr.length - 1) {
  48. id = id + arr[i]['id'];
  49. } else {
  50. id = id + arr[i]['id'] + "_";
  51. }
  52. }
  53. var cur_sku_arr = {};
  54. if(options.sku_mu_list[id]) {
  55. cur_sku_arr = options.sku_mu_list[id];
  56. } else {
  57. let idArr = id.split("_");
  58. idArr = idArr.reverse();
  59. id = idArr.join("_");
  60. cur_sku_arr = options.sku_mu_list[id];
  61. }
  62. cur_sku_arr.oneday_limit_count = e.allData && e.allData.oneday_limit_count || 0;
  63. cur_sku_arr.total_limit_count = e.allData && e.allData.total_limit_count || 0;
  64. cur_sku_arr.one_limit_count = e.allData && e.allData.one_limit_count || 0;
  65. cur_sku_arr.goods_start_count = e.allData && e.allData.goods_start_count || 1;
  66. let { car_quantity, goods_start_count } = cur_sku_arr;
  67. if(car_quantity && car_quantity>=goods_start_count) goods_start_count=1;
  68. that.setData({
  69. sku: arr,
  70. sku_val: goods_start_count || 1,
  71. cur_sku_arr: cur_sku_arr,
  72. skuList: e.skuList,
  73. visible: true,
  74. showSku: true
  75. });
  76. } else {
  77. let goodsInfo = e.skuList;
  78. that.setData({
  79. sku: [],
  80. sku_val: 1,
  81. skuList: [],
  82. cur_sku_arr: goodsInfo
  83. })
  84. let formIds = {
  85. detail: {
  86. formId: ""
  87. }
  88. };
  89. formIds.detail.formId = "the formId is a mock one";
  90. that.gocarfrom(formIds);
  91. }
  92. },
  93. /**
  94. * 确认加入购物车
  95. */
  96. gocarfrom: function (e) {
  97. var that = this;
  98. wx.showLoading();
  99. a.collectFormIds(e.detail.formId);
  100. that.goOrder();
  101. },
  102. goOrder: function () {
  103. let that = this;
  104. let tdata = that.data;
  105. if (tdata.can_car) {
  106. tdata.can_car = false;
  107. }
  108. var community = wx.getStorageSync('community');
  109. var community_id = community.communityId;
  110. var goods_id = tdata.addCar_goodsid;
  111. var quantity = tdata.sku_val;
  112. var cur_sku_arr = tdata.cur_sku_arr;
  113. var sku_str = '';
  114. var is_just_addcar = 1;
  115. let updateCart = that.data.updateCart || 0;
  116. if (cur_sku_arr && cur_sku_arr.option_item_ids) {
  117. sku_str = cur_sku_arr.option_item_ids;
  118. }
  119. // 接龙
  120. let soli_info = this.data.soli_info || '';
  121. let soli_id = (soli_info && soli_info.id) || '';
  122. let buy_type = this.data.buy_type || 'dan';
  123. let data= {
  124. goods_id,
  125. community_id,
  126. quantity,
  127. sku_str,
  128. buy_type,
  129. pin_id: 0,
  130. is_just_addcar,
  131. soli_id
  132. }
  133. util.addCart(data).then(res=>{
  134. if(res.showVipModal==1) {
  135. wx.hideLoading();
  136. let { pop_vipmember_buyimage } = res.data;
  137. that.setData({ pop_vipmember_buyimage, showVipModal: true, visible: false });
  138. } else {
  139. if (res.data.code == 3) {
  140. wx.showToast({
  141. title: res.data.msg,
  142. icon: 'none',
  143. duration: 2000
  144. })
  145. } else if (res.data.code == 4) {
  146. wx.showToast({
  147. title: '您未登录',
  148. duration: 2000,
  149. success: () => {
  150. that.setData({
  151. needAuth: true
  152. })
  153. }
  154. })
  155. } else if (res.data.code == 6 || res.data.code == 7) {
  156. let max_quantity = res.data.max_quantity || '';
  157. (max_quantity > 0) && that.setData({
  158. sku_val: max_quantity
  159. })
  160. var msg = res.data.msg;
  161. wx.showToast({
  162. title: msg,
  163. icon: 'none',
  164. duration: 2000
  165. })
  166. } else {
  167. if (is_just_addcar == 1) {
  168. that.closeSku();
  169. status.indexListCarCount(goods_id, res.data.cur_count);
  170. that.setData({
  171. cartNum: res.data.total || 0,
  172. updateCart: updateCart+1
  173. })
  174. wx.showToast({
  175. title: "已加入购物车",
  176. image: "../../images/addShopCart.png"
  177. })
  178. } else {
  179. var pages_all = getCurrentPages();
  180. if (pages_all.length > 3) {
  181. wx.redirectTo({
  182. url: '/lionfish_comshop/pages/buy/index?type=' + tdata.order.buy_type
  183. })
  184. } else {
  185. wx.navigateTo({
  186. url: '/lionfish_comshop/pages/buy/index?type=' + tdata.order.buy_type
  187. })
  188. }
  189. }
  190. }
  191. }
  192. })
  193. },
  194. changeCartNum(e) {
  195. let cartNum = e.detail || 0;
  196. cartNum>=0 && this.setData({ cartNum })
  197. },
  198. /**
  199. * 关闭购物车选项卡
  200. */
  201. closeSku: function () {
  202. this.setData({
  203. visible: false,
  204. stopClick: false
  205. });
  206. },
  207. changeNumber: function (t) {
  208. var e = t.detail;
  209. e && this.addCart(e);
  210. },
  211. outOfMax: function (t) {
  212. console.log(t)
  213. var e = t.detail, canBuyNum = this.data.spuItem.spuCanBuyNum;
  214. if (this.data.number >= canBuyNum) {
  215. wx.showToast({
  216. title: "不能购买更多啦",
  217. icon: "none"
  218. })
  219. }
  220. },
  221. addCart: function (t) {
  222. // {value: 2, type: "plus/minus"}
  223. let idx = t.idx;
  224. let list = this.data.list;
  225. var token = wx.getStorageSync('token');
  226. var community = wx.getStorageSync('community');
  227. var goods_id = list[idx].actId;
  228. var community_id = community.communityId;
  229. // 接龙
  230. let soli_info = this.data.soli_info || '';
  231. let soli_id = (soli_info && soli_info.id) || '';
  232. let buy_type = this.data.buy_type || 'dan';
  233. let that = this;
  234. if (t.type == 'plus') {
  235. let data = {
  236. goods_id: goods_id,
  237. community_id: community_id,
  238. quantity: 1,
  239. sku_str: '',
  240. buy_type,
  241. pin_id: 0,
  242. is_just_addcar: 1,
  243. soli_id
  244. }
  245. util.addCart(data).then(res=>{
  246. if(res.showVipModal==1) {
  247. let { pop_vipmember_buyimage } = res.data;
  248. that.triggerEvent("vipModal", { pop_vipmember_buyimage, showVipModal: true, visible: false });
  249. } else {
  250. if (res.data.code == 3) {
  251. let max_quantity = res.data.max_quantity || '';
  252. list[idx].car_count = max_quantity;
  253. (max_quantity > 0) && that.setData({ list })
  254. wx.showToast({
  255. title: res.data.msg,
  256. icon: 'none',
  257. duration: 2000
  258. })
  259. } else if (res.data.code == 6 || res.data.code == 7) {
  260. let max_quantity = res.data.max_quantity || '';
  261. list[idx].car_count = max_quantity;
  262. (max_quantity > 0) && that.setData({ cartNum: res.data.total || 0, list })
  263. var msg = res.data.msg;
  264. wx.showToast({
  265. title: msg,
  266. icon: 'none',
  267. duration: 2000
  268. })
  269. } else {
  270. let cartNum = res.data.total || 0;
  271. if(soli_id>0) cartNum = res.data.goods_total_count || 0;
  272. list[idx].car_count = res.data.cur_count;
  273. that.setData({ cartNum, list })
  274. wx.showToast({
  275. title: "已加入购物车",
  276. image: "../../images/addShopCart.png"
  277. })
  278. status.indexListCarCount(goods_id, res.data.cur_count);
  279. }
  280. }
  281. })
  282. } else {
  283. app.util.request({
  284. url: 'entry/wxapp/user',
  285. data: {
  286. controller: 'car.reduce_car_goods',
  287. token: token,
  288. goods_id: goods_id,
  289. community_id: community_id,
  290. quantity: 1,
  291. sku_str: '',
  292. buy_type,
  293. pin_id: 0,
  294. is_just_addcar: 1,
  295. soli_id
  296. },
  297. dataType: 'json',
  298. method: 'POST',
  299. success: function (res) {
  300. if (res.data.code == 3) {
  301. wx.showToast({
  302. title: res.data.msg,
  303. icon: 'none',
  304. duration: 2000
  305. })
  306. } else {
  307. let cartNum = res.data.total || 0;
  308. if(soli_id>0) cartNum = res.data.goods_total_count || 0;
  309. list[idx].car_count = res.data.cur_count;
  310. that.setData({ list, cartNum})
  311. status.indexListCarCount(goods_id, res.data.cur_count);
  312. }
  313. }
  314. })
  315. }
  316. }
  317. }