123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- const util = require('../../utils/util.js');
- const api = require('../../config/api.js');
- const user = require('../../services/user.js');
- //获取应用实例
- const app = getApp();
- Page({
- data: {
- groupGoods: [],
- hotGoods: [],
- topics: [],
- brands: [],
- floorGoods: [],
- banner: [],
- channel: [],
- groupBanner: {},
- storeName: '',
- showPop: false,//活动弹窗
- couponVo: {},
- storeId: ''
- },
- showCouponPop() {
- let that = this;
- this.setData({
- showPop: false
- });
- // wx.showToast({
- // title: '恭喜获取优惠券一张' + that.data.couponVo.name,
- // duration: 2000
- // });
- wx.showModal({
- title: '获取优惠券一张',
- showCancel: false,
- content: that.data.couponVo.name
- })
- },
- onShareAppMessage: function () {
- return {
- title: '商业版',
- desc: '新人好礼送券',
- path: '/pages/index/index'
- }
- },
- getIndexData: function () {
- let that = this;
- util.request(api.IndexUrl).then(function (res) {
- if (res.errno === 0) {
- // console.log(res.data.banner);
- that.setData({
- // newGoods: res.data.newGoodsList,
- hotGoods: res.data.hotGoodsList,
- // topics: res.data.topicList,
- // brand: res.data.brandList,
- // floorGoods: res.data.categoryList,
- banner: res.data.banner,
- // groupBanner: res.data.groupBanner,
- channel: res.data.channel
- });
- }
- });
- },
- getGroupData: function () {
- let that = this;
- util.request(api.GroupList).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- groupGoods: res.data.data,
- });
- }
- });
- },
- onLoad: function (options) {
- let that = this;
- wx.setStorageSync("navUrl", "/pages/index/index");
- },
- onReady: function () {
- // 页面渲染完成
- },
- onShow: function () {
- // 页面显示
- let that = this;
- wx.setStorageSync("navUrl", "/pages/index/index");
- if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
- that.syncStore();
- } else {
- wx.navigateTo({
- url: '/pages/auth/btnAuth/btnAuth',
- })
- }
- },
- onHide: function () {
- // 页面隐藏
- },
- onUnload: function () {
- // 页面关闭
- },
- handleStore() {
- wx.navigateTo({
- url: '../map/map',
- })
- },
- goSearch() {
- wx.navigateTo({
- url: '../search/search',
- })
- },
- goCatalog: function (e) {
- let url = '';
- // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
- app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
- // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
-
- wx.switchTab({
- url: '/pages/catalog/catalog',
- });
- },
- onReachBottom: function () {
- if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
- return false;
- }
- this.setData({
- bottomLoading: true
- });
- // this.getFloorCategory();
- },
- reLoad: function () {
- let that = this;
- if (wx.getStorageSync('storeId')) {
- // console.log(wx.getStorageSync('userId'));
- // console.log(wx.getStorageSync('storeId'));
- console.log(wx.getStorageSync('merchSn'));
- if (wx.getStorageSync('userId')){
- wx.request({
- url: api.updateLoginUser,
- data: {
- userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
- },
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- },
- success: function (wxRes) {
- if (wxRes.data.errno === 0) {
- console.log("用户信息更新成功");
- }
- },
- fail: function (err) {
- console.log("failed");
- }
- });
- }
- that.getIndexData();
- that.enableActivity();
- that.getGroupData();
- }
- },
- // 同步门店
- syncStore: function () {
- let that = this;
- if (!wx.getStorageSync('storeId')) {
- util.getLocation((lng, lat) => {
- wx.setStorageSync('location', JSON.stringify({ lng, lat }));
- util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
- let nlist = res.data;
- if (!nlist.length) {
- wx.removeStorageSync('nearStoreList');
- wx.removeStorageSync('storeId');
- wx.removeStorageSync('storeVo');
- that.setData({
- storeName: '附近暂无门店'
- })
- } else {
- that.setData({
- storeName: nlist[0].storeName,
- storeId: nlist[0].id
- })
- that.chooseStore(nlist[0].id, nlist[0].merchSn);
- wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
- wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
- }
- })
- });
- } else {
- var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
- that.chooseStore(storeVo.id,storeVo.merchSn);
- that.setData({
- storeName: storeVo.storeName,
- storeId: storeVo.id
- });
- }
- },
- // 更新门店Id
- chooseStore: function (storeId,merchSn) {
- let that = this;
- util.request(api.ChooseStoreId, { storeId: storeId, merchSn: merchSn }, 'POST').then(function (res) {
- if (res.errno === 0) {
- wx.setStorageSync('storeId', storeId);
- wx.setStorageSync('merchSn', merchSn);
- that.reLoad();
- }
- });
- },
- //购物车减少
- cutNumber: function (e) {
- let that = this;
- var goodsId = e.currentTarget.dataset.goodsId;
- var productId = e.currentTarget.dataset.productId;
- var hotGoods = that.data.hotGoods;
- // hotGoods.forEach(function (val, index, arr) {
- // if (val.product_id == productId) {
- // val.cart_num = val.cart_num - 1;
- // if (val.cart_num >= 0) {
- // hotGoods[index] = val;
- // }
- // }
- // });
- // that.setData({ hotGoods: hotGoods });
- util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
- if (res.errno === 0 && null != res.data) {
- var hotGoods = that.data.hotGoods;
- hotGoods.forEach(function (val, index, arr) {
- if (val.product_id == productId) {
- val.cart_num = res.data;
- hotGoods[index] = val;
- that.setData({ hotGoods: hotGoods });
- }
- }, that);
- }
- });
- },
- //购物车增加
- addNumber: function (e) {
- let that = this;
- var goodsId = e.currentTarget.dataset.goodsId;
- var productId = e.currentTarget.dataset.productId;
- var hotGoods = that.data.hotGoods;
- // hotGoods.forEach(function (val, index, arr) {
- // if (val.product_id == productId) {
- // val.cart_num = val.cart_num + 1;
- // hotGoods[index] = val;
- // }
- // });
- // that.setData({ hotGoods: hotGoods });
- util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
- if (res.errno === 0 && null != res.data) {
- hotGoods.forEach(function (val, index, arr) {
- res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
- if (val.product_id == cartVal.product_id) {
- val.cart_num = cartVal.number;
- hotGoods[index] = val;
- }
- });
- that.setData({ hotGoods: hotGoods });
- }, that);
- } else {
- wx.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- });
- },
- // 查询是否有活动
- enableActivity: function () {
- let that = this;
- let couponIds = wx.getStorageSync('couponIds');
- if (!couponIds) {
- couponIds = new Array();
- }
- util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
- // if (res.errno === 0 && null != res.data.showCoupon) {
- // if (couponIds.contains(res.data.showCoupon.id)) {
- // return;
- // }
- // couponIds.push(res.data.showCoupon.id);
- // wx.setStorageSync('couponIds', couponIds);
- // that.setData({
- // couponVo: res.data.showCoupon,
- // showPop: true
- // });
- // } else
- if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
- that.setData({
- couponVo: res.data.takeCoupon,
- showPop: true
- });
- }
- });
- },
- // 商品扫码
- scanGoodsCode: function (e) {
- var that = this;
- var code;
- var value;
- var substrValue;
- var scanType;
- // 调起客户端扫码界面进行扫码
- wx.scanCode({
- // 是否只能从相机扫码
- onlyFromCamera: true,
- // 扫码类型, barCode:一维码, qrCode:二维码
- scanType: ['barCode', 'qrCode'],
- success: function (res) {
- that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
- that.value = res.result;
- that.scanType = res.scanType;
- that.setData({
- goodsCode: that.code
- });
- if (that.scanType == 'QR_CODE') {//二维码
- that.substrValue = that.value.substring(0, 5);
- that.value = that.value.substring(5, that.value.length);
- var goodId = that.value.substring(18, that.value.length);
- if (that.substrValue != 'emato') {//../goods/goods?id=
- wx.showModal({
- title: '',
- content: '您所扫描的商品无效',
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- console.log('用户点击确定')
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- });
- } else {
- util.request(api.GoodsDetail, { id: goodId, referrer: '' }).then(function (res) {
- if (res.errno === 0) {
- // 跳转页面
- wx.navigateTo({
- url: that.value,
- success: function (e) {
- console.log('跳转成功');
- },
- fail: function (e) {
- console.log('跳转失败');
- }
- })
- } else {
- wx.showModal({
- title: '扫描结果',
- content: '商品不存在',
- showCancel: false
- });
- }
- });
- }
- }else{//其他码
- //弹框显示结果
- wx.showModal({
- title: '扫描结果',
- content: that.value,
- showCancel: false
- });
- }
- },
- fail: function () {
- // 显示提示框
- wx.showToast({
- title: '扫码失败',
- icon: 'none',
- // 提示的延迟时间
- duration: 3000
- })
- }
- })
- }
- })
|