// pages/index/index.js const app = getApp() const util = require("../../utils/util") const urlDef = require("../../utils/urls") var QQMapWX = require('../../utils/qqmap-wx-jssdk.js'); Page({ /** * 页面的初始数据 */ data: { quanList: [], quanTipFlag: false, isShow: true, array: ['艾克斯郎校区1', '艾克斯郎校区2', '艾克斯郎校区3', '艾克斯郎校区4'], index: 0, curStu: {}, classList: [{ img: '/images/kc.png', title: '今日课程', num: '0', url: "/pages/schedule/schedule" }, { img: '/images/jtzy.png', title: '今日作业', num: '0', url: "/pages/task/task" }, { img: '/images/cq.png', title: '出勤率', num: '0%', url: "/pages/classTotalList/classTotalList" }, ], appList: [{ img: '/images/kb.png', title: '我的课表', url: "/pages/schedule/schedule", }, { img: '/images/zy.png', title: '提交作业', url: "/pages/task/task", }, { img: '/images/dp.png', title: '课堂点评', url: "/pages/evaluation/evaluation" }, { img: '/images/zj.png', title: '成长足迹', url: "/pages/growUp/growUp" }, { img: '/images/qj.png', title: '请假', url: "/pages/leave/leave" }, { img: '/images/bk.png', title: '补课', url: "/pages/lessons/lessons" }, { img: '/images/jk.png', title: '加课', url: "/pages/addClass/addClass" }, { img: '/images/dk.png', title: '调课', url: "/pages/theClasses/theClasses" }, ], bannerList: [], userList: [], userItems: [], orgItems: [], stuShowList: [], teaShowList: [], }, queryQuan: function () { const urls = urlDef.urls; let stu = wx.getStorageSync('student'); if (stu) { let params = { 'q.studentId': stu.studentId, 'q.orgId': stu.orgId } let url = null; url = urls.coupon_receive_list; params.pageNum = 9999 util.apiPost(url, params).then(rs => { let quanList = rs.list for(var i in quanList){ quanList[i].lastDate = quanList[i].endDate.substring(0,10) } this.setData({ quanList }) if(quanList.length > 0 ) { const indexQuanTip = wx.getStorageSync('indexQuanTip') if(indexQuanTip){ this.setData({ quanTipFlag: true }) wx.setStorageSync('indexQuanTip', false) } } }) } }, //切换身份 changeStu: function (e) { let sid = e.currentTarget.dataset.id; this.doChangeUser(sid); }, doChangeUser: function (sid) { if (this.data.curStu.studentId == sid) { console.log('无须切换') return; } wx.showLoading({ title: '正在切换用户', mask: true }) console.log('切换用户 : ' + sid); const urls = urlDef.urls; util.apiPost(urls.change_user, { targetId: sid }).then(rs => { wx.hideLoading(); if (rs === 1) { console.log('切换成功'); this.loadIndexData(); } else { wx.showToast({ title: '切换失败,请稍后重试', icon: 'none' }) } }); }, //切换校区 bindPickerChange: function (e) { let i = e.detail.value; if (i == this.data.index) { console.log('无须切换...'); return; } // 取该校区 第1位学生 作为默认登录用户 let targetId = this.data.orgItems[i].orgId; console.log('切换校区 ' + targetId); this.data.userItems.some(u => { if (u.orgId === targetId) { this.doChangeUser(u.studentId); this.setData({ index: i }); return true; } }); }, toPage: function (e) { if (typeof e.currentTarget.dataset.url == 'undefined') { return } let today = util.curTime().substring(0, 10).replace(/\//g, '-') wx.navigateTo({ url: e.currentTarget.dataset.url + '?beginDate=' + (today.substring(0, 5) + '01-01') + '&endDate=' + today, }) }, getMoreShow: function (e) { wx.navigateTo({ url: '/pages/showVideoList/showVideoList?personType=' + e.currentTarget.dataset.type, }) }, closeTip:function(){ this.setData({ quanTipFlag:false }) }, toQuan:function(){ wx.navigateTo({ url: '/pages/quan/quan', }) this.setData({ quanTipFlag:false }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ isShow: wx.getStorageSync('isShow'), }) this.localtionMethod() // this.queryQuan() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { wx.hideHomeButton() const latlng = wx.getStorageSync('latlng') if(!latlng){ this.localtionMethod() } if(!wx.getStorageSync('openId')){ // wx.redirectTo({ // url: '/pages/login/login', // }) // return } else { this.loadIndexData(); } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, strLen(s) { return (s ? s.length : 0) }, loadVideoInfo: function (videos, personType) { let urls = urlDef.urls videos.forEach(v => { util.apiPost(urls.video_loadInfo + v.videoId).then((rs) => { v.imgUrl = rs.img v.playUrl = rs.url if (personType == 0) { this.setData({ 'stuShowList': videos }) } if (personType == 1) { this.setData({ 'teaShowList': videos }) } }); }); }, queryVideoShow: function (personType) { const urls = urlDef.urls; //获取登录用户数据, 并加载首页数据 util.apiPost(urls.person_video_list, { 'q.orgId': this.data.curStu.orgId, 'q.doPersonId': this.data.curStu.studentId, 'q.personType': personType, 'q.sortBy': 'rand', 'q.limit4': 'true' }).then(rs => { this.loadVideoInfo(rs, personType) }) }, queryStudentShow: function () { this.queryVideoShow(0) }, queryTeacherShow: function () { this.queryVideoShow(1) }, loadIndexData: function () { this.setData({ 'stuShowList': [], 'teaShowList': [], }) const urls = urlDef.urls; //获取登录用户数据, 并加载首页数据 util.apiPost(urls.get_cur_user, {}).then(rs => { // console.log(JSON.stringify(rs)); let userItems = rs.attr.others let orgItems = [] let student = {} userItems.forEach(u => { let exists = false; orgItems.some(o => { if (o.orgId === u.orgId) { exists = true; return true; } }) if (!exists) { orgItems.push({ orgId: u.orgId, orgName: u.orgName }) } if (u.studentId === rs.attr.personId) { // 当前用户 const headImg = u.imageUrl ? urls.oss_file + 'image/' + u.imageUrl : '/images/head.png'; student = { studentId: u.studentId, orgId: u.orgId, orgName: u.orgName, name: u.studentName, sex: u.sex, headImg: headImg } this.setData({ 'curStu': student }) // 放入 strong中 const stu = wx.getStorageSync('student') if(stu && stu.studentId != student.studentId){ wx.setStorageSync('indexQuanTip', true) } wx.setStorageSync('student', student); this.queryQuan() } if (this.strLen(u.imageUrl) === 0) { u.imageUrl = '/images/head.png' } else { u.imageUrl = urls.oss_file + 'image/' + u.imageUrl } u.headName = u.studentName.substring(u.studentName.length - 1, u.studentName.length) }) this.setData({ 'userItems': userItems, 'orgItems': orgItems }) let personId = rs.attr.personId let orgId = rs.attr.orgId let today = util.curTime().substring(0, 10).replace(/\//g, '-') // banner 图片 util.apiPost(urls.get_advert, { '&q.use': 1, 'q.orgId': orgId, 'q.exceptStatus': 2 }).then(r => { // console.log('banner : ' + JSON.stringify(r)); this.setData({ 'bannerList': r }) }); // 今日课程 util.apiPost(urls.leave_get_classes_date, { 'q.studentId': personId, 'q.attenceDate': today }).then(r => { // console.log('今日课程 : ' + JSON.stringify(r)); let courseCount = 0 if (r && r.length > 0) { courseCount = r[0].total; } this.setData({ 'classList[0].num': courseCount }) }) // 作业未提交 util.apiPost(urls.my_homework, { 'q.studentId': personId, 'q.status': 0 }).then(r => { // console.log('作业未提交 : ' + JSON.stringify(r)); this.setData({ 'classList[1].num': (r ? r.length : 0) }) }); // 出勤率 util.apiPost(urls.get_attendance_rate, { 'q.studentId': personId }).then(r => { // console.log('出勤率 : ' + JSON.stringify(r)); let attendRate = 0, alreadyCount = 0, shouldCount = 0; if (r != null) { r.forEach(it => { if (it.isAttend === '1') { alreadyCount = it.num } else if (it.isAttend === '0') { shouldCount = it.num } }); } let t = (alreadyCount + shouldCount) if (t > 0) { attendRate = alreadyCount * 100 / t } this.setData({ 'classList[2].num': attendRate.toFixed(2) + '%' }) }) this.queryVideoShow(0) this.queryVideoShow(1) }); }, doThumbsUp: function (o) { const urls = urlDef.urls; let pid = this.data.curStu.studentId; let pt = o.detail.personType; let vid = o.detail.videoId; let entity = { videoId: vid, personId: pid, personType: pt, actionType: 1 }; util.apiPost(urls.person_video_view_save, entity, 'application/json').then(rs => { if (pt == 0) { this.data.stuShowList.map(o => { if (o.videoId == vid) { o.goodCount += 1 } }) this.setData({ 'stuShowList': this.data.stuShowList }) } if (pt == 1) { this.data.teaShowList.map(o => { if (o.videoId == vid) { o.goodCount += 1 } }) this.setData({ 'teaShowList': this.data.teaShowList }) } }).catch(e => { console.log(e); }) }, getMyLocal: function () { //获取当前定位具体位置名称 var qqmapsdk = new QQMapWX({ key: 'FCLBZ-ZQ2Y3-DC23W-3EXRK-LUKX6-W7F6Z' }); var that = this var lat = wx.getStorageSync('latlng').latitude; var lng = wx.getStorageSync('latlng').longitude; // 在组件实例进入页面节点树时执行 qqmapsdk.reverseGeocoder({ location: { latitude: lat, longitude: lng }, success: function (res) { //获取当前地址成功 that.setData({ address: res.result.address, location: { latitude: res.result.location.lat, longitude: res.result.location.lng } }) const name = res.result.address_component.city wx.setStorageSync('wxLocalName', name) // const locationNavTop = that.selectComponent("#locationNavTop") // locationNavTop.getAddr() // that.getLocationList(res.result.address_reference) }, fail: function (res) { console.log('获取当前地址失败'); } }); }, localtionMethod: function () { const that = this wx.getLocation({ type: 'wgs84', success(res) { // console.log(res) wx.setStorageSync('latlng', res) that.getCoder() that.getMyLocal() }, fail(res) { console.log(JSON.stringify(res)) wx.hideLoading({ success: (res) => { wx.showModal({ title: '提示', content: "定位失败,请检查手机'位置服务'或GPS功能已开启并授权小程序位置信息", success(res){ if(res.confirm){ wx.openSetting({ success (res) { console.log(res.authSetting) // res.authSetting = { // "scope.userInfo": true, // "scope.userLocation": true // } } }) } } }) }, }) } }) }, getCoder:function(){ const lat = wx.getStorageSync('latlng').latitude const lng = wx.getStorageSync('latlng').longitude wx.request({ url: 'https://apis.map.qq.com/ws/geocoder/v1/?location='+lat+','+lng+"&key=FCLBZ-ZQ2Y3-DC23W-3EXRK-LUKX6-W7F6Z", method: 'GET', success: function( res ) { let cityCode = res.data.result.ad_info.city_code let nationCode = res.data.result.ad_info.nation_code cityCode = cityCode.substring(nationCode.length, cityCode.length) wx.setStorageSync('adCode', cityCode) }, fail:function(res){ console.log(res) } }) } })