123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407 |
- // pages/prod/prod.js
- const app = getApp()
- var http = require('../../utils/http.js');
- var config = require('../../utils/config.js');
- var util = require('../../utils/util.js');
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- shopId: 1,
- shopName: '',
- picDomain: config.picDomain,
- indicatorDots: true,
- indicatorColor: '#f2f2f2',
- indicatorActiveColor: '#FF941A',
- isAuthInfo: true,
- autoplay: true,
- interval: 3000,
- duration: 1000,
- prodNum: 1,
- totalCartNum: 0,
- groupActivityId: 0,
- pic: "",
- imgs: '',
- prodName: '',
- price: 0,
- actPrice: 0,
- content: '',
- prodId: 0,
- brief: '',
- skuId: 0,
- popupShow: false,
- // 是否获取过用户领取过的优惠券id
- loadCouponIds: false,
- skuShow: false,
- skuGroupShow: false,
- commentShow: false,
- couponList: [],
- skuList: [],
- skuGroup: {},
- findSku: true,
- defaultSku: undefined,
- detaultGroupSku: undefined,
- selectedProp: [],
- selectedPropObj: {},
- selectedProperties: "",
- propKeys: [],
- allProperties: [],
- prodCommData: {},
- prodCommPage: {
- current: 0,
- pages: 0,
- records: []
- },
- littleCommPage: [],
- evaluate: -1,
- isCollection: false,
- shareShow: false,
- isDist: false, //是否分销员
- isDistProd: false, //是否分销商品
- distributionCardNo: "", //分销员卡号
- wxCodeShow: false, //是否显示二维码弹层
- shareWxCode: "", //分享二维码图片路径
- groupActivity: {}, // 团购活动
- endOfGroupTime: {}, // 距离团购活动结束还剩多久
- joinGroupList: [], // 可加入的团列表
- skuShowType: 0, // sku的显示类型 0普通sku 1拼团sku
- totalStocks: 0, // 活动剩余库存
- hasMaxNum: false, // 是否限购
- maxNum: 0, // 限购数量
- joinGroupListTimer: '',
- endOfGroupTimer: '',
- scene: '',
- prodDiscountList: [], // 商品促销活动列表
- phoneNumber: '', //客服电话
- showBacktop: false, //回到顶部显隐
- showDiscountPopup: false, //促销弹窗显隐
- videoContext: '', // 视频组件
- isPlaying: false, // 视频是否播放中
- currentSwiper: 0, // 当前轮播index
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- console.log(options)
- //根据Ticket获取保存的内容(pc团购扫码 || 分销)
- if (options.scene) {
- this.setData({
- scene: options.scene,
- })
- this.getContent() //根据Ticket获取保存的内容
- }
- if(options.shopid){
- this.setData({
- shopId: Number(options.shopid) ,
- });
- }
- // 加载商品信息
- if (options.prodid) {
- this.setData({
- prodId: options.prodid,
- });
- this.executionFunction() //所有需要加载的函数
- }
- if (options.cardno && options.type == 2) {
- // this.bindDistUser(options.cardno)
- this.setData({
- distributionCardNo: options.cardno,
- })
- }
- this.getCartCount() //请求购物车数量
- },
- /**
- * 执行函数
- */
- executionFunction: function () {
- // 加载评论数据
- this.getProdCommData();
- // 加载评论项
- this.getLittleProdComm();
- // 获取商品是否被收藏信息
- this.getCollection();
- // 查询分销开关是否开启
- this.getDistInfo();
- //加载商品数据
- this.getProdInfo();
- // 获取商品所有促销活动
- this.getPordDiscountList()
- },
- /**
- * 根据Ticket获取保存的内容
- */
- getContent: function () {
- http.request({
- url: "/qrcodeTicket/getContent",
- method: "GET",
- data: {
- ticket: this.data.scene
- },
- callBack: (res) => {
- console.log(res)
- var content = JSON.parse(res.content);
- var ids = JSON.parse(content.content)
- if (res.type == 1) { //pc团购二维码跳转
- this.setData({
- prodId: ids.prodId,
- groupActivityId: ids.groupActivityId
- })
- this.executionFunction() //所有需要加载的函数
- } else if (res.type == 2) { //分销员二维码跳转
- this.setData({
- prodId: ids.shareProdId,
- distributionCardNo: ids.cardNo
- })
- this.executionFunction() //所有需要加载的函数
- }
- }
- })
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- clearTimeout(this.data.endOfGroupTimer)
- },
- /**
- * 分销绑定用户
- */
- // bindDistUser(cardno) {
- // var params = {
- // url: "/p/distribution/bindUser",
- // method: "post",
- // data: cardno,
- // callBack:res=>{
- // console.log('绑定分销员',res)
- // }
- // }
- // http.request(params)
- // },
- /**
- * 查询分销相关信息
- */
- getDistInfo() {
- //查询分销开关是否开启
- http.request({
- url: "/p/distribution/distributionBasicSet/canDistribution",
- method: "GET",
- dontTrunLogin: true,
- // data: {
- // shopId: this.data.shopId,
- // },
- callBack: (res) => {
- if (res == 1) {
- this.getIsDistInfo();
- }
- }
- })
- },
- /**
- * 查询用户 是否为分销员
- */
- getIsDistInfo() {
- http.request({
- url: "/p/distribution/user/distributionUserInfo",
- method: "GET",
- callBack: (res) => {
- console.log('是否分销员', res)
- if (res && res.state == 1) {
- this.setData({
- isDist: true
- });
- this.getIsDistProd();
- wx.setStorageSync("distCardNo", res.cardNo);
- }
- }
- })
- },
- /**
- * 查询是否为分销商品
- */
- getIsDistProd() {
- http.request({
- url: "/p/distribution/prod/isDistributionProd",
- method: "GET",
- data: {
- prodId: this.data.prodId,
- },
- callBack: (res) => {
- console.log('是否分销商品', res)
- if (res) {
- this.setData({
- isDistProd: true
- });
- }
- }
- })
- },
- /**
- * 获取商品是否被收藏信息
- */
- getCollection() {
- wx.showLoading();
- var params = {
- url: "/p/user/collection/isCollection",
- method: "GET",
- dontTrunLogin: true,
- data: {
- prodId: this.data.prodId
- },
- callBack: (res) => {
- this.setData({
- isCollection: res
- })
- wx.hideLoading();
- }
- };
- http.request(params);
- },
- /**
- * 添加或者取消收藏商品
- */
- addOrCannelCollection() {
- util.checkAuthInfo(() => {
- var params = {
- url: "/p/user/collection/addOrCancel",
- method: "POST",
- data: this.data.prodId,
- callBack: (res) => {
- this.setData({
- isCollection: !this.data.isCollection
- })
- wx.showToast({
- title: res ? '已添加收藏' : '已取消收藏',
- duration: 3000,
- icon: 'none',
- })
- setTimeout(() => {
- wx.hideToast()
- }, 3000)
- }
- };
- http.request(params);
- })
- },
- // 获取商品信息
- getProdInfo() {
- wx.showLoading();
- var params = {
- url: "/prod/prodInfo",
- method: "GET",
- data: {
- prodId: this.data.prodId,
- platform:1,
- shopId:this.data.shopId
- // userType: 0
- },
- callBack: (res) => {
- let pic = ''
- var imgStrs = res.imgs;
- if(imgStrs){
- var imgs = imgStrs.split(",");
- }
- var content = res.content?util.formatHtml(res.content):'';
- if(res.pic){
- pic = res.pic.split(',')
- res.pic = pic[0]
- }
- this.setData({
- imgs: imgs,
- content: content,
- price: res.price,
- prodName: res.prodName,
- prodId: res.prodId,
- brief: res.brief,
- totalStocks: res.totalStocks,
- // skuId: res.skuId
- skuList: res.skuList,
- pic: res.pic,
- // shopId: res.shopId,
- video: res.video,
- prodType: res.prodType
- });
- if (res.video) {
- this.getVideoInfo()
- }
- // 团购商品
- if (res.prodType === 1) {
- this.setData({
- groupActivityId: res.activityId
- });
- this.getGroupActivity();
- this.getJoinGroupList();
- }
- // 获取优惠券
- this.getCouponList();
- // 组装sku
- res.skuList&&this.groupSkuProp(res.skuList);
- this.getShopInfo();
- wx.hideLoading();
- },
- errCallBack: (err) => {
- console.log(err)
- if (err.statusCode == 400) {
- wx.showModal({
- title: '提示',
- content: err.data,
- showCancel: false,
- success: (res) => {
- if (res.confirm) {
- wx.navigateBack({
- delta: 1
- })
- }
- }
- })
- }
- }
- };
- http.request(params);
- },
- /**
- * 获取店铺信息
- */
- getShopInfo() {
- http.request({
- url: "/shop/headInfo",
- method: "GET",
- data: {
- shopId: this.data.shopId,
- },
- callBack: (res) => {
- this.setData({
- shopName: res.shopName,
- shopLogo: res.shopLogo,
- phoneNumber: res.tel
- })
- }
- })
- },
- getProdCommData() {
- http.request({
- url: "/prod/prodCommData",
- method: "GET",
- data: {
- prodId: this.data.prodId,
- },
- callBack: (res) => {
- this.setData({
- prodCommData: res
- })
- }
- })
- },
- // 获取部分评论
- getLittleProdComm() {
- if (this.data.prodCommPage.records.length) {
- return;
- }
- this.getProdCommPage();
- },
- getMoreCommPage(e) {
- this.getProdCommPage();
- },
- // 获取分页获取评论
- getProdCommPage(e) {
- if (e) {
- if (e.currentTarget.dataset.evaluate === this.data.evaluate) {
- return;
- }
- this.setData({
- prodCommPage: {
- current: 0,
- pages: 0,
- records: []
- },
- evaluate: e.currentTarget.dataset.evaluate
- })
- }
- http.request({
- url: "/prod/prodCommPageByProd",
- method: "GET",
- data: {
- prodId: this.data.prodId,
- size: 10,
- current: this.data.prodCommPage.current + 1,
- evaluate: this.data.evaluate
- },
- callBack: (res) => {
- res.records.forEach(item => {
- if (item.pics) {
- item.pics = item.pics.split(',')
- }
- })
- let records = this.data.prodCommPage.records
- records = records.concat(res.records)
- this.setData({
- prodCommPage: {
- current: res.current,
- pages: res.pages,
- records: records
- }
- })
- // 如果商品详情中没有评论的数据,截取两条到商品详情页商品详情
- if (!this.data.littleCommPage.length) {
- this.setData({
- littleCommPage: records.slice(0, 2)
- })
- }
- }
- })
- },
- /**
- * 评论小图点击事件(点击图片显示大图)
- */
- clickImg: function (e) {
- const current = e.currentTarget.dataset.index //获取当前点击的 图片 url
- const pics = e.currentTarget.dataset.pics
- wx.previewImage({
- current: pics[current],
- urls: pics
- })
- },
- getCouponList() {
- http.request({
- url: "/coupon/listByProdId",
- method: "GET",
- data: {
- prodId: this.data.prodId,
- shopId: this.data.shopId,
- },
- callBack: (res) => {
- this.setData({
- couponList: res
- })
- }
- })
- },
- /**
- * 团购商品详情信息
- */
- getGroupActivity() {
- http.request({
- url: "/groupProd/info",
- method: "GET",
- data: {
- prodId: this.data.prodId,
- groupActivityId: this.data.groupActivityId,
- },
- callBack: (res) => {
- if (res.success) {
- this.setData({
- groupActivity: res.obj,
- maxNum: res.obj.maxNum,
- hasMaxNum: res.obj.hasMaxNum
- })
- this.setDefaultGroupSku();
- this.groupActivityCountdown(res.obj)
- } else {
- this.setData({
- groupActivityId: null
- });
- }
- }
- })
- },
- // 团购倒计时
- groupActivityCountdown(groupActivity) {
- let endOfGroupTime;
- // activityStatus 1 未开始
- if (groupActivity.activityStatus === 1) {
- endOfGroupTime = util.endOfStartTime(util.dateToTimestamp(groupActivity.startTime), new Date().getTime())
- } else {
- endOfGroupTime = util.endOfStartTime(new Date().getTime(), util.dateToTimestamp(groupActivity.endTime))
- }
- this.setData({
- endOfGroupTime: endOfGroupTime,
- endOfGroupTimer: setTimeout(() => this.groupActivityCountdown(groupActivity), 1000)
- })
- },
- // 可加入的拼团列表
- getJoinGroupList() {
- http.request({
- url: "/groupProd/joinGroupList",
- method: "GET",
- data: {
- prodId: this.data.prodId,
- groupActivityId: this.data.groupActivityId,
- showSize: 3
- },
- callBack: (res) => {
- this.setData({
- joinGroupList: res
- })
- this.joinGroupListCountdown(res)
- }
- })
- },
- joinGroupListCountdown(joinGroupList) {
- joinGroupList.forEach(item => {
- item.endOfGroupTime = util.endOfStartTime(new Date().getTime(), util.dateToTimestamp(item.endTime))
- })
- this.setData({
- joinGroupList: joinGroupList,
- joinGroupListTimer: setTimeout(() => this.joinGroupListCountdown(joinGroupList), 1000)
- })
- },
- //根据sku的属性 分组
- groupSkuProp: function (skuList) {
- // var skuList = this.data.skuList;
- if (skuList.length == 1 && skuList[0].properties == "") {
- this.setData({
- defaultSku: skuList[0]
- });
- this.setDefaultGroupSku();
- return;
- }
- var skuGroup = {};
- var allProperties = [];
- var propKeys = [];
- this.setData({
- selectedPropObj: {}
- });
- for (var i = 0; i < skuList.length; i++) {
- var defaultSku = this.data.defaultSku;
- var isDefault = false;
- if (!defaultSku && skuList[i].price == this.data.price) { //找到和商品价格一样的那个SKU,作为默认选中的SKU
- defaultSku = skuList[i];
- isDefault = true;
- this.setData({
- defaultSku: defaultSku
- });
- }
- var properties = skuList[i].properties; //版本:公开版;颜色:金色;内存:64GB
- allProperties.push(properties);
- var propList = properties.split(";"); // ["版本:公开版","颜色:金色","内存:64GB"]
- var selectedPropObj = this.data.selectedPropObj;
- for (var j = 0; j < propList.length; j++) {
- var propval = propList[j].split(":"); //["版本","公开版"]
- var props = skuGroup[propval[0]]; //先取出 版本对应的值数组
- //如果当前是默认选中的sku,把对应的属性值 组装到selectedProp
- if (isDefault) {
- propKeys.push(propval[0]);
- selectedPropObj[propval[0]] = propval[1];
- }
- if (props == undefined) {
- props = []; //假设还没有版本,新建个新的空数组
- props.push(propval[1]); //把 "公开版" 放进空数组
- } else {
- if (!this.array_contain(props, propval[1])) { //如果数组里面没有"公开版"
- props.push(propval[1]); //把 "公开版" 放进数组
- }
- }
- skuGroup[propval[0]] = props; //最后把数据 放回版本对应的值
- }
- this.setData({
- selectedPropObj: selectedPropObj,
- propKeys: propKeys
- });
- }
- this.parseSelectedObjToVals();
- this.setData({
- skuGroup: skuGroup,
- allProperties: allProperties
- });
- this.setDefaultGroupSku();
- },
- //将已选的 {key:val,key2:val2}转换成 [val,val2]
- parseSelectedObjToVals: function () {
- var selectedPropObj = this.data.selectedPropObj;
- var selectedProperties = "";
- var selectedProp = [];
- for (var key in selectedPropObj) {
- selectedProp.push(selectedPropObj[key]);
- selectedProperties += key + ":" + selectedPropObj[key] + ";";
- }
- selectedProperties = selectedProperties.substring(0, selectedProperties.length - 1);
- this.setData({
- selectedProp: selectedProp,
- selectedProperties: selectedProperties
- });
- var findSku = false;
- for (var i = 0; i < this.data.skuList.length; i++) {
- if (this.data.skuList[i].properties == selectedProperties) {
- findSku = true;
- this.setData({
- defaultSku: this.data.skuList[i]
- });
- break;
- }
- }
- this.setData({
- findSku: findSku
- });
- this.setDefaultGroupSku();
- },
- //点击选择规格
- toChooseItem: function (e) {
- var ok = e.currentTarget.dataset.ok;
- if (ok == 0) {
- return;
- }
- var val = e.currentTarget.dataset.val;
- var key = e.currentTarget.dataset.key;
- var selectedPropObj = this.data.selectedPropObj;
- selectedPropObj[key] = val;
- this.setData({
- selectedPropObj: selectedPropObj
- });
- this.parseSelectedObjToVals();
- },
- //判断数组是否包含某对象
- array_contain: function (array, obj) {
- for (var i = 0; i < array.length; i++) {
- if (array[i] == obj) //如果要求数据类型也一致,这里可使用恒等号===
- return true;
- }
- return false;
- },
- /**
- * 设置选中的拼团sku
- */
- setDefaultGroupSku() {
- if (this.data.groupActivityId) {
- var groupSkuList = this.data.groupActivity.groupSkuList;
- if (groupSkuList) {
- for (var i = 0; i < groupSkuList.length; i++) {
- if (groupSkuList[i].properties == this.data.selectedProperties) {
- this.setData({
- detaultGroupSku: groupSkuList[i]
- });
- break;
- }
- }
- }
- }
- },
- /**
- * 去凑团
- */
- toSpellGroupDetail(e) {
- const groupTeamId = e.currentTarget.dataset.groupteamid
- util.checkAuthInfo(() => {
- wx.navigateTo({
- url: '/pages/spellGroupDetails/spellGroupDetails?groupTeamId=' + groupTeamId,
- })
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- this.videoContext = wx.createVideoContext('myVideo')
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- this.getCartCount()
- if (this.data.prodType == 1) {
- this.getJoinGroupList()
- }
- },
- getCartCount: function () {
- var params = {
- url: "/p/shopCart/prodCount",
- method: "GET",
- dontTrunLogin: true,
- data: {platform:1},
- callBack: (res) => {
- if (res > 0) {
- // wx.setTabBarBadge({
- // index: 2,
- // text: res + "",
- // })
- var app = getApp();
- app.globalData.totalCartCount = res;
- } else {
- // wx.removeTabBarBadge({
- // index: 2
- // })
- var app = getApp();
- app.globalData.totalCartCount = 0;
- }
- this.setData({
- totalCartNum: res
- })
- }
- };
- http.request(params);
- },
- /**
- * 阻止滚动
- */
- preventScorll() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 跳转到首页
- */
- toHomePage: function () {
- wx.switchTab({
- url: '/pages/index/index',
- })
- },
- /**
- * 跳转到购物车
- */
- toCartPage: function () {
- wx.switchTab({
- url: '/pages/basket/basket',
- })
- },
- handleContact: function (e) {
- console.log(e.detail, '----路径')
- console.log(e.detail.query, '----参数')
- // this.get_register()
- },
- /**
- * 创建商户
- * @param {*} event
- */
- // get_register: function () {
- // let url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc032a09413289004&secret=453c5047ae43288f1d015d48df32a5c0'
- // wx.request({
- // url:url,
- // method: 'GET',
- // data: {},
- // header: {
- // 'content-type': 'application/json' // 默认值
- // },
- // success(res) {
- // // console.log(res.data.access_token)
- // wx.request({
- // url: 'https://api.weixin.qq.com/cgi-bin/business/register?access_token='+res.data.access_token,
- // method: 'POST',
- // data: {
- // account_name: "test_shopname",
- // nickname: "苹果",
- // icon_media_id: "png",
- // transfer_to_commkf: true
- // },
- // header: {
- // 'content-type': 'application/json' // 默认值
- // },
- // success(res) {
- // console.log(res.data)
- // }
- // })
- // }
- // })
- // },
- /**
- * 加入购物车
- */
- addToCart: function (event) {
- if(this.data.totalStocks == 0) return
- // let skuList = this.data.skuList[0]
- // if (skuList.weight > 20000) {
- // wx.showToast({
- // title: "商品超过20kg,太重咯~",
- // icon: "none"
- // })
- // return;
- // }
- if(this.data.totalStocks<0){
- wx.showToast({
- title: "该商品暂无库存~",
- icon: "none"
- })
- return;
- }
- if (this.data.isPlaying) {
- this._stopPlay()
- }
- if (!this.data.skuShow) {
- this.setData({
- skuShow: true
- })
- return
- }
- wx.showLoading({
- mask: true
- });
- // 查看是否授权
- util.checkAuthInfo(this.callChangeItem)
- },
- callChangeItem() {
- wx.showLoading({
- mask: true
- });
- http.request({
- url: "/p/shopCart/changeItem",
- method: "POST",
- data: {
- basketId: 0,
- count: this.data.prodNum,
- prodId: this.data.prodId,
- shopId: this.data.shopId,
- shopName: this.data.shopName,
- skuId: this.data.defaultSku.skuId,
- distributionCardNo: this.data.distributionCardNo,
- platform:1
- },
- callBack: (res) => {
- this.setData({
- totalCartNum: this.data.totalCartNum + this.data.prodNum,
- skuShow: false,
- });
- wx.hideLoading();
- wx.showToast({
- title: "加入购物车成功",
- icon: "none"
- })
- }
- });
- },
- /**
- * 立即购买
- */
- buyNow: function () {
- if(this.data.totalStocks == 0) return
- // let skuList = this.data.skuList[0]
- // if (skuList.weight > 20000) {
- // wx.showToast({
- // title: "商品超过20kg,太重咯~",
- // icon: "none"
- // })
- // return;
- // }
- if(this.data.totalStocks<0){
- wx.showToast({
- title: "该商品暂无库存~",
- icon: "none"
- })
- return;
- }
- if (this.data.isPlaying) {
- this._stopPlay()
- }
- if (!this.data.skuShow) {
- this.setData({
- skuShow: true
- })
- return
- }
- wx.showLoading({
- mask: true
- });
- // 查看是否授权
- util.checkAuthInfo(this._buyNowData(this))
- },
- /**
- * 立即购买参数配置
- */
- _buyNowData(ctx) {
- wx.setStorageSync("orderItem", JSON.stringify({
- prodId: ctx.data.prodId,
- skuId: ctx.data.defaultSku.skuId,
- prodCount: ctx.data.prodNum,
- shopId: ctx.data.shopId,
- distributionCardNo: ctx.data.distributionCardNo
- }));
- wx.navigateTo({
- url: '/pages/submit-order/submit-order?orderEntry=1',
- })
- },
- /**
- * 减数量
- */
- onCountMinus: function () {
- var prodNum = this.data.prodNum;
- if (prodNum > 1) {
- this.setData({
- prodNum: prodNum - 1
- });
- }
- },
- /**
- * 加数量
- */
- onCountPlus: function () {
- var prodNum = this.data.prodNum;
- // 判断是否限购
- if (this.data.hasMaxNum) {
- if (prodNum < this.data.totalStocks && prodNum < this.data.maxNum) {
- this.setData({
- prodNum: prodNum + 1
- });
- } else {
- wx.showToast({
- title: '限购' + this.data.maxNum + '件',
- icon: 'none',
- })
- }
- } else {
- if (prodNum < this.data.totalStocks) {
- this.setData({
- prodNum: prodNum + 1
- });
- } else {
- wx.showToast({
- title: '库存不足!',
- icon: 'none'
- })
- }
- }
- },
- /**
- * 用户点击转发
- */
- onShareAppMessage: function (res) {
- if (res.from === 'button') {
- var cardno = wx.getStorageSync("distCardNo");
- return {
- title: this.data.prodName,
- path: '/pages/prod/prod?prodid=' + this.data.prodId + '&cardno=' + cardno + '&type=' + this.data.shareType,
- imageUrl: this.data.pic,
- }
- } else {
- return {
- title: this.data.prodName,
- path: '/pages/prod/prod?prodid=' + this.data.prodId
- }
- }
- },
- /**
- * 优惠券
- */
- showPopup: function () {
- if (this.data.loadCouponIds) {
- this.setData({
- popupShow: true
- });
- return;
- }
- http.request({
- url: "/p/myCoupon/listCouponIds",
- method: "GET",
- data: {},
- callBack: (couponIds) => {
- var couponList = this.data.couponList;
- couponList.forEach(coupon => {
- if (couponIds && couponIds.length) {
- // 领取该优惠券数量
- var couponLimit = 0;
- couponIds.forEach(couponId => {
- if (couponId == coupon.couponId) {
- couponLimit++;
- }
- });
- // 小于用户领取优惠券上限,可以领取优惠券
- if (couponLimit < coupon.limitNum) {
- coupon.canReceive = true;
- } else {
- coupon.canReceive = false;
- }
- } else {
- coupon.canReceive = true;
- }
- });
- this.setData({
- couponList: couponList,
- popupShow: true,
- loadCouponIds: true
- })
- }
- })
- },
- showSku: function (e) {
- if (e.currentTarget.dataset.alonebuy == 1) {
- this.setData({
- skuShow: true,
- skuShowType: 0
- });
- } else {
- if (this.data.groupActivityId) {
- this.setData({
- skuShow: true,
- skuShowType: 1
- });
- } else {
- this.setData({
- skuShow: true,
- skuShowType: 0
- });
- }
- }
- },
- showGroupSku: function () {
- if (this.data.isPlaying) {
- this._stopPlay()
- }
- this.setData({
- skuShow: true,
- skuShowType: 1
- });
- },
- showComment: function () {
- this.setData({
- commentShow: true
- });
- },
- closePopup: function () {
- this.setData({
- popupShow: false,
- skuShow: false,
- commentShow: false
- });
- },
- /**
- * 赚字浮层
- */
- onShowShare: function (e) {
- if (this.data.isPlaying) {
- this._stopPlay()
- }
- this.setData({
- shareShow: true,
- shareType: e.currentTarget.dataset.type,
- });
- },
- /**
- * 回到顶部
- */
- backToTop: function () {
- wx.pageScrollTo({
- scrollTop: 0
- })
- },
- /**
- * 监听页面滚动
- */
- onPageScroll: function (e) {
- if (e.scrollTop > 80) {
- this.setData({
- showBacktop: true
- })
- } else if (e.scrollTop < 80) {
- this.setData({
- showBacktop: false
- })
- }
- },
- closeEarn: function () {
- this.setData({
- shareShow: false
- })
- },
- toDistCenterPage: function () {
- wx.navigateTo({
- url: '/pages/dis-center/dis-center',
- })
- },
- /**
- * 生成小程序二维码
- */
- genWeixinCode() {
- wx.showLoading({
- mask: true
- })
- var cardNo = wx.getStorageSync("distCardNo");
- var content = JSON.stringify({
- shareProdId: this.data.prodId,
- cardNo: cardNo,
- })
- http.request({
- url: "/qrcodeTicket/miniQrCode",
- method: "GET",
- responseType: 'arraybuffer',
- data: {
- content: content,
- type: 2
- },
- callBack: (res) => {
- this.setData({
- wxCodeShow: true,
- shareShow: false,
- shareWxCode: "data:image/jpg;base64," + wx.arrayBufferToBase64(res)
- });
- wx.hideLoading();
- }
- })
- console.log('分销员id:' + cardNo)
- },
- /**
- * 关闭二维码弹窗
- */
- closeCodePopup() {
- this.setData({
- wxCodeShow: false
- });
- },
- /**
- * 保存图片至相册
- */
- downloadImg() {
- var ths = this
- var imgSrc = this.data.shareWxCode.slice(22); //base64编码
- var save = wx.getFileSystemManager();
- save.writeFile({
- filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
- data: imgSrc,
- encoding: 'base64',
- success: res => {
- wx.saveImageToPhotosAlbum({
- filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
- success: function (res) {
- wx.showToast({
- title: '保存成功',
- })
- ths.setData({
- wxCodeShow: false
- })
- },
- fail: function (err) {
- wx.showModal({
- title: '提示',
- content: '保存图片需要授予存储权限',
- success: modalRes => {
- if (modalRes.confirm) {
- wx.openSetting({
- success(settingdata) {
- if (settingdata.authSetting['scope.writePhotosAlbum']) {
- console.log('用户权限成功')
- } else {
- console.log('用户取消权限')
- }
- }
- })
- }
- }
- })
- }
- })
- // console.log(res)
- },
- fail: err => {
- // console.log(err)
- }
- })
- },
- /**
- * 获取用户信息
- */
- onGotUserInfo: function (e) {
- if (e.detail.errMsg != "getUserInfo:fail auth deny") {
- this.setData({
- isAuthInfo: true
- });
- http.updateUserInfo();
- }
- },
- /**
- * 确认开团
- */
- groupConfirmOrder: function (e) {
- if (!this.data.findSku) {
- return;
- }
- wx.showLoading({
- mask: true
- });
- this.setData({
- teamId: e.currentTarget.dataset.teamid
- })
- // 查看是否授权
- util.checkAuthInfo(this._groupConfirmParams)
- },
- // 拼团确认订单参数
- _groupConfirmParams() {
- wx.setStorageSync("groupOrderItem", JSON.stringify({
- groupSkuId: this.data.detaultGroupSku.groupSkuId, // this.data.groupSkuId,
- prodCount: this.data.prodNum, // this.data.prodNum,
- groupTeamId: this.data.teamId // this.data.groupTeamId
- }));
- wx.navigateTo({
- url: '/pages/groupConfirmOrder/groupConfirmOrder',
- })
- },
- /**
- * 跳转到店铺页
- */
- toShopPage: function () {
- wx.navigateTo({
- url: '/pages/shopPage/shopPage?shopId=' + this.data.shopId,
- })
- },
- /**
- * 通过商品id获取商品所有促销活动
- */
- getPordDiscountList() {
- http.request({
- url: "/marking/discount/getDiscountByProdId",
- method: "GET",
- data: {
- prodId: this.data.prodId
- },
- callBack: res => {
- this.setData({
- prodDiscountList: res
- })
- }
- })
- },
- /**
- * 促销活动弹窗
- */
- clickDiscount: function () {
- var showDiscountPopup = !this.data.showDiscountPopup
- this.setData({
- showDiscountPopup
- })
- },
- /**
- * 页面从前台变为后台时执行
- */
- onHide: function () {
- this.setData({
- isCollection: false,
- skuShow: false,
- })
- clearTimeout(this.data.joinGroupListTimer)
- },
- /**
- * 视频信息
- */
- getVideoInfo() {
- wx.showLoading()
- wx.request({
- url: this.data.video + '?avinfo',
- success: res => {
- wx.hideLoading()
- let time = Number(res.data.format.duration).toFixed(0)
- this.setData({
- videoDuration: time < 60 ? '00′' + time + '″' : ((parseInt(time / 60) < 10 ? '0' + parseInt(time / 60) : parseInt(time / 60)) + '′' + (time % 60 < 10 ? '0' + time % 60 : time % 60) + '″')
- })
- }
- })
- },
- /**
- * 视频播放/停止按钮
- */
- videoOper() {
- if (this.data.isPlaying) {
- this._stopPlay()
- } else {
- this._startPlay()
- }
- },
- /**
- * 视频播放结束时
- */
- playEnd() {
- this._stopPlay()
- },
- _startPlay() {
- this.setData({
- isPlaying: true,
- })
- this.videoContext.play()
- this.videoContext.seek(0)
- },
- _stopPlay() {
- this.setData({
- isPlaying: false
- })
- this.videoContext.stop()
- this.videoContext.seek(0)
- },
- })
|