index.js 12 KB

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