home.js 3.92 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
/*首页*/
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()
Page({
/*---------属性变量--------*/
data: {
isLogin: '',
item_big_height:0,
item_small: 0,
},
/*---------生命周期--------*/
onLoad: function () {
// var that = this
// wx.showShareMenu(
// 计算功能模块的尺寸
this.calculatePageSize()
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) {
console.log('网络状态改变', 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) {
},
calculatePageSize(){
var windowWidth = wx.getSystemInfoSync().windowWidth
var smallWidth = ((windowWidth - 10) / 2 - 10)
this.setData({
item_big_height: 'height:' + (windowWidth - 10) / 2 + 'px',
item_small: 'width:' + smallWidth / 2 + 'px;height:' + smallWidth / 2 + 'px',
})
}
})