const Http = require("../../utils/HttpBasics"); const imgurl = require("../../utils/imgurl"); const config = require("../../config/config"); const QR = require("../../utils/memberqrcode.js"); let app = getApp(); Page({ data: { newUrl: imgurl.new.url, icon001: imgurl.icon001.url, icon002: imgurl.icon002.url, icon003: imgurl.icon003.url, icon004: imgurl.icon004.url, market: app.globalData.market, list: [], loading: true, swiperCurrent: 0, title: null, desc: null, scrollTop: 0, showGame: false, gamedata: {}, couponId: '',//游戏返回时传回的字段 played: false,//从游戏页面跳回首页返回true havePlayEd: app.globalData.havePlayEd, staticGamedata: {}, showIf: false, discountEnable:false, page: 1 // 刷新进入页面时已经加载了第一页数据,onReachBottom时 page++,从第2页开始加载 }, gotodiscountCardList:function(){ wx.navigateTo({ url: '/pages/discountCardList/discountCardList' }) }, gotoRushBuy:function(){ wx.navigateTo({ url: '/pages/rushToBuy/index', }) }, showSpecial:function(){ let that = this; Http.get({ url: config.api.scoreLevelInfo, data: {} }) .then(res => { // if true 则开启特享礼遇 if (res.data.discountEnable) { that.setData({ discountEnable: true }) } else { that.setData({ discountEnable: false }) } }) }, qrcode: function (e) { var that = this; that.setData({ showQrcode: true }) wx.showToast({ title: '生成中...', icon: 'loading', duration: 2000 }); console.log(e) var st = setTimeout(function () { wx.hideToast() var size = that.setCanvasSize(); var url = JSON.stringify({ flagid: that.data.memberId }); that.createQrCode(url, "mycanvas2", size.w, size.h); that.setData({ maskHidden: true }); clearTimeout(st); }, 1000) }, hideQrcode: function () { let that = this; that.setData({ showQrcode: false }) }, //适配不同屏幕大小的canvas setCanvasSize: function () { var size = {}; try { var res = wx.getSystemInfoSync(); var scale = 750 / 500; //不同屏幕下canvas的适配比例;设计稿是750宽 var width = res.windowWidth / scale; var height = width; //canvas画布为正方形 size.w = width; size.h = height; } catch (e) { // Do something when catch error console.log("获取设备信息失败" + e); } return size; }, createQrCode: function (url, canvasId, cavW, cavH) { //调用插件中的draw方法,绘制二维码图片 QR.api.draw(url, canvasId, cavW, cavH); }, swiperChange: function (e) { this.setData({ swiperCurrent: e.detail.current }); }, gotogame: function () { let that = this; Http.post({ url: config.api.checkPhoneStatus, data: {} }) .then(res => { var data = { couponChannelId: "" + that.data.couponChannelId, couponId: "" + that.data.couponId }; if (that.data.couponChannelId == null) { var data = { couponId: "" + that.data.couponId }; } wx.navigateTo({ url: '/pages/game/index?url=' + that.data.staticGamedata.url + "&id=" + that.data.staticGamedata.id + "&gameId=" + that.data.staticGamedata.gameId, }) }) .catch(err => { if (err.code == 11005) { // 用户手机未授权 /** * 将值传到用户手机号授权的页面 * */ wx.redirectTo({ url: "/pages/getphoneInfo/index?path=index&url=" + that.data.staticGamedata.url + "&id=" + that.data.staticGamedata.id + "&gameId=" + that.data.staticGamedata.gameId, }); } else if (err.code == 11006) { // 用户手机已加密 wx.redirectTo({ url: "/pages/phoneinput/phoneinput?path=index&url=" + that.data.staticGamedata.url + "&id=" + that.data.staticGamedata.id + "&gameId=" + that.data.staticGamedata.gameId, }); } else { wx.showToast({ title: err.message, icon: 'none', duration: 2000, mask: false }); } }) }, mySpecial:function(){ console.log("special"); wx.navigateTo({ url: '/pages/specialcourtesy/specialcourtesy', }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onLoad: function (options) { wx.setStorageSync('imgurl', imgurl) if (options.played == "true") { this.setData({ played: true }) } var that = this; if (decodeURIComponent(options.scene) == "undefined") { that.setData({ scene: 0 }); } else { that.setData({ scene: decodeURIComponent(options.scene) }); } app.getLocation(); if (options.couponChannelId && options.couponId||options.orderId) { that.userLogin(options.couponChannelId, options.couponId,options.orderId); } else { that.userLogin() } Http.get({ url: config.api.getWeapNote, data: { appId: config.weapp.AppId, } }) .then(res => { let weapNote = JSON.parse(res.data.weapNote); that.setData({ desc: weapNote.firstpage.desc, title: weapNote.firstpage.title }) }) .catch(err => { wx.showModal({ title: "提示", content:err.errMsg, showCancel:false }); }); }, onShow: function () { this.setData({ havePlayEd: app.globalData.havePlayEd ? app.globalData.havePlayEd : false }) let num = wx.getStorageSync('couponNum'); if (num == 'couponNum') { wx.showTabBarRedDot({ index: 2 }) }; }, // 会员码获取 getmemberId: function (token){ let that = this; Http.get({ url: config.api.getScore, data: { token: token } }).then(res => { console.log(res) that.setData({ levelName: res.data.levelName }) if (res.data.nickName) { that.setData({ ismember: true, memberId: res.data.id }) var size = this.setCanvasSize(); var initUrl = JSON.stringify({ flagid: res.data.id }); that.createQrCode(initUrl, "mycanvas1", size.w, size.h); } }) .catch(err => { wx.showModal({ title: '提示', content: err.message, showCancel: false }) }) }, /** * * @param {code,page} * 子组件向父组件传值 */ onGetCode: function (e) { this.setData({ code: e.detail.val, page: e.detail.pageNum, }); console.log(e.detail.val) console.log(e.detail.pageNum) }, /** * 用户登录 */ userLogin: function (couponChannelId, couponId,orderId) { var that = this; // 登录 wx.login({ success: ({ code }) => { wx.getSystemInfo({ success: function (res) { that.setData({ systemInfo: JSON.stringify(res) }) } }) var usrdata = { appId: config.weapp.AppId, code: code, sceneAddress: app.globalData.sceneAddress, scene: that.data.scene, systemInfo: that.data.systemInfo }; if (app.globalData.locationInfo) { usrdata = { appId: config.weapp.AppId, code: code, sceneAddress: app.globalData.sceneAddress, latitude: "" + app.globalData.locationInfo.latitude, longitude: "" + app.globalData.locationInfo.longitude, scene: that.data.scene, systemInfo: that.data.systemInfo }; } Http.post({ url: config.api.login, data: usrdata }) .then(res => { app.globalData.token = res.data.token; if (res.data.token){ that.getGameOne(res.data.token) that.getStaticGame(res.data.token) that.getmemberId(res.data.token); } Http.setToken(res.data.token); that.checkUserCarStatus(); that.showSpecial(); that.getUserInfo(); that.getBannerlist(); if (app.couponChannelListCallback) { app.couponChannelListCallback(app.globalData.token); } if (app.couponListCallback) { app.couponListCallback(app.globalData.token); } if (app.businessListCallback) { app.businessListCallback(app.globalData.token); } that.checkuerstatus(couponChannelId, couponId,orderId); }) .catch(err => { wx.showToast({ title: err.message, icon:"none" }) }); } }); }, checkuerstatus(couponChannelId, couponId,orderId) { Http.post({ url: config.api.checkUserStatus, data: {} }) .then(res => { if (couponChannelId && couponId) { wx.navigateTo({ url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`, }) }else if(orderId){ wx.navigateTo({ url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${orderId}&from='${"discount"}`, }) } }) .catch(err => { if (err.code == 11004) { // 用户昵称未授权 if (couponChannelId && couponId) { wx.redirectTo({ url: `/pages/getuserinfo/index?couponChannelId=${couponChannelId}&couponId=${couponId}` }); } else if (orderId) { wx.redirectTo({ url: `/pages/getuserinfo/index?orderId=${orderId}&from='${"discount"}` }); } else { wx.redirectTo({ url: '/pages/getuserinfo/index' }); } } }); }, getStaticGame(token) { let _this = this; Http.get({ url: config.api.getGame, data: { triggleAction: 2, token: token } }).then(res => { if (res.data.id) { _this.setData({ showIf:true }) } _this.setData({ staticGamedata: res.data }) }) .catch(err => { console.log(err) }) }, getGameOne: function (token) { let _this = this; Http.get({ url: config.api.getGame, data:{ triggleAction: 1, token: token } }).then(res => { if (res.data.id) { _this.setData({ showGame: true }) } _this.setData({ gamedata: res.data }) }) .catch(err => { console.log(err) }) }, /** * banner */ getBannerlist: function () { let that = this; Http.get({ url: config.api.bannerlist, data: { pageNum: 1, pageSize: 7 } }).then(res => { that.setData({ list: res.data.list }); }); }, /** * 检查用户是否有车 */ checkUserCarStatus: function () { var that = this; Http.get({ url: config.api.userCarCount, data: {} }).then(res => { if (res.data > 0) { // 用户名下有车 app.globalData.phone = res.data.phone; app.globalData.supportCar = true; // 共同登录 that.userCarLogin(); } }); }, /** * car共同登录 */ userCarLogin: function () { var that = this; if (!app.globalData.carLogin) { // 共同登录 Http.post({ url: config.api.carInit, data: { phone: app.globalData.phone } }).then(res => { app.globalData.carLogin = true; app.globalData.parkVendor = res.data.vendor; if (res.data.token != "undefined") { app.globalData.etcpToken = res.data.token; } }); } }, /** * 获取用户信息 */ getUserInfo: function () { // 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting["scope.userInfo"]) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId } }); } } }); }, gotoBargain:function(){ wx.navigateTo({ url: '/pages/bargain/bargain' }) }, /** * 刷新 */ onPullDownRefresh: function (e) { let that = this; that.userLogin(); that.setData({ page:1 }) if (that.data.code == 0 || that.data.code == undefined) { that.selectComponent("#lists").getList(0, 1, "refresh"); wx.stopPullDownRefresh(); } else { that.selectComponent("#lists").getList(that.data.code, 1, "refresh"); wx.stopPullDownRefresh(); }; }, //加载更多 onReachBottom: function () { let that = this; that.data.page++; that.setData({ page: that.data.page }); console.log("加载更多页数"+that.data.page); console.log("加载更多key"+that.data.code); //父组件获得子组件的方法 //如果code == 0 if (that.data.code == 0 || that.data.code == undefined) { that.selectComponent("#lists").getList(0, that.data.page); } else { that.selectComponent("#lists").getList(that.data.code, that.data.page); } }, // 用户点击右上角分享 onShareAppMessage: function () { return { title: this.data.title, desc: this.data.desc, success: function (res) { wx.showToast({ title: "分享成功", duration: 1000, icon: "success" }); } }; } });