|
- const Http = require("../../utils/HttpBasics");
- const config = require("../../config/config");
- let app = getApp();
- Page({
- data: {
- market: app.globalData.market,
- list: [],
- swiperCurrent: 0,
- scrollTop: 0,
- page: 1 // 刷新进入页面时已经加载了第一页数据,onReachBottom时 page++,从第2页开始加载
- },
- swiperChange: function (e) {
- this.setData({
- swiperCurrent: e.detail.current
- });
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onLoad: function (options) {
- var that = this;
- var scene = decodeURIComponent(options.scene);
- app.getLocation();
- },
- /**
- * 下拉刷新
- */
- onPullDownRefresh: function (e) {
- let that = this;
- that.userLogin();
- console.log(this.data.code);
- if (that.data.code == 0 || that.data.code == undefined) {
- that.selectComponent("#lists").getList(0, 1);
- wx.stopPullDownRefresh();
- console.log("下拉刷新");
- } else {
- that.selectComponent("#lists").getList(that.data.code, 1);
- wx.stopPullDownRefresh();
- };
- },
- onShow: function () {
- this.userLogin();
- },
-
- onGetCode: function (e) {
- //子组件传递给父组件的值
- this.setData({
- code: e.detail.val,
- page: e.detail.pageNum
- });
- },
-
- /**
- * 用户登录
- */
- userLogin: function () {
- var that = this;
- // 登录
- wx.login({
- success: ({
- code
- }) => {
- var usrdata = {
- appId: config.weapp.AppId,
- code: code,
- sceneAddress: app.globalData.sceneAddress
- };
- 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
- };
- }
- Http.post({
- url: config.api.login,
- data: usrdata
- })
- .then(res => {
- console.log("userlogin:app", res);
- app.globalData.token = res.data.token;
- Http.setToken(res.data.token);
- that.checkUserCarStatus();
- 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);
- }
- return Http.post({
- url: config.api.checkUserStatus,
- data: {}
- });
- })
- .then(res => {
- console.log("checkUserStatus:res", res);
- })
- .catch(err => {
- console.log("checkUserStatus:err", err);
- if (err.code == 11004) {
- // 用户昵称未授权
- wx.redirectTo({
- url: "/pages/getuserinfo/index"
- });
- }
- });
- }
- });
- },
-
- /**
- * banner
- */
- getBannerlist: function () {
- let that = this;
- Http.get({
- url: config.api.bannerlist,
- data: {
- pageNum: 1,
- pageSize: 7
- }
- }).then(res => {
- console.log(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;
- console.log("etcpToken", app.globalData.etcpToken);
- }
- });
- }
- },
- /**
- * 获取用户信息
- */
- getUserInfo: function () {
- // 获取用户信息
- wx.getSetting({
- success: res => {
- console.log("getSetting", res);
- if (res.authSetting["scope.userInfo"]) {
- // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
- wx.getUserInfo({
- success: res => {
- // 可以将 res 发送给后台解码出 unionId
- console.log("getUserInfo", res);
- }
- });
- }
- }
- });
- },
- //下拉加载更多
- onReachBottom: function () {
- let that = this;
- that.data.page++;
- that.setData({
- page: that.data.page
- });
- console.log("我是第"+that.data.page+"几页")
- //父组件获得子组件的方法
- //如果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: "富茂链客",
- desc: "分享个小程序,希望你喜欢",
- success: function (res) {
- wx.showToast({
- title: "分享成功",
- duration: 1000,
- icon: "success"
- });
- }
- };
- }
- });
|