/*首页*/
var strophe = require('../../utils/strophe.js')
var WebIM = require('../../utils/WebIM.js').default
// 网络请求工具类
var networkUtil = require('../../utils/network_util.js')
var api = require('../../utils/apiFile.js')
// 数字类数据处理
var numberUtil = require('../../utils/numberUtil.js')
var util = require('../../utils/util.js')
// 事件监听
var event = require('../../utils/event.js')
// 获取应用实例
var app = getApp()
var timeCount = 60;
var timer;
var phone;
var name;
Page({
/*---------属性变量--------*/
data: {
isLogin: '',
item_big_height: 0,
item_small: 0,
// 首页类型 1登录 2用户首页 3欢迎页
pageType: 0,
time: '0s',
},
/*---------生命周期--------*/
onLoad: function () {
var isFirst = wx.getStorageSync('isFirst')
if (!isFirst) {
this.setData({
pageType: 3
})
console.log('我是空的')
} else {
console.log('我是空的')
}
// var that = this
this.networkStatusChange()
// this.networkstatus()
if (app.globalData.token == null || app.globalData.token == '') {
this.loginPage()
} else {
networkUtil.showLoading()
this.getUserInfo()
this.homePage()
// 监听事件
event.on('likeChanged', this, function (data) {
if (data.categoryIndex == -1) {
var artList = this.data.crisisCategorie
artList[data.articleIdIndex].likeCount++
this.setData({
crisisCategorie: artList
});
} else {
var artList = this.data.articleList
artList[data.categoryIndex].article[data.articleIdIndex].likeCount++
this.setData({
articleList: artList
});
}
})
}
},
onShow: function () {
// 生命周期函数--监听页面显示
if (app.globalData.token.length > 0 && this.data.isLogin == true) {
this.getUserInfo()
this.homePage()
}
if (wx.getStorageSync("access_token") != null && !wx.getStorageSync("isHxOpend")) {
}
},
onUnload: function () {
// 移除监听
event.remove('likeChanged', this);
},
/*---------交互--------*/
// 下拉刷新回调接口
onPullDownRefresh: function () {
if (app.globalData.token == null || app.globalData.token == '') {
wx.stopPullDownRefresh()
return
}
// 网络请求,重新请求一遍数据
this.getUserInfo()
this.homePage()
},
// 点击头像
tapAvatar: function () {
var that = this
wx.showActionSheet({
itemList: ['退出当前用户'],
success: function (res) {
if (res.tapIndex == 0) {
wx.setStorageSync('userToken', '')
getApp().globalData.token = ''
that.loginPage()
WebIM.conn.close();
}
},
fail: function (res) {
console.log(res.errMsg)
}
})
},
/*---------自定义函数--------*/
homePage() {
this.getAccessToken()
var that = this
that.setData({
isLogin: false
})
},
loginPage() {
var that = this
that.setData({
isLogin: true
})
},
// 监听网络状态
networkStatusChange() {
var that = this
wx.onNetworkStatusChange(function (res) {
if (res.isConnected == true) {
that.connectWebIM()
}
})
},
// 获取用户信息
getUserInfo() {
var self = this
var param = { 'token': app.globalData.token }
networkUtil._get(api.getUserInfo, param, function (res) {
}, function (res) {
networkUtil.showErrorToast(res.errormsg)
})
},
getArticleInfo(days) {
var self = this
// 文章列表
networkUtil._get(api.homeArtList, { page: 0, limit: 100, days: days }, function (res) {
}, function (res) {
wx.stopPullDownRefresh()
})
},
// 设置UI数据
setUserInfo(data) {
},
/*---------------欢迎介绍页面---------------*/
toLogin() {
this.setData({
pageType: 1
})
},
// 获取验证码
getVerifyCode: function (e) {
console.log("name:" + name)
console.log("phone:" + phone)
if (timeCount < 60) {
return
}
var that = this
networkUtil._post(api.bindDoc, { username: name, phone: phone }, function (res) {
console.log("getVerifyCode:",res)
}, function (res) {
timeCount = 60
clearTimeout(timer);
that.setData({
time: '0s'
})
networkUtil.showErrorToast('获取验证码失败,请稍后再试')
})
if (timeCount == 60) {
this.keepTime()
}
// if (numberUtil.IsTelPhoneNumber(phone)) {
// } else {
// wx.showToast({
// title: '请输入正确手机号码',
// })
// return
// }
},
// 计时器
keepTime: function () {
var that = this
timeCount--
that.setData({
time: timeCount + 's'
})
if (timeCount == 0) {
timeCount = 60
clearTimeout(timer);
return
}
timer = setTimeout(function () {
that.keepTime()
}, 1000)
},
blurInput: function (e) {
console.log(e)
phone = e.detail.value
},
blurNameInput:function(e){
console.log(e)
name = e.detail.value
},
// 提交表单
formSubmit: function (e) {
// wx.navigateTo({
// url: '../childcare_knowledge/childcare_knowledge'
// })
var self = this
if (e.detail.value.code == "") {
wx.showModal({
title: '提示',
content: '请输入验证码',
showCancel: false,
success: function (res) {
}
})
return
}
networkUtil.showLoading()
var param = { code: e.detail.value.code, phone: phone }
if (getApp().globalData.openInfo.mpid) {
param.gzopenid = getApp().globalData.openInfo.mpid
console.log('绑定mpid', param)
}
if (getApp().globalData.openInfo.code) {
param.jscode = getApp().globalData.openInfo.code
console.log('绑定code', param)
}
networkUtil._get(api.verifyCode, param, function (res) {
console.log(res)
self.loginSuccess(res.data)
}, function (res) {
networkUtil.showErrorToast(res.errormsg)
})
},
loginSuccess(data) {
console.log(data)
wx.setStorageSync('userToken', data.token)
wx.setStorageSync('hxName', data.hxName)
wx.setStorageSync('hxPassWord', data.hxPassWord)
getApp().globalData.token = data.token
// wx.navigateTo({
// url: '../HasDocumentedVerifySuccess/HasDocumentedVerifySuccess?name=' + name
// })
}
})