const util = require('../../utils/util.js'); const api = require('../../config/api.js'); const user = require('../../utils/user.js'); const map = require('../../utils/map.js'); const area = require('../../utils/area.js'); //获取应用实例 const app = getApp(); Page({ data: { newGoods: [], hotGoods: [], topics: [], brands: [], groupons: [], floorGoods: [], banner: [], channel: [], coupon: [], goodsCount: 0, hasLogin: true, city: '', //当前业务城市 citys: [], cityIndex: 0, addressInfo: { city: '上海市', shopId: 0, lng: 121.4737, lat: 31.23037, address: "上海市浦东新区祖冲之路1505弄100号3栋", shopName: '', }, showModalStatus: false, couponsData: [], randomNum: (Math.random() * 100000000).toFixed(0) }, onShareAppMessage: function () { return { title: '保洁擦窗 家电清洗 专区收纳', desc: '鲸致生活,为美好生活', path: '/pages/index/index' } }, onPullDownRefresh() { wx.showNavigationBarLoading() //在标题栏中显示加载 this.getIndexData(); wx.hideNavigationBarLoading() //完成停止加载 wx.stopPullDownRefresh() //停止下拉刷新 }, getIndexData: function () { //新人登陆优惠券发放判断 !app.globalData.hasLogin ||,需要获取地址后再拿券 if (wx.getStorageSync('userInfo').id == null) { console.log('未登陆进入' + app.globalData.hasLogin + "----" + wx.getStorageSync('userInfo').id); this.youhuiquan(); } wx.showLoading({ title: '加载中', }); let that = this; console.info(that.data.addressInfo.shopId) util.request(api.IndexUrl, { city: that.data.addressInfo.city, //shopId: that.data.addressInfo.shopId }).then(function (res) { if (res.errno === 0) { res.data.hotGoodsList.forEach(h => { if (h.gallery != null) { h.gallery = h.gallery.split(","); } }) that.setData({ // newGoods: res.data.newGoodsList, hotGoods: res.data.hotGoodsList, // topics: res.data.topicList, //brands: res.data.brandList, //floorGoods: res.data.floorGoodsList, banner: res.data.banner, //groupons: res.data.grouponList, channel: res.data.channel, //coupon: res.data.couponList }); //console.info(res.data.banner); } wx.hideLoading(); }); // util.request(api.GoodsCount).then(function (res) { // that.setData({ // goodsCount: res.data // }); // }); }, youhuiquan() { //进入小程序优惠券展示 let that = this; util.request(api.CouponRegister, { city: that.data.addressInfo.city }).then(function (res) { //console.info(res.data); if (res.errno === 0 && res.data.length > 0 && wx.getStorageSync('userInfo').nickName == null) { that.setData({ showModalStatus: true, couponsData: res.data }) } }) }, onLoad: function (options) { let that = this; // 页面初始化 options为页面跳转所带来的参数 if (options.rId) { // wx.setStorageSync('r_userid', options.rId); app.globalData.rId = options.rId; } if (options.ch) { wx.setStorageSync('channelId', options.ch); } if (options.goodsId) { console.info(options.goodsId) wx.navigateTo({ url: '../goods/goods?id=' + options.goodsId }); return; } var shareId; if (options.scene) { //这个scene的值存在则证明首页的开启来源于朋友圈分享的图,同时可以通过获取到的goodId的值跳转导航到对应的详情页 var scene = decodeURIComponent(options.scene); console.log(scene); //console.info(scene); sId:分享id //goods,1181009;rId,10;sId:0 //g,1181009;rId,10;sId:0 因为超长 缩写了goods //三种格式(1) rId,2 (2)g,3;rId,2 (3)g,3;rId,3;sId:10(阿姨开单分享) // 第四种 新增fid,2 或 fid,2;gid,3(标识加盟商的id) 商品id // 第五种 新增rId,2(修改加盟商的阿姨开单) //第六种 ch,2(渠道二维码 下单order_channel归属) //shopId=1031; if (scene.indexOf("rId") >= 0) { //阿姨开单 let sceneStr = scene.split(','); wx.setStorageSync('rId', sceneStr[1]); } else if (scene.indexOf("ch") >= 0) { //下单渠道 let sceneStr = scene.split(','); wx.setStorageSync('channelId', sceneStr[1]); } else if (scene.indexOf("fid") >= 0) { //加盟商id if (scene.indexOf("gid") < 0) { //没有商品id let sceneStr = scene.split(','); wx.setStorageSync('fraId', sceneStr[1]); } else { let fraId = scene.split(';')[0].split(",")[1]; wx.setStorageSync('fraId', fraId); console.info(fraId); let goods = scene.split(';')[1].split(","); wx.navigateTo({ url: '../goods/goods?id=' + goods[1] }); } } else if (scene.indexOf("shopId") >= 0) { let sceneStr = scene.split('='); let shopId = sceneStr[1]; console.log('scene:shopId=' + shopId); // 通过shopId获取地址信息 app.globalData.shopId = shopId; util.request(api.OrderBookGetShop, { shopId: shopId }).then(res => { if (res.errno == 0) { console.log(res); var addressInfo = { address: res.data.address, lng: res.data.longitude, lat: res.data.latitude, shopId: res.data.shopId, city: res.data.city, shopName: res.data.shopName } wx.setStorageSync('addressInfo', addressInfo); console.log(addressInfo); //that.getLocation(); } }).catch(res => { }); } else { if (scene.indexOf("rId") >= 0 || scene.indexOf("r_userid") >= 0) { //兼容之前的r_userid let value = null; if (scene.indexOf(";") > 0) { //包含分号 表示是good和user两种 value = scene.split(";")[1]; //console.info(value); wx.setStorageSync('r_userid', value.split(",")[1]); //获取r_userid app.globalData.rId = value.split(",")[1]; if (scene.indexOf("sId") >= 0 || scene.indexOf("shareId") >= 0) { //阿姨开单分享id ||兼容之前的shareId shareId = scene.split(";")[2].split(",")[1]; } scene = scene.split(";")[0]; //console.info(scene); } } let info_arr = []; info_arr = scene.split(','); let _type = info_arr[0]; let id = info_arr[1]; console.info(_type); if (_type == 'goods' || _type == 'g') { if (shareId) { wx.navigateTo({ url: '../goods/goods?id=' + id + '&shareId=' + shareId }); } else { let rId = wx.getStorageSync('r_userid'); if (!rId) rId = 0; wx.navigateTo({ url: '../goods/goods?id=' + id + '&rId=' + rId }); } } else if (_type == 'groupon') { wx.navigateTo({ url: '../goods/goods?grouponId=' + id }); } else { wx.navigateTo({ url: '../index/index' }); } } } // 页面初始化 options为页面跳转所带来的参数 if (options.grouponId) { //这个pageId的值存在则证明首页的开启来源于用户点击来首页,同时可以通过获取到的pageId的值跳转导航到对应的详情页 wx.navigateTo({ url: '../goods/goods?grouponId=' + options.grouponId }); } // 页面初始化 options为页面跳转所带来的参数 if (options.goodId) { //这个goodId的值存在则证明首页的开启来源于分享,同时可以通过获取到的goodId的值跳转导航到对应的详情页 if (options.share_id) wx.navigateTo({ url: '../goods/goods?id=' + options.goodId + '&share_id=' + options.share_id }); else wx.navigateTo({ url: '../goods/goods?id=' + options.goodId }); } // 页面初始化 options为页面跳转所带来的参数 if (options.orderId) { //这个orderId的值存在则证明首页的开启来源于订单模版通知,同时可以通过获取到的pageId的值跳转导航到对应的详情页 wx.navigateTo({ url: '../ucenter/orderDetail/orderDetail?id=' + options.orderId }); } }, getLocation() { let that = this //获取地理位置 缓存中是否有地址 /** * 分2重判断 * 1:缓存中是否有地址 * 2:未登录 取当前地理位置判断 */ var addressInfo = wx.getStorageSync("addressInfo"); if (addressInfo) { console.info(1); console.info(addressInfo.shopId == null); that.setData({ addressInfo: addressInfo }) this.getIndexData(); } else { if (!app.globalData.hasLogin) { //未登陆 map.getCity().then(res => { console.log("getcity啊啊啊啊啊啊啊啊啊啊" + res); that.setData({ addressInfo: res }) console.info(res) this.getIndexData(); }).catch(res => { //reject内容 1:未授权 2:不在服务范围 3:异常解析 console.log(res); wx.showModal({ title: '提醒', showCancel: false, content: '您未授权或非业务城市,已切换成默认【上海市】' }) // if (res == 1 || res == 2) { this.getdefaultCityShop(); // } //路由到上海市 latitude: 31.23037 longitude: 121.4737 }); } } }, getdefaultCityShop() { let that = this; util.request(api.getDefaultCityShop, {}, 'get').then(res => { if (res.errno === 0) { that.setData({ addressInfo: res.data }) wx.setStorageSync('addressInfo', res.data); that.getIndexData(); } else { util.showErrorToast(res.errmsg); } }) }, goCheckAddress() { user.checkLogin().then(res => { wx.navigateTo({ url: '/pages/ucenter/address/address', }) }).catch(res => { wx.navigateTo({ url: '/pages/auth/login/login', }) }) }, onReady: function () { // 页面渲染完成 }, onShow: function () { //this.naviToMiniProgram(); // if (!app.globalData.shopId) { // this.getLocation(); // } let addressInfo = { city: '上海市', shopId: 0, lng: 121.4737, lat: 31.23037, address: "上海市浦东新区祖冲之路1505弄100号3栋", shopName: '', }; wx.setStorageSync('addressInfo', addressInfo); this.getIndexData(); }, naviToMiniProgram() { let that = this; wx.showModal({ title: '温馨提示', content: '尊敬的客户,鲸致生活小程序已迁移到【鲸致生活lifeplus】,您的相关权益也全部迁移保留,请点击确认 使用新小程序。', showCancel: false, //是否显示取消按钮 cancelText: "取消", //默认是“取消” cancelColor: '#000000', //取消文字的颜色 confirmText: "确认跳转", //默认是“确定” confirmColor: '#3cc51f', //确定文字的颜色 success: function (res) { if (res.cancel) { //点击取消,wx.navigateBack } else { // 使用wx.navigateToMiniProgram跳转到小程序 wx.navigateToMiniProgram({ appId: 'wxf0a77f9993d8b60d', path: 'pages/index/index', envVersion: 'release', // 打开正式版 success(res) { // 打开成功 }, fail: function (err) { console.log(err); that.naviToMiniProgram(); } }) } }, fail: function (res) { //接口调用失败的回调函数,wx.navigateBack }, complete: function (res) { console.info(res) //接口调用结束的回调函数(调用成功、失败都会执行) }, }) }, goLogin: function (e) { if (e.detail.userInfo == null) { app.globalData.hasLogin = false; util.showErrorToast('微信授权失败'); return; } app.globalData.hasLogin = false; wx.setStorageSync('userInfo', null); user.loginByWeixin(e.detail.userInfo).then(res => { //跳转注册页 if (user.checkRegisted() == false) { wx.navigateTo({ url: "/pages/auth/register/register" }); } else { this.setData({ hasLogin: true }) } }).catch((err) => { app.globalData.hasLogin = false; }); }, onHide: function () { // 页面隐藏 }, onUnload: function () { // 页面关闭 }, getCoupon(e) { if (!app.globalData.hasLogin) { wx.navigateTo({ url: "/pages/auth/login/login" }); } let couponId = e.currentTarget.dataset.index util.request(api.CouponReceive, { couponId: couponId }, 'POST').then(res => { if (res.errno === 0) { wx.showToast({ title: "领取成功" }) } else { util.showErrorToast(res.errmsg); } }) }, goinviteFriends(e) { console.log(app.globalData.hasLogin); //跳转到邀请页面 if (app.globalData.hasLogin) { wx.navigateTo({ url: '/pages/activity/invitefriends/inviteFriends' }) } else { wx.navigateTo({ url: "/pages/auth/login/login" }); }; }, guanbicoupon() { this.setData({ showModalStatus: false }) }, gocoupon(e) { this.setData({ showModalStatus: false }) if (!app.globalData.hasLogin) { wx.navigateTo({ url: "/pages/auth/login/login" }); }; }, myshowtost: function (v) { wx.showToast({ title: v, icon: 'none', duration: 3000 }) }, goCatalog() { wx.navigateTo({ url: '/pages/tabBar/catalog/catalog', }) }, goHotGoods() { wx.navigateTo({ url: '/pages/goods/hotGoods/hotGoods', }) }, callPhone: function () { wx.makePhoneCall({ phoneNumber: '4009218987', }) }, goJoin() { wx.navigateTo({ url: "/pages/ucenter/wx/join" }); // if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) { // console.log(2222); // util.request(api.AuthInfo, {}, "GET") // .then(res => { // if (res.errno == 0) { // let mobile=res.data.mobile; // let randomBoolean = Math.random() < 0.5; // let cp=randomBoolean==true?"13127931250" : "13122656821"; // wx.navigateTo({ // url: '/pages/ucenter/wx/join?mobile='+mobile+'&cp='+cp // }) // // url: '/pages/ucenter/wx/join?mobile='+mobile // // url: '/pages/ucenter/wx/join?mobile='+mobile+'&cp='+cp, // } // }) // }else{ // console.log(1111); // wx.navigateTo({ // url: "/pages/ucenter/wx/join?cp=13122656821" // }); // } }, tbb(e) { let nm = e.target.dataset.name; if (nm.indexOf('花王') != -1) { wx.navigateToMiniProgram({ appId: 'wxe185311df7c1c235', path: 'pages/index/index', extraData: { // 需要传递的数据 }, envVersion: 'develop', // 开发版、体验版和正式版,默认正式版 success(res) { // 打开成功 } }) return; } wx.navigateTo({ url: e.target.dataset.url, }) } })