123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- var app = getApp();
- var WxParse = require('../../lib/wxParse/wxParse.js');
- var util = require('../../utils/util.js');
- var api = require('../../config/api.js');
- var goodsUtil = require('../../utils/goods.js');
- Page({
- data: {
- winHeight: "",
- id: 0,
- referrer: 0,
- goods: {},
- gallery: [],
- attribute: [],
- issueList: [],
- comment: [],
- brand: {},
- specificationList: [],
- productList: [],
- relatedGoods: [],
- cartGoodsCount: 0,
- userHasCollect: 0,
- crashList: {},
- number: 1,
- checkedSpecText: '请选择规格数量',
- openAttr: false,
- noCollectImage: "/static/images/icon_collect.png",
- hasCollectImage: "/static/images/icon_collect_checked.png",
- collectBackImage: "/static/images/icon_collect.png",
- showNavList: false,
- stockNum: '',
- cartNumber: 0,
- autoplay: false,
- defaultFreight: 0,
- current: 0,//banner当前的index
- isPlay: true,
- indexNum: 0,
- windowWidth: 0,
- leftWidth: 0,
- startX:0,
- curr_id: '',
- videoHiddenName: true,
- imgHiddenName: false,
- page: 1,
- size: 4,
- detailContent: '',
- storeId: '',
- detailStoreId: ''
- },
- toggleNav() {
- this.setData({
- showNavList: !this.data.showNavList
- })
- },
- switchNav(event) {
- let name = event.currentTarget.dataset.name;
- wx.switchTab({
- url: `/pages/${name}/${name}`,
- });
- },
- //小程序里的转义方法
- escape2Html: function (str) {
- var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
- return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; });
- },
- //调用在需要的地方直接调用即可。如:this.escape2Html(contents)
- getGoodsInfo: function () {
- wx.showLoading({
- title: '加载中...',
- });
- setTimeout(function () {
- }, 350)
- let that = this;
- const regex = new RegExp('<img', 'gi');
- console.log('分享的storeId'+ wx.getStorageSync('storeId'))
- util.request(api.GoodsDetail, { id: that.data.id, referrer: this.data.referrer,
- merchSn: wx.getStorageSync('merchSn'), storeId: wx.getStorageSync('storeId') }).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- goods: res.data.info,
- gallery: res.data.gallery,
- attribute: res.data.attribute,
- issueList: res.data.issue,
- comment: res.data.comment,
- brand: res.data.brand,
- specificationList: res.data.specificationList,
- productList: res.data.productList,
- userHasCollect: res.data.userHasCollect,
- stockNum: res.data.stockNum,
- cartNumber: res.data.cartNumber,
- defaultFreight: res.data.defaultFreight,
- checkedSpecText: res.data.specificationList[0].valueList[0].value,
- detailContent: that.escape2Html(res.data.info.goods_desc).replace(regex, `<img style="width: 100%;"`),
- detailStoreId:res.data.info.storeId
- });
- if (res.data.userHasCollect == 1) {
- that.setData({
- 'collectBackImage': that.data.hasCollectImage
- });
- } else {
- that.setData({
- 'collectBackImage': that.data.noCollectImage
- });
- }
- // WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
- that.getGoodsRelated();
- that.getGoodsCrashList();
- //
- let _specificationList = that.data.specificationList;
- for (let i = 0; i < _specificationList.length; i++) {
- if (_specificationList[i].valueList.length == 1) {
- //如果已经选中,则反选,前端默认选中
- _specificationList[i].valueList[0].checked = true;
- }
- }
- that.setData({
- 'specificationList': _specificationList
- });
- }
- wx.hideLoading();
- });
- if (null != that.data.referrer && that.data.referrer > 0) {
- util.request(api.GoodsTransferCoupon, {
- goods_id: that.data.id,
- referrer: that.data.referrer, send_type: 2
- }).then(function (res) {
- });
- }
- util.request(api.GoodsGallery, {
- goods_id: that.data.id
- }).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- gallery: res.data
- })
- }
- });
- },
- getGoodsCrashList: function () {
- let that = this;
- util.request(api.GoodsCrashList, { goodsId: that.data.id }).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- crashList: res.data,
- });
- that.data.crashList.length;
- }
- });
- },
- clickSkuValue: function (event) {
- let that = this;
- let specNameId = event.currentTarget.dataset.nameId;
- let specValueId = event.currentTarget.dataset.valueId;
- //
- let _specificationList = this.data.specificationList;
- for (let i = 0; i < _specificationList.length; i++) {
- if (_specificationList[i].specification_id == specNameId) {
- for (let j = 0; j < _specificationList[i].valueList.length; j++) {
- if (_specificationList[i].valueList[j].id == specValueId) {
- //如果已经选中,则反选
- if (_specificationList[i].valueList[j].checked) {
- _specificationList[i].valueList[j].checked = false;
- } else {
- _specificationList[i].valueList[j].checked = true;
- }
- } else {
- _specificationList[i].valueList[j].checked = false;
- }
- }
- }
- }
- this.setData({
- 'specificationList': _specificationList
- });
- //重新计算spec改变后的信息
- goodsUtil.changeSpecInfo(that);
- },
- onLoad: function (options) {
- // 页面初始化 options为页面跳转所带来的参数
- if (options.scene) {
- var scene = decodeURIComponent(options.scene);
- console.log("scene is ", scene);
- var scanArray = scene.split('&');
- var id = scanArray[0];
- var storeId = scanArray[1];
- var code = scanArray[2];
- console.log("id is ", id);
- console.log("storeId is ", storeId);
- console.log("code is ", code);
- this.setData({
- id: id,
- storeId: storeId
- });
- if (storeId) {
- wx.setStorageSync('storeId', storeId);
- wx.setStorageSync('isShare', 'true');
- }
- }else{
- this.setData({
- id: parseInt(options.id),
- storeId: options.storeId
- });
- if (options.storeId) {
- wx.setStorageSync('storeId', options.storeId);
- wx.setStorageSync('isShare', 'true');
- }
- }
- console.log('分享的storeId1:' + wx.getStorageSync('storeId'))
- var that = this;
- // 高度自适应
- wx.getSystemInfo({
- success: function (res) {
- var clientHeight = res.windowHeight,
- clientWidth = res.windowWidth,
- rpxR = 750 / clientWidth;
- var calc = clientHeight * rpxR - 100;
- // console.log(calc);
- that.setData({
- winHeight: calc,
- windowWidth: res.windowWidth
- });
- }
- });
- },
- onReady: function () {
- let that = this;
- this.videoContext = wx.createVideoContext('myVideo');
- // 页面渲染完成
- // wx.setClipboardData({
- // data: '/pages/goods/goods?id=' + that.data.id,
- // success: function (res) {
- // wx.getClipboardData({
- // success: function (res) {
- // // console.log(res.data) // data
- // }
- // })
- // }
- // })
- },
- //购物车增加
- addNumber2: function (e) {
- let that = this;
- var goodsId = e.currentTarget.dataset.goodsId;
- var productId = e.currentTarget.dataset.productId;
- var hotGoods = that.data.hotGoods;
- util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
- if (res.errno === 0 && null != res.data) {
- util.request(api.CartGoodsCount).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoodsCount: res.data.cartTotal.goodsCount
- });
- }
- });
- wx.showToast({
- title: '添加成功',
- icon: 'success',
- mask: true
- });
- } else {
- wx.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- });
- },
- onShow: function () {
- let that = this;
- // 页面显示
- if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
- if (wx.getStorageSync('storeId')) {
- util.request(api.ChooseStoreId, {
- storeId: wx.getStorageSync('storeId'),
- merchSn: wx.getStorageSync('merchSn')
- }, 'POST').then(function (res) {
- if (res.errno === 0) {
- wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
- wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
- that.reLoad();
- that.getGoodsInfo();
- util.request(api.CartGoodsCount, { storeId: wx.getStorageSync('storeId') }).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoodsCount: res.data.cartTotal.goodsCount
- });
- }
- });
- }
- });
- }
- } else {
- wx.navigateTo({
- url: '/pages/auth/btnAuth/btnAuth',
- })
- }
- },
- onHide: function () {
- // 页面隐藏
- },
- onUnload: function () {
- // 页面关闭
- },
- switchAttrPop: function () {
- this.setData({
- openAttr: !this.data.openAttr
- })
- },
- hideSwitchAttrPop: function() {
- this.setData({
- openAttr: false
- })
- },
- reLoad: function () {
- let that = this;
- if (wx.getStorageSync('storeId')) {
- if (wx.getStorageSync('userId')) {
- wx.request({
- url: api.updateLoginUser,
- data: {
- userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
- },
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- },
- success: function (wxRes) {
- if (wxRes.data.errno === 0) {
- // console.log("用户信息更新成功");
- }
- },
- fail: function (err) {
- console.log("failed");
- }
- });
- }
- }
- },
- changeProperty: function (e) {
- var propertyName = e.currentTarget.dataset.propertyName;
- console.log(propertyName);
- },
- closeAttrOrCollect: function () {
- let that = this;
- //添加或是取消收藏
- util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST")
- .then(function (res) {
- let _res = res;
- if (_res.errno == 0) {
- if (_res.data.type == 'add') {
- that.setData({
- 'collectBackImage': that.data.hasCollectImage
- });
- wx.showToast({
- title: "收藏成功",
- mask: true
- });
- } else {
- that.setData({
- 'collectBackImage': that.data.noCollectImage
- });
- wx.showToast({
- title: "收藏取消",
- mask: true
- });
- }
- } else {
- wx.showToast({
- image: '/static/images/icon_error.png',
- title: _res.errmsg,
- mask: true
- });
- }
- });
- },
- openCartPage: function () {
- wx.switchTab({
- url: '/pages/cart/cart',
- });
- },
- addToCart: function () {
- var that = this;
- if (this.data.openAttr == false) {
- //打开规格选择窗口
- this.setData({
- openAttr: !this.data.openAttr
- });
- } else {
- //提示选择完整规格
- if (!goodsUtil.isCheckedAllSpec(that)) {
- return false;
- }
- if (that.data.number + that.data.cartNumber > that.data.stockNum){
- // wx.showToast({
- // title: '库存不足',
- // mask: true
- // });
- util.showErrorToast('库存不足');
- //找不到对应的product信息,提示没有库存
- return false;
- }
- // console.log(that.data.productList)
- // //根据选中的规格,判断是否有对应的sku信息
- let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
- // console.log(goodsUtil.getCheckedSpecKey(that))
- if (!checkedProduct || checkedProduct.length <= 0) {
- wx.showToast({
- title: '库存不足',
- icon: 'none'
- });
- //找不到对应的product信息,提示没有库存
- return false;
- }
- //验证库存
- // if (checkedProduct.goods_number < this.data.number) {
- // //找不到对应的product信息,提示没有库存
- // return false;
- // }
- //添加到购物车
- util.request(api.CartAdd, {
- goodsId: this.data.goods.id,
- number: this.data.number,
- productId: checkedProduct[0].id
- }, "POST")
- .then(function (res) {
- let _res = res;
- if (_res.errno == 0) {
- wx.showToast({
- title: '添加成功'
- });
- that.setData({
- openAttr: !that.data.openAttr,
- cartGoodsCount: _res.data.cartTotal.goodsCount
- });
- if (that.data.userHasCollect == 1) {
- that.setData({
- 'collectBackImage': that.data.hasCollectImage
- });
- } else {
- that.setData({
- 'collectBackImage': that.data.noCollectImage
- });
- }
- } else {
- wx.showToast({
- title: _res.errmsg,
- icon: 'none'
- })
- that.hideSwitchAttrPop();
- // that.setData({
- // stockNum: 0
- // });
- }
- });
- }
- },
- cutNumber: function () {
- this.setData({
- number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
- });
- },
- addNumber: function () {
- this.setData({
- number: this.data.number + 1
- });
- },
- onShareAppMessage: function () {
- var that = this;
- // console.log("url:" + that.data.goods.list_pic_url);
- var userId = wx.getStorageSync('userId');
- console.log("userId:" + userId);
- console.log("detailStoreId:" + that.data.detailStoreId);
- return {
- title: '商品详情',
- desc: null != that.data.goods.name ? that.data.goods.name : "商品详情",
- imageUrl: that.data.goods.list_pic_url,
- path: '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId') + '&&storeId=' + that.data.detailStoreId,
- success: function (res) {
- console.log("转发成功");
- // 转发成功
- },
- fail: function (res) {
- // 转发失败
- console.log("转发失败");
- }
- }
- },
- //购物车增加
- addCrashNumber: function (e) {
- let that = this;
- var goodsId = e.currentTarget.dataset.goodsId;
- var productId = e.currentTarget.dataset.productId;
- util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
- if (res.errno === 0 && null != res.data) {
- var hotGoods = that.data.hotGoods;
- hotGoods.forEach(function (val, index, arr) {
- if (val.id == goodsId) {
- val.cart_num = res.data;
- hotGoods[index] = val;
- that.setData({ hotGoods: hotGoods });
- }
- }, that);
- }
- });
- },
- previewPic(e) {
- let url = e.currentTarget.dataset.url;
- let urls = [];
- urls[0] = url;
- wx.previewImage({
- urls
- })
- },
- switchNav(event) {
- wx.switchTab({
- url: '/pages/index/index'
- });
- },
- //触摸事件start
- touchStart(e) {
- let startX = e.changedTouches[0].pageX
- this.setData({
- startX: startX
- })
- console.log("startX:" + startX);
- },
- //触摸事件end
- touchEnd(e) {
- let _self = this;
- let windowWidth = this.data.windowWidth;
- let moveWidth = e.changedTouches[0].pageX - this.data.startX;
- let defineWidth = windowWidth / 20;
- let rankList = this.data.gallery;
- let index_now = e.currentTarget.dataset.index;
- if (moveWidth >= defineWidth) {
- //上一张
- let transWidth = (index_now - 1) * (-this.data.windowWidth);
- if (index_now >= 1) {
- this.setData({
- indexNum: index_now - 1,
- rankList: rankList,
- leftWidth: transWidth
- })
- }
- } else if (moveWidth <= (0 - defineWidth)) {
- //下一张
- this.videoContext.pause();
- let transWidth = (index_now + 1) * (-this.data.windowWidth);
- if (index_now < rankList.length - 1) {
- this.setData({
- indexNum: index_now + 1,
- rankList: rankList,
- leftWidth: transWidth
- })
- }
- } else {
- //console.log('不能修改样式')
- }
- },
- videoPlay(e) {
- // if (this.data.curr_id == e.currentTarget.dataset.id) {
- // this.setData({
- // curr_id: 0,
- // })
- // } else {
- // this.setData({
- // curr_id: e.currentTarget.dataset.id,
- // })
- // }
- this.setData({
- imgHiddenName: true,
- videoHiddenName: false
- })
- this.videoContext.play();
- },
- //触摸事件start
- touchStart2(e) {
-
- },
- touchEnd2(e) {
- this.setData({
- curr_id: 0,
- imgHiddenName: false,
- videoHiddenName: true
- })
- this.videoContext.pause();
- },
- getGoodsRelated: function () {
- let that = this;
- util.request(api.GoodsRelated, { id: that.data.id, page: that.data.page, size: that.data.size }).then(function (res) {
- if (res.errno === 0) {
- let goodsList = that.data.relatedGoods.concat(res.data.goodsList);
- that.setData({
- relatedGoods: goodsList,
- });
- }
- });
- },
- onReachBottom() {
- var that = this;
- wx.showLoading({
- title: '加载中...',
- })
- that.setData({
- page: that.data.page + 1
- });
- that.getGoodsRelated();
- }
- })
|