12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295 |
- // 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,总数量】
- loginStatic:false, //是否是渠道登陆
- },
- 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){
- let data = {
- user_id: un_id,
- channel_account_id: wx.getStorageSync('channelIdObj').channelAccountId ? wx.getStorageSync('channelIdObj').channelAccountId :"",
- channel_id: wx.getStorageSync('channelIdObj').channelId ? wx.getStorageSync('channelIdObj').channelId : ""
- }
- wx.request({
- url: host + "shoppingcartapi/shoppingcartview",
- data: data,
- 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,
- channel_id: wx.getStorageSync('channelIdObj').channelId ? wx.getStorageSync('channelIdObj').channelId : ""
- },
- 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;
- if (wx.getStorageSync('channelIdObj').channelId) {
- that.setData({
- loginStatic: true,
- })
- }
- 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");
- console.log(newCarts,"newCarts")
- // 立即采购
- 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;//取消勾选
- wx.hideLoading();
- shixiao_num++;
- 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);
- that.getTotalPrice();
- }
-
- }else{
- 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 {
- 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;
- if (this.data.loginStatic) { //判断是否登陆
- 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]的商品,才会计算价格
- //还得检测渠道价格
- if (carts[i].product_channel_price){
- total += carts[i].num * (parseFloat(carts[i].product_channel_price) + parseFloat(carts[i].box_price)); // 所有价格加起来
- }else {
- total += carts[i].num * (parseFloat(carts[i].cost) + parseFloat(carts[i].box_price)); // 所有价格加起来
- }
-
- }
- }
- } else {
- 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');
|