home.js 10 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
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
/*首页*/
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',
list:[],
userInfo:'',
inputFocus: { nameInput: false, phoneInput: false, codeInput: false },
currentBabyIndex:0
},
/*---------生命周期--------*/
onLoad: function () {
this.calculatePageSize()
var isFirst = wx.getStorageSync('isFirst')
// 网络监听
this.networkStatusChange()
if (!isFirst) {
wx.setStorageSync('isFirst', 'isFirst')
this.welcomePage()
} else {
if (app.globalData.token == null || app.globalData.token == '') {
this.toLogin()
} else {
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
});
}
})
}
}
},
onUnload: function () {
// 移除监听
event.remove('likeChanged', this);
},
/*---------交互--------*/
// 下拉刷新回调接口
onPullDownRefresh: function () {
if (app.globalData.token == null || app.globalData.token == '') {
wx.stopPullDownRefresh()
return
}
// 网络请求,重新请求一遍数据
this.homePage()
},
// 点击头像
tapAvatar: function () {
var that = this
wx.showActionSheet({
itemList: ['退出当前用户'],
success: function (res) {
if (res.tapIndex == 0) {
wx.setStorageSync('userToken', '')
wx.setStorageSync('userId', '')
getApp().globalData.token = ''
getApp().globalData.userId = ''
that.toLogin()
}
},
fail: function (res) {
console.log(res.errMsg)
}
})
},
getNameFocus:function(){
var self=this
var inputFocus = { nameInput: true, phoneInput: false, codeInput: false }
self.setData({
inputFocus: inputFocus
})
},
getPhoneFocus: function () {
var self = this
var inputFocus = { nameInput: false, phoneInput: true, codeInput: false }
self.setData({
inputFocus: inputFocus
})
},
getCodeFocus: function () {
var self = this
var inputFocus = { nameInput: false, phoneInput: false, codeInput: true }
self.setData({
inputFocus: inputFocus
})
},
loseNameFocus: function () {
var self = this
var inputFocus = { nameInput: false}
self.setData({
inputFocus: inputFocus
})
},
losePhoneFocus: function () {
var self = this
var inputFocus = { phoneInput: false, }
self.setData({
inputFocus: inputFocus
})
},
loseCodeFocus: function () {
var self = this
var inputFocus = { codeInput: false }
self.setData({
inputFocus: inputFocus
})
},
// 改变baby
changeBaby(e){
this.setData({
currentBabyIndex: e.currentTarget.dataset.index
})
},
/*---------自定义函数--------*/
homePage() {
var that = this
this.requestHomeInfo()
that.setData({
pageType: 2
})
},
toLogin() {
var that = this
that.setData({
pageType: 1
})
},
welcomePage(){
var that = this
that.setData({
pageType: 3
})
},
calculatePageSize() {
var windowWidth = wx.getSystemInfoSync().windowWidth
var smallWidth = ((windowWidth - 10) / 2 - 10)
this.setData({
item_big_height: 'height:' + windowWidth / 4 + 'px',
item_small: 'width:' + windowWidth / 4 * 0.45 + 'px;height:' + windowWidth / 4 * 0.45 + 'px',
})
},
// 监听网络状态
networkStatusChange() {
var that = this
wx.onNetworkStatusChange(function (res) {
if (res.isConnected == true) {
that.connectWebIM()
}
})
},
requestHomeInfo(){
// 需要先请求用户信息
this.getUserInfo()
},
// 获取用户信息
getUserInfo() {
networkUtil.showLoading()
var self = this
var param = { 'token': app.globalData.token }
networkUtil._get(api.userInfo + app.globalData.userId[0], {}, function (res) {
console.log('获取用户信息',res)
app.globalData.days = res.data.data.days
app.globalData.userInfo = res.data.data
self.getArticleInfo(res.data.data.days)
self.setData({
userData: res.data.data
})
}, function (res) {
networkUtil.showErrorToast(res.errormsg)
})
},
getArticleInfo(days) {
var self = this
// 文章列表
networkUtil._get(api.homeArtList, { page: 0, limit: 100, days: days }, function (res) {
console.log('获取文章信息', res.data.list)
app.globalData.artList = res.data.list
self.setData({
list: res.data.list
})
}, function (res) {
wx.stopPullDownRefresh()
})
},
// 文章点赞
articleLike(e) {
console.log(e)
var self = this
// 已经点赞
if (e.currentTarget.dataset.islike == 1) {
networkUtil.showErrorToast('您已经点过赞啦')
return;
}
networkUtil._post(api.articleLike, { id: e.currentTarget.dataset.artid }, function (res) {
var artList = self.data.list
artList[e.currentTarget.dataset.categaryindex].article[e.currentTarget.dataset.artindex].likeCount++
artList[e.currentTarget.dataset.categaryindex].article[e.currentTarget.dataset.artindex].isLike = 1
self.setData({
list: artList
});
}, function (res) {
console.log(res)
})
},
// 标签页面
clickLabel(e){
console.log(e)
wx.navigateTo({
url: '../articleLists/articleLists?category=' + e.currentTarget.dataset.categary,
})
},
/*---------------欢迎介绍页面---------------*/
// 获取验证码
getVerifyCode: function (e) {
console.log("name:" + name)
console.log("phone:" + phone)
if (timeCount < 60) {
return
}
if (numberUtil.IsTelPhoneNumber(phone) && name.length>0) {
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(res.errormsg)
})
if (timeCount == 60) {
this.keepTime()
}
} else {
networkUtil.showErrorToast('请输入正确手机号码和姓名')
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) {
phone = e.detail.value
// console.log(e)
},
blurNameInput:function(e){
name = e.detail.value
// console.log(e)
},
// 提交表单
formSubmit: function (e) {

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) {
wx.setStorageSync('userToken', data.token)
wx.setStorageSync('userId', data.patientIds)
wx.setStorageSync('hxName', data.hxName)
wx.setStorageSync('hxPassWord', data.hxPassWord)
getApp().globalData.token = data.token
getApp().globalData.userId = data.patientIds
this.homePage()
}

})