| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- let WxParse = require('../../lib/wxParse/wxParse')
- let util = require('../../utils/util')
- let api = require('../../config/api')
- Page({
- data: {
- id: 0, // 商品id
- productId: 0, // 规格id
- shareId: 0, // 分享id
- shareUrl: '', // 分享图片二维码
- show_spec: false, // 是否显示 商品规格弹出框
-
- swiper_index: 0, // 当前图片index
- goodsData: null,
-
- collectId: 0, // 收藏id
- },
- onLoad: function({ scene, obj }) {
- let id = 0
- let productId = 0
- let shareId = 0
- if (scene) { // 分享
- // 商品id,商品类型,商品活动id,分享用户id
- [id, shareId] = decodeURIComponent(scene).split(',')
- } else {
- const data = JSON.parse(obj)
- id = data.id
- productId = data.productId ? data.productId : 0
- }
- this.setData({ id, productId, shareId })
- if(wx.getStorageSync('token')) this.setBrowse(id)
- },
-
- onShow() {
- this.getGoodsDetail()
- },
- // 足迹
- setBrowse(goodsId) {
- util.showLoad('加载中')
- util.request(api.AddBrowse, { goodsId },'POST').then(res => {
- util.hideLoad()
- if (res.errno != 0) util.showErrorToast(res.errmsg)
- }).catch(() => {
- util.hideLoad()
- util.showErrorToast('网络连接失败')
- })
- },
- getGoodsDetail() {
- const { id, productId } = this.data
- util.showLoad('加载中')
- util.request(api.GoodsDetail, { id }).then(res => {
- util.hideLoad()
- if (res.errno === 0) {
- const { info, productList, collectId } = res.data
- const products = productList.map(p => {
- p.stock = p.number
- return p
- })
-
- WxParse.wxParse('goodsDetail', 'html', info.detail, this)
- const picUrl = info.picUrl.split(',')
- const pro_i = productId ? products.findIndex(p => p.id == productId) : 0
- const goodsData = {
- ...info,
- detail: undefined,
- products,
- curSpec: products[pro_i],
- picUrl
- }
- delete goodsData.detail
- this.setData({ goodsData, collectId: collectId || 0 })
- } else util.showErrorToast(res.errmsg)
- }).catch(() => {
- util.hideLoad()
- util.showErrorToast('网络连接失败')
- })
- },
- prevent() {
- return false
- },
- // 设置当前图片index
- setPicIndex({ detail }) {
- this.setData({ swiper_index: detail.current })
- },
- // 预览商品图片
- previewImage({ currentTarget }) {
- const { current, urls } = currentTarget.dataset
- wx.previewImage({ current, urls })
- },
- // 收藏取消收藏
- toggleCollect() {
- const { collectId, id } = this.data
- util.showLoad('加载中')
- if (collectId) { // 取消收藏
- util.request(api.CollectDelete, [collectId], 'POST').then(res => {
- util.hideLoad()
- if (res.errno === 0) {
- util.showToast('已取消收藏')
- this.setData({ collectId: 0 })
- } else util.showErrorToast(res.errmsg)
- }).catch(() => {
- util.hideLoad()
- util.showErrorToast('网络连接失败')
- })
- } else {
- util.request(api.CollectAdd, id, 'POST').then(res => {
- util.hideLoad()
- if (res.errno === 0) {
- util.showToast('已添加收藏')
- this.setData({ collectId: res.data.id })
- } else util.showErrorToast(res.errmsg)
- }).catch(() => {
- util.hideLoad()
- util.showErrorToast('网络连接失败')
- })
- }
- },
- // 显示规格弹出框
- showSpec() {
- this.setData({ show_spec: true })
- },
-
- // 隐藏规格弹出框
- hideSpec() {
- this.setData({ show_spec: false })
- },
- onShareAppMessage({ from }) {
- const { id, productId, goodsData } = this.data
- const obj = { id, productId }
- const promise = new Promise(resolve => {
- resolve({
- title: '富玖铭商户',
- path: `/packageGoods/detail/detail?obj=${JSON.stringify(obj)}`
- })
- })
- return from == "button" ? { title: goodsData.name, imageUrl: goodsData.picUrl[0] } : promise
- },
- onShareTimeline() {}
- })
|