index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // pages/index/index.js
  2. const app = getApp()
  3. const util = require("../../utils/util")
  4. const urlDef = require("../../utils/urls")
  5. var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. quanTipFlag: false,
  12. isShow: true,
  13. array: ['艾克斯郎校区1', '艾克斯郎校区2', '艾克斯郎校区3', '艾克斯郎校区4'],
  14. index: 0,
  15. curStu: {},
  16. classList: [{
  17. img: '/images/kc.png',
  18. title: '今日课程',
  19. num: '0',
  20. url: "/pages/schedule/schedule"
  21. },
  22. {
  23. img: '/images/jtzy.png',
  24. title: '今日作业',
  25. num: '0',
  26. url: "/pages/task/task"
  27. },
  28. {
  29. img: '/images/cq.png',
  30. title: '出勤率',
  31. num: '0%',
  32. url: "/pages/classTotalList/classTotalList"
  33. },
  34. ],
  35. appList: [{
  36. img: '/images/kb.png',
  37. title: '我的课表',
  38. url: "/pages/schedule/schedule",
  39. },
  40. {
  41. img: '/images/zy.png',
  42. title: '提交作业',
  43. url: "/pages/task/task",
  44. },
  45. {
  46. img: '/images/dp.png',
  47. title: '课堂点评',
  48. url: "/pages/evaluation/evaluation"
  49. },
  50. {
  51. img: '/images/zj.png',
  52. title: '成长足迹',
  53. url: "/pages/growUp/growUp"
  54. },
  55. {
  56. img: '/images/qj.png',
  57. title: '请假',
  58. url: "/pages/leave/leave"
  59. },
  60. {
  61. img: '/images/bk.png',
  62. title: '补课',
  63. url: "/pages/lessons/lessons"
  64. },
  65. {
  66. img: '/images/jk.png',
  67. title: '加课',
  68. url: "/pages/addClass/addClass"
  69. },
  70. {
  71. img: '/images/dk.png',
  72. title: '调课',
  73. url: "/pages/theClasses/theClasses"
  74. },
  75. ],
  76. bannerList: [],
  77. userList: [],
  78. userItems: [],
  79. orgItems: [],
  80. stuShowList: [],
  81. teaShowList: [],
  82. },
  83. //切换身份
  84. changeStu: function (e) {
  85. let sid = e.currentTarget.dataset.id;
  86. this.doChangeUser(sid);
  87. },
  88. doChangeUser: function (sid) {
  89. if (this.data.curStu.studentId == sid) {
  90. console.log('无须切换')
  91. return;
  92. }
  93. wx.showLoading({
  94. title: '正在切换用户',
  95. mask: true
  96. })
  97. console.log('切换用户 : ' + sid);
  98. const urls = urlDef.urls;
  99. util.apiPost(urls.change_user, {
  100. targetId: sid
  101. }).then(rs => {
  102. wx.hideLoading();
  103. if (rs === 1) {
  104. console.log('切换成功');
  105. this.loadIndexData();
  106. } else {
  107. wx.showToast({
  108. title: '切换失败,请稍后重试',
  109. icon: 'none'
  110. })
  111. }
  112. });
  113. },
  114. //切换校区
  115. bindPickerChange: function (e) {
  116. let i = e.detail.value;
  117. if (i == this.data.index) {
  118. console.log('无须切换...');
  119. return;
  120. }
  121. // 取该校区 第1位学生 作为默认登录用户
  122. let targetId = this.data.orgItems[i].orgId;
  123. console.log('切换校区 ' + targetId);
  124. this.data.userItems.some(u => {
  125. if (u.orgId === targetId) {
  126. this.doChangeUser(u.studentId);
  127. this.setData({
  128. index: i
  129. });
  130. return true;
  131. }
  132. });
  133. },
  134. toPage: function (e) {
  135. if (typeof e.currentTarget.dataset.url == 'undefined') {
  136. return
  137. }
  138. let today = util.curTime().substring(0, 10).replace(/\//g, '-')
  139. wx.navigateTo({
  140. url: e.currentTarget.dataset.url + '?beginDate=' + (today.substring(0, 5) + '01-01') + '&endDate=' + today,
  141. })
  142. },
  143. getMoreShow: function (e) {
  144. wx.navigateTo({
  145. url: '/pages/showVideoList/showVideoList?personType=' + e.currentTarget.dataset.type,
  146. })
  147. },
  148. closeTip:function(){
  149. this.setData({
  150. quanTipFlag:false
  151. })
  152. },
  153. toQuan:function(){
  154. wx.navigateTo({
  155. url: '/pages/quan/quan',
  156. })
  157. this.setData({
  158. quanTipFlag:false
  159. })
  160. },
  161. /**
  162. * 生命周期函数--监听页面加载
  163. */
  164. onLoad: function (options) {
  165. setTimeout(() => {
  166. this.setData({
  167. quanTipFlag: true
  168. })
  169. }, 3000);
  170. this.setData({
  171. isShow: wx.getStorageSync('isShow'),
  172. })
  173. this.localtionMethod()
  174. },
  175. /**
  176. * 生命周期函数--监听页面初次渲染完成
  177. */
  178. onReady: function () {
  179. },
  180. /**
  181. * 生命周期函数--监听页面显示
  182. */
  183. onShow: function () {
  184. wx.hideHomeButton()
  185. const latlng = wx.getStorageSync('latlng')
  186. if(!latlng){
  187. this.localtionMethod()
  188. }
  189. if(!wx.getStorageSync('openId')){
  190. // wx.redirectTo({
  191. // url: '/pages/login/login',
  192. // })
  193. // return
  194. } else {
  195. this.loadIndexData();
  196. }
  197. },
  198. /**
  199. * 生命周期函数--监听页面隐藏
  200. */
  201. onHide: function () {
  202. },
  203. /**
  204. * 生命周期函数--监听页面卸载
  205. */
  206. onUnload: function () {
  207. },
  208. /**
  209. * 页面相关事件处理函数--监听用户下拉动作
  210. */
  211. onPullDownRefresh: function () {
  212. },
  213. /**
  214. * 页面上拉触底事件的处理函数
  215. */
  216. onReachBottom: function () {
  217. },
  218. /**
  219. * 用户点击右上角分享
  220. */
  221. onShareAppMessage: function () {
  222. },
  223. strLen(s) {
  224. return (s ? s.length : 0)
  225. },
  226. loadVideoInfo: function (videos, personType) {
  227. let urls = urlDef.urls
  228. videos.forEach(v => {
  229. util.apiPost(urls.video_loadInfo + v.videoId).then((rs) => {
  230. v.imgUrl = rs.img
  231. v.playUrl = rs.url
  232. if (personType == 0) {
  233. this.setData({
  234. 'stuShowList': videos
  235. })
  236. }
  237. if (personType == 1) {
  238. this.setData({
  239. 'teaShowList': videos
  240. })
  241. }
  242. });
  243. });
  244. },
  245. queryVideoShow: function (personType) {
  246. const urls = urlDef.urls;
  247. //获取登录用户数据, 并加载首页数据
  248. util.apiPost(urls.person_video_list, {
  249. 'q.orgId': this.data.curStu.orgId,
  250. 'q.doPersonId': this.data.curStu.studentId,
  251. 'q.personType': personType,
  252. 'q.sortBy': 'rand',
  253. 'q.limit4': 'true'
  254. }).then(rs => {
  255. this.loadVideoInfo(rs, personType)
  256. })
  257. },
  258. queryStudentShow: function () {
  259. this.queryVideoShow(0)
  260. },
  261. queryTeacherShow: function () {
  262. this.queryVideoShow(1)
  263. },
  264. loadIndexData: function () {
  265. this.setData({
  266. 'stuShowList': [],
  267. 'teaShowList': [],
  268. })
  269. const urls = urlDef.urls;
  270. //获取登录用户数据, 并加载首页数据
  271. util.apiPost(urls.get_cur_user, {}).then(rs => {
  272. // console.log(JSON.stringify(rs));
  273. let userItems = rs.attr.others
  274. let orgItems = []
  275. let student = {}
  276. userItems.forEach(u => {
  277. let exists = false;
  278. orgItems.some(o => {
  279. if (o.orgId === u.orgId) {
  280. exists = true;
  281. return true;
  282. }
  283. })
  284. if (!exists) {
  285. orgItems.push({
  286. orgId: u.orgId,
  287. orgName: u.orgName
  288. })
  289. }
  290. if (u.studentId === rs.attr.personId) { // 当前用户
  291. const headImg = u.imageUrl ? urls.oss_file + 'image/' + u.imageUrl : '/images/head.png';
  292. student = {
  293. studentId: u.studentId,
  294. orgId: u.orgId,
  295. orgName: u.orgName,
  296. name: u.studentName,
  297. sex: u.sex,
  298. headImg: headImg
  299. }
  300. this.setData({
  301. 'curStu': student
  302. })
  303. // 放入 strong中
  304. wx.setStorageSync('student', student);
  305. }
  306. if (this.strLen(u.imageUrl) === 0) {
  307. u.imageUrl = '/images/head.png'
  308. } else {
  309. u.imageUrl = urls.oss_file + 'image/' + u.imageUrl
  310. }
  311. u.headName = u.studentName.substring(u.studentName.length - 1, u.studentName.length)
  312. })
  313. this.setData({
  314. 'userItems': userItems,
  315. 'orgItems': orgItems
  316. })
  317. let personId = rs.attr.personId
  318. let orgId = rs.attr.orgId
  319. let today = util.curTime().substring(0, 10).replace(/\//g, '-')
  320. // banner 图片
  321. util.apiPost(urls.get_advert, {
  322. '&q.use': 1,
  323. 'q.orgId': orgId,
  324. 'q.exceptStatus': 2
  325. }).then(r => {
  326. // console.log('banner : ' + JSON.stringify(r));
  327. this.setData({
  328. 'bannerList': r
  329. })
  330. });
  331. // 今日课程
  332. util.apiPost(urls.leave_get_classes_date, {
  333. 'q.studentId': personId,
  334. 'q.attenceDate': today
  335. }).then(r => {
  336. // console.log('今日课程 : ' + JSON.stringify(r));
  337. let courseCount = 0
  338. if (r && r.length > 0) {
  339. courseCount = r[0].total;
  340. }
  341. this.setData({
  342. 'classList[0].num': courseCount
  343. })
  344. })
  345. // 作业未提交
  346. util.apiPost(urls.my_homework, {
  347. 'q.studentId': personId,
  348. 'q.status': 0
  349. }).then(r => {
  350. // console.log('作业未提交 : ' + JSON.stringify(r));
  351. this.setData({
  352. 'classList[1].num': (r ? r.length : 0)
  353. })
  354. });
  355. // 出勤率
  356. util.apiPost(urls.get_attendance_rate, {
  357. 'q.studentId': personId
  358. }).then(r => {
  359. // console.log('出勤率 : ' + JSON.stringify(r));
  360. let attendRate = 0,
  361. alreadyCount = 0,
  362. shouldCount = 0;
  363. if (r != null) {
  364. r.forEach(it => {
  365. if (it.isAttend === '1') {
  366. alreadyCount = it.num
  367. } else if (it.isAttend === '0') {
  368. shouldCount = it.num
  369. }
  370. });
  371. }
  372. let t = (alreadyCount + shouldCount)
  373. if (t > 0) {
  374. attendRate = alreadyCount * 100 / t
  375. }
  376. this.setData({
  377. 'classList[2].num': attendRate.toFixed(2) + '%'
  378. })
  379. })
  380. this.queryVideoShow(0)
  381. this.queryVideoShow(1)
  382. });
  383. },
  384. doThumbsUp: function (o) {
  385. const urls = urlDef.urls;
  386. let pid = this.data.curStu.studentId;
  387. let pt = o.detail.personType;
  388. let vid = o.detail.videoId;
  389. let entity = {
  390. videoId: vid,
  391. personId: pid,
  392. personType: pt,
  393. actionType: 1
  394. };
  395. util.apiPost(urls.person_video_view_save, entity, 'application/json').then(rs => {
  396. if (pt == 0) {
  397. this.data.stuShowList.map(o => {
  398. if (o.videoId == vid) {
  399. o.goodCount += 1
  400. }
  401. })
  402. this.setData({
  403. 'stuShowList': this.data.stuShowList
  404. })
  405. }
  406. if (pt == 1) {
  407. this.data.teaShowList.map(o => {
  408. if (o.videoId == vid) {
  409. o.goodCount += 1
  410. }
  411. })
  412. this.setData({
  413. 'teaShowList': this.data.teaShowList
  414. })
  415. }
  416. }).catch(e => {
  417. console.log(e);
  418. })
  419. },
  420. getMyLocal: function () {
  421. //获取当前定位具体位置名称
  422. var qqmapsdk = new QQMapWX({
  423. key: 'FCLBZ-ZQ2Y3-DC23W-3EXRK-LUKX6-W7F6Z'
  424. });
  425. var that = this
  426. var lat = wx.getStorageSync('latlng').latitude;
  427. var lng = wx.getStorageSync('latlng').longitude;
  428. // 在组件实例进入页面节点树时执行
  429. qqmapsdk.reverseGeocoder({
  430. location: {
  431. latitude: lat,
  432. longitude: lng
  433. },
  434. success: function (res) {
  435. //获取当前地址成功
  436. that.setData({
  437. address: res.result.address,
  438. location: {
  439. latitude: res.result.location.lat,
  440. longitude: res.result.location.lng
  441. }
  442. })
  443. const name = res.result.address_component.city
  444. wx.setStorageSync('wxLocalName', name)
  445. // const locationNavTop = that.selectComponent("#locationNavTop")
  446. // locationNavTop.getAddr()
  447. // that.getLocationList(res.result.address_reference)
  448. },
  449. fail: function (res) {
  450. console.log('获取当前地址失败');
  451. }
  452. });
  453. },
  454. localtionMethod: function () {
  455. const that = this
  456. wx.getLocation({
  457. type: 'wgs84',
  458. success(res) {
  459. // console.log(res)
  460. wx.setStorageSync('latlng', res)
  461. that.getCoder()
  462. that.getMyLocal()
  463. },
  464. fail(res) {
  465. wx.hideLoading({
  466. success: (res) => {
  467. wx.showModal({
  468. title: '提示',
  469. content: "定位失败,请检查手机'位置服务'或GPS功能已开启并授权小程序位置信息",
  470. success(res){
  471. if(res.confirm){
  472. wx.openSetting({
  473. success (res) {
  474. console.log(res.authSetting)
  475. // res.authSetting = {
  476. // "scope.userInfo": true,
  477. // "scope.userLocation": true
  478. // }
  479. }
  480. })
  481. }
  482. }
  483. })
  484. },
  485. })
  486. }
  487. })
  488. },
  489. getCoder:function(){
  490. const lat = wx.getStorageSync('latlng').latitude
  491. const lng = wx.getStorageSync('latlng').longitude
  492. wx.request({
  493. url: 'https://apis.map.qq.com/ws/geocoder/v1/?location='+lat+','+lng+"&key=FCLBZ-ZQ2Y3-DC23W-3EXRK-LUKX6-W7F6Z",
  494. method: 'GET',
  495. success: function( res ) {
  496. let cityCode = res.data.result.ad_info.city_code
  497. let nationCode = res.data.result.ad_info.nation_code
  498. cityCode = cityCode.substring(nationCode.length, cityCode.length)
  499. wx.setStorageSync('adCode', cityCode)
  500. },
  501. fail:function(res){
  502. console.log(res)
  503. }
  504. })
  505. }
  506. })