123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269 |
- // index.js
- var likeList = [
- { url: '/pages/buy/buy?com_id=3292', imgSrc: 'https://www.daliangzao.net/images/c62671d2f0454054a9d9fbbc87965fcf.jpg', name: '【Herbacin/贺本清】小甘菊护手霜礼盒 ', oldPrice: '98', newPrice: '68' },
- { url: '/pages/buy/buy?com_id=3296', imgSrc: 'https://www.daliangzao.net/images/11d3bda623b241adb6aff125bd2a2477.jpg', name: '【FOREO】LUNA2 电动硅胶洁面仪', oldPrice: '1880', newPrice: '1373' },
- { url: '/pages/buy/buy?com_id=3539', imgSrc: 'https://www.daliangzao.net/images/359f87b24aaa4df899b1b7dfaa448911.jpg', name: '【Dyson戴森】Supersonic HD01吹风机(中国红甄选礼盒)', oldPrice: '3190', newPrice: '3190' },
- { url: '/pages/buy/buy?com_id=3294', imgSrc: 'https://www.daliangzao.net/images/323cce98818944248fb791115004b18e.jpg', name: '【大良造】粉色少女心生活礼盒(毛毯+眼罩+拖鞋)', oldPrice: '388', newPrice: '388' },
- { url: '/pages/buy/buy?com_id=3270', imgSrc: 'https://www.daliangzao.net/images/b942f40c711f4336ae8f6efecebd11b6.jpg', name: '【大良造】精致生活礼盒(香槟+巧克力)', oldPrice: '195', newPrice: '193' },
- { url: '/pages/buy/buy?com_id=3275', imgSrc: 'https://www.daliangzao.net/images/4152d74f27634b77bdae594c07c05f1c.jpg', name: '【大良造】创意办公礼盒(竹节笔+笔记本)', oldPrice: '311', newPrice: '311' }
- ];
- Page({
- data: {
- contentShow: '',//初始整体内容隐藏,加载完成后显示
- likeList: likeList,
- carts: [],
- buyCarts: [],
- hasList: false, // 列表是否有数据
- totalPrice: 0, // 总价,初始为0
- selected: '',
- selectAllStatus: true, // 全选状态,默认全选
- delBtnWidth: 160, //删除按钮宽度单位(rpx)
- txtStyle: "",
- touchDel: 0,
- scrollY: true, //页面是否可滚动
- isMinus: true,
- isAdd: true,
- hidden: 'hidden',
- hiddenmodal: true, //弹窗
- maskBg: false,//支付提示蒙层
- paddingBottom: 0,
- un_id: '',
- shixiao_num: 0,//失效商品数量
- firstClick: 0, //没有连点购买按钮
- combineList: [],//存放相同规格,不同礼盒和工艺的商品,合并后的数组【仅需 商品id,组合规格id,总数量】
- },
- loadFun: function () {
- //wx.showNavigationBarLoading();
- var that = this;
- var host = getApp().globalData.servsers;
- var un_id = getApp().globalData.un_id;
- wx.showLoading({
- title: '',
- icon: 'loading',
- mask: true,
- success: function () {
- }
- })
- that.setData({
- contentShow: '',//初始整体内容隐藏,加载完成后显示
- carts: [],
- buyCarts: [],
- hasList: false, // 列表是否有数据
- totalPrice: 0, // 总价,初始为0
- selected: '',
- selectAllStatus: true, // 全选状态,默认全选
- delBtnWidth: 160, //删除按钮宽度单位(rpx)
- txtStyle: "",
- touchDel: 0,
- scrollY: true, //页面是否可滚动
- isMinus: true,
- isAdd: true,
- hidden: 'hidden',
- hiddenmodal: true, //弹窗
- maskBg: false,//支付提示蒙层
- paddingBottom: 0,
- un_id: '',
- shixiao_num: 0,//失效商品数量
- firstClick: 0, //没有连点购买按钮
- })
- var shixiao_num = 0;
- that.setData({
- un_id: un_id,
- firstClick: 0
- })
- if (un_id != undefined && un_id != '' && un_id != null) {
- wx.request({
- url: host + "shoppingcartapi/shoppingcartview",
- data: {
- user_id: un_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- console.log(res);
- if (res.data.total > 0) {
- that.setData({
- carts: res.data.rows,
- hasList: true,
- paddingBottom: 100
- });
- var carts = res.data.rows;
- console.log(res.data.rows);
- var cartsLength = res.data.rows.length;
- //检测库存
- that.checkCommodityGroupRepertory(cartsLength, 0, 0, 0);
- } else {
- that.setData({
- contentShow: 'contentShow'
- })
- wx.hideLoading();
- }
- if (res == null || res.data == null) {
- console.error('网络请求失败');
- return;
- }
- }
- });
- }
- },
- //检测商品售罄
- checkCommodityGroupRepertory: function (cartsLength, total, flagNum, shixiao_num) {
- var that = this;
- var host = getApp().globalData.servsers;
- var un_id = getApp().globalData.un_id;
- //shixiao_num : 失效商品数量【失效+售罄】
- var carts = that.data.carts;
- if (cartsLength == 0) {
- console.log("cartsLength:" + cartsLength);
- console.log("shixiao_num:" + shixiao_num);
- console.log("flagNum:" + flagNum);
- var len = carts.length;
- if (flagNum == (len - shixiao_num) && flagNum > 0) {
- that.setData({
- selectAllStatus: true,
- totalPrice: total.toFixed(2)
- });
- } else {
- that.setData({
- selectAllStatus: false,
- totalPrice: total.toFixed(2)
- });
- }
- that.setData({
- carts: carts
- })
- that.getTotalPrice();
- that.setData({
- contentShow: 'contentShow'
- })
- console.log(carts);
- wx.hideLoading();
- } else {
- cartsLength -= 1;
- wx.request({
- url: host + "commodityapi/commoditview",
- data: {
- com_id: carts[cartsLength].com_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- console.log("load:");
- console.log(res);
- //有此商品
- if (res.data.total > 0) {
- carts[cartsLength].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
- that.setData({
- carts: carts
- })
- if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
- carts[cartsLength].num = parseInt(carts[cartsLength].num);
- carts[cartsLength].limitNum = parseInt(carts[cartsLength].limitNum);
- carts[cartsLength].flag = 0;//取消勾选
- carts[cartsLength].canBuy = true;//默认有库存
- shixiao_num += 1;
- that.setData({
- shixiao_num: shixiao_num,
- carts: carts
- })
- that.checkCommodityGroupRepertory(cartsLength, total, flagNum, shixiao_num);
- } else {
- //不是失效商品,检测库存是否充足
- wx.request({
- url: host + "api/commodityGroup/checkCommodityGroupRepertory",
- data: {
- datasheetGroupId: carts[cartsLength].com_group_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- console.log(res);
- if (res.data.code == '200') {
- carts[cartsLength].canBuy = res.data.data.canBuy;//有无库存
- carts[cartsLength].repertory = res.data.data.repertory;//当前规格的库存数量
- //库存充足
- if (res.data.data.canBuy == true) {
- if (res.data.data.repertory < carts[cartsLength].limitNum || res.data.data.repertory < carts[cartsLength].num || parseInt(carts[cartsLength].num) < parseInt(carts[cartsLength].limitNum)) {
- carts[cartsLength].canBuy = false;
- carts[cartsLength].flag = 0;//取消勾选
- shixiao_num += 1;
- } else {
- if (carts[cartsLength].box_price == '' || carts[cartsLength].box_price == null || carts[cartsLength].box_price == undefined) {
- carts[cartsLength].box_price = 0;
- }
- carts[cartsLength].new_price = parseFloat(carts[cartsLength].cost) + parseFloat(carts[cartsLength].box_price);
- if (carts[cartsLength].flag == 1) {//当前为选中
- flagNum += 1;
- total += carts[cartsLength].num * carts[cartsLength].new_price;
- }
- }
- } else {
- carts[cartsLength].flag = 0;//取消勾选
- shixiao_num += 1;
- }
- carts[cartsLength].num = parseInt(carts[cartsLength].num);
- that.setData({
- shixiao_num: shixiao_num,
- carts: carts
- })
- that.checkCommodityGroupRepertory(cartsLength, total, flagNum, shixiao_num);
- }
- }
- })
- //request结束
- }
- } else {
- carts[cartsLength].num = parseInt(carts[cartsLength].num);
- carts[cartsLength].limitNum = parseInt(carts[cartsLength].limitNum);
- carts[cartsLength].flag = 0;//取消勾选
- carts[cartsLength].canBuy = true;//默认有库存
- shixiao_num += 1;
- that.setData({
- shixiao_num: shixiao_num,
- carts: carts
- })
- that.checkCommodityGroupRepertory(cartsLength, total, flagNum, shixiao_num);
- }
- }
- })
- //非失效商品判断结束
- }
- },
- onShow: function (e) {
- var that = this;
- that.loadFun();
- },
- //当前商品选中事件
- selectList(e) {
- var that = this;
- var host = getApp().globalData.servsers;
- var index = e.currentTarget.dataset.index;
- var carts = that.data.carts;
- var selected = carts[index].flag;
- var com_id = carts[index].com_id;//商品唯一标识id
- var cartsLength = that.data.carts.length;
- var shixiao_num = that.data.shixiao_num;
- var flagNum = 0;
- //检测商品是否下架
- wx.request({
- url: host + "commodityapi/commoditview",
- data: {
- com_id: com_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- //有此商品
- if (res.data.total > 0) {
- carts[index].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
- that.setData({
- carts: carts
- })
- console.log(res.data.commodity.commodity_flag);
- if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- shixiao_num: shixiao_num,
- })
- } else {
- //不是失效商品,检测库存是否充足
- wx.request({
- url: host + "api/commodityGroup/checkCommodityGroupRepertory",
- data: {
- datasheetGroupId: carts[index].com_group_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- if (res.data.code == '200') {
- carts[index].canBuy = res.data.data.canBuy;//有无库存
- carts[index].repertory = res.data.data.repertory;//当前规格的库存数量
- if (res.data.data.canBuy) {
- if (res.data.data.repertory < carts[index].limitNum || res.data.data.repertory < carts[index].num || parseInt(carts[index].num) < parseInt(carts[index].limitNum)) {//库存小于 商品数量或最低起订量
- carts[index].canBuy = false;
- carts[index].flag = 0;//取消勾选
- shixiao_num += 1;
- that.setData({
- shixiao_num: shixiao_num,
- hiddenmodal: false,
- modalCont: '商品已售罄!'
- })
- setTimeout(function () {
- that.setData({
- hiddenmodal: true,
- firstClick: 0
- })
- }, 1500)
- } else {
- //同类别数量和【商品id相同,规格id相同】
- var combineNum = 0;
- if (selected == 0) {//勾选操作,分别和勾选商品加和比对
- var sumNum = parseInt(carts[index].num);
- for (var t = 0; t < carts.length; t++) {
- if (carts[t].flag == 1) {//同勾选商品比对
- if (carts[t].com_id == carts[index].com_id && carts[t].com_group_id == carts[index].com_group_id) {
- sumNum += parseInt(carts[t].num);
- }
- }
- }
- console.log("sumNum:" + sumNum);
- console.log("repertory:" + res.data.data.repertory);
- if (sumNum > res.data.data.repertory) {
- that.setData({
- hiddenmodal: false,
- modalCont: '库存不足!'
- })
- setTimeout(function () {
- that.setData({
- hiddenmodal: true
- })
- }, 1500)
- return false;
- } else {
- carts[index].flag = 1;
- for (var i = 0; i < cartsLength; i++) {
- if (carts[i].flag == 1) {
- flagNum++;
- }
- }
- if (flagNum == (cartsLength - shixiao_num) && flagNum > 0) {
- that.setData({
- selectAllStatus: true,
- })
- }
- }
- } else {
- carts[index].flag = 0;
- if (that.data.selectAllStatus == true) {
- that.setData({
- selectAllStatus: false
- })
- }
- }
- }
- } else {
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- shixiao_num: shixiao_num,
- hiddenmodal: false,
- modalCont: '此商品已售罄!'
- })
- setTimeout(function () {
- that.setData({
- hiddenmodal: true,
- firstClick: 0
- })
- }, 1500)
- return false;
- }
- that.setData({
- carts: carts
- })
- that.getTotalPrice();
- }
- }
- })
- }
- } else {//无此商品
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- hiddenmodal: false,
- modalCont: '此商品已下架!'
- })
- that.getTotalPrice();
- setTimeout(function () {
- that.setData({
- hiddenmodal: true,
- firstClick: 0
- })
- }, 1500)
- return false;
- }
- }
- })
- },
- //购物车全选事件
- selectAll(e) {
- var that = this;
- var host = getApp().globalData.servsers;
- var selectAllStatus = that.data.selectAllStatus;
- selectAllStatus = !selectAllStatus;
- let carts = that.data.carts;
- var len = carts.length;
- wx.showLoading({
- title: '',
- icon: 'loading',
- mask: true,
- success: function () {
- }
- })
- that.selectSta(len, selectAllStatus);
- },
- //全选-分别做请求,判断是否下架,是否有库存
- selectSta: function (len, selectAllStatus) {
- var that = this;
- var host = getApp().globalData.servsers;
- var carts = that.data.carts;
- var shixiao_num = that.data.shixiao_num;
- if (len == 0) {
- that.setData({
- selectAllStatus: selectAllStatus,
- });
- that.getTotalPrice();
- wx.hideLoading();
- } else {
- len -= 1;
- var com_id = carts[len].com_id;
- if (selectAllStatus == true) {//全部选中
- //检测商品是否下架
- wx.request({
- url: host + "commodityapi/commoditview",
- data: {
- com_id: com_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- //有此商品
- if (res.data.total > 0) {
- carts[len].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
- that.setData({
- carts: carts
- })
- if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
- carts[len].commodity_flag = 1;
- carts[len].flag = 0;
- shixiao_num++;
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- selectAllStatus: selectAllStatus,
- })
- that.selectSta(len, selectAllStatus);
- } else {
- //不是失效商品,检测库存是否充足
- wx.request({
- url: host + "api/commodityGroup/checkCommodityGroupRepertory",
- data: {
- datasheetGroupId: carts[len].com_group_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- console.log(res);
- if (res.data.code == '200') {
- carts[len].canBuy = res.data.data.canBuy;//有无库存
- carts[len].repertory = res.data.data.repertory;//当前规格的库存数量
- if (res.data.data.canBuy) {
- if (res.data.data.repertory < carts[len].limitNum || res.data.data.repertory < carts[len].num || parseInt(carts[len].num) < parseInt(carts[len].limitNum)) {
- carts[len].canBuy = false;
- carts[len].flag = 0;//取消勾选
- shixiao_num += 1;
- } else {
- var sumNum = parseInt(carts[len].num);
- for (var t = 0; t < len; t++) {
- if (carts[t].com_id == carts[len].com_id && carts[t].com_group_id == carts[len].com_group_id) {
- sumNum += parseInt(carts[t].num);
- }
- }
- console.log("全选-sumNum:" + sumNum);
- console.log("全选-repertory:" + res.data.data.repertory);
- if (sumNum > res.data.data.repertory) {
- carts[len].flag = 0;
- selectAllStatus = false;
- } else {
- carts[len].flag = 1;
- carts[len].new_price = parseFloat(carts[len].cost) + parseFloat(carts[len].box_price);
- }
- }
- } else {
- carts[len].flag = 0;
- shixiao_num++;
- }
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- selectAllStatus: selectAllStatus,
- })
- that.selectSta(len, selectAllStatus);
- }
- }
- })
- }
- } else {
- carts[len].commodity_flag = 1;
- shixiao_num++;
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- selectAllStatus: selectAllStatus,
- })
- that.selectSta(len, selectAllStatus);
- }
- }
- })
- } else {
- carts[len].flag = 0;
- that.setData({
- carts: carts
- })
- that.selectSta(len, selectAllStatus);
- }
- }
- },
- //绑定加数量事件
- addCount(e) {
- var that = this;
- var host = getApp().globalData.servsers;
- var index = e.currentTarget.dataset.index;
- var carts = that.data.carts;
- var com_id = carts[index].com_id;
- var num = parseInt(carts[index].num);
- var com_group_id = carts[index].com_group_id;//组合id
- var shixiao_num = that.data.shixiao_num;
- //检测商品是否下架
- wx.request({
- url: host + "commodityapi/commoditview",
- data: {
- com_id: com_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- //有此商品
- if (res.data.total > 0) {
- carts[index].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
- that.setData({
- carts: carts
- })
- if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- })
- that.getTotalPrice();
- } else {
- //判断库存
- wx.request({
- url: host + "api/commodityGroup/checkCommodityGroupRepertory",
- data: {
- datasheetGroupId: com_group_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- if (res.data.code == '200') {
- var canBuy = res.data.data.canBuy;
- carts[index].canBuy = res.data.data.canBuy;
- carts[index].repertory = res.data.data.repertory;
- //库存充足
- if (res.data.data.canBuy == true) {
- if (res.data.data.repertory > carts[index].num) {
- num++;
- carts[index].num = num;
- } else if (res.data.data.repertory < carts[index].num) {
- carts[index].num = res.data.data.repertory;
- that.setData({
- hiddenmodal: false,
- modalCont: '仅有' + res.data.data.repertory + '件该宝贝,赶快抢购吧~'
- })
- setTimeout(function () {
- that.setData({
- hiddenmodal: true,
- carts: carts
- })
- }, 2000)
- }
- } else {
- shixiao_num++;
- }
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- })
- that.getTotalPrice();
- } else {
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- })
- that.getTotalPrice();
- }
- }
- })
- }
- } else {
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- })
- that.getTotalPrice();
- }
- }
- })
- },
- //绑定减数量事件
- minusCount(e) {
- var that = this;
- var host = getApp().globalData.servsers;
- const index = e.currentTarget.dataset.index;
- let carts = this.data.carts;
- var com_id = carts[index].com_id;
- let num = parseInt(carts[index].num);
- let minnum = parseInt(carts[index].limitNum);
- var shixiao_num = that.data.shixiao_num;
- //检测商品是否下架
- wx.request({
- url: host + "commodityapi/commoditview",
- data: {
- com_id: com_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- //有此商品
- if (res.data.total > 0) {
- carts[index].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
- that.setData({
- carts: carts
- })
- if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- shixiao_num: shixiao_num,
- })
- } else {
- //判断库存
- wx.request({
- url: host + "api/commodityGroup/checkCommodityGroupRepertory",
- data: {
- datasheetGroupId: carts[index].com_group_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- if (res.data.code == '200') {
- var canBuy = res.data.data.canBuy;
- carts[index].canBuy = res.data.data.canBuy;
- carts[index].repertory = res.data.data.repertory;
- //库存充足
- if (res.data.data.canBuy == true) {
- console.log("+:num" + parseInt(carts[index].num));
- console.log("+:limitNum" + parseInt(carts[index].limitNum));
- if (res.data.data.repertory >= parseInt(carts[index].num)) {
- if (parseInt(carts[index].num) > parseInt(carts[index].limitNum)) {
- num--;
- carts[index].num = num;
- } else {
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- })
- }
- } else {
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- shixiao_num: shixiao_num,
- })
- }
- } else {
- shixiao_num++;
- }
- that.setData({
- carts: carts,
- shixiao_num: shixiao_num,
- })
- that.getTotalPrice();
- }
- }
- })
- }
- } else {
- carts[index].commodity_flag = 1;
- carts[index].flag = 0;
- shixiao_num++;
- that.setData({
- shixiao_num: shixiao_num,
- })
- }
- that.setData({
- carts: carts
- })
- that.getTotalPrice();
- }
- })
- },
- //立即购买
- tz: function () {
- var that = this;
- var carts = that.data.carts;
- var len = that.data.carts.length;
- var chooseNum = 0;//当前勾选商品数量
- that.setData({
- firstClick: 1
- })
- for (var i = 0; i < len; i++) {
- if (that.data.carts[i].flag == 1) {
- chooseNum++;
- }
- }
- if (chooseNum > 0) {
- that.setData({
- buyCarts: []
- })
- wx.showLoading({
- title: '',
- icon: 'loading',
- mask: true,
- success: function () {
- }
- })
- that.selectCartsSta(len);
- } else {
- that.setData({
- hiddenmodal: false,
- modalCont: '还没有选择宝贝!'
- })
- setTimeout(function () {
- that.setData({
- hiddenmodal: true,
- firstClick: 0
- })
- }, 1500)
- return false;
- }
- },
- //检查立即购买的商品中是否有下架商品
- selectCartsSta: function (len) {
- var that = this;
- var host = getApp().globalData.servsers;
- var carts = that.data.carts;
- var lenOld = that.data.carts.length;
- var shixiao_num = that.data.shixiao_num;
- if (len == 0) {
- if (lenOld > 0) {
- that.setData({
- carts: carts,
- buyCarts: that.data.buyCarts
- });
- var newCarts = JSON.stringify(that.data.buyCarts);
- newCarts = newCarts.replace(/&/g, "zss");
- wx.navigateTo({
- url: '/pages/confirmOrder/confirmOrder?carts=' + newCarts,
- })
- that.getTotalPrice();
- } else {
- return false;
- }
- } else {
- len -= 1;
- if (carts[len].flag == 1) {//检测立即购买中的商品,是否下架
- var com_id = carts[len].com_id;
- //检测商品是否下架
- wx.request({
- url: host + "commodityapi/commoditview",
- data: {
- com_id: com_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- //有此商品
- if (res.data.total > 0) {
- carts[len].limitNum = parseInt(res.data.commodity.commodity_num);//同步起订量
- that.setData({
- carts: carts
- })
- if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
- carts[len].commodity_flag = 1;
- carts[len].flag = 0;
- that.getTotalPrice();
- shixiao_num++;
- wx.hideLoading();
- that.setData({
- hiddenmodal: false,
- modalCont: '"' + carts[len].name + '"已下架,请重新选择!'
- })
- setTimeout(function () {
- that.setData({
- shixiao_num: shixiao_num,
- carts: carts,
- firstClick: 0,
- hiddenmodal: true
- })
- }, 3000)
- return false;
- } else {
- //判断库存
- wx.request({
- url: host + "api/commodityGroup/checkCommodityGroupRepertory",
- data: {
- datasheetGroupId: carts[len].com_group_id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- if (res.data.code == '200') {
- var canBuy = res.data.data.canBuy;
- carts[len].canBuy = res.data.data.canBuy;
- //库存充足
- if (res.data.data.canBuy == true) {
- if (res.data.data.repertory < carts[len].num) {
- carts[len].canBuy = false;
- carts[len].flag = 0;//取消勾选
- shixiao_num++;
- wx.hideLoading();
- that.setData({
- hiddenmodal: false,
- modalCont: '"' + carts[len].name + '"库存不足,请重新选择!'
- })
- setTimeout(function () {
- that.setData({
- carts: carts,
- firstClick: 0,
- shixiao_num: shixiao_num,
- hiddenmodal: true
- })
- }, 3000)
- that.getTotalPrice();
- return false;
- } else {
- var sumNum = parseInt(carts[len].num);
- console.log("num:" + sumNum);
- for (var t = 0; t < len; t++) {
- if (carts[t].com_id == carts[len].com_id && carts[t].com_group_id == carts[len].com_group_id) {
- sumNum += parseInt(carts[t].num);
- }
- }
- console.log("立即购买-sumNum:" + sumNum);
- console.log("立即购买-repertory:" + res.data.data.repertory);
- if (sumNum > res.data.data.repertory) {
- wx.hideLoading();
- that.setData({
- hiddenmodal: false,
- 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 + '件,请重新选择!'
- })
- setTimeout(function () {
- that.setData({
- carts: carts,
- firstClick: 0,
- hiddenmodal: true
- })
- }, 3000);
- return false;
- } else {
- carts[len].flag = 1;
- carts[len].new_price = parseFloat(carts[len].cost) + parseFloat(carts[len].box_price);
- }
- that.data.buyCarts.push(carts[len]);
- that.selectCartsSta(len);
- }
- } else {
- carts[len].flag = 0;//取消勾选
- that.getTotalPrice();
- shixiao_num++;
- wx.hideLoading();
- that.setData({
- hiddenmodal: false,
- modalCont: '"' + carts[len].name + '"已售罄,请重新选择!'
- })
- setTimeout(function () {
- that.setData({
- carts: carts,
- firstClick: 0,
- shixiao_num: shixiao_num,
- hiddenmodal: true
- })
- }, 3000);
- that.getTotalPrice();
- return false;
- }
- }
- }
- })
- }
- } else {
- carts[len].commodity_flag = 1;
- carts[len].flag = 0;
- shixiao_num++;
- wx.hideLoading();
- that.setData({
- hiddenmodal: false,
- modalCont: '存在下架商品,请重新选择!'
- })
- setTimeout(function () {
- that.setData({
- carts: carts,
- firstClick: 0,
- shixiao_num: shixiao_num,
- hiddenmodal: true
- })
- }, 2000);
- that.getTotalPrice();
- return false;
- }
- }
- })
- } else {
- that.selectCartsSta(len);
- }
- }
- },
- buyUrl: function (e) {//跳转到当前商品对应的购买页
- var that = this;
- var host = getApp().globalData.servsers;
- var id = e.currentTarget.dataset.comid;
- wx.request({
- url: host + "commodityapi/commoditview",
- data: {
- com_id: id,
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- if (res.data.total > 0) {
- wx.navigateTo({
- url: '/pages/buy/buy?com_id=' + id
- })
- } else {
- that.setData({
- hiddenmodal: false,
- modalCont: '该商品已下架!'
- })
- setTimeout(function () {
- that.setData({
- hiddenmodal: true
- })
- }, 2000)
- return false;
- }
- }
- })
- },
- touchS: function (e) {
- if (e.touches.length == 1) {
- this.setData({
- //设置触摸起始点水平方向位置
- startX: e.touches[0].clientX
- });
- }
- },
- touchM: function (e) {
- if (e.touches.length == 1) {
- //手指移动时水平方向位置
- var moveX = e.touches[0].clientX;
- //手指起始点位置与移动期间的差值
- var disX = this.data.startX - moveX;
- var delBtnWidth = this.data.delBtnWidth;
- var index = e.currentTarget.dataset.index;
- // var txtStyle = "";
- if (disX == 0 || disX < 0) {//如果移动距离小于等于0,文本层位置不变
- this.setData({
- touchDel: index,
- txtStyle: "0"
- })
- } else if (disX > 0) {//移动距离大于0,文本层left值等于手指移动距离
- this.setData({
- touchDel: index,
- txtStyle: "-" + disX + "rpx",
- // scrollY: false
- })
- if (disX >= delBtnWidth) {
- //控制手指移动距离最大值为删除按钮的宽度
- this.setData({
- touchDel: index,
- txtStyle: "-" + delBtnWidth + "rpx",
- // scrollY: false
- })
- }
- }
- //获取手指触摸的是哪一项
- // var index = e.target.dataset.index;
- // var list = this.data.list;
- // list[index].txtStyle = txtStyle;
- // //更新列表的状态
- // this.setData({
- // list: list
- // });
- }
- },
- touchE: function (e) {
- if (e.changedTouches.length == 1) {
- //手指移动结束后水平位置
- var endX = e.changedTouches[0].clientX;
- //触摸开始与结束,手指移动的距离
- var disX = this.data.startX - endX;
- var delBtnWidth = this.data.delBtnWidth;
- var index = e.currentTarget.dataset.index;
- //如果距离小于删除按钮的1/2,不显示删除按钮
- // var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
- this.setData({
- touchDel: index,
- txtStyle: disX > delBtnWidth / 2 ? "-" + delBtnWidth + "rpx" : "0px",
- // scrollY : disX > delBtnWidth / 2 ? false : true
- })
- //获取手指触摸的是哪一项
- // var index = e.target.dataset.index;
- // var list = this.data.list;
- // list[index].txtStyle = txtStyle;
- // //更新列表的状态
- // this.setData({
- // list: list
- // });
- }
- },
- //获取元素自适应后的实际宽度
- getEleWidth: function (w) {
- var real = 0;
- try {
- var res = wx.getSystemInfoSync().windowWidth;
- var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
- real = Math.floor(res / scale);
- return real;
- } catch (e) {
- return false;
- }
- },
- //点击删除按钮事件
- delItem: function (e) {
- var that = this;
- //获取列表中要删除项的下标
- var host = getApp().globalData.servsers;
- var index = e.currentTarget.dataset.index;
- var id = e.currentTarget.dataset.id;
- var flag = e.currentTarget.dataset.flag;
- var shixiao_num = that.data.shixiao_num;//失效数量
- let carts = that.data.carts;
- var cartsLength = that.data.carts.length;
- that.setData({
- txtStyle: 0
- });
- //删除购物车数据
- wx.request({
- url: host + "shoppingcartapi/deleteshoppingcart",
- data: {
- id: id
- },
- method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- header: {
- 'Accept': 'application/json'
- },
- success: function (res) {
- carts.splice(index, 1);
- that.setData({
- carts: carts
- });
- var cartsLength = carts.length;
- var flagNum = cartsLength - shixiao_num;
- if (cartsLength == 0) {
- that.setData({
- hasList: false,
- paddingBottom: 0
- })
- } else {
- if (flag == 0) {
- if (flagNum > 0) {
- that.setData({
- selectAllStatus: true
- });
- } else {
- that.setData({
- selectAllStatus: false
- });
- }
- that.getTotalPrice();
- } else {
- //删除的是失效商品
- shixiao_num--;
- that.setData({
- shixiao_num: shixiao_num
- })
- }
- }
- if (res == null || res.data == null) {
- console.error('网络请求失败');
- return;
- }
- }
- });
- },
- //下拉刷新
- onPullDownRefresh: function (e) {
- var that = this;
- that.loadFun();
- wx.stopPullDownRefresh();
- },
- /**
- * 计算总价
- */
- getTotalPrice() {
- let carts = this.data.carts;//获取购物车列表
- let total = 0;
- for (let i = 0; i < this.data.carts.length; i++) {
- if (carts[i].flag == 1 && carts[i].commodity_flag == 0) { // 判断选中[flag = 1]并且是上架[commodity_flag = 0]的商品,才会计算价格
- total += carts[i].num * (parseFloat(carts[i].cost) + parseFloat(carts[i].box_price)); // 所有价格加起来
- }
- }
- this.setData({
- carts: carts,
- totalPrice: total.toFixed(2)
- });
- }
- })
- var that;
- var Util = require('../../utils/util.js');
|