var app = getApp(); Page({ /** * 页面的初始数据 */ data: { icon:false, id:'', status:'1', poster:false, share:false, tipShow:false, show:false, isLoadMore: true, page: 1, total_page: 0, list:[], activity_id: 0, activity_data: {}, hand_goods_obj: {}, hand_goods_id: 0, shareShow: false, specsShow: false, goodsPopupShow: false, cartListShow: false, time: 0, timeData: {}, shareOptions: [ { name: '生成海报', icon: 'poster' }, { name: '分享给好友', icon: 'wechat', openType: 'share' } ], goods_list: [], buyerListData: [], carList: [], goodsCartsTotal: {}, language: {}, posterShow: false, cartsNum: 0, cartsTotal: 0, cart_ids: [], showChoose: false, radioChoose: '', dispatch_types: [], }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ id: options.id }); if (options.scene) { let scene = decodeURIComponent(options.scene); let info_arr = []; info_arr = scene.split(","); //后端说参数过多超过微信限制,改为只穿值用逗号分离,第一个是ID,第二个是mid,二维码由后台生成,mid暂时无用 this.setData({ id: info_arr[0] }); console.log(info_arr,scene); } let language = wx.getStorageSync('langIndex'); this.setData({ 'language': language.en }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { if (this.data.id && this.data.id != 0) { this.search(); this.getData(); this.getCartList(); } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { // 加载更多 if (this.data.isLoadMore) { this.getMoreData(); } else { console.log('没有更多数据'); } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { return { path: "/packageE/others/fixedPrice/fixedPrice?id="+this.data.id }; }, toMember(){ wx.reLaunch({ url: '/packageG/member_v2/member_v2' }); }, changeIcon(){ this.setData({ icon:true }); }, closeIcon() { this.setData({ icon: false }); }, openGoodsDetail(e) { let item = e.currentTarget.dataset.item; if (item.id == this.data.hand_goods_id) { this.setData({ goodsPopupShow: true, }); } else { this.data.hand_goods_id = item.id; this.getGoodsDetails(item.id, 'goodsDetails'); } }, getGoodsDetails(id, type) { let urlStr = app.getNetAddresss('goods.goods.get-goods-page'); app._postNetWork({ url: urlStr, data: { id: Number(id) }, success: (resdata) => { let res = resdata.data; if (res.result !== 1) return this.tips(res.msg); let name = ''; if (type == 'goodsDetails') { name = 'goodsPopupShow'; } else if (type == 'specs') { name = 'specsShow'; } res.data.get_goods.end_time = this.data.time; this.setData({ [name]: true, hand_goods_obj: res.data.get_goods }); } }); }, close_yz_specs_popup(e) { console.log(e); if (e.detail.clicktype == 1) { //点击确认按钮 this.addCartRequest({ goods: { id: e.detail.goodsId }, cartid: e.detail.cartid, total: e.detail.goodsCount, optionsId: e.detail.optionsId }); this.setData({ specsShow: false, // optionsId: e.detail.optionsId, // goodsCount: e.detail.goodsCount }); } else { this.setData({ specsShow: false, }); } }, openSpecs(e) { console.log(this.data.specsShow); let item = e.currentTarget.dataset.item; if (item.id == this.data.hand_goods_id) { this.setData({ specsShow: true, }); } else { this.data.hand_goods_id = item.id; this.getGoodsDetails(item.id, 'specs'); } }, //不同规格的同一商品求和 calculateTotal(arr) { let temp = {}; for (let i in arr) { let key = arr[i].goods_id; this.data.cart_ids.push(arr[i].id); if (temp[key]) { let goods_id = temp[key].goods_id; temp[key].goods_id = goods_id; temp[key].total = temp[key].total + arr[i].total; } else { temp[key] = {}; temp[key].goods_id = arr[i].goods_id; temp[key].total = arr[i].total; temp[key].id = arr[i].id; } } this.setData({ goodsCartsTotal: temp }); }, //统计购物车的总价格和总数量 getFirstCart(data) { this.data.carts = data; let cartsNum = 0; let cartsTotal = 0; this.data.carts.forEach(item => { cartsNum += item.total; cartsTotal += item.total * item.goods.price; }); cartsTotal = parseFloat(cartsTotal).toFixed(2); this.setData({ cartsNum, cartsTotal }); this.calculateTotal(this.data.carts); }, openCartListPopup() { this.setData({ cartListShow: (!this.data.cartListShow) }); }, cartListAdd(item) { console.log(item.detail); console.log(this.data.goodsCartsTotal); this.addCartRequest({ goods: item.detail.goods, cartid: item.detail.id, total: item.detail.total + 1 }); }, cartListReduce(item) { console.log(item.detail); this.reduceCartRequest({ goods: item.detail.goods, cartid: item.detail.id, total: item.detail.total - 1 }); }, close_yz_cartList_popup() { this.setData({ cartListShow: false, }); }, submitGoods() { if (this.data.showChoose && !this.data.radioChoose) { return this.tips("请先选择配送方式"); } if (this.data.showChoose) { // 配送方式有冲突 this.data.cart_ids = []; for (let i = 0; i < this.data.dispatch_types[this.data.radioChoose].member_carts.length; i++) { this.data.cart_ids.push(this.data.dispatch_types[this.data.radioChoose].member_carts[i].id); } } wx.navigateTo({ url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-10&cart_ids=' + JSON.stringify(this.data.cart_ids) }); }, checkGoods() { if(this.data.cartsNum<=0){ this.tips('去选择商品'); return; } let urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.checkout.index'); app._getNetWork({ url: urlStr, data: { cart_ids: this.data.cart_ids.join(',') }, success: (resdata) => { let res = resdata.data; if (res.result !== 1) return this.tips(res.msg); if (res.data.need_choose == 1) { this.setData({ dispatch_types: res.data.dispatch_types, showChoose: true }); } else { this.submitGoods(); } } }); }, getCartList() { let urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.member-cart.index'); app._postNetWork({ url: urlStr, data: {id:this.data.id}, success: (resdata) => { let res = resdata.data; if (res.result !== 1) return this.tips(res.msg); let carList = res.data; this.setData({ carList }); this.getFirstCart(carList); // this.calculateTotal(carList); console.log(carList); } }); }, addCartNum(evt) { let item = evt.currentTarget.dataset.item; let cartid = evt.currentTarget.dataset.cartid; console.log(item); this.addCartRequest({ goods: item, cartid }); }, //加入购物车 addCartRequest(obj) { let { goods, cartid, total, optionsId } = obj; console.log("加入购物车"); if (goods.vip_level_status && goods.vip_level_status.status == 1) { return this.tips(goods.vip_level_status.tips); } if (goods.stock == 0) { return this.tips("库存为零"); } let num = 1; let urlStr = ''; let jsons = {}; if (!app._isTextEmpty(this.data.goodsCartsTotal[goods.id]) && cartid != 0) { if (this.data.goodsCartsTotal[goods.id].total >= goods.stock) { return this.tips("库存不足"); } let obj = this.data.goodsCartsTotal; num = Number(obj[goods.id].total) + 1; if (total && total > 0) { num = total; } this.data.goodsCartsTotal[goods.id].total = num; urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.member-cart.update-num'); jsons = { id: cartid, num: num }; } else { urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.member-cart.store'); if (total && total > 0) { num = total; } jsons = { goods_id: goods.id, total: num, id: this.data.id }; if (optionsId && optionsId > 0) jsons.option_id = optionsId; } app._getNetWork({ url: urlStr, data: jsons, success: (resdata) => { var res = resdata.data; this.tips(res.msg); if (res.result == 1) { this.getCartList(); } } }); }, tips(msg) { wx.showToast({ title: msg, icon: 'none' }); return false; }, previewImage() { wx.showToast({ title: '长按分享好友', icon: 'none', duration: 1800 }); wx.previewImage({ urls: [this.data.info.applets_poster] // 当前显示图片的链接 }); }, saveImg() { // var times = _this.data.timeend - _this.data.timestart; wx.getSetting({ success(res) { if (!res.authSetting['scope.writePhotosAlbum']) { wx.authorize({ scope: 'scope.writePhotosAlbum', success() { console.log('授权成功'); } }); } } }); var imgSrc = this.data.info.applets_poster; wx.downloadFile({ url: imgSrc, success: function (res) { console.log(res); //图片保存到本地 wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function (data) { wx.showToast({ title: '保存成功', icon: 'none', duration: 2000 }); }, fail: function (err) { console.log(err); if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") { console.log("当初用户拒绝,再次发起授权"); wx.openSetting({ success(settingdata) { if (settingdata.authSetting['scope.writePhotosAlbum']) { wx.showToast({ icon: 'none', title: '获取权限成功,再次长按保存图片', duration: 1500 }); } else { wx.showToast({ icon: 'none', title: '获取权限失败,保存图片需要用户授权', duration: 1500 }); } } }); } }, complete(res) { console.log(res); } }); } }); }, copy(){ wx.setClipboardData({ data: this.data.info.applets_url, success(res){ wx.showToast({ title: '复制成功', }); } }); this.setData({ share: false }); }, closeposter() { this.setData({ poster: false, }); }, openposter(){ this.setData({ poster: true, share: false }); }, close(){ this.setData({ show: false }); }, closeShare() { this.setData({ share: false }); }, closeTip() { this.setData({ tipShow: false }); }, openTip() { this.setData({ tipShow: true }); }, openShare() { this.setData({ share: true }); }, open(){ this.setData({ show:true }); }, change(t) { if (t < 10) { return "0" + t; } else { return t; } }, //将时间戳转换成正常时间格式 timestampToTime(timestamp) { var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 let Y = date.getFullYear() + "/"; let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "/"; let D = this.change(date.getDate()) + " "; let h = this.change(date.getHours()) + ":"; let m = this.change(date.getMinutes()); let s = this.change(date.getSeconds()); return Y + M + D + h + m + s; }, toIndex(){ wx.reLaunch({ url: '/packageG/index/index' }); }, getTime(timeStamp) { var day = 0; var hours = 0; var minute = 0; var seconds = 0; day = Math.floor(timeStamp /1000 / 60 / 60 / 24);//计算天数 hours = Math.floor(timeStamp / 1000 / 60 / 60 % 24); minute = Math.floor(timeStamp / 1000 / 60 % 60); seconds = Math.floor(timeStamp / 1000 % 60); var h = this.toDouble(hours); var m = this.toDouble(minute); var s = this.toDouble(seconds); var rest_time = { 'day': day, 'hours': h, 'minutes': m, 'seconds': s }; this.setData({ rest_time: rest_time }); }, toDouble(n) { //将时分秒转化成二位数格式 return n < 10 ? "0" + n : n; }, checktime(){ let that=this; var flag = null; if (that.data.info.start_at * 1000 > (new Date()).getTime()) { that.setData({ status: '1', }); flag = setInterval(()=> { let timeStamp = that.data.info.start_at * 1000 - (new Date()).getTime(); if (timeStamp > 0) { timeStamp--; that.getTime(timeStamp); } else { clearInterval(flag); this.checktime(); } }, 1000); } if (that.data.info.start_at * 1000 < (new Date()).getTime() && (new Date()).getTime()< that.data.info.end_at * 1000) { that.setData({ status: '2', time:that.data.info.end_at * 1000 - (new Date()).getTime() }); // 倒计时 flag = setInterval(()=> { var timeStamp = that.data.info.end_at * 1000 - (new Date()).getTime(); if (timeStamp > 0) { timeStamp--; that.getTime(timeStamp); } else { clearInterval(flag); this.checktime(); } }, 1000); } if ((new Date()).getTime() > that.data.info.end_at * 1000){ this.setData({ status:'3' }); } }, goodsDetailCartAdd(evt) { this.setData({ goodsPopupShow: false, specsShow: true }); }, getData(){ let that=this; let urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.activity.get'); app._getNetWork({ url: urlStr, data: { id: this.data.id, }, success: (resdata) => { var response = resdata.data; if (response.result == 1) { that.setData({ info: response.data, }); that.checktime(); that.setData({ 'info.start_time': that.timestampToTime(that.data.info.start_at), 'info.end_time': that.timestampToTime(that.data.info.end_at), }); } else { this.setData({ status: '3' }); wx.showToast({ title: response.msg, icon: 'none' }); } }, fail: function (res) { console.log(res); } }); }, search(e) { let urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.goods.get-goods-list'); app._getNetWork({ url: urlStr, data: { id:this.data.id, }, success: (resdata) => { var response = resdata.data; if (response.result == 1) { this.setData({ isLoadMore: true, total_page: response.data.last_page, list: response.data.data }); if (!this.data.total_page) { this.setData({ total_page: 0 }); } } else { wx.showToast({ title: response.msg, icon: 'none' }); } }, fail: function (res) { console.log(res); } }); }, reduceCartNum(evt) { let item = evt.currentTarget.dataset.item; let cartid = evt.currentTarget.dataset.cartid; this.reduceCartRequest({ goods: item, cartid }); }, reduceCartRequest(obj) { let { goods, cartid, total } = obj; let id = goods.id; let num = 0; if (total || total === 0) { num = total; } else { let obj = this.data.goodsCartsTotal; num = Number(obj[id].total) - 1; this.data.goodsCartsTotal[id].total = num; } let urlStr = ''; let json = {}; if (num <= 0) { //清楚购物车 urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.member-cart.destroy'); json = { ids: cartid }; } else { urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.member-cart.update-num'); json = { id: cartid, num: num }; } app._getNetWork({ url: urlStr, data: json, success: (resdata) => { var res = resdata.data; this.tips(res.msg); if (res.result == 1) { this.getCartList(); } } }); }, // 获取更多数据 getMoreData() { this.setData({ isLoadMore: false }); // 防止多次请求分页数据 if (this.data.page >= this.data.total_page) { return; } else { let page = this.data.page + 1; this.setData({ page }); let urlStr = app.getNetAddresss('plugin.pack-fixed-price.api.goods.get-goods-list'); app._getNetWork({ url: urlStr, data: { id:this.data.id, page: this.data.page }, success: (resdata) => { var response = resdata.data; if (response.result == 1) { var myData = this.data.list.concat(response.data.data); this.setData({ list: myData }); } else { wx.showToast({ title: response.msg, icon: 'none' }); let pages = this.data.page - 1; this.setData({ page: pages, isLoadMore: false }); } }, fail: function (res) { console.log(res); } }); } }, });