mini-system-examination-signup/pages/index/index.js

309 lines
7.5 KiB
JavaScript
Raw Permalink Normal View History

2021-04-29 10:08:41 +08:00
// index.js
// 获取应用实例
const app = getApp()
2021-07-23 11:18:54 +08:00
var utils = require('../../utils/util.js')
2021-04-29 10:08:41 +08:00
Page({
data: {
index: 1,
2021-07-23 11:18:54 +08:00
noticeList: [], //开班通告
answerList: [], //报考问答
2021-04-29 10:08:41 +08:00
userIcon: '../../images/icons/ic_user_default.png',
nickName: '点击授权',
2022-06-08 16:49:02 +08:00
// show: false,
2021-07-23 11:18:54 +08:00
phone: '', //要绑定的手机号
2022-06-08 16:49:02 +08:00
categoryId: '87a51bb1-451b-4b49-903b-4d847ba9c730',
2021-04-29 10:08:41 +08:00
},
onLoad() {
this.getUserInfo()
2021-07-23 11:18:54 +08:00
this.getNoticeList()
this.getAnswerList()
2022-06-08 16:49:02 +08:00
this.doLogin()
2021-07-23 11:18:54 +08:00
},
2022-06-08 16:49:02 +08:00
//获取最新公告
2021-07-23 11:18:54 +08:00
getNoticeList() {
var _self = this
app.restAjax.get(app.restAjax.path(app.apis.getArticleList, [app.baseUrls.cardUrl]), {
2022-06-08 16:49:02 +08:00
categoryId: _self.data.categoryId,
2021-07-23 11:18:54 +08:00
page: '1',
rows: '6'
}, {},
function (code, data) {
if (code == 200) {
for (var i = 0; i < data.rows.length; i++) {
var item = data.rows[i]
if (item.coverPhotos.length > 0) {
var photos = item.coverPhotos.split(',')
data.rows[i].showPhoto = app.baseUrls.cardUrl + app.baseUrls.baseImgUrl + photos[0]
} else {
2022-06-08 16:49:02 +08:00
data.rows[i].showPhoto = '../../images/icons/notice_default.png'
2021-07-23 11:18:54 +08:00
}
}
_self.setData({
noticeList: data.rows
})
}
},
function (code, err) {
console.log(err)
})
},
//报考问答
getAnswerList() {
var _self = this
app.restAjax.get(app.restAjax.path(app.apis.getArticleList, [app.baseUrls.cardUrl]), {
categoryId: '494bf3cc-cab6-4710-a0b2-d84f4591c992',
page: '1',
rows: '6'
}, {},
function (code, data) {
if (code == 200) {
for (var i = 0; i < data.rows.length; i++) {
var item = data.rows[i]
if (item.coverPhotos.length > 0) {
var photos = item.coverPhotos.split(',')
data.rows[i].showPhoto = app.baseUrls.cardUrl + app.baseUrls.baseImgUrl + photos[0]
} else {
data.rows[i].showPhoto = '../../images/icons/answer_default.jpeg'
}
}
_self.setData({
answerList: data.rows
})
}
},
function (code, err) {
console.log(err)
})
2021-04-29 10:08:41 +08:00
},
//获取个人信息
getUserInfo() {
let _self = this
try {
var name = wx.getStorageSync('nickName')
var icon = wx.getStorageSync('userIcon')
if (name) {
_self.setData({
nickName: name
})
}
if (icon) {
_self.setData({
userIcon: icon
})
2022-06-08 16:49:02 +08:00
// _self.checkBindPhone()
2021-04-29 10:08:41 +08:00
} else {
wx.showModal({
title: '提示',
content: '小程序需要您微信头像以及昵称用于展示,请授权.',
showCancel: false,
success(res) {
if (res.confirm) {
_self.getUserProfile(3)
}
}
})
}
} catch (e) {
}
},
2021-07-23 11:18:54 +08:00
showMineInfo(e) {
try {
var icon = wx.getStorageSync('userIcon')
if (icon) {
wx.navigateTo({
url: './mineinfo',
})
} else {
this.getUserProfile(e)
}
} catch (err) {
}
},
//弹框关闭
onClose(e) {
this.setData({
show: false
})
},
//进行手机号绑定
doBindPhone(e) {
var _self = this
if (_self.checkPhonelegal()) {
app.globalData.bindPhone = _self.data.phone
wx.setStorage({
data: _self.data.phone,
key: 'bindPhone',
})
_self.setData({
show: false
})
}
},
checkPhonelegal() {
var _self = this
if (!utils.isMobile(_self.data.phone)) {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none'
})
return false
} else {
return true
}
},
//绑定手机号输入监听
bindPhone(e) {
this.setData({
phone: e.detail.value
})
},
2021-04-29 10:08:41 +08:00
//显示更多数据
showMoreList() {
2022-06-08 16:49:02 +08:00
var self = this
if (this.data.index == 1 || this.data.index == 3) {
2021-04-29 10:08:41 +08:00
wx.navigateTo({
2022-06-08 16:49:02 +08:00
url: '../notices/noticeslist?index=' + self.data.index,
2021-04-29 10:08:41 +08:00
})
} else {
wx.navigateTo({
url: '../answer/answerlist',
})
}
},
2021-07-23 11:18:54 +08:00
//新闻切换
2021-04-29 10:08:41 +08:00
tabSelect(e) {
this.setData({
index: e.currentTarget.dataset.id
})
2022-06-08 16:49:02 +08:00
if (e.currentTarget.dataset.id != '2') {
if (e.currentTarget.dataset.id == '1') {
this.setData({
categoryId: '87a51bb1-451b-4b49-903b-4d847ba9c730'
})
} else if (e.currentTarget.dataset.id == '3') {
this.setData({
categoryId: '92737534-c2ef-487c-b763-4d1fb597388f'
})
}
this.getNoticeList()
}
2021-04-29 10:08:41 +08:00
},
2021-07-23 11:18:54 +08:00
//页面跳转
2021-04-29 10:08:41 +08:00
choosePage(e) {
var pagePath = ''
switch (e.currentTarget.dataset.page) {
case '1': //培训机构查询
pagePath = '../institutions/institutionslist'
break
case '2': //成绩查询
pagePath = '../results/resultslist'
break
case '3': //准考证查询
pagePath = '../card/cardlist'
break
case '4': //考试网点查询
pagePath = '../branches/brancheslist'
break
2022-06-08 16:49:02 +08:00
case '5': //培训报名
pagePath = '../sign/sign'
break
case '6': //报名情况
pagePath = '../signStatus/signStatus'
break
2021-04-29 10:08:41 +08:00
}
2022-06-08 16:49:02 +08:00
wx.navigateTo({
url: pagePath,
})
// if (e.currentTarget.dataset.page == '2' || e.currentTarget.dataset.page == '3') {
// if (app.globalData.bindPhone != '' && app.globalData.bindPhone.length > 0) {
// } else {
// // this.checkBindPhone()
// }
// } else {
// wx.navigateTo({
// url: pagePath,
// })
// }
2021-04-29 10:08:41 +08:00
}, // 获取个人信息
getUserProfile(e) {
let _self = this
wx.getUserProfile({
desc: '获取微信头像以及昵称用于展示',
success: (res) => {
wx.setStorage({
data: res.userInfo.nickName,
key: 'nickName',
})
wx.setStorage({
data: res.userInfo.avatarUrl,
key: 'userIcon',
})
_self.setData({
nickName: res.userInfo.nickName,
userIcon: res.userInfo.avatarUrl
})
2022-06-08 16:49:02 +08:00
// _self.checkBindPhone()
2021-04-29 10:08:41 +08:00
},
fail(err) {
2021-07-23 11:18:54 +08:00
//判断是否绑定手机号
2021-04-29 10:08:41 +08:00
wx.showToast({
title: '获取个人信息失败',
2021-07-23 11:18:54 +08:00
icon: 'error',
success() {
_self.checkBindPhone()
}
2021-04-29 10:08:41 +08:00
})
}
})
},
2021-07-23 11:18:54 +08:00
//判断是否绑定手机号
2022-06-08 16:49:02 +08:00
// checkBindPhone() {
// try {
// var _self = this
// var bindPhone = wx.getStorageSync('bindPhone')
// if (bindPhone == '' || bindPhone.length <= 0) {
// _self.setData({
// show: true
// })
// }
// } catch (error) {
2021-07-23 11:18:54 +08:00
2022-06-08 16:49:02 +08:00
// }
// },
2021-04-29 10:08:41 +08:00
showNoticeDetail(e) {
var item = e.currentTarget.dataset.item
wx.navigateTo({
2021-07-23 11:18:54 +08:00
url: '../notices/noticedetail?id=' + item.contentId,
2021-04-29 10:08:41 +08:00
})
},
showAnswerDetail(e) {
var item = e.currentTarget.dataset.item
2021-07-23 11:18:54 +08:00
wx.navigateTo({
url: '../answer/answerdetail?id=' + item.contentId,
})
2022-06-08 16:49:02 +08:00
},
// 登录
doLogin: function () {
wx.login({
success: function (res) {
app.restAjax.post(app.restAjax.path(app.apis.login, [app.baseUrls.testUrl]), {
jsCode: res.code
}, {},
function (code, data) {
var token = data.data
wx.setStorage({
key: 'token',
data: token
})
},
function (code, err) {
console.log(err)
})
}
})
2021-04-29 10:08:41 +08:00
}
})