carInsert.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. // index.js
  2. var likeList = [
  3. { url: '/pages/buy/buy?com_id=3292', imgSrc: 'https://www.daliangzao.net/images/c62671d2f0454054a9d9fbbc87965fcf.jpg', name: '【Herbacin/贺本清】小甘菊护手霜礼盒 ', oldPrice: '98', newPrice: '68' },
  4. { url: '/pages/buy/buy?com_id=3296', imgSrc: 'https://www.daliangzao.net/images/11d3bda623b241adb6aff125bd2a2477.jpg', name: '【FOREO】LUNA2 电动硅胶洁面仪', oldPrice: '1880', newPrice: '1373' },
  5. { url: '/pages/buy/buy?com_id=3539', imgSrc: 'https://www.daliangzao.net/images/359f87b24aaa4df899b1b7dfaa448911.jpg', name: '【Dyson戴森】Supersonic HD01吹风机(中国红甄选礼盒)', oldPrice: '3190', newPrice: '3190' },
  6. { url: '/pages/buy/buy?com_id=3294', imgSrc: 'https://www.daliangzao.net/images/323cce98818944248fb791115004b18e.jpg', name: '【大良造】粉色少女心生活礼盒(毛毯+眼罩+拖鞋)', oldPrice: '388', newPrice: '388' },
  7. { url: '/pages/buy/buy?com_id=3270', imgSrc: 'https://www.daliangzao.net/images/b942f40c711f4336ae8f6efecebd11b6.jpg', name: '【大良造】精致生活礼盒(香槟+巧克力)', oldPrice: '195', newPrice: '193' },
  8. { url: '/pages/buy/buy?com_id=3275', imgSrc: 'https://www.daliangzao.net/images/4152d74f27634b77bdae594c07c05f1c.jpg', name: '【大良造】创意办公礼盒(竹节笔+笔记本)', oldPrice: '311', newPrice: '311' }
  9. ];
  10. Page({
  11. data: {
  12. contentShow: '',//初始整体内容隐藏,加载完成后显示
  13. likeList: likeList,
  14. carts: [],
  15. buyCarts: [],
  16. hasList: false, // 列表是否有数据
  17. totalPrice: 0, // 总价,初始为0
  18. selected: '',
  19. selectAllStatus: true, // 全选状态,默认全选
  20. delBtnWidth: 160, //删除按钮宽度单位(rpx)
  21. txtStyle: "",
  22. touchDel: 0,
  23. scrollY: true, //页面是否可滚动
  24. isMinus: true,
  25. isAdd: true,
  26. hidden: 'hidden',
  27. hiddenmodal: true, //弹窗
  28. maskBg: false,//支付提示蒙层
  29. paddingBottom: 0,
  30. un_id: '',
  31. shixiao_num: 0,//失效商品数量
  32. firstClick: 0, //没有连点购买按钮
  33. combineList: [],//存放相同规格,不同礼盒和工艺的商品,合并后的数组【仅需 商品id,组合规格id,总数量】
  34. },
  35. loadFun: function () {
  36. //wx.showNavigationBarLoading();
  37. var that = this;
  38. var host = getApp().globalData.servsers;
  39. var un_id = getApp().globalData.un_id;
  40. wx.showLoading({
  41. title: '',
  42. icon: 'loading',
  43. mask: true,
  44. success: function () {
  45. }
  46. })
  47. that.setData({
  48. contentShow: '',//初始整体内容隐藏,加载完成后显示
  49. carts: [],
  50. buyCarts: [],
  51. hasList: false, // 列表是否有数据
  52. totalPrice: 0, // 总价,初始为0
  53. selected: '',
  54. selectAllStatus: true, // 全选状态,默认全选
  55. delBtnWidth: 160, //删除按钮宽度单位(rpx)
  56. txtStyle: "",
  57. touchDel: 0,
  58. scrollY: true, //页面是否可滚动
  59. isMinus: true,
  60. isAdd: true,
  61. hidden: 'hidden',
  62. hiddenmodal: true, //弹窗
  63. maskBg: false,//支付提示蒙层
  64. paddingBottom: 0,
  65. un_id: '',
  66. shixiao_num: 0,//失效商品数量
  67. firstClick: 0, //没有连点购买按钮
  68. })
  69. var shixiao_num = 0;
  70. that.setData({
  71. un_id: un_id,
  72. firstClick: 0
  73. })
  74. if (un_id != undefined && un_id != '' && un_id != null) {
  75. wx.request({
  76. url: host + "shoppingcartapi/shoppingcartview",
  77. data: {
  78. user_id: un_id
  79. },
  80. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  81. header: {
  82. 'Accept': 'application/json'
  83. },
  84. success: function (res) {
  85. console.log(res);
  86. if (res.data.total > 0) {
  87. that.setData({
  88. carts: res.data.rows,
  89. hasList: true,
  90. paddingBottom: 100
  91. });
  92. var carts = res.data.rows;
  93. console.log(res.data.rows);
  94. var cartsLength = res.data.rows.length;
  95. //检测库存
  96. that.checkCommodityGroupRepertory(cartsLength, 0, 0, 0);
  97. } else {
  98. that.setData({
  99. contentShow: 'contentShow'
  100. })
  101. wx.hideLoading();
  102. }
  103. if (res == null || res.data == null) {
  104. console.error('网络请求失败');
  105. return;
  106. }
  107. }
  108. });
  109. }
  110. },
  111. //检测商品售罄
  112. checkCommodityGroupRepertory: function (cartsLength, total, flagNum, shixiao_num) {
  113. var that = this;
  114. var host = getApp().globalData.servsers;
  115. var un_id = getApp().globalData.un_id;
  116. //shixiao_num : 失效商品数量【失效+售罄】
  117. var carts = that.data.carts;
  118. if (cartsLength == 0) {
  119. console.log("cartsLength:" + cartsLength);
  120. console.log("shixiao_num:" + shixiao_num);
  121. console.log("flagNum:" + flagNum);
  122. var len = carts.length;
  123. if (flagNum == (len - shixiao_num) && flagNum > 0) {
  124. that.setData({
  125. selectAllStatus: true,
  126. totalPrice: total.toFixed(2)
  127. });
  128. } else {
  129. that.setData({
  130. selectAllStatus: false,
  131. totalPrice: total.toFixed(2)
  132. });
  133. }
  134. that.setData({
  135. carts: carts
  136. })
  137. that.getTotalPrice();
  138. that.setData({
  139. contentShow: 'contentShow'
  140. })
  141. console.log(carts);
  142. wx.hideLoading();
  143. } else {
  144. cartsLength -= 1;
  145. wx.request({
  146. url: host + "commodityapi/commoditview",
  147. data: {
  148. com_id: carts[cartsLength].com_id
  149. },
  150. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  151. header: {
  152. 'Accept': 'application/json'
  153. },
  154. success: function (res) {
  155. console.log("load:");
  156. console.log(res);
  157. //有此商品
  158. if (res.data.total > 0) {
  159. carts[cartsLength].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
  160. that.setData({
  161. carts: carts
  162. })
  163. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  164. carts[cartsLength].num = parseInt(carts[cartsLength].num);
  165. carts[cartsLength].limitNum = parseInt(carts[cartsLength].limitNum);
  166. carts[cartsLength].flag = 0;//取消勾选
  167. carts[cartsLength].canBuy = true;//默认有库存
  168. shixiao_num += 1;
  169. that.setData({
  170. shixiao_num: shixiao_num,
  171. carts: carts
  172. })
  173. that.checkCommodityGroupRepertory(cartsLength, total, flagNum, shixiao_num);
  174. } else {
  175. //不是失效商品,检测库存是否充足
  176. wx.request({
  177. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  178. data: {
  179. datasheetGroupId: carts[cartsLength].com_group_id
  180. },
  181. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  182. header: {
  183. 'Accept': 'application/json'
  184. },
  185. success: function (res) {
  186. console.log(res);
  187. if (res.data.code == '200') {
  188. carts[cartsLength].canBuy = res.data.data.canBuy;//有无库存
  189. carts[cartsLength].repertory = res.data.data.repertory;//当前规格的库存数量
  190. //库存充足
  191. if (res.data.data.canBuy == true) {
  192. if (res.data.data.repertory < carts[cartsLength].limitNum || res.data.data.repertory < carts[cartsLength].num || parseInt(carts[cartsLength].num) < parseInt(carts[cartsLength].limitNum)) {
  193. carts[cartsLength].canBuy = false;
  194. carts[cartsLength].flag = 0;//取消勾选
  195. shixiao_num += 1;
  196. } else {
  197. if (carts[cartsLength].box_price == '' || carts[cartsLength].box_price == null || carts[cartsLength].box_price == undefined) {
  198. carts[cartsLength].box_price = 0;
  199. }
  200. carts[cartsLength].new_price = parseFloat(carts[cartsLength].cost) + parseFloat(carts[cartsLength].box_price);
  201. if (carts[cartsLength].flag == 1) {//当前为选中
  202. flagNum += 1;
  203. total += carts[cartsLength].num * carts[cartsLength].new_price;
  204. }
  205. }
  206. } else {
  207. carts[cartsLength].flag = 0;//取消勾选
  208. shixiao_num += 1;
  209. }
  210. carts[cartsLength].num = parseInt(carts[cartsLength].num);
  211. that.setData({
  212. shixiao_num: shixiao_num,
  213. carts: carts
  214. })
  215. that.checkCommodityGroupRepertory(cartsLength, total, flagNum, shixiao_num);
  216. }
  217. }
  218. })
  219. //request结束
  220. }
  221. } else {
  222. carts[cartsLength].num = parseInt(carts[cartsLength].num);
  223. carts[cartsLength].limitNum = parseInt(carts[cartsLength].limitNum);
  224. carts[cartsLength].flag = 0;//取消勾选
  225. carts[cartsLength].canBuy = true;//默认有库存
  226. shixiao_num += 1;
  227. that.setData({
  228. shixiao_num: shixiao_num,
  229. carts: carts
  230. })
  231. that.checkCommodityGroupRepertory(cartsLength, total, flagNum, shixiao_num);
  232. }
  233. }
  234. })
  235. //非失效商品判断结束
  236. }
  237. },
  238. onShow: function (e) {
  239. var that = this;
  240. that.loadFun();
  241. },
  242. //当前商品选中事件
  243. selectList(e) {
  244. var that = this;
  245. var host = getApp().globalData.servsers;
  246. var index = e.currentTarget.dataset.index;
  247. var carts = that.data.carts;
  248. var selected = carts[index].flag;
  249. var com_id = carts[index].com_id;//商品唯一标识id
  250. var cartsLength = that.data.carts.length;
  251. var shixiao_num = that.data.shixiao_num;
  252. var flagNum = 0;
  253. //检测商品是否下架
  254. wx.request({
  255. url: host + "commodityapi/commoditview",
  256. data: {
  257. com_id: com_id
  258. },
  259. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  260. header: {
  261. 'Accept': 'application/json'
  262. },
  263. success: function (res) {
  264. //有此商品
  265. if (res.data.total > 0) {
  266. carts[index].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
  267. that.setData({
  268. carts: carts
  269. })
  270. console.log(res.data.commodity.commodity_flag);
  271. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  272. carts[index].commodity_flag = 1;
  273. carts[index].flag = 0;
  274. shixiao_num++;
  275. that.setData({
  276. shixiao_num: shixiao_num,
  277. })
  278. } else {
  279. //不是失效商品,检测库存是否充足
  280. wx.request({
  281. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  282. data: {
  283. datasheetGroupId: carts[index].com_group_id
  284. },
  285. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  286. header: {
  287. 'Accept': 'application/json'
  288. },
  289. success: function (res) {
  290. if (res.data.code == '200') {
  291. carts[index].canBuy = res.data.data.canBuy;//有无库存
  292. carts[index].repertory = res.data.data.repertory;//当前规格的库存数量
  293. if (res.data.data.canBuy) {
  294. if (res.data.data.repertory < carts[index].limitNum || res.data.data.repertory < carts[index].num || parseInt(carts[index].num) < parseInt(carts[index].limitNum)) {//库存小于 商品数量或最低起订量
  295. carts[index].canBuy = false;
  296. carts[index].flag = 0;//取消勾选
  297. shixiao_num += 1;
  298. that.setData({
  299. shixiao_num: shixiao_num,
  300. hiddenmodal: false,
  301. modalCont: '商品已售罄!'
  302. })
  303. setTimeout(function () {
  304. that.setData({
  305. hiddenmodal: true,
  306. firstClick: 0
  307. })
  308. }, 1500)
  309. } else {
  310. //同类别数量和【商品id相同,规格id相同】
  311. var combineNum = 0;
  312. if (selected == 0) {//勾选操作,分别和勾选商品加和比对
  313. var sumNum = parseInt(carts[index].num);
  314. for (var t = 0; t < carts.length; t++) {
  315. if (carts[t].flag == 1) {//同勾选商品比对
  316. if (carts[t].com_id == carts[index].com_id && carts[t].com_group_id == carts[index].com_group_id) {
  317. sumNum += parseInt(carts[t].num);
  318. }
  319. }
  320. }
  321. console.log("sumNum:" + sumNum);
  322. console.log("repertory:" + res.data.data.repertory);
  323. if (sumNum > res.data.data.repertory) {
  324. that.setData({
  325. hiddenmodal: false,
  326. modalCont: '库存不足!'
  327. })
  328. setTimeout(function () {
  329. that.setData({
  330. hiddenmodal: true
  331. })
  332. }, 1500)
  333. return false;
  334. } else {
  335. carts[index].flag = 1;
  336. for (var i = 0; i < cartsLength; i++) {
  337. if (carts[i].flag == 1) {
  338. flagNum++;
  339. }
  340. }
  341. if (flagNum == (cartsLength - shixiao_num) && flagNum > 0) {
  342. that.setData({
  343. selectAllStatus: true,
  344. })
  345. }
  346. }
  347. } else {
  348. carts[index].flag = 0;
  349. if (that.data.selectAllStatus == true) {
  350. that.setData({
  351. selectAllStatus: false
  352. })
  353. }
  354. }
  355. }
  356. } else {
  357. carts[index].flag = 0;
  358. shixiao_num++;
  359. that.setData({
  360. shixiao_num: shixiao_num,
  361. hiddenmodal: false,
  362. modalCont: '此商品已售罄!'
  363. })
  364. setTimeout(function () {
  365. that.setData({
  366. hiddenmodal: true,
  367. firstClick: 0
  368. })
  369. }, 1500)
  370. return false;
  371. }
  372. that.setData({
  373. carts: carts
  374. })
  375. that.getTotalPrice();
  376. }
  377. }
  378. })
  379. }
  380. } else {//无此商品
  381. carts[index].commodity_flag = 1;
  382. carts[index].flag = 0;
  383. shixiao_num++;
  384. that.setData({
  385. carts: carts,
  386. shixiao_num: shixiao_num,
  387. hiddenmodal: false,
  388. modalCont: '此商品已下架!'
  389. })
  390. that.getTotalPrice();
  391. setTimeout(function () {
  392. that.setData({
  393. hiddenmodal: true,
  394. firstClick: 0
  395. })
  396. }, 1500)
  397. return false;
  398. }
  399. }
  400. })
  401. },
  402. //购物车全选事件
  403. selectAll(e) {
  404. var that = this;
  405. var host = getApp().globalData.servsers;
  406. var selectAllStatus = that.data.selectAllStatus;
  407. selectAllStatus = !selectAllStatus;
  408. let carts = that.data.carts;
  409. var len = carts.length;
  410. wx.showLoading({
  411. title: '',
  412. icon: 'loading',
  413. mask: true,
  414. success: function () {
  415. }
  416. })
  417. that.selectSta(len, selectAllStatus);
  418. },
  419. //全选-分别做请求,判断是否下架,是否有库存
  420. selectSta: function (len, selectAllStatus) {
  421. var that = this;
  422. var host = getApp().globalData.servsers;
  423. var carts = that.data.carts;
  424. var shixiao_num = that.data.shixiao_num;
  425. if (len == 0) {
  426. that.setData({
  427. selectAllStatus: selectAllStatus,
  428. });
  429. that.getTotalPrice();
  430. wx.hideLoading();
  431. } else {
  432. len -= 1;
  433. var com_id = carts[len].com_id;
  434. if (selectAllStatus == true) {//全部选中
  435. //检测商品是否下架
  436. wx.request({
  437. url: host + "commodityapi/commoditview",
  438. data: {
  439. com_id: com_id
  440. },
  441. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  442. header: {
  443. 'Accept': 'application/json'
  444. },
  445. success: function (res) {
  446. //有此商品
  447. if (res.data.total > 0) {
  448. carts[len].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
  449. that.setData({
  450. carts: carts
  451. })
  452. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  453. carts[len].commodity_flag = 1;
  454. carts[len].flag = 0;
  455. shixiao_num++;
  456. that.setData({
  457. carts: carts,
  458. shixiao_num: shixiao_num,
  459. selectAllStatus: selectAllStatus,
  460. })
  461. that.selectSta(len, selectAllStatus);
  462. } else {
  463. //不是失效商品,检测库存是否充足
  464. wx.request({
  465. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  466. data: {
  467. datasheetGroupId: carts[len].com_group_id
  468. },
  469. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  470. header: {
  471. 'Accept': 'application/json'
  472. },
  473. success: function (res) {
  474. console.log(res);
  475. if (res.data.code == '200') {
  476. carts[len].canBuy = res.data.data.canBuy;//有无库存
  477. carts[len].repertory = res.data.data.repertory;//当前规格的库存数量
  478. if (res.data.data.canBuy) {
  479. if (res.data.data.repertory < carts[len].limitNum || res.data.data.repertory < carts[len].num || parseInt(carts[len].num) < parseInt(carts[len].limitNum)) {
  480. carts[len].canBuy = false;
  481. carts[len].flag = 0;//取消勾选
  482. shixiao_num += 1;
  483. } else {
  484. var sumNum = parseInt(carts[len].num);
  485. for (var t = 0; t < len; t++) {
  486. if (carts[t].com_id == carts[len].com_id && carts[t].com_group_id == carts[len].com_group_id) {
  487. sumNum += parseInt(carts[t].num);
  488. }
  489. }
  490. console.log("全选-sumNum:" + sumNum);
  491. console.log("全选-repertory:" + res.data.data.repertory);
  492. if (sumNum > res.data.data.repertory) {
  493. carts[len].flag = 0;
  494. selectAllStatus = false;
  495. } else {
  496. carts[len].flag = 1;
  497. carts[len].new_price = parseFloat(carts[len].cost) + parseFloat(carts[len].box_price);
  498. }
  499. }
  500. } else {
  501. carts[len].flag = 0;
  502. shixiao_num++;
  503. }
  504. that.setData({
  505. carts: carts,
  506. shixiao_num: shixiao_num,
  507. selectAllStatus: selectAllStatus,
  508. })
  509. that.selectSta(len, selectAllStatus);
  510. }
  511. }
  512. })
  513. }
  514. } else {
  515. carts[len].commodity_flag = 1;
  516. shixiao_num++;
  517. that.setData({
  518. carts: carts,
  519. shixiao_num: shixiao_num,
  520. selectAllStatus: selectAllStatus,
  521. })
  522. that.selectSta(len, selectAllStatus);
  523. }
  524. }
  525. })
  526. } else {
  527. carts[len].flag = 0;
  528. that.setData({
  529. carts: carts
  530. })
  531. that.selectSta(len, selectAllStatus);
  532. }
  533. }
  534. },
  535. //绑定加数量事件
  536. addCount(e) {
  537. var that = this;
  538. var host = getApp().globalData.servsers;
  539. var index = e.currentTarget.dataset.index;
  540. var carts = that.data.carts;
  541. var com_id = carts[index].com_id;
  542. var num = parseInt(carts[index].num);
  543. var com_group_id = carts[index].com_group_id;//组合id
  544. var shixiao_num = that.data.shixiao_num;
  545. //检测商品是否下架
  546. wx.request({
  547. url: host + "commodityapi/commoditview",
  548. data: {
  549. com_id: com_id
  550. },
  551. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  552. header: {
  553. 'Accept': 'application/json'
  554. },
  555. success: function (res) {
  556. //有此商品
  557. if (res.data.total > 0) {
  558. carts[index].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
  559. that.setData({
  560. carts: carts
  561. })
  562. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  563. carts[index].commodity_flag = 1;
  564. carts[index].flag = 0;
  565. shixiao_num++;
  566. that.setData({
  567. carts: carts,
  568. shixiao_num: shixiao_num,
  569. })
  570. that.getTotalPrice();
  571. } else {
  572. //判断库存
  573. wx.request({
  574. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  575. data: {
  576. datasheetGroupId: com_group_id
  577. },
  578. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  579. header: {
  580. 'Accept': 'application/json'
  581. },
  582. success: function (res) {
  583. if (res.data.code == '200') {
  584. var canBuy = res.data.data.canBuy;
  585. carts[index].canBuy = res.data.data.canBuy;
  586. carts[index].repertory = res.data.data.repertory;
  587. //库存充足
  588. if (res.data.data.canBuy == true) {
  589. if (res.data.data.repertory > carts[index].num) {
  590. num++;
  591. carts[index].num = num;
  592. } else if (res.data.data.repertory < carts[index].num) {
  593. carts[index].num = res.data.data.repertory;
  594. that.setData({
  595. hiddenmodal: false,
  596. modalCont: '仅有' + res.data.data.repertory + '件该宝贝,赶快抢购吧~'
  597. })
  598. setTimeout(function () {
  599. that.setData({
  600. hiddenmodal: true,
  601. carts: carts
  602. })
  603. }, 2000)
  604. }
  605. } else {
  606. shixiao_num++;
  607. }
  608. that.setData({
  609. carts: carts,
  610. shixiao_num: shixiao_num,
  611. })
  612. that.getTotalPrice();
  613. } else {
  614. carts[index].commodity_flag = 1;
  615. carts[index].flag = 0;
  616. shixiao_num++;
  617. that.setData({
  618. carts: carts,
  619. shixiao_num: shixiao_num,
  620. })
  621. that.getTotalPrice();
  622. }
  623. }
  624. })
  625. }
  626. } else {
  627. carts[index].commodity_flag = 1;
  628. carts[index].flag = 0;
  629. shixiao_num++;
  630. that.setData({
  631. carts: carts,
  632. shixiao_num: shixiao_num,
  633. })
  634. that.getTotalPrice();
  635. }
  636. }
  637. })
  638. },
  639. //绑定减数量事件
  640. minusCount(e) {
  641. var that = this;
  642. var host = getApp().globalData.servsers;
  643. const index = e.currentTarget.dataset.index;
  644. let carts = this.data.carts;
  645. var com_id = carts[index].com_id;
  646. let num = parseInt(carts[index].num);
  647. let minnum = parseInt(carts[index].limitNum);
  648. var shixiao_num = that.data.shixiao_num;
  649. //检测商品是否下架
  650. wx.request({
  651. url: host + "commodityapi/commoditview",
  652. data: {
  653. com_id: com_id
  654. },
  655. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  656. header: {
  657. 'Accept': 'application/json'
  658. },
  659. success: function (res) {
  660. //有此商品
  661. if (res.data.total > 0) {
  662. carts[index].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
  663. that.setData({
  664. carts: carts
  665. })
  666. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  667. carts[index].commodity_flag = 1;
  668. carts[index].flag = 0;
  669. shixiao_num++;
  670. that.setData({
  671. shixiao_num: shixiao_num,
  672. })
  673. } else {
  674. //判断库存
  675. wx.request({
  676. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  677. data: {
  678. datasheetGroupId: carts[index].com_group_id
  679. },
  680. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  681. header: {
  682. 'Accept': 'application/json'
  683. },
  684. success: function (res) {
  685. if (res.data.code == '200') {
  686. var canBuy = res.data.data.canBuy;
  687. carts[index].canBuy = res.data.data.canBuy;
  688. carts[index].repertory = res.data.data.repertory;
  689. //库存充足
  690. if (res.data.data.canBuy == true) {
  691. console.log("+:num" + parseInt(carts[index].num));
  692. console.log("+:limitNum" + parseInt(carts[index].limitNum));
  693. if (res.data.data.repertory >= parseInt(carts[index].num)) {
  694. if (parseInt(carts[index].num) > parseInt(carts[index].limitNum)) {
  695. num--;
  696. carts[index].num = num;
  697. } else {
  698. carts[index].flag = 0;
  699. shixiao_num++;
  700. that.setData({
  701. carts: carts,
  702. shixiao_num: shixiao_num,
  703. })
  704. }
  705. } else {
  706. carts[index].commodity_flag = 1;
  707. carts[index].flag = 0;
  708. shixiao_num++;
  709. that.setData({
  710. shixiao_num: shixiao_num,
  711. })
  712. }
  713. } else {
  714. shixiao_num++;
  715. }
  716. that.setData({
  717. carts: carts,
  718. shixiao_num: shixiao_num,
  719. })
  720. that.getTotalPrice();
  721. }
  722. }
  723. })
  724. }
  725. } else {
  726. carts[index].commodity_flag = 1;
  727. carts[index].flag = 0;
  728. shixiao_num++;
  729. that.setData({
  730. shixiao_num: shixiao_num,
  731. })
  732. }
  733. that.setData({
  734. carts: carts
  735. })
  736. that.getTotalPrice();
  737. }
  738. })
  739. },
  740. //立即购买
  741. tz: function () {
  742. var that = this;
  743. var carts = that.data.carts;
  744. var len = that.data.carts.length;
  745. var chooseNum = 0;//当前勾选商品数量
  746. that.setData({
  747. firstClick: 1
  748. })
  749. for (var i = 0; i < len; i++) {
  750. if (that.data.carts[i].flag == 1) {
  751. chooseNum++;
  752. }
  753. }
  754. if (chooseNum > 0) {
  755. that.setData({
  756. buyCarts: []
  757. })
  758. wx.showLoading({
  759. title: '',
  760. icon: 'loading',
  761. mask: true,
  762. success: function () {
  763. }
  764. })
  765. that.selectCartsSta(len);
  766. } else {
  767. that.setData({
  768. hiddenmodal: false,
  769. modalCont: '还没有选择宝贝!'
  770. })
  771. setTimeout(function () {
  772. that.setData({
  773. hiddenmodal: true,
  774. firstClick: 0
  775. })
  776. }, 1500)
  777. return false;
  778. }
  779. },
  780. //检查立即购买的商品中是否有下架商品
  781. selectCartsSta: function (len) {
  782. var that = this;
  783. var host = getApp().globalData.servsers;
  784. var carts = that.data.carts;
  785. var lenOld = that.data.carts.length;
  786. var shixiao_num = that.data.shixiao_num;
  787. if (len == 0) {
  788. if (lenOld > 0) {
  789. that.setData({
  790. carts: carts,
  791. buyCarts: that.data.buyCarts
  792. });
  793. var newCarts = JSON.stringify(that.data.buyCarts);
  794. newCarts = newCarts.replace(/&/g, "zss");
  795. wx.navigateTo({
  796. url: '/pages/confirmOrder/confirmOrder?carts=' + newCarts,
  797. })
  798. that.getTotalPrice();
  799. } else {
  800. return false;
  801. }
  802. } else {
  803. len -= 1;
  804. if (carts[len].flag == 1) {//检测立即购买中的商品,是否下架
  805. var com_id = carts[len].com_id;
  806. //检测商品是否下架
  807. wx.request({
  808. url: host + "commodityapi/commoditview",
  809. data: {
  810. com_id: com_id
  811. },
  812. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  813. header: {
  814. 'Accept': 'application/json'
  815. },
  816. success: function (res) {
  817. //有此商品
  818. if (res.data.total > 0) {
  819. carts[len].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
  820. that.setData({
  821. carts: carts
  822. })
  823. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  824. carts[len].commodity_flag = 1;
  825. carts[len].flag = 0;
  826. that.getTotalPrice();
  827. shixiao_num++;
  828. wx.hideLoading();
  829. that.setData({
  830. hiddenmodal: false,
  831. modalCont: '"' + carts[len].name + '"已下架,请重新选择!'
  832. })
  833. setTimeout(function () {
  834. that.setData({
  835. shixiao_num: shixiao_num,
  836. carts: carts,
  837. firstClick: 0,
  838. hiddenmodal: true
  839. })
  840. }, 3000)
  841. return false;
  842. } else {
  843. //判断库存
  844. wx.request({
  845. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  846. data: {
  847. datasheetGroupId: carts[len].com_group_id
  848. },
  849. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  850. header: {
  851. 'Accept': 'application/json'
  852. },
  853. success: function (res) {
  854. if (res.data.code == '200') {
  855. var canBuy = res.data.data.canBuy;
  856. carts[len].canBuy = res.data.data.canBuy;
  857. //库存充足
  858. if (res.data.data.canBuy == true) {
  859. if (res.data.data.repertory < carts[len].num) {
  860. carts[len].canBuy = false;
  861. carts[len].flag = 0;//取消勾选
  862. shixiao_num++;
  863. wx.hideLoading();
  864. that.setData({
  865. hiddenmodal: false,
  866. modalCont: '"' + carts[len].name + '"库存不足,请重新选择!'
  867. })
  868. setTimeout(function () {
  869. that.setData({
  870. carts: carts,
  871. firstClick: 0,
  872. shixiao_num: shixiao_num,
  873. hiddenmodal: true
  874. })
  875. }, 3000)
  876. that.getTotalPrice();
  877. return false;
  878. } else {
  879. var sumNum = parseInt(carts[len].num);
  880. console.log("num:" + sumNum);
  881. for (var t = 0; t < len; t++) {
  882. if (carts[t].com_id == carts[len].com_id && carts[t].com_group_id == carts[len].com_group_id) {
  883. sumNum += parseInt(carts[t].num);
  884. }
  885. }
  886. console.log("立即购买-sumNum:" + sumNum);
  887. console.log("立即购买-repertory:" + res.data.data.repertory);
  888. if (sumNum > res.data.data.repertory) {
  889. wx.hideLoading();
  890. that.setData({
  891. hiddenmodal: false,
  892. modalCont: '"' + carts[len].name + '-' + carts[len].style1_name + carts[len].style2_name + carts[len].style3_name + carts[len].style4_name + carts[len].style5_name + '",库存仅剩' + res.data.data.repertory + '件,请重新选择!'
  893. })
  894. setTimeout(function () {
  895. that.setData({
  896. carts: carts,
  897. firstClick: 0,
  898. hiddenmodal: true
  899. })
  900. }, 3000);
  901. return false;
  902. } else {
  903. carts[len].flag = 1;
  904. carts[len].new_price = parseFloat(carts[len].cost) + parseFloat(carts[len].box_price);
  905. }
  906. that.data.buyCarts.push(carts[len]);
  907. that.selectCartsSta(len);
  908. }
  909. } else {
  910. carts[len].flag = 0;//取消勾选
  911. that.getTotalPrice();
  912. shixiao_num++;
  913. wx.hideLoading();
  914. that.setData({
  915. hiddenmodal: false,
  916. modalCont: '"' + carts[len].name + '"已售罄,请重新选择!'
  917. })
  918. setTimeout(function () {
  919. that.setData({
  920. carts: carts,
  921. firstClick: 0,
  922. shixiao_num: shixiao_num,
  923. hiddenmodal: true
  924. })
  925. }, 3000);
  926. that.getTotalPrice();
  927. return false;
  928. }
  929. }
  930. }
  931. })
  932. }
  933. } else {
  934. carts[len].commodity_flag = 1;
  935. carts[len].flag = 0;
  936. shixiao_num++;
  937. wx.hideLoading();
  938. that.setData({
  939. hiddenmodal: false,
  940. modalCont: '存在下架商品,请重新选择!'
  941. })
  942. setTimeout(function () {
  943. that.setData({
  944. carts: carts,
  945. firstClick: 0,
  946. shixiao_num: shixiao_num,
  947. hiddenmodal: true
  948. })
  949. }, 2000);
  950. that.getTotalPrice();
  951. return false;
  952. }
  953. }
  954. })
  955. } else {
  956. that.selectCartsSta(len);
  957. }
  958. }
  959. },
  960. buyUrl: function (e) {//跳转到当前商品对应的购买页
  961. var that = this;
  962. var host = getApp().globalData.servsers;
  963. var id = e.currentTarget.dataset.comid;
  964. wx.request({
  965. url: host + "commodityapi/commoditview",
  966. data: {
  967. com_id: id,
  968. },
  969. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  970. header: {
  971. 'Accept': 'application/json'
  972. },
  973. success: function (res) {
  974. if (res.data.total > 0) {
  975. wx.navigateTo({
  976. url: '/pages/buy/buy?com_id=' + id
  977. })
  978. } else {
  979. that.setData({
  980. hiddenmodal: false,
  981. modalCont: '该商品已下架!'
  982. })
  983. setTimeout(function () {
  984. that.setData({
  985. hiddenmodal: true
  986. })
  987. }, 2000)
  988. return false;
  989. }
  990. }
  991. })
  992. },
  993. touchS: function (e) {
  994. if (e.touches.length == 1) {
  995. this.setData({
  996. //设置触摸起始点水平方向位置
  997. startX: e.touches[0].clientX
  998. });
  999. }
  1000. },
  1001. touchM: function (e) {
  1002. if (e.touches.length == 1) {
  1003. //手指移动时水平方向位置
  1004. var moveX = e.touches[0].clientX;
  1005. //手指起始点位置与移动期间的差值
  1006. var disX = this.data.startX - moveX;
  1007. var delBtnWidth = this.data.delBtnWidth;
  1008. var index = e.currentTarget.dataset.index;
  1009. // var txtStyle = "";
  1010. if (disX == 0 || disX < 0) {//如果移动距离小于等于0,文本层位置不变
  1011. this.setData({
  1012. touchDel: index,
  1013. txtStyle: "0"
  1014. })
  1015. } else if (disX > 0) {//移动距离大于0,文本层left值等于手指移动距离
  1016. this.setData({
  1017. touchDel: index,
  1018. txtStyle: "-" + disX + "rpx",
  1019. // scrollY: false
  1020. })
  1021. if (disX >= delBtnWidth) {
  1022. //控制手指移动距离最大值为删除按钮的宽度
  1023. this.setData({
  1024. touchDel: index,
  1025. txtStyle: "-" + delBtnWidth + "rpx",
  1026. // scrollY: false
  1027. })
  1028. }
  1029. }
  1030. //获取手指触摸的是哪一项
  1031. // var index = e.target.dataset.index;
  1032. // var list = this.data.list;
  1033. // list[index].txtStyle = txtStyle;
  1034. // //更新列表的状态
  1035. // this.setData({
  1036. // list: list
  1037. // });
  1038. }
  1039. },
  1040. touchE: function (e) {
  1041. if (e.changedTouches.length == 1) {
  1042. //手指移动结束后水平位置
  1043. var endX = e.changedTouches[0].clientX;
  1044. //触摸开始与结束,手指移动的距离
  1045. var disX = this.data.startX - endX;
  1046. var delBtnWidth = this.data.delBtnWidth;
  1047. var index = e.currentTarget.dataset.index;
  1048. //如果距离小于删除按钮的1/2,不显示删除按钮
  1049. // var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
  1050. this.setData({
  1051. touchDel: index,
  1052. txtStyle: disX > delBtnWidth / 2 ? "-" + delBtnWidth + "rpx" : "0px",
  1053. // scrollY : disX > delBtnWidth / 2 ? false : true
  1054. })
  1055. //获取手指触摸的是哪一项
  1056. // var index = e.target.dataset.index;
  1057. // var list = this.data.list;
  1058. // list[index].txtStyle = txtStyle;
  1059. // //更新列表的状态
  1060. // this.setData({
  1061. // list: list
  1062. // });
  1063. }
  1064. },
  1065. //获取元素自适应后的实际宽度
  1066. getEleWidth: function (w) {
  1067. var real = 0;
  1068. try {
  1069. var res = wx.getSystemInfoSync().windowWidth;
  1070. var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
  1071. real = Math.floor(res / scale);
  1072. return real;
  1073. } catch (e) {
  1074. return false;
  1075. }
  1076. },
  1077. //点击删除按钮事件
  1078. delItem: function (e) {
  1079. var that = this;
  1080. //获取列表中要删除项的下标
  1081. var host = getApp().globalData.servsers;
  1082. var index = e.currentTarget.dataset.index;
  1083. var id = e.currentTarget.dataset.id;
  1084. var flag = e.currentTarget.dataset.flag;
  1085. var shixiao_num = that.data.shixiao_num;//失效数量
  1086. let carts = that.data.carts;
  1087. var cartsLength = that.data.carts.length;
  1088. that.setData({
  1089. txtStyle: 0
  1090. });
  1091. //删除购物车数据
  1092. wx.request({
  1093. url: host + "shoppingcartapi/deleteshoppingcart",
  1094. data: {
  1095. id: id
  1096. },
  1097. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  1098. header: {
  1099. 'Accept': 'application/json'
  1100. },
  1101. success: function (res) {
  1102. carts.splice(index, 1);
  1103. that.setData({
  1104. carts: carts
  1105. });
  1106. var cartsLength = carts.length;
  1107. var flagNum = cartsLength - shixiao_num;
  1108. if (cartsLength == 0) {
  1109. that.setData({
  1110. hasList: false,
  1111. paddingBottom: 0
  1112. })
  1113. } else {
  1114. if (flag == 0) {
  1115. if (flagNum > 0) {
  1116. that.setData({
  1117. selectAllStatus: true
  1118. });
  1119. } else {
  1120. that.setData({
  1121. selectAllStatus: false
  1122. });
  1123. }
  1124. that.getTotalPrice();
  1125. } else {
  1126. //删除的是失效商品
  1127. shixiao_num--;
  1128. that.setData({
  1129. shixiao_num: shixiao_num
  1130. })
  1131. }
  1132. }
  1133. if (res == null || res.data == null) {
  1134. console.error('网络请求失败');
  1135. return;
  1136. }
  1137. }
  1138. });
  1139. },
  1140. //下拉刷新
  1141. onPullDownRefresh: function (e) {
  1142. var that = this;
  1143. that.loadFun();
  1144. wx.stopPullDownRefresh();
  1145. },
  1146. /**
  1147. * 计算总价
  1148. */
  1149. getTotalPrice() {
  1150. let carts = this.data.carts;//获取购物车列表
  1151. let total = 0;
  1152. for (let i = 0; i < this.data.carts.length; i++) {
  1153. if (carts[i].flag == 1 && carts[i].commodity_flag == 0) { // 判断选中[flag = 1]并且是上架[commodity_flag = 0]的商品,才会计算价格
  1154. total += carts[i].num * (parseFloat(carts[i].cost) + parseFloat(carts[i].box_price)); // 所有价格加起来
  1155. }
  1156. }
  1157. this.setData({
  1158. carts: carts,
  1159. totalPrice: total.toFixed(2)
  1160. });
  1161. }
  1162. })
  1163. var that;
  1164. var Util = require('../../utils/util.js');