index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. var a = require("../../utils/public"), app = getApp();
  2. var util = require('../../utils/util.js');
  3. Component({
  4. properties: {
  5. visible: {
  6. type: Boolean,
  7. value: false,
  8. observer: function (t) {
  9. t && this.setData({
  10. value: 1,
  11. loading: false
  12. })
  13. }
  14. },
  15. cur_sku_arr: {
  16. type: Object,
  17. value: {}
  18. },
  19. skuList: {
  20. type: Object,
  21. value: {}
  22. },
  23. sku_val: {
  24. type: Number,
  25. value: 1
  26. },
  27. sku: {
  28. type: Array,
  29. value: []
  30. },
  31. goodsid: {
  32. type: Number,
  33. value: 0
  34. },
  35. type: {
  36. type: Number,
  37. value: 0
  38. },
  39. buyType: {
  40. type: String,
  41. value: ''
  42. },
  43. soliId: {
  44. type: Number,
  45. value: 0
  46. },
  47. vipInfo: {
  48. type: Object,
  49. value: {
  50. is_open_vipcard_buy: 0,
  51. is_vip_card_member: 0,
  52. is_member_level_buy: 0
  53. }
  54. },
  55. skin: {
  56. type: Object,
  57. value: {
  58. color: '#ff5344',
  59. subColor: '#ed7b3a',
  60. lighter: '#fff9f4'
  61. }
  62. }
  63. },
  64. focusFlag: false,
  65. data: {
  66. value: 1,
  67. loading: false,
  68. showLimitTip: false,
  69. showLimitType: 0
  70. },
  71. methods: {
  72. close: function () {
  73. this.setData({showLimitTip: false})
  74. this.triggerEvent("cancel");
  75. },
  76. selectSku: function (event) {
  77. var that = this;
  78. let str = event.currentTarget.dataset.type;
  79. let obj = str.split("_");
  80. let { sku, cur_sku_arr, skuList, sku_val } = this.data;
  81. let temp = {
  82. name: obj[3],
  83. id: obj[2],
  84. index: obj[0],
  85. idx: obj[1]
  86. };
  87. sku.splice(obj[0], 1, temp);
  88. var id = '';
  89. for (let i = 0; i < sku.length; i++) {
  90. if (i == sku.length - 1) {
  91. id = id + sku[i]['id'];
  92. } else {
  93. id = id + sku[i]['id'] + "_";
  94. }
  95. }
  96. cur_sku_arr = Object.assign(cur_sku_arr, skuList.sku_mu_list[id]);
  97. let h = {};
  98. h.noEougnStock = false;
  99. sku_val = sku_val || 1;
  100. let canBuyNum = cur_sku_arr.canBuyNum*1;
  101. if(sku_val > canBuyNum) {
  102. h.sku_val = canBuyNum==0?1:canBuyNum;
  103. (canBuyNum>0) && wx.showToast({
  104. title: `最多只能购买${cur_sku_arr.canBuyNum}件`,
  105. icon: 'none'
  106. })
  107. }
  108. let { car_quantity, goods_start_count } = this.data.cur_sku_arr;
  109. if(car_quantity && car_quantity>=goods_start_count) goods_start_count=1;
  110. // 库存小于起购数量 按钮变灰 数量变起购
  111. if(canBuyNum<goods_start_count) {
  112. h.sku_val = goods_start_count;
  113. h.noEougnStock = true;
  114. } else {
  115. h.sku_val = goods_start_count;
  116. }
  117. if(this.data.buyType=='integral') {
  118. h.sku_val = 1;
  119. }
  120. that.setData({
  121. cur_sku_arr,
  122. sku,
  123. showLimitTip: false,
  124. showLimitType: 0,
  125. ...h
  126. })
  127. },
  128. /**
  129. * 数量加减
  130. */
  131. setNum: function (event) {
  132. let types = event.currentTarget.dataset.type;
  133. var that = this;
  134. var num = 1;
  135. let sku_val = this.data.sku_val * 1;
  136. let showLimitTip = false;
  137. let showLimitType = 0;
  138. if (types == 'add') {
  139. num = sku_val + 1;
  140. let {one_limit_count, total_limit_count, oneday_limit_count} = this.data.cur_sku_arr;
  141. if(one_limit_count>0 && num > one_limit_count) {
  142. wx.showToast({
  143. title: `您本次只能购买${one_limit_count}份`,
  144. icon: 'none'
  145. })
  146. num = one_limit_count;
  147. showLimitTip = true;
  148. showLimitType = 1;
  149. } else if(oneday_limit_count>0 && num > oneday_limit_count) {
  150. wx.showToast({
  151. title: `您今天只能购买${oneday_limit_count}份`,
  152. icon: 'none'
  153. })
  154. num = oneday_limit_count;
  155. showLimitTip = true;
  156. showLimitType = 2;
  157. } else if(total_limit_count>0 && num > total_limit_count) {
  158. wx.showToast({
  159. title: `您最多只能购买${total_limit_count}份`,
  160. icon: 'none'
  161. })
  162. num = total_limit_count;
  163. showLimitTip = true;
  164. showLimitType = 3;
  165. }
  166. } else if (types == 'decrease') {
  167. let { car_quantity, goods_start_count } = this.data.cur_sku_arr;
  168. if(car_quantity && car_quantity>=goods_start_count) goods_start_count=1;
  169. if (sku_val > 1) {
  170. num = sku_val - 1;
  171. if(num<goods_start_count){
  172. num = goods_start_count;
  173. wx.showToast({
  174. title: `${goods_start_count}件起售`,
  175. icon: 'none'
  176. })
  177. }
  178. }
  179. }
  180. let arr = that.data.sku;
  181. var options = this.data.skuList;
  182. if (arr.length > 0) {
  183. var id = '';
  184. for (let i = 0; i < arr.length; i++) {
  185. if (i == arr.length - 1) {
  186. id = id + arr[i]['id'];
  187. } else {
  188. id = id + arr[i]['id'] + "_";
  189. }
  190. }
  191. }
  192. if (options.length > 0) {
  193. let cur_sku_arr = options.sku_mu_list[id];
  194. let max = cur_sku_arr['canBuyNum'];
  195. if (num > max) {
  196. num = num - 1;
  197. wx.showToast({
  198. title: `最多只能购买${max}件`,
  199. icon: 'none'
  200. })
  201. }
  202. } else {
  203. let cur_sku_arr = this.data.cur_sku_arr;
  204. if (num > cur_sku_arr['canBuyNum']) {
  205. num = num - 1;
  206. }
  207. }
  208. this.setData({
  209. sku_val: num,
  210. showLimitTip,
  211. showLimitType
  212. })
  213. },
  214. gocarfrom: function (e) {
  215. var that = this;
  216. wx.showLoading();
  217. a.collectFormIds(e.detail.formId);
  218. that.goOrder();
  219. },
  220. goOrder: function () {
  221. let that = this;
  222. let tdata = that.data;
  223. if (tdata.can_car) tdata.can_car = false;
  224. var token = wx.getStorageSync('token');
  225. var community = wx.getStorageSync('community');
  226. var community_id = community.communityId;
  227. var goods_id = tdata.goodsid;
  228. var quantity = tdata.sku_val;
  229. var cur_sku_arr = tdata.cur_sku_arr;
  230. var sku_str = '';
  231. var is_just_addcar = 1;
  232. if (cur_sku_arr && cur_sku_arr.option_item_ids) sku_str = cur_sku_arr.option_item_ids;
  233. let buy_type = this.data.buyType ? this.data.buyType: 'dan';
  234. if(buy_type=='integral') is_just_addcar = 0;
  235. // 接龙
  236. let soli_id = this.data.soliId || '';
  237. let data = {
  238. goods_id,
  239. community_id,
  240. quantity,
  241. sku_str,
  242. buy_type,
  243. pin_id: 0,
  244. is_just_addcar,
  245. soli_id
  246. }
  247. util.addCart(data).then(res=>{
  248. if(res.showVipModal==1) {
  249. let { pop_vipmember_buyimage } = res.data;
  250. wx.hideLoading();
  251. that.triggerEvent("vipModal", { pop_vipmember_buyimage, showVipModal: true, visible: false });
  252. } else if (res.data.code == 3 || res.data.code == 7) {
  253. wx.showToast({
  254. title: res.data.msg,
  255. icon: 'none',
  256. duration: 2000
  257. })
  258. } else {
  259. if (buy_type =='integral'){
  260. if (res.data.code == 6) {
  261. // 积分不足
  262. var msg = res.data.msg;
  263. wx.showToast({
  264. title: msg,
  265. icon: 'none',
  266. duration: 2000
  267. })
  268. } else {
  269. //跳转结算页面
  270. wx.navigateTo({
  271. url: `/lionfish_comshop/pages/order/placeOrder?type=integral`,
  272. })
  273. }
  274. } else {
  275. if (res.data.code == 4) {
  276. wx.showToast({
  277. title: '您未登录',
  278. duration: 2000,
  279. success: () => {
  280. that.setData({
  281. needAuth: true
  282. })
  283. }
  284. })
  285. } else if (res.data.code == 6) {
  286. let max_quantity = res.data.max_quantity || '';
  287. (max_quantity > 0) && that.setData({
  288. sku_val: max_quantity
  289. })
  290. var msg = res.data.msg;
  291. wx.showToast({
  292. title: msg,
  293. icon: 'none',
  294. duration: 2000
  295. })
  296. } else {
  297. if (is_just_addcar == 1) {
  298. that.close();
  299. wx.hideLoading();
  300. // 接龙商品
  301. if (buy_type == 'soitaire') {
  302. let { goods_total_count, total } = res.data;
  303. that.triggerEvent("changeCartNum", { goods_total_count, total, goods_id });
  304. } else {
  305. res.data.total && that.triggerEvent("changeCartNum", res.data.total);
  306. }
  307. wx.showToast({
  308. title: "已加入购物车",
  309. image: "../../images/addShopCart.png"
  310. })
  311. } else {
  312. var pages_all = getCurrentPages();
  313. if (pages_all.length > 3) {
  314. wx.redirectTo({
  315. url: '/lionfish_comshop/pages/buy/index?type=' + tdata.order.buy_type
  316. })
  317. } else {
  318. wx.navigateTo({
  319. url: '/lionfish_comshop/pages/buy/index?type=' + tdata.order.buy_type
  320. })
  321. }
  322. }
  323. }
  324. }
  325. }
  326. })
  327. },
  328. // 输入框获得焦点
  329. handleFocus: function () {
  330. this.focusFlag = true;
  331. },
  332. handleBlur: function (t) {
  333. let a = t.detail;
  334. let val = parseInt(a.value);
  335. if (val == '' || isNaN(val)) {
  336. let { car_quantity, goods_start_count } = this.data.cur_sku_arr;
  337. if(car_quantity && car_quantity>=goods_start_count){
  338. goods_start_count=1;
  339. } else {
  340. wx.showToast({
  341. title: `${goods_start_count}件起售`,
  342. icon: 'none'
  343. })
  344. }
  345. this.setData({ sku_val: goods_start_count })
  346. }
  347. },
  348. // 监控输入框变化
  349. changeNumber: function (t) {
  350. let { cur_sku_arr, sku_val } = this.data;
  351. let max = cur_sku_arr.stock * 1;
  352. let a = t.detail;
  353. this.focusFlag = false;
  354. let showLimitTip = false;
  355. let showLimitType = 0;
  356. let { car_quantity, goods_start_count } = this.data.cur_sku_arr;
  357. if(car_quantity && car_quantity>=goods_start_count) goods_start_count=1;
  358. if (a) {
  359. let val = parseInt(a.value);
  360. val = val < 1 ? 1 : val;
  361. // 限购
  362. let {one_limit_count, total_limit_count, oneday_limit_count} = cur_sku_arr;
  363. if(one_limit_count>0 && val > one_limit_count) {
  364. wx.showToast({
  365. title: `您本次只能购买${one_limit_count}份`,
  366. icon: 'none'
  367. })
  368. sku_val = one_limit_count;
  369. showLimitTip = true;
  370. showLimitType = 1;
  371. } else if(oneday_limit_count>0 && val > oneday_limit_count) {
  372. wx.showToast({
  373. title: `您今天只能购买${oneday_limit_count}份`,
  374. icon: 'none'
  375. })
  376. sku_val = oneday_limit_count;
  377. showLimitTip = true;
  378. showLimitType = 2;
  379. } else if(total_limit_count>0 && val > total_limit_count) {
  380. wx.showToast({
  381. title: `您最多只能购买${total_limit_count}份`,
  382. icon: 'none'
  383. })
  384. sku_val = total_limit_count;
  385. showLimitTip = true;
  386. showLimitType = 3;
  387. } else if (val > max) {
  388. wx.showToast({
  389. title: `最多只能购买${max}件`,
  390. icon: 'none'
  391. })
  392. sku_val = max;
  393. } else {
  394. if(val<goods_start_count){
  395. val = goods_start_count;
  396. wx.showToast({
  397. title: `${goods_start_count}件起售`,
  398. icon: 'none'
  399. })
  400. }
  401. sku_val = val;
  402. }
  403. }
  404. this.setData({
  405. sku_val,
  406. showLimitTip,
  407. showLimitType
  408. })
  409. }
  410. }
  411. });