| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762 |
- // goods.js
- var api = require('../../api.js');
- var utils = require('../../utils.js');
- var app = getApp();
- var WxParse = require('../../wxParse/wxParse.js');
- var p = 1;
- var is_loading_comment = false;
- var is_more_comment = true;
- var share_count = 0;
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- id: null,
- goods: {},
- show_attr_picker: false,
- form: {
- number: 1,
- },
- tab_detail: "active",
- tab_comment: "",
- comment_list: [],
- comment_count: {
- score_all: 0,
- score_3: 0,
- score_2: 0,
- score_1: 0,
- },
- autoplay: false,
- hide: "hide",
- show: false,
- x: wx.getSystemInfoSync().windowWidth,
- y: wx.getSystemInfoSync().windowHeight - 20,
- miaosha_end_time_over: {
- h: "--",
- m: "--",
- s: "--",
- },
- type: 0,
- page: 1
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- app.pageOnLoad(this);
- console.log(wx.getSystemInfoSync());
- share_count = 0;
- p = 1;
- is_loading_comment = false;
- is_more_comment = true;
- this.setData({
- store: wx.getStorageSync('store'),
- });
- var parent_id = 0;
- var user_id = options.user_id;
- console.log("options=>" + JSON.stringify(options));
- var scene = decodeURIComponent(options.scene);
- if (user_id != undefined) {
- parent_id = user_id;
- } else if (scene != undefined) {
- console.log("scene string=>" + scene);
- var scene_obj = utils.scene_decode(scene);
- console.log("scene obj=>" + JSON.stringify(scene_obj));
- if (scene_obj.uid && scene_obj.gid) {
- parent_id = scene_obj.uid;
- options.id = scene_obj.gid;
- } else {
- parent_id = scene;
- }
- }
- app.loginBindParent({parent_id: parent_id});
- var page = this;
- page.setData({
- id: options.id
- });
- page.getGoods();
- page.getCommentList();
- },
- call: function(){
- var that = this;
- wx.makePhoneCall({
- phoneNumber: that.data.goods.contact_tel
- })
- },
- changeState: function(e){
- var that = this;
- var state = e.currentTarget.dataset.state;
- var type = e.currentTarget.dataset.type;
- var index = e.currentTarget.dataset.index;
- var comment_list = that.data.comment_list;
- if(type == 'content'){
- comment_list[index].up_down = state;
- }
- else{
- comment_list[index].reply_up_down = state;
- }
- that.setData({
- comment_list: comment_list
- })
- },
- change: function(e){
- var type = e.currentTarget.dataset.type;
- this.setData({
- type: type,
- page: 1
- })
- this.getCommentList();
- },
- getGoods: function () {
- var page = this;
- app.request({
- url: api.default.goods,
- data: {
- id: page.data.id
- },
- success: function (res) {
- if (res.code == 0) {
- var detail = res.data.detail;
- WxParse.wxParse("detail", "html", detail, page);
- page.setData({
- goods: res.data,
- attr_group_list: res.data.attr_group_list,
- });
- if (page.data.goods.miaosha)
- page.setMiaoshaTimeOver();
- page.selectDefaultAttr();
- }
- if (res.code == 1) {
- wx.showModal({
- title: "提示",
- content: res.msg,
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- wx.switchTab({
- url: "/pages/index/index"
- });
- }
- }
- });
- }
- }
- });
- },
- selectDefaultAttr: function () {
- var page = this;
- if (!page.data.goods || page.data.goods.use_attr !== 0)
- return;
- for (var i in page.data.attr_group_list) {
- for (var j in page.data.attr_group_list[i].attr_list) {
- if (i == 0 && j == 0)
- page.data.attr_group_list[i].attr_list[j]['checked'] = true;
- }
- }
- page.setData({
- attr_group_list: page.data.attr_group_list,
- });
- },
- getCommentList: function (more) {
- var page = this;
- var p = page.data.page;
- if (more)
- return;
- // if (is_loading_comment)
- // return;
- // if (!is_more_comment)
- // return;
- is_loading_comment = true;
- app.request({
- url: api.default.comment_list,
- data: {
- goods_id: page.data.id,
- page: p,
- type: page.data.type
- },
- success: function (res) {
- if (res.code != 0)
- return;
- is_loading_comment = false;
- // p++;
- page.setData({
- comment_count: res.data.comment_count,
- comment_list: more ? page.data.comment_list.concat(res.data.list) : res.data.list,
- page: p++
- });
- if (res.data.list.length == 0)
- is_more_comment = false;
- }
- });
- },
- onGoodsImageClick: function (e) {
- var page = this;
- var urls = [];
- var index = e.currentTarget.dataset.index;
- //console.log(page.data.goods.pic_list);
- for (var i in page.data.goods.pic_list) {
- urls.push(page.data.goods.pic_list[i].pic_url);
- }
- wx.previewImage({
- urls: urls, // 需要预览的图片http链接列表
- current: urls[index],
- });
- },
- numberSub: function () {
- var page = this;
- var num = page.data.form.number;
- if (num <= 1)
- return true;
- num--;
- page.setData({
- form: {
- number: num,
- }
- });
- },
- numberAdd: function () {
- var page = this;
- var num = page.data.form.number;
- num++;
- page.setData({
- form: {
- number: num,
- }
- });
- },
- numberBlur: function (e) {
- var page = this;
- var num = e.detail.value;
- num = parseInt(num);
- if (isNaN(num))
- num = 1;
- if (num <= 0)
- num = 1;
- page.setData({
- form: {
- number: num,
- }
- });
- },
- addCart: function () {
- this.submit('ADD_CART');
- },
- buyNow: function () {
- this.submit('BUY_NOW');
- },
- submit: function (type) {
- var page = this;
- if (!page.data.show_attr_picker) {
- page.setData({
- show_attr_picker: true,
- });
- return true;
- }
- if (page.data.miaosha_data && page.data.miaosha_data.rest_num > 0 && page.data.form.number > page.data.miaosha_data.rest_num) {
- wx.showToast({
- title: "商品库存不足,请选择其它规格或数量",
- image: "/images/icon-warning.png",
- });
- return true;
- }
- if (page.data.form.number > page.data.goods.num) {
- wx.showToast({
- title: "商品库存不足,请选择其它规格或数量",
- image: "/images/icon-warning.png",
- });
- return true;
- }
- var attr_group_list = page.data.attr_group_list;
- var checked_attr_list = [];
- for (var i in attr_group_list) {
- var attr = false;
- for (var j in attr_group_list[i].attr_list) {
- if (attr_group_list[i].attr_list[j].checked) {
- attr = {
- attr_id: attr_group_list[i].attr_list[j].attr_id,
- attr_name: attr_group_list[i].attr_list[j].attr_name,
- };
- break;
- }
- }
- if (!attr) {
- wx.showToast({
- title: "请选择" + attr_group_list[i].attr_group_name,
- image: "/images/icon-warning.png",
- });
- return true;
- } else {
- checked_attr_list.push({
- attr_group_id: attr_group_list[i].attr_group_id,
- attr_group_name: attr_group_list[i].attr_group_name,
- attr_id: attr.attr_id,
- attr_name: attr.attr_name,
- });
- }
- }
- if (type == 'ADD_CART') {//加入购物车
- wx.showLoading({
- title: "正在提交",
- mask: true,
- });
- app.request({
- url: api.cart.add_cart,
- method: "POST",
- data: {
- goods_id: page.data.id,
- attr: JSON.stringify(checked_attr_list),
- num: page.data.form.number,
- },
- success: function (res) {
- wx.showToast({
- title: res.msg,
- duration: 1500
- });
- page.getGoods();
- wx.hideLoading();
- page.setData({
- show_attr_picker: false,
- });
- }
- });
- }
- if (type == 'BUY_NOW') {//立即购买
- page.setData({
- show_attr_picker: false,
- });
- wx.redirectTo({
- url: "/pages/order-submit/order-submit?goods_info=" + JSON.stringify({
- goods_id: page.data.id,
- attr: checked_attr_list,
- num: page.data.form.number,
- }),
- });
- }
- },
- hideAttrPicker: function () {
- var page = this;
- page.setData({
- show_attr_picker: false,
- });
- },
- showAttrPicker: function () {
- var page = this;
- page.setData({
- show_attr_picker: true,
- });
- },
- attrClick: function (e) {
- var page = this;
- var attr_group_id = e.target.dataset.groupId;
- var attr_id = e.target.dataset.id;
- var attr_group_list = page.data.attr_group_list;
- for (var i in attr_group_list) {
- if (attr_group_list[i].attr_group_id != attr_group_id)
- continue;
- for (var j in attr_group_list[i].attr_list) {
- if (attr_group_list[i].attr_list[j].attr_id == attr_id) {
- attr_group_list[i].attr_list[j].checked = true;
- } else {
- attr_group_list[i].attr_list[j].checked = false;
- }
- }
- }
- page.setData({
- attr_group_list: attr_group_list,
- });
- var check_attr_list = [];
- var check_all = true;
- for (var i in attr_group_list) {
- var group_checked = false;
- for (var j in attr_group_list[i].attr_list) {
- if (attr_group_list[i].attr_list[j].checked) {
- check_attr_list.push(attr_group_list[i].attr_list[j].attr_id);
- group_checked = true;
- break;
- }
- }
- if (!group_checked) {
- check_all = false;
- break;
- }
- }
- if (!check_all)
- return;
- wx.showLoading({
- title: "正在加载",
- mask: true,
- });
- app.request({
- url: api.default.goods_attr_info,
- data: {
- goods_id: page.data.goods.id,
- attr_list: JSON.stringify(check_attr_list),
- },
- success: function (res) {
- wx.hideLoading();
- if (res.code == 0) {
- var goods = page.data.goods;
- goods.price = res.data.price;
- goods.num = res.data.num;
- goods.attr_pic = res.data.pic;
- page.setData({
- goods: goods,
- miaosha_data: res.data.miaosha,
- });
- }
- }
- });
- },
- favoriteAdd: function () {
- var page = this;
- app.request({
- url: api.user.favorite_add,
- method: "post",
- data: {
- goods_id: page.data.goods.id,
- },
- success: function (res) {
- if (res.code == 0) {
- var goods = page.data.goods;
- goods.is_favorite = 1;
- page.setData({
- goods: goods,
- });
- }
- }
- });
- },
- favoriteRemove: function () {
- var page = this;
- app.request({
- url: api.user.favorite_remove,
- method: "post",
- data: {
- goods_id: page.data.goods.id,
- },
- success: function (res) {
- if (res.code == 0) {
- var goods = page.data.goods;
- goods.is_favorite = 0;
- page.setData({
- goods: goods,
- });
- }
- }
- });
- },
- tabSwitch: function (e) {
- var page = this;
- var tab = e.currentTarget.dataset.tab;
- if (tab == "detail") {
- page.setData({
- tab_detail: "active",
- tab_comment: "",
- });
- } else {
- page.setData({
- tab_detail: "",
- tab_comment: "active",
- });
- }
- },
- commentPicView: function (e) {
- console.log(e);
- var page = this;
- var index = e.currentTarget.dataset.index;
- var pic_index = e.currentTarget.dataset.picIndex;
- wx.previewImage({
- current: page.data.comment_list[index].pic_list[pic_index],
- urls: page.data.comment_list[index].pic_list,
- });
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- var page = this;
- page.getCommentList(true);
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- var page = this;
- var user_info = wx.getStorageSync("user_info");
- var res = {
- path: "/pages/goods/goods?id=" + this.data.id + "&user_id=" + user_info.id,
- success: function (e) {
- console.log(e);
- share_count++;
- if (share_count == 1)
- app.shareSendCoupon(page);
- },
- title: page.data.goods.name,
- imageUrl: page.data.goods.pic_list[0].pic_url,
- };
- return res;
- },
- play: function (e) {
- var url = e.target.dataset.url;//获取视频链接
- this.setData({
- url: url,
- hide: '',
- show: true,
- });
- var videoContext = wx.createVideoContext('video');
- videoContext.play();
- },
- close: function (e) {
- if (e.target.id == 'video') {
- return true;
- }
- this.setData({
- hide: "hide",
- show: false
- });
- var videoContext = wx.createVideoContext('video');
- videoContext.pause();
- },
- hide: function (e) {
- if (e.detail.current == 0) {
- this.setData({
- img_hide: ""
- });
- } else {
- this.setData({
- img_hide: "hide"
- });
- }
- },
- showShareModal: function () {
- var page = this;
- page.setData({
- share_modal_active: "active",
- no_scroll: true,
- });
- },
- shareModalClose: function () {
- var page = this;
- page.setData({
- share_modal_active: "",
- no_scroll: false,
- });
- },
- getGoodsQrcode: function () {
- var page = this;
- page.setData({
- goods_qrcode_active: "active",
- share_modal_active: "",
- });
- if (page.data.goods_qrcode)
- return true;
- app.request({
- url: api.default.goods_qrcode,
- data: {
- goods_id: page.data.id,
- },
- success: function (res) {
- if (res.code == 0) {
- page.setData({
- goods_qrcode: res.data.pic_url,
- });
- }
- if (res.code == 1) {
- page.goodsQrcodeClose();
- wx.showModal({
- title: "提示",
- content: res.msg,
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- }
- }
- });
- }
- },
- });
- },
- goodsQrcodeClose: function () {
- var page = this;
- page.setData({
- goods_qrcode_active: "",
- no_scroll: false,
- });
- },
- saveGoodsQrcode: function () {
- var page = this;
- if (!wx.saveImageToPhotosAlbum) {
- // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
- wx.showModal({
- title: '提示',
- content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
- showCancel: false,
- });
- return;
- }
- wx.showLoading({
- title: "正在保存图片",
- mask: false,
- });
- wx.downloadFile({
- url: page.data.goods_qrcode,
- success: function (e) {
- wx.showLoading({
- title: "正在保存图片",
- mask: false,
- });
- wx.saveImageToPhotosAlbum({
- filePath: e.tempFilePath,
- success: function () {
- wx.showModal({
- title: '提示',
- content: '商品海报保存成功',
- showCancel: false,
- });
- },
- fail: function (e) {
- wx.showModal({
- title: '图片保存失败',
- content: e.errMsg,
- showCancel: false,
- });
- },
- complete: function (e) {
- console.log(e);
- wx.hideLoading();
- }
- });
- },
- fail: function (e) {
- wx.showModal({
- title: '图片下载失败',
- content: e.errMsg + ";" + page.data.goods_qrcode,
- showCancel: false,
- });
- },
- complete: function (e) {
- console.log(e);
- wx.hideLoading();
- }
- });
- },
- goodsQrcodeClick: function (e) {
- var src = e.currentTarget.dataset.src;
- wx.previewImage({
- urls: [src],
- });
- },
- closeCouponBox: function (e) {
- this.setData({
- get_coupon_list: ""
- });
- },
- setMiaoshaTimeOver: function () {
- var page = this;
- function _init() {
- var time_over = page.data.goods.miaosha.end_time - page.data.goods.miaosha.now_time;
- time_over = time_over < 0 ? 0 : time_over;
- page.data.goods.miaosha.now_time++;
- page.setData({
- goods: page.data.goods,
- miaosha_end_time_over: secondToTime(time_over),
- });
- }
- _init();
- setInterval(function () {
- _init();
- }, 1000);
- function secondToTime(second) {
- var _h = parseInt(second / 3600);
- var _m = parseInt((second % 3600) / 60);
- var _s = second % 60;
- return {
- h: _h < 10 ? ("0" + _h) : ("" + _h),
- m: _m < 10 ? ("0" + _m) : ("" + _m),
- s: _s < 10 ? ("0" + _s) : ("" + _s),
- };
- }
- },
- to_dial: function (e) {
- var contact_tel = this.data.store.contact_tel;
- wx.makePhoneCall({
- phoneNumber: contact_tel
- })
- },
- });
|