Newer
Older
miniCreditFactory / pages / index / index.js
bello on 10 Jun 2020 6 KB login
//index.js
//获取应用实例
const app = getApp()

const test = require('../../utils/testData.js')

var U = require('../../utils/Urls.js')
var Api = require('../../utils/api.js')


Page({
    data: {
        bingMob: false,
        listEmpty: false,
        openId: '',
        pageNo: 1,
        pageSize: 10,
        orderList: [],
    },

    onLoad: function() {
        wx.showLoading({
            title: 'loading...',
        })

        var that = this
        // 获取wx系统参数
        wx.getSystemInfo({
            success: function (res) {
                console.log(res)
                app.globalData.wxBrand = res.windowHeight
                app.globalData.wxModel = res.model
                app.globalData.wxPixelRatio = res.pixelRatio
                app.globalData.wxScreenWidth = res.screenWidth
                app.globalData.wxScreenHeight = res.screenHeight
                app.globalData.wxWindowWidth = res.windowWidth
                app.globalData.wxWindowHeight = res.windowHeight
                app.globalData.wxStatusBarHeight = res.statusBarHeight
                app.globalData.wxVersion = res.version
                app.globalData.wxSystem = res.system
                app.globalData.wxPlatform = res.platform
            },
        })

        // this.onPullDownRefresh();

        // this.setData({
        //     // 设置模拟数据
        //     orderList: test.testList.rows
        // })

        this.login();
        
    },

    

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function(e) {
        var that = this
        
        if(!app.globalData.uid){
            return
        }

        wx.stopPullDownRefresh({
            success: (res) => {
                that.setData({
                    pageNo: 1
                })
                this.getList()
            }
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        // this.setData({
        //     pageNo: this.data.pageNo + 1
        // })

        if (!app.globalData.uid) {
            return
        }


        wx.showLoading({
            title: 'loading...',
        })

        this.getList()
    },

    /**
     * 获取任务列表数据
     */
    getList: function() {

        var that = this;

        var info = {
            pageNo: this.data.pageNo,
            pageSize: 10,
            taskType: '01'
        };

        // 请求列表数据
        Api.Request(info, U.URLS['missionList'], function(r) {
            wx.hideLoading()
            
            console.log(r)
            if (r != null) {
                var list = []
                if (r['rows'] != null && r['rows'].length > 0) {
                    if (r['pageNo'] == 1) {
                        list = r['rows']
                    } else {
                        list = that.data.orderList.concat(r['rows'])
                    }
                    that.setData({
                        pageNo: r['pageNo']+1,
                        orderList: list,
                        listEmpty: false
                    })
                    
                } else {
                    if(that.data.pageNo == 1){
                        that.setData({
                            listEmpty: true
                        })
                    }
                    
                }
            }
        });

    },

    // 登录获取openid
    login: function(e) {
        console.log('login start ...')
        var that = this
        wx.login({
            success(res) {
                console.log('code => ' + res.code)
                if (res.code) {
                    that.doLogin(res.code)
                  
                }
            }

        })
    },
    
    
    doLogin: function(code){
        var that = this;

        var info = {
            code: code,
        };

        // 请求用户数据
        Api.Request(info, U.URLS['login'], function (r) {
            wx.hideLoading()
            console.log(r)
            if (r['openId']){
                if (r['userCode']){
                    console.log('保存用户')
                    //保存用户
                    app.globalData.openId = r['openId']
                    app.globalData.uid = r['userCode']
                    app.globalData.deptName = r['deptName']
                    app.globalData.mendian = r['deptCode']
                    app.globalData.mobile = r['userMobile']
                    app.globalData.username = r['fullName']

                    that.onPullDownRefresh();
                    
                } else {
                    console.log('开始绑定手机')
                    //开始绑定手机
                    that.setData({
                        bingMob: true,
                        openId: r['openId']
                    })
                }
            } else {
                wx.showToast({
                    title: '请重新打开小程序',
                })
            }
        });
    },

    // 提交手机号
    formSubmit:function(e){
        console.log('form发生了submit事件,携带数据为:', e.detail.value)
        var m = e.detail.value.input
        if(!m){
            wx.showModal({
                title: '提示',
                content: '请输入手机号',
                showCancel: false,
            })
        } else {
            var that = this;

            var info = {
                userMobile: m,
                openId: that.data.openId
            };
            // 绑定用户数据
            Api.Request(info, U.URLS['binding'], function (r) {
                wx.hideLoading()
                console.log(r)
                if(r['userCode']){
                    console.log('保存用户')
                    //保存用户
                    app.globalData.openId = r['openId']
                    app.globalData.uid = r['userCode']
                    app.globalData.deptName = r['deptName']
                    app.globalData.mendian = r['deptCode']
                    app.globalData.mobile = r['userMobile']
                    app.globalData.username = r['fullName']

                    that.onPullDownRefresh();
                } else {
                    wx.showModal({
                        title: '提示',
                        content: r,
                        showCancel: false,
                    })
                }
            })
        }

    },

    // 打开详情页
    openDetail: function(e) {
        var order = e.currentTarget.dataset.order
        var model = encodeURIComponent(JSON.stringify(order))
        wx.navigateTo({
            url: '/pages/detail/detail?model='+model,
        })
    },


})