123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- var app = getApp();
- var WxParse = require('../../lib/wxParse/wxParse.js');
- var util = require('../../utils/util.js');
- var api = require('../../config/api.js');
- import { Router } from '../../utils/router.js'
- Router({
- data: {
- winHeight: "",
- id: 0,
- goods: {},
- gallery: [],
- attribute: [],
- issueList: [],
- comment: [],
- brand: {},
- specificationList: [],
- productList: [],
- relatedGoods: [],
- cartGoodsCount: 0,
- userHasCollect: 0,
- number: 1,
- checkedSpecText: '请选择规格数量',
- openAttr: false,
- selectId:'',
- noCollectImage: "/static/images/icon_collect.png",
- hasCollectImage: "/static/images/icon_collect_checked.png",
- collectBackImage: "/static/images/icon_collect.png",
- collected:'',
- isfromBrand:false
- },
-
- getGoodsInfo: function () {
- let that = this;
- util.request(api.GoodsDetail, { id: that.data.id }).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- is_on_sale: res.data.info.is_on_sale,
- goods: res.data.info,
- checkGoods: res.data.productList[0],
- 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
- });
- //设置默认值
- that.setDefSpecInfo(that.data.specificationList);
- if (that.callOper === 'addCard' || that.callOper === 'buyGoods') {
- that.setData({
- openAttr: true,
- collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/detail_back.png"
- });
- } else if (res.data.userHasCollect == 1) {
- that.setData({
- 'collectBackImage': that.data.hasCollectImage,
- collected: that.data.hasCollectImage
- });
- } else {
- that.setData({
- 'collectBackImage': that.data.noCollectImage,
- collected: that.data.hasCollectImage
- });
- }
- WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
- that.getGoodsRelated();
- that.addGoodsViewCount()
- }
- });
- },
- getGoodsRelated: function () {
- let that = this;
- util.request(api.GoodsRelated, { id: that.data.id }).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- relatedGoods: res.data.goodsList,
- });
- }
- });
- },
- back:function(){
- wx.navigateBack({
-
- })
- },
- clickSkuValue: function (event) {
- let that = this;
- let specNameId = event.currentTarget.dataset.nameId;
- let specValueId = event.currentTarget.dataset.valueId;
- let picUrl = event.currentTarget.dataset.url;
- let selects = []
-
- //判断是否可以点击
- //TODO 性能优化,可在wx:for中添加index,可以直接获取点击的属性名和属性值,不用循环
- 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;
- }
- }
- }
- }
- _specificationList.forEach(sf => {
- sf.valueList.forEach(value => {
- value.checked && selects.push(value.id)
- })
- })
- let checkGoods = that.data.productList.find(({goods_specification_ids}) => {
- let ids = goods_specification_ids.split('_').map(id => Number(id))
- ids.shift()
- return ids.length === selects.length && ids.every(id => ~selects.indexOf(id))
- })
- checkGoods = checkGoods ? checkGoods : that.data.goods
- // let number = checkGoods ? checkGoods.goods_number : that.data.goods.goods_number
-
- this.setData({
- checkGoods,
- // 'kcNumber': number,
- 'specificationList': _specificationList,
- 'goods.list_pic_url': picUrl,
- selectId: specValueId
- });
- //重新计算spec改变后的信息
- this.changeSpecInfo();
- //重新计算哪些值不可以点击
- },
- //获取选中的规格信息
- getCheckedSpecValue: function () {
- let checkedValues = [];
- let _specificationList = this.data.specificationList;
- for (let i = 0; i < _specificationList.length; i++) {
- let _checkedObj = {
- nameId: _specificationList[i].specification_id,
- valueId: 0,
- valueText: ''
- };
- for (let j = 0; j < _specificationList[i].valueList.length; j++) {
- if (_specificationList[i].valueList[j].checked) {
- _checkedObj.valueId = _specificationList[i].valueList[j].id;
- _checkedObj.valueText = _specificationList[i].valueList[j].value;
- }
- }
- checkedValues.push(_checkedObj);
- }
- console.log(checkedValues)
- return checkedValues;
- },
- //根据已选的值,计算其它值的状态
- setSpecValueStatus: function () {
- },
- //判断规格是否选择完整
- isCheckedAllSpec: function () {
- return !this.getCheckedSpecValue().some(function (v) {
- if (v.valueId == 0) {
- return true;
- }
- });
- },
- getCheckedSpecKey: function () {
- let checkedValue = this.getCheckedSpecValue().map(function (v) {
- return v.valueId;
- });
- return checkedValue.join('_');
- },
- changeSpecInfo: function () {
- let checkedNameValue = this.getCheckedSpecValue();
- //设置选择的信息
- let checkedValue = checkedNameValue.filter(function (v) {
- if (v.valueId != 0) {
- return true;
- } else {
- return false;
- }
- }).map(function (v) {
- return v.valueText;
- });
- if (checkedValue.length > 0) {
- this.setData({
- 'checkedSpecText': checkedValue.join(' ')
- });
- } else {
- this.setData({
- 'checkedSpecText': '请选择规格数量'
- });
- }
- },
- getCheckedProductItem: function (key) {
- // console.log(this.data.productList, key)
- let keys = key.split('_')
- keys.shift()
- return this.data.productList.filter(function (v) {
- if (keys.every(key => String(v.goods_specification_ids).indexOf(key) > -1)) {
- return true;
- } else {
- return false;
- }
- });
- },
- onLoad: function (options) {
- getApp().checkNetStatu();
- // 页面初始化 options为页面跳转所带来的参数
- options.isBrand = true
- this.setData({
- id: parseInt(options.id),
- isfromBrand: options.isBrand||false,
- imgServer: util.imgServer
- // id: 1181000
- });
- this.callOper = options.oper
- if (options.oper === 'addCard') {
- this.addToCart()
- } else if (options.oper === 'buyGoods') {
- this.buyGoods()
- }
- console.log(this.data.collectBackImage)
- },
- onReady: function () {
- // 页面渲染完成
- },
- onShow: function () {
- var that = this;
- this.getGoodsInfo();
- if (this.data.collected) {
- this.setData({
- collectBackImage: this.data.collected
- })
- }
-
- util.request(api.CartGoodsCount).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoodsCount: res.data.cartTotal.goodsCount
- });
- }
- });
- var that = this
- // 高度自适应
- wx.getSystemInfo({
- success: function (res) {
- var clientHeight = res.windowHeight,
- clientWidth = res.windowWidth,
- rpxR = 750 / clientWidth;
- var calc = clientHeight * rpxR - 100;
- that.setData({
- winHeight: calc
- });
- }
- });
- },
- onHide: function () {
- // 页面隐藏
- },
- onUnload: function () {
- // 页面关闭
- },
- switchAttrPop: function () {
- if (this.data.openAttr == false) {
- this.setData({
- openAttr: !this.data.openAttr,
- collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/detail_back.png"
- });
- }
- },
- closeAttrOrCollect: function () {
- let that = this;
- if (this.data.openAttr) {
- this.setData({
- openAttr: false,
- });
- if (that.data.userHasCollect == 1) {
- that.setData({
- 'collectBackImage': that.data.hasCollectImage,
- collected: that.data.hasCollectImage
- });
- } else {
- that.setData({
- 'collectBackImage': that.data.noCollectImage,
- collected: that.data.noCollectImage
- });
- }
- } else {
- //添加或是取消收藏
- util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST", "application/json")
- .then(function (res) {
- let _res = res;
- if (_res.errno == 0) {
- if ( _res.data.type == 'add') {
- that.setData({
- 'collectBackImage': that.data.hasCollectImage,
- collected: that.data.hasCollectImage
- });
- wx.showToast({
- title: '收藏成功'
- })
- } else {
- that.setData({
- 'collectBackImage': that.data.noCollectImage,
- collected: that.data.noCollectImage
- });
- }
- } else {
- wx.showToast({
- image: 'https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/icon_error.png',
- title: _res.errmsg,
- mask: true
- });
- }
- });
- }
- },
- openCartPage: function () {
- wx.switchTab({
- url: '/pages/cart/cart',
- });
- },
- /**
- * 直接购买
- */
- buyGoods: function () {
- var that = this;
- if (!this.data.is_on_sale) {
- util.showErrorToast('商品已下架');
- return false;
- }
- if (this.data.openAttr == false) {
- //打开规格选择窗口
- this.setData({
- openAttr: !this.data.openAttr,
- collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/detail_back.png",
- collected: this.data.collectBackImage
- });
- } else {
- //提示选择完整规格
- if (!this.isCheckedAllSpec()) {
- wx.showToast({
- title: '请选择完整规格'
- });
- return false;
- }
- //根据选中的规格,判断是否有对应的sku信息
- let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
-
- if (!checkedProduct || checkedProduct.length <= 0) {
- util.showErrorToast('库存不足');
- //找不到对应的product信息,提示没有库存
- return false;
- }
- //验证库存
- if (checkedProduct.goods_number < this.data.number) {
- util.showErrorToast('库存不足');
- //找不到对应的product信息,提示没有库存
- return false;
- }
- // 直接购买商品
- util.request(api.BuyAdd, { goodsSpecificationIds:this.data.selectId, goodsId: this.data.goods.id, number: this.data.number, productId: this.data.checkGoods.id }, "POST",'application/json')
- .then(function (res) {
- let _res = res;
- if (_res.errno == 0) {
-
- if (!(that.callOper === 'addCard' || that.callOper === 'buyGoods')) {
- that.setData({
- openAttr: !that.data.openAttr,
- });
- }
- wx.navigateTo({
- url: '/pages/shopping/checkout/checkout?isBuy=true',
- })
- } else {
- util.showErrorToast(_res.errmsg);
- }
- });
- }
- },
- /**
- * 添加到购物车
- */
- addToCart: function () {
- var that = this;
- if (this.data.openAttr == false) {
- //打开规格选择窗口
- this.setData({
- openAttr: !this.data.openAttr,
- collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/detail_back.png",
- // collected: this.data.collectBackImage
- });
- } else {
- //提示选择完整规格
- if (!this.isCheckedAllSpec()) {
- util.showErrorToast('请选择完整规格');
- return false;
- }
- //根据选中的规格,判断是否有对应的sku信息
- let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
- if (!checkedProduct || checkedProduct.length <= 0) {
- //找不到对应的product信息,提示没有库存
- util.showErrorToast('库存不足');
- return false;
- }
- //验证库存
- if (this.data.checkGoods.goods_number < this.data.number) {
- //找不到对应的product信息,提示没有库存
- util.showErrorToast('库存不足');
- return false;
- }
- //添加到购物车
- util.request(api.CartAdd, { goodsSpecificationIds: this.data.selectId, goodsId: this.data.goods.id, number: this.data.number, productId: this.data.checkGoods.id }, 'POST', 'application/json')
- .then(function (res) {
- let _res = res;
- console.log('addCart', res)
- if (_res.errno == 0) {
- wx.showToast({
- title: '添加成功'
- });
- if (that.callOper === 'addCard' || that.callOper === 'buyGoods') {
- wx.navigateBack()
- } else {
- that.setData({
- openAttr: !that.data.openAttr,
- collectBackImage: that.data.collected,
- cartGoodsCount: _res.data.cartTotal.goodsCount,
- number:1
- });
- }
- // if (that.data.userHasCollect == 1) {
- // that.setData({
- // 'collectBackImage': that.data.hasCollectImage
- // });
- // } else {
- // that.setData({
- // 'collectBackImage': that.data.noCollectImage
- // });
- // }
- } else {
- return util.showErrorToast(_res.errmsg);
- }
- });
- }
- },
- cutNumber: function () {
- if (tjos.data.checkGoods.goods_number === 0 ) return;
- this.setData({
- number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
- });
- },
- addNumber: function () {
- if (this.data.number >= this.data.checkGoods.goods_number)
- return util.showErrorToast('库存不足');
- this.setData({
- number: this.data.number + 1
- });
- },
- setDefSpecInfo: function (specificationList) {
- //未考虑规格联动情况
- let that = this;
- if (!specificationList)return;
- for (let i = 0; i < specificationList.length;i++){
- let specification = specificationList[i];
- let specNameId = specification.specification_id;
- //规格只有一个时自动选择规格
- if (specification.valueList && specification.valueList.length == 1){
- let specValueId = specification.valueList[0].id;
- that.clickSkuValue({ currentTarget: { dataset: { "nameId": specNameId, "valueId": specValueId } } });
- }
- }
- specificationList.map(function(item){
- });
- },
- addCount () {
- let user = wx.getStorageSync('userinfoDetail')
- util.request(api.AddTalkCount, {
- goodsId: this.data.goods.id,
- viewId: user && user.userId || '',
- sceneNum: this.data.brand.sceneNum
- }, 'GET')
- },
- addGoodsViewCount () {
- let user = wx.getStorageSync('userinfoDetail')
- util.request(api.AddGoodsViewCount, {
- goodsId: this.data.goods.id,
- userId: user && user.userId || ''
- }, 'get')
- }
- })
|