index.js 44 KB

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