cartMixin.js 8.2 KB

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