123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812 |
- import { getProductDetail, getProductCode, collectAdd, collectDel, postCartAdd, storeListApi } from '../../api/store.js';
- import { getUserInfo, userShare } from '../../api/user.js';
- import { getCoupons } from '../../api/api.js';
- import { getCartCounts } from '../../api/order.js';
- import WxParse from '../../wxParse/wxParse.js';
- import util from '../../utils/util.js';
- import wxh from '../../utils/wxh.js';
- import { CACHE_LONGITUDE, CACHE_LATITUDE } from '../../config.js';
- const app = getApp();
- Page({
-
- data: {
- parameter: {
- 'navbar': '1',
- 'return': '1',
- 'title': '商品详情'
- },
- attribute: { 'cartAttr': false },
- coupon: {
- 'coupon': false,
- list: [],
- },
- attr: '请选择',
- attrValue: '',
- animated: false,
- id: 0,
- replyCount: 0,
- reply: [],
- storeInfo: {},
- productAttr: [],
- productValue: [],
- couponList: [],
- productSelect: {},
- cart_num: 1,
- isAuto: false,
- iShidden: true,
- isOpen: false,
- isLog: app.globalData.isLog,
- actionSheetHidden: true,
- posterImageStatus: false,
- storeImage: '',
- PromotionCode: '',
- canvasStatus: false,
- posterImage: '',
- posterbackgd: '/images/posterbackgd.png',
- sharePacket: {
- isState: true,
- },
- uid: 0,
- circular: false,
- autoplay: false,
- interval: 3000,
- duration: 500,
- clientHeight: "",
- systemStore: {},
- good_list: [],
- isDown: true,
- storeSelfMention: true,
- storeItems: {},
- storeList: [],
- activity:[],
- iSplus:true,
- navH: "",
- navList: [],
- opacity: 0,
- scrollY:0,
- topArr:[],
- toView: '',
- height: 0,
- heightArr: [],
- lock: false,
- scrollTop:0
- },
- returns: function () {
- wx.navigateBack();
- },
- tap: function (e) {
- var id = e.currentTarget.dataset.id;
- var index = e.currentTarget.dataset.index;
- var that = this;
- if (!this.data.good_list.length && id == "past2") {
- id = "past3"
- }
- this.setData({
- toView: id,
- navActive: index,
- lock: true,
- scrollTop:index>0?that.data.topArr[index]-(app.globalData.navHeight/2):that.data.topArr[index]
- });
- },
- scroll: function (e) {
- var that = this, scrollY = e.detail.scrollTop;
- var opacity = scrollY / 200;
- opacity = opacity > 1 ? 1 : opacity;
- that.setData({
- opacity: opacity,
- scrollY: scrollY
- })
- if (that.data.lock) {
- that.setData({
- lock: false
- })
- return;
- }
- for (var i = 0; i < that.data.topArr.length; i++) {
- if (scrollY < that.data.topArr[i]-(app.globalData.navHeight/2) + that.data.heightArr[i]) {
- that.setData({
- navActive: i
- });
- break
- }
- }
- },
-
- onLoadFun: function (e) {
- this.setData({ isLog: true });
- this.getCouponList();
- this.getCartCount();
- this.downloadFilePromotionCode();
- this.getUserInfo();
- this.get_product_collect();
- },
- ChangCouponsClone: function () {
- this.setData({ 'coupon.coupon': false });
- },
- goActivity: function (e) {
- let item = e.currentTarget.dataset.items;
- if (item.type === "1") {
- wx.navigateTo({
- url: `/pages/activity/goods_seckill_details/index?id=${item.id}&time=${item.time}&status=1`
- });
- } else if (item.type === "2") {
- wx.navigateTo({ url: `/pages/activity/goods_bargain_details/index?id=${item.id}`});
- } else {
- wx.navigateTo({ url: `/pages/activity/goods_combination_details/index?id=${item.id}`});
- }
- },
-
- goDetail: function (e) {
- let item = e.currentTarget.dataset.items;
- if (item.activity && item.activity.type === "1") {
- wx.navigateTo({
- url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
- });
- } else if (item.activity && item.activity.type === "2") {
- wx.navigateTo({ url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}`});
- } else if (item.activity && item.activity.type === "3") {
- wx.navigateTo({
- url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
- });
- } else {
- wx.navigateTo({ url: `/pages/goods_details/index?id=${item.id}` });
- }
- },
-
- showStoreList: function () {
- wx.navigateTo({
- url: '/pages/goods_details_store/index?go=details'
- })
- },
-
- getList: function () {
- let longitude = wx.getStorageSync(CACHE_LONGITUDE);
- let latitude = wx.getStorageSync(CACHE_LATITUDE);
- let data = {
- latitude: latitude,
- longitude: longitude,
- page: 1,
- limit: 10
- }
- storeListApi(data).then(res => {
- let list = res.data.list || [];
- this.setData({
- storeList: list,
- storeItems: list[0]
- });
- }).catch(err => {
- })
- },
-
- getUserInfo: function () {
- var that = this;
- getUserInfo().then(res => {
- let isState = true;
- if ((res.data.is_promoter || res.data.statu == 2) && this.data.sharePacket.priceName != 0) {
- isState = false;
- }
- that.setData({
- 'sharePacket.isState': isState,
- uid: res.data.uid
- });
- });
- },
-
- iptCartNum: function (e) {
- this.setData({
- ['productSelect.cart_num']: e.detail,
- cart_num: e.detail
- });
- },
-
- ChangeCartNum: function (e) {
-
- var changeValue = e.detail;
-
- var productSelect = this.data.productValue[this.data.attrValue];
- if (this.data.cart_num) {
- productSelect.cart_num = this.data.cart_num;
- };
-
- if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
-
- if (productSelect === undefined) return;
-
- var stock = productSelect.stock || 0;
-
- if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
-
- if (changeValue) {
- productSelect.cart_num++;
-
- if (productSelect.cart_num > stock) productSelect.cart_num = stock;
- this.setData({
- ['productSelect.cart_num']: productSelect.cart_num,
- cart_num: productSelect.cart_num
- });
- } else {
-
- productSelect.cart_num--;
-
- if (productSelect.cart_num < 1) productSelect.cart_num = 1;
- this.setData({
- ['productSelect.cart_num']: productSelect.cart_num,
- cart_num: productSelect.cart_num
- });
- }
- },
-
- ChangeAttr: function (e) {
- var values = e.detail;
- var productSelect = this.data.productValue[values];
- var storeInfo = this.data.storeInfo;
- if (productSelect) {
- this.setData({
- ["productSelect.image"]: productSelect.image,
- ["productSelect.price"]: productSelect.price,
- ["productSelect.stock"]: productSelect.stock,
- ['productSelect.unique']: productSelect.unique,
- ['productSelect.cart_num']: 1,
- attrValue: values,
- attr: '已选择'
- });
- } else {
- this.setData({
- ["productSelect.image"]: storeInfo.image,
- ["productSelect.price"]: storeInfo.price,
- ["productSelect.stock"]: 0,
- ['productSelect.unique']: '',
- ['productSelect.cart_num']: 0,
- attrValue: '',
- attr: '请选择'
- });
- }
- },
-
- ChangCoupons: function (e) {
- var coupon = e.detail;
- var couponList = util.ArrayRemove(this.data.couponList, 'id', coupon.id);
- this.setData({ couponList: couponList });
- },
-
- onLoad: function (options) {
- var that = this;
- this.setData({
- navH: app.globalData.navHeight
- });
-
- wx.getSystemInfo({
- success: function (res) {
- that.setData({
- height: res.windowHeight
-
- })
- },
- });
-
- if (options.scene) {
- var value = util.getUrlParams(decodeURIComponent(options.scene));
- if (value.id) options.id = value.id;
-
- if (value.pid) app.globalData.spid = value.pid;
- }
- if (!options.id) return app.Tips({ title: '缺少参数无法查看商品' }, { tab: 3, url: 1 });
- this.setData({ id: options.id });
-
- if (options.spid) app.globalData.spid = options.spid;
- this.getGoodsDetails();
- this.getList();
- },
- setClientHeight: function () {
- if (!this.data.good_list.length) return;
- var query = wx.createSelectorQuery().in(this);
- query.select("#list0").boundingClientRect();
- var that = this;
- query.exec(function (res) {
- that.setData({
- clientHeight: res[0].height + 20
- });
- });
- },
- infoScroll: function () {
- var that = this, topArr = [], heightArr = [];
- for (var i = 0; i < that.data.navList.length; i++) {
-
- var query = wx.createSelectorQuery().in(this);
- var idView = "#past" + i;
- if (!that.data.good_list.length && i == 2) {
- var idView = "#past" + 3;
- }
- query.select(idView).boundingClientRect();
- query.exec(function (res) {
- console.log(res);
- var top = res[0].top;
- var height = res[0].height;
- topArr.push(top);
- heightArr.push(height);
- that.setData({
- topArr: topArr,
- heightArr: heightArr
- });
- });
- };
- },
-
- getGoodsDetails: function () {
- var that = this;
- getProductDetail(that.data.id).then(res => {
- var storeInfo = res.data.storeInfo;
- var good_list = res.data.good_list || [];
- var count = Math.ceil(good_list.length / 6);
- var goodArray = new Array();
- for (var i = 0; i < count; i++) {
- var list = good_list.slice(i * 6, i * 6 + 6);
- if (list.length) goodArray.push({ list: list });
- }
- that.setData({
- storeInfo: storeInfo,
- reply: res.data.reply ? [res.data.reply] : [],
- replyCount: res.data.replyCount,
- description: storeInfo.description,
- replyChance: res.data.replyChance,
- productAttr: res.data.productAttr,
- productValue: res.data.productValue,
- ["sharePacket.priceName"]: res.data.priceName,
-
- systemStore: res.data.system_store,
- storeSelfMention: res.data.store_self_mention,
- good_list: goodArray,
- activity:res.data.activity ? res.data.activity : []
- });
- var navList = ['商品', '评价', '详情'];
- if (goodArray.length) {
- navList.splice(2, 0, '推荐')
- }
- that.setData({
- navList: navList
- })
- that.downloadFilestoreImage();
- that.DefaultSelect();
- setTimeout(function () {
- that.setClientHeight();
- }, 500);
- setTimeout(function () {
- that.infoScroll();
- }, 500);
-
- WxParse.wxParse('description', 'html', that.data.description, that, 0);
- }).catch(err => {
-
- return app.Tips({ title: err.toString() }, { tab: 3, url: 1 });
- })
- },
- goPages: function (e) {
- wx.navigateTo({ url: 'pages/goods_details/index?id=' + e.currentTarget.dataset.id });
- },
-
- makePhone: function () {
- wx.makePhoneCall({
- phoneNumber: this.data.systemStore.phone
- })
- },
-
- showMaoLocation: function () {
- if (!this.data.systemStore.latitude || !this.data.systemStore.longitude) return app.Tips({ title: '缺少经纬度信息无法查看地图!' });
- wx.openLocation({
- latitude: parseFloat(this.data.systemStore.latitude),
- longitude: parseFloat(this.data.systemStore.longitude),
- scale: 8,
- name: this.data.systemStore.name,
- address: this.data.systemStore.address + this.data.systemStore.detailed_address,
- success: function () {
- },
- });
- },
-
- DefaultSelect: function () {
- var productAttr = this.data.productAttr, storeInfo = this.data.storeInfo , productValue = this.data.productValue,
- value = [];
- for (var key in productValue) {
- if (productValue[key].stock > 0) {
- value = this.data.productAttr.length ? key.split(",") : [];
- break;
- }
- }
- console.log(666);
- console.log(value);
- for (var i = 0, len = productAttr.length; i < len; i++) {
- if (productAttr[i].attr_value[0]) productAttr[i].checked = value[i];
- };
- var productSelect = this.data.productValue[value.sort().join(',')];
- if (productSelect) {
- this.setData({
- ["productSelect.store_name"]: storeInfo.store_name,
- ["productSelect.image"]: productSelect.image,
- ["productSelect.price"]: productSelect.price,
- ["productSelect.stock"]: productSelect.stock,
- ['productSelect.unique']: productSelect.unique,
- ['productSelect.cart_num']: 1,
- attrValue: value,
- attr: '已选择'
- });
- } else {
- this.setData({
- ["productSelect.store_name"]: storeInfo.store_name,
- ["productSelect.image"]: storeInfo.image,
- ["productSelect.price"]: storeInfo.price,
- ["productSelect.stock"]: this.data.productAttr.length ? 0 : storeInfo.stock,
- ['productSelect.unique']: '',
- ['productSelect.cart_num']: this.data.productAttr.length ? 0 : 1,
- attrValue: '',
- attr: '请选择'
- });
- }
- this.setData({ productAttr: productAttr });
- },
-
- get_product_collect: function () {
- var that = this;
- getProductDetail(that.data.id).then(res => {
- that.setData({ 'storeInfo.userCollect': res.data.storeInfo.userCollect });
- });
- },
-
- getCouponList() {
- var that = this;
- getCoupons({ page: 1, limit: 10, type: 1, product_id: that.data.id}).then(res => {
- var couponList = [];
- for (var i = 0; i < res.data.length; i++) {
- if (!res.data[i].is_use && couponList.length < 2) couponList.push(res.data[i]);
- }
- that.setData({
- ['coupon.list']: res.data,
- couponList: couponList
- });
- });
- },
-
- setCollect: function () {
- if (app.globalData.isLog === false) {
- this.setData({
- isAuto: true,
- iShidden: false,
- });
- } else {
- var that = this;
- if (this.data.storeInfo.userCollect) {
- collectDel(this.data.storeInfo.id).then(res => {
- that.setData({
- ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
- })
- })
- } else {
- collectAdd(this.data.storeInfo.id).then(res => {
- that.setData({
- ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
- })
- })
- }
- }
- },
-
- selecAttr: function () {
- if (app.globalData.isLog === false)
- this.setData({ isAuto: true, iShidden: false })
- else
- this.setData({ 'attribute.cartAttr': true, isOpen: true })
- },
-
- coupon: function () {
- if (app.globalData.isLog === false)
- this.setData({ isAuto: true, iShidden: false })
- else {
- this.getCouponList();
- this.setData({ 'coupon.coupon': true })
- }
- },
- onMyEvent: function (e) {
- this.setData({ 'attribute.cartAttr': e.detail.window, isOpen: false });
- },
-
- joinCart: function (e) {
-
- if (app.globalData.isLog === false)
- this.setData({ isAuto: true, iShidden: false, });
- else {
- this.goCat();
- }
- },
-
- goCat: function (isPay) {
- var that = this;
- var productSelect = this.data.productValue[this.data.attrValue];
-
- if (this.data.attrValue) {
-
- this.setData({ 'attribute.cartAttr': !this.data.isOpen ? true : false })
- } else {
- if (this.data.isOpen)
- this.setData({ 'attribute.cartAttr': true })
- else
- this.setData({ 'attribute.cartAttr': !this.data.attribute.cartAttr });
- }
-
- if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({ isOpen: true });
-
- if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen == true) return app.Tips({ title: '请选择属性' });
- if (!that.data.cart_num || parseInt(that.data.cart_num) <= 0) return app.Tips({ title: '请输入购买数量' });
- postCartAdd({
- productId: that.data.id,
- cartNum: that.data.cart_num,
- uniqueId: productSelect !== undefined ? productSelect.unique : '',
- 'new': isPay === undefined ? 0 : 1,
- }).then(res => {
- that.setData({ isOpen: false, 'attribute.cartAttr': false });
- if (isPay)
- wx.navigateTo({ url: '/pages/order_confirm/index?cartId=' + res.data.cartId });
- else
- app.Tips({ title: '添加购物车成功', icon: 'success' }, function () {
- that.getCartCount(true);
- });
- }).catch(err => {
- return app.Tips({ title: err });
- });
- },
-
- getCartCount: function (isAnima) {
- var that = this;
- getCartCounts().then(res => {
- that.setData({ CartCount: res.data.count });
-
- if (isAnima) {
- that.setData({
- animated: true,
- attrValue: '',
- attr: '请选择',
- ["productSelect.image"]: that.data.storeInfo.image,
- ["productSelect.price"]: that.data.storeInfo.price,
- ["productSelect.stock"]: that.data.storeInfo.stock,
- ['productSelect.unique']: '',
- ['productSelect.cart_num']: 1,
- });
- that.selectComponent('#product-window').ResetAttr();
- setTimeout(function () {
- that.setData({
- animated: false
- });
- }, 500);
- }
- });
- },
-
- goBuy: function (e) {
- if (app.globalData.isLog === false)
- this.setData({ isAuto: true, iShidden: false });
- else
- this.goCat(true);
- },
-
- listenerActionSheet: function () {
- if (app.globalData.isLog === false)
- this.setData({ isAuto: true, iShidden: false });
- else
- this.setData({ actionSheetHidden: !this.data.actionSheetHidden })
- },
-
- posterImageClose: function () {
- this.setData({ posterImageStatus: false, })
- },
-
- setDomain: function (url) {
- url = url ? url.toString() : '';
-
-
- if (url.indexOf("https://") > -1) return url;
- else return url.replace('http://', 'https://');
- },
-
- downloadFilestoreImage: function () {
- var that = this;
- wx.downloadFile({
- url: that.setDomain(that.data.storeInfo.image),
- success: function (res) {
- that.setData({
- storeImage: res.tempFilePath
- })
- },
- fail: function () {
- return app.Tips({ title: '' });
- that.setData({
- storeImage: '',
- })
- },
- });
- },
-
- downloadFilePromotionCode: function (successFn) {
- var that = this;
- getProductCode(this.data.id).then(res => {
- wx.downloadFile({
- url: that.setDomain(res.data.code),
- success: function (res) {
- that.setData({ isDown: false });
- if (typeof successFn == 'function')
- successFn && successFn(res.tempFilePath);
- else
- that.setData({ PromotionCode: res.tempFilePath });
- },
- fail: function () {
- that.setData({ isDown: false });
- that.setData({ PromotionCode: '' });
- },
- });
- }).catch(err => {
- that.setData({ isDown: false });
- that.setData({ PromotionCode: '' });
- });
- },
-
- goPoster: function () {
- var that = this;
- that.setData({ canvasStatus: true });
- var arr2 = [that.data.posterbackgd, that.data.storeImage, that.data.PromotionCode];
- if (that.data.isDown) return app.Tips({ title: '正在下载海报,请稍后再试!' });
- wx.getImageInfo({
- src: that.data.PromotionCode,
- fail: function (res) {
- return app.Tips({ 'title': '小程序二维码需要发布正式版后才能获取到' });
- },
- success() {
- if (arr2[2] == '') {
-
- that.downloadFilePromotionCode(function (msgPromotionCode) {
- arr2[2] = msgPromotionCode;
- if (arr2[2] == '') return app.Tips({ title: '海报二维码生成失败!' });
- util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath) {
- that.setData({
- posterImage: tempFilePath,
- posterImageStatus: true,
- canvasStatus: false,
- actionSheetHidden: !that.data.actionSheetHidden
- })
- });
- });
- } else {
-
- util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath) {
- that.setData({
- posterImage: tempFilePath,
- posterImageStatus: true,
- canvasStatus: false,
- actionSheetHidden: !that.data.actionSheetHidden
- })
- });
- }
- },
- });
- },
-
- savePosterPath: function () {
- var that = this;
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.writePhotosAlbum']) {
- wx.authorize({
- scope: 'scope.writePhotosAlbum',
- success() {
- wx.saveImageToPhotosAlbum({
- filePath: that.data.posterImage,
- success: function (res) {
- that.posterImageClose();
- app.Tips({ title: '保存成功', icon: 'success' });
- },
- fail: function (res) {
- app.Tips({ title: '保存失败' });
- }
- })
- }
- })
- } else {
- wx.saveImageToPhotosAlbum({
- filePath: that.data.posterImage,
- success: function (res) {
- that.posterImageClose();
- app.Tips({ title: '保存成功', icon: 'success' });
- },
- fail: function (res) {
- app.Tips({ title: '保存失败' });
- },
- })
- }
- }
- })
- },
-
- onShareAppMessage: function () {
- var that = this;
- that.setData({ actionSheetHidden: !that.data.actionSheetHidden });
- userShare();
- return {
- title: that.data.storeInfo.store_name || '',
- imageUrl: that.data.storeInfo.image || '',
- path: '/pages/goods_details/index?id=' + that.data.id + '&spid=' + that.data.uid,
- }
- }
- })
|