123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- const WXAPI = require('apifm-wxapi')
- const AUTH = require('../../../utils/auth')
- import Poster from 'wxa-plugin-canvas/poster/poster'
- Page({
- data: {
- beianPass: 0, // 0 未判断,1 未备案, 2 已备案
- },
- onLoad(e) {
- // e.id = 819066
- // 读取分享链接中的邀请人编号
- if (e && e.inviter_id) {
- wx.setStorageSync('referrer', e.inviter_id)
- }
- // 读取小程序码中的邀请人编号
- if (e && e.scene) {
- const scene = decodeURIComponent(e.scene) // 处理扫码进商品详情页面的逻辑
- if (scene && scene.split(',').length >= 2) {
- e.id = scene.split(',')[0]
- wx.setStorageSync('referrer', scene.split(',')[1])
- }
- }
- this.data.goodsId = e.id
- },
- async cpsPddBeian() {
- if (this.data.beianPass == 2) {
- return
- }
- AUTH.bindSeller()
- const token = wx.getStorageSync('token')
- const res = await WXAPI.cpsPddBeian(token)
- if (res.code == 10000) {
- wx.setNavigationBarTitle({
- title: '商品详情',
- })
- this.setData({
- beianPass: 2
- })
- this.goodsDetail()
- return
- }
- if (res.code == 0) {
- wx.setNavigationBarTitle({
- title: '认证页',
- })
- this.setData({
- beianPass: 1,
- beianData: res.data
- })
- } else {
- wx.showModal({
- title: '错误',
- content: res.msg,
- showCancel: false,
- success: res => {
- wx.navigateBack()
- }
- })
- }
- },
- goBeian() {
- wx.navigateToMiniProgram({
- appId: this.data.beianData.we_app_info.app_id,
- path: this.data.beianData.we_app_info.page_path
- })
- },
- async goodsDetail() {
- const token = wx.getStorageSync('token')
- const res = await WXAPI.goodsDetail(this.data.goodsId, token ? token : '')
- if (res.code == 0) {
- this.setData({
- goodsDetail: res.data,
- })
- this.cpsPddGoodsDetail(res.data.basicInfo.yyId)
- }
- },
- async cpsPddGoodsDetail(skuId) {
- const token = wx.getStorageSync('token')
- const res = await WXAPI.cpsPddGoodsDetail({
- token,
- goodsSign: this.data.goodsDetail.basicInfo.yyIdStr
- })
- if (res.code == 0) {
- this.setData({
- cpsPddGoodsDetail: res.data
- })
- }
- },
- onShow (){
- AUTH.checkHasLogined().then(isLogined => {
- if (isLogined) {
- this.cpsPddBeian()
- this.goodsFavCheck()
- }
- })
- },
- async goodsFavCheck() {
- const res = await WXAPI.goodsFavCheck(wx.getStorageSync('token'), this.data.goodsId)
- if (res.code == 0) {
- this.setData({
- faved: true
- })
- } else {
- this.setData({
- faved: false
- })
- }
- },
- async addFav(){
- AUTH.checkHasLogined().then(isLogined => {
- this.setData({
- wxlogin: isLogined
- })
- if (isLogined) {
- if (this.data.faved) {
- // 取消收藏
- WXAPI.goodsFavDeleteV2({
- token: wx.getStorageSync('token'),
- goodsId: this.data.goodsId,
- type: 1
- }).then(res => {
- this.goodsFavCheck()
- })
- } else {
- const extJsonStr = {
- wxaurl: `/packageCps/pages/goods-details/cps-pdd?id=${this.data.goodsId}`,
- skuId: this.data.goodsId,
- pic: this.data.goodsDetail.basicInfo.pic,
- name: this.data.goodsDetail.basicInfo.name
- }
- // 加入收藏
- WXAPI.goodsFavAdd({
- token: wx.getStorageSync('token'),
- goodsId: this.data.goodsId,
- type: 1,
- extJsonStr: JSON.stringify(extJsonStr)
- }).then(res => {
- this.goodsFavCheck()
- })
- }
- }
- })
- },
- goShopCar: function() {
- wx.reLaunch({
- url: "/pages/shop-cart/index"
- });
- },
- toAddShopCar: function() {
- this.setData({
- shopType: "addShopCar"
- })
- this.bindGuiGeTap();
- },
- async tobuy() {
- const token = wx.getStorageSync('token')
- if (!token) {
- wx.showToast({
- title: '请先登陆',
- icon: 'none'
- })
- return
- }
- const res = await WXAPI.cpsPddGoodsShotUrl(token, this.data.goodsDetail.basicInfo.yyIdStr)
- if (res.code != 0) {
- wx.showToast({
- title: res.msg,
- icon: 'none'
- })
- return
- }
- wx.navigateToMiniProgram({
- appId: res.data.we_app_info.app_id,
- path: res.data.we_app_info.page_path
- })
- },
- toPingtuan: function(e) {
- let pingtuanopenid = 0
- if (e.currentTarget.dataset.pingtuanopenid) {
- pingtuanopenid = e.currentTarget.dataset.pingtuanopenid
- }
- this.setData({
- shopType: "toPingtuan",
- selectSizePrice: this.data.goodsDetail.basicInfo.pingtuanPrice,
- selectSizeOPrice: this.data.goodsDetail.basicInfo.originalPrice,
- pingtuanopenid: pingtuanopenid,
-
- hideShopPopup: false,
- skuGoodsPic: this.data.goodsDetail.basicInfo.pic
- });
-
- },
- /**
- * 规格选择弹出框
- */
- bindGuiGeTap: function() {
- this.setData({
- hideShopPopup: false
- })
- },
- /**
- * 规格选择弹出框隐藏
- */
- closePopupTap: function() {
- this.setData({
- hideShopPopup: true
- })
- },
- stepChange(event) {
- this.setData({
- buyNumber: event.detail
- })
- },
- /**
- * 选择商品规格
- */
- async labelItemTap(e) {
- const propertyindex = e.currentTarget.dataset.propertyindex
- const propertychildindex = e.currentTarget.dataset.propertychildindex
- const property = this.data.goodsDetail.properties[propertyindex]
- const child = property.childsCurGoods[propertychildindex]
- // 取消该分类下的子栏目所有的选中状态
- property.childsCurGoods.forEach(child => {
- child.active = false
- })
- // 设置当前选中状态
- property.optionValueId = child.id
- child.active = true
- // 获取所有的选中规格尺寸数据
- const needSelectNum = this.data.goodsDetail.properties.length
- let curSelectNum = 0;
- let propertyChildIds = "";
- let propertyChildNames = "";
- this.data.goodsDetail.properties.forEach(p => {
- p.childsCurGoods.forEach(c => {
- if (c.active) {
- curSelectNum++;
- propertyChildIds = propertyChildIds + p.id + ":" + c.id + ",";
- propertyChildNames = propertyChildNames + p.name + ":" + c.name + " ";
- }
- })
- })
- let canSubmit = false;
- if (needSelectNum == curSelectNum) {
- canSubmit = true;
- }
- let skuGoodsPic = this.data.skuGoodsPic
- if (this.data.goodsDetail.subPics && this.data.goodsDetail.subPics.length > 0) {
- const _subPic = this.data.goodsDetail.subPics.find(ele => {
- return ele.optionValueId == child.id
- })
- if (_subPic) {
- skuGoodsPic = _subPic.pic
- }
- }
- this.setData({
- goodsDetail: this.data.goodsDetail,
- canSubmit,
- skuGoodsPic,
- propertyChildIds,
- propertyChildNames,
- })
- this.calculateGoodsPrice()
- },
- async calculateGoodsPrice() {
- // 计算最终的商品价格
- let price = this.data.goodsDetail.basicInfo.minPrice
- let originalPrice = this.data.goodsDetail.basicInfo.originalPrice
- let totalScoreToPay = this.data.goodsDetail.basicInfo.minScore
- let buyNumMax = this.data.goodsDetail.basicInfo.stores
- let buyNumber = this.data.goodsDetail.basicInfo.minBuyNumber
- if (this.data.shopType == 'toPingtuan') {
- price = this.data.goodsDetail.basicInfo.pingtuanPrice
- }
- // 计算 sku 价格
- if (this.data.canSubmit) {
- const token = wx.getStorageSync('token')
- const res = await WXAPI.goodsPriceV2({
- token: token ? token : '',
- goodsId: this.data.goodsDetail.basicInfo.id,
- propertyChildIds: this.data.propertyChildIds
- })
- if (res.code == 0) {
- price = res.data.price
- if (this.data.shopType == 'toPingtuan') {
- price = res.data.pingtuanPrice
- }
- originalPrice = res.data.originalPrice
- totalScoreToPay = res.data.score
- buyNumMax = res.data.stores
- }
- }
- // 计算配件价格
- if (this.data.goodsAddition) {
- this.data.goodsAddition.forEach(big => {
- big.items.forEach(small => {
- if (small.active) {
- price = (price*100 + small.price*100) / 100
- }
- })
- })
- }
- this.setData({
- selectSizePrice: price,
- selectSizeOPrice: originalPrice,
- totalScoreToPay: totalScoreToPay,
- buyNumMax,
- buyNumber: (buyNumMax >= buyNumber) ? buyNumber : 0
- });
- },
- /**
- * 选择可选配件
- */
- async labelItemTap2(e) {
- const propertyindex = e.currentTarget.dataset.propertyindex
- const propertychildindex = e.currentTarget.dataset.propertychildindex
- const goodsAddition = this.data.goodsAddition
- const property = goodsAddition[propertyindex]
- const child = property.items[propertychildindex]
- if (child.active) {
- // 该操作为取消选择
- child.active = false
- this.setData({
- goodsAddition
- })
- this.calculateGoodsPrice()
- return
- }
- // 单选配件取消所有子栏目选中状态
- if (property.type == 0) {
- property.items.forEach(child => {
- child.active = false
- })
- }
- // 设置当前选中状态
- child.active = true
- this.setData({
- goodsAddition
- })
- this.calculateGoodsPrice()
- },
- /**
- * 组建立即购买信息
- */
- buliduBuyNowInfo: function(shoptype) {
- var shopCarMap = {};
- shopCarMap.goodsId = this.data.goodsId;
- shopCarMap.pic = this.data.imageDomain + this.data.price.pic;
- shopCarMap.name = this.data.price.skuName;
- shopCarMap.price = this.data.price.priceSale;
- shopCarMap.left = "";
- shopCarMap.active = true;
- shopCarMap.number = this.data.buyNumber;
-
- var buyNowInfo = {};
- buyNowInfo.shopNum = 0;
- buyNowInfo.shopList = [shopCarMap];
-
- return buyNowInfo;
- },
- onShareAppMessage() {
- let _data = {
- title: this.data.goodsDetail.basicInfo.name,
- path: '/packageCps/pages/goods-details/cps-pdd?id=' + this.data.goodsId + '&inviter_id=' + wx.getStorageSync('uid'),
- success: function(res) {
- // 转发成功
- },
- fail: function(res) {
- // 转发失败
- }
- }
- if (this.data.kjJoinUid) {
- _data.title = this.data.curKanjiaprogress.joiner.nick + '邀请您帮TA砍价'
- _data.path += '&kjJoinUid=' + this.data.kjJoinUid
- }
- return _data
- },
- reputation: function(goodsId) {
- var that = this;
- WXAPI.goodsReputation({
- goodsId: goodsId
- }).then(function(res) {
- if (res.code == 0) {
- res.data.forEach(ele => {
- if (ele.goods.goodReputation == 0) {
- ele.goods.goodReputation = 1
- } else if (ele.goods.goodReputation == 1) {
- ele.goods.goodReputation = 3
- } else if (ele.goods.goodReputation == 2) {
- ele.goods.goodReputation = 5
- }
- })
- that.setData({
- reputation: res.data
- });
- }
- })
- },
- pingtuanList: function(goodsId) {
- var that = this;
- WXAPI.pingtuanList({
- goodsId: goodsId,
- status: 0
- }).then(function(res) {
- if (res.code == 0) {
- that.setData({
- pingtuanList: res.data.result
- });
- }
- })
- },
- getVideoSrc: function(videoId) {
- var that = this;
- WXAPI.videoDetail(videoId).then(function(res) {
- if (res.code == 0) {
- that.setData({
- videoMp4Src: res.data.fdMp4
- });
- }
- })
- },
- joinKanjia(){
- AUTH.checkHasLogined().then(isLogined => {
- if (isLogined) {
- this.doneJoinKanjia();
- } else {
- this.setData({
- wxlogin: false
- })
- }
- })
- },
- doneJoinKanjia: function() { // 报名参加砍价活动
- const _this = this;
- if (!_this.data.curGoodsKanjia) {
- return;
- }
- wx.showLoading({
- title: '加载中',
- mask: true
- })
- WXAPI.kanjiaJoin(wx.getStorageSync('token'), _this.data.curGoodsKanjia.id).then(function(res) {
- wx.hideLoading()
- if (res.code == 0) {
- _this.setData({
- kjJoinUid: wx.getStorageSync('uid'),
- myHelpDetail: null
- })
- _this.getGoodsDetailAndKanjieInfo(_this.data.goodsDetail.basicInfo.id)
- } else {
- wx.showToast({
- title: res.msg,
- icon: 'none'
- })
- }
- })
- },
- joinPingtuan: function(e) {
- let pingtuanopenid = e.currentTarget.dataset.pingtuanopenid
- wx.navigateTo({
- url: "/pages/to-pay-order/index?orderType=buyNow&pingtuanOpenId=" + pingtuanopenid
- })
- },
- goIndex() {
- wx.switchTab({
- url: '/pages/index/index',
- });
- },
- helpKanjia() {
- const _this = this;
- AUTH.checkHasLogined().then(isLogined => {
- _this.setData({
- wxlogin: isLogined
- })
- if (isLogined) {
- _this.helpKanjiaDone()
- }
- })
- },
- helpKanjiaDone(){
- const _this = this;
- WXAPI.kanjiaHelp(wx.getStorageSync('token'), _this.data.kjId, _this.data.kjJoinUid, '').then(function (res) {
- if (res.code != 0) {
- wx.showToast({
- title: res.msg,
- icon: 'none'
- })
- return;
- }
- _this.setData({
- myHelpDetail: res.data
- });
- wx.showModal({
- title: '成功',
- content: '成功帮TA砍掉 ' + res.data.cutPrice + ' 元',
- showCancel: false
- })
- _this.getGoodsDetailAndKanjieInfo(_this.data.goodsDetail.basicInfo.id)
- })
- },
- cancelLogin() {
- this.setData({
- wxlogin: true
- })
- },
- closePop(){
- this.setData({
- posterShow: false
- })
- },
- async drawSharePic() {
- const _this = this
- const qrcodeRes = await WXAPI.wxaQrcode({
- scene: _this.data.goodsId + ',' + wx.getStorageSync('uid'),
- page: '/packageCps/pages/goods-details/cps-pdd',
- is_hyaline: true,
- autoColor: true,
- expireHours: 1
- })
- if (qrcodeRes.code != 0) {
- wx.showToast({
- title: qrcodeRes.msg,
- icon: 'none'
- })
- return
- }
- const qrcode = qrcodeRes.data
- const pic = _this.data.goodsDetail.basicInfo.pic
- wx.getImageInfo({
- src: pic,
- success(res) {
- const height = 490 * res.height / res.width
- _this.drawSharePicDone(height, qrcode)
- },
- fail(e) {
- console.error(e)
- }
- })
- },
- drawSharePicDone(picHeight, qrcode) {
- const _this = this
- const _baseHeight = 74 + (picHeight + 120)
- this.setData({
- posterConfig: {
- width: 750,
- height: picHeight + 660,
- backgroundColor: '#fff',
- debug: false,
- blocks: [
- {
- x: 76,
- y: 74,
- width: 604,
- height: picHeight + 120,
- borderWidth: 2,
- borderColor: '#c2aa85',
- borderRadius: 8
- }
- ],
- images: [
- {
- x: 133,
- y: 133,
- url: _this.data.goodsDetail.basicInfo.pic, // 商品图片
- width: 490,
- height: picHeight
- },
- {
- x: 76,
- y: _baseHeight + 199,
- url: qrcode, // 二维码
- width: 222,
- height: 222
- }
- ],
- texts: [
- {
- x: 375,
- y: _baseHeight + 80,
- width: 650,
- lineNum:2,
- text: _this.data.goodsDetail.basicInfo.name,
- textAlign: 'center',
- fontSize: 40,
- color: '#333'
- },
- {
- x: 375,
- y: _baseHeight + 180,
- text: '¥' + _this.data.goodsDetail.basicInfo.minPrice,
- textAlign: 'center',
- fontSize: 50,
- color: '#e64340'
- },
- {
- x: 352,
- y: _baseHeight + 320,
- text: '长按识别小程序码',
- fontSize: 28,
- color: '#999'
- }
- ],
- }
- }, () => {
- Poster.create();
- });
- },
- onPosterSuccess(e) {
- console.log('success:', e)
- this.setData({
- posterImg: e.detail,
- showposterImg: true
- })
- },
- onPosterFail(e) {
- console.error('fail:', e)
- },
- savePosterPic() {
- const _this = this
- wx.saveImageToPhotosAlbum({
- filePath: this.data.posterImg,
- success: (res) => {
- wx.showModal({
- content: '已保存到手机相册',
- showCancel: false,
- confirmText: '知道了',
- confirmColor: '#333'
- })
- },
- complete: () => {
- _this.setData({
- showposterImg: false
- })
- },
- fail: (res) => {
- wx.showToast({
- title: res.errMsg,
- icon: 'none',
- duration: 2000
- })
- }
- })
- },
- previewImage(e) {
- const url = e.currentTarget.dataset.url
- wx.previewImage({
- current: url, // 当前显示图片的http链接
- urls: [url] // 需要预览的图片http链接列表
- })
- },
- onTabsChange(e) {
- var index = e.detail.index
- this.setData({
- toView: this.data.tabs[index].view_id
- })
- },
- backToHome() {
- wx.switchTab({
- url: '/pages/index/index',
- })
- }
- })
|