index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // pages/index/index.js
  2. const app = getApp()
  3. const util = require("../../utils/util")
  4. const urlDef = require("../../utils/urls")
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. array: ['艾克斯郎校区1', '艾克斯郎校区2', '艾克斯郎校区3', '艾克斯郎校区4'],
  11. index: 0,
  12. curStu: {},
  13. classList: [
  14. { img: '/images/kc.png', title: '今日课程', num: '0', url:"/pages/schedule/schedule" },
  15. { img: '/images/jtzy.png', title: '今日作业', num: '0' },
  16. { img: '/images/cq.png', title: '出勤率', num: '0%' },
  17. ],
  18. appList: [
  19. { img: '/images/kb.png', title: '我的课表',url:"/pages/schedule/schedule" },
  20. { img: '/images/zy.png', title: '提交作业',url:"/pages/task/task" },
  21. { img: '/images/dp.png', title: '课堂点评' },
  22. { img: '/images/zj.png', title: '成长足迹' },
  23. { img: '/images/qj.png', title: '请假',url:"/pages/leave/leave" },
  24. { img: '/images/bk.png', title: '补课',url:"/pages/lessons/lessons" },
  25. { img: '/images/jk.png', title: '加课' },
  26. { img: '/images/dk.png', title: '调课' },
  27. ],
  28. bannerList: [],
  29. userList: [],
  30. userItems: [],
  31. orgItems: [],
  32. },
  33. //切换身份
  34. changeStu: function (e) {
  35. let sid = e.currentTarget.dataset.id;
  36. this.doChangeUser(sid);
  37. },
  38. doChangeUser: function (sid) {
  39. if (this.data.curStu.studentId == sid) {
  40. console.log('无须切换')
  41. return;
  42. }
  43. wx.showLoading({
  44. title: '正在切换用户',
  45. mask: true
  46. })
  47. console.log('切换用户 : ' + sid);
  48. const urls = urlDef.urls;
  49. util.apiPost(urls.change_user, { targetId: sid }).then(rs => {
  50. wx.hideLoading();
  51. if (rs === 1) {
  52. console.log('切换成功');
  53. this.loadIndexData();
  54. } else {
  55. wx.showToast({
  56. title: '切换失败,请稍后重试',
  57. icon: 'none'
  58. })
  59. }
  60. });
  61. },
  62. //切换校区
  63. bindPickerChange: function (e) {
  64. let i = e.detail.value;
  65. if (i == this.data.index) {
  66. console.log('无须切换...');
  67. return;
  68. }
  69. // 取该校区 第1位学生 作为默认登录用户
  70. let targetId = this.data.orgItems[i].orgId;
  71. console.log('切换校区 ' + targetId);
  72. this.data.userItems.some(u => {
  73. console.log('用户: ' + JSON.stringify(u));
  74. if (u.orgId === targetId) {
  75. this.doChangeUser(u.studentId);
  76. this.setData({
  77. index: i
  78. });
  79. return true;
  80. }
  81. });
  82. },
  83. toPage:function(e){
  84. wx.navigateTo({
  85. url: e.currentTarget.dataset.url,
  86. })
  87. },
  88. /**
  89. * 生命周期函数--监听页面加载
  90. */
  91. onLoad: function (options) {
  92. this.loadIndexData();
  93. },
  94. /**
  95. * 生命周期函数--监听页面初次渲染完成
  96. */
  97. onReady: function () {
  98. },
  99. /**
  100. * 生命周期函数--监听页面显示
  101. */
  102. onShow: function () {
  103. },
  104. /**
  105. * 生命周期函数--监听页面隐藏
  106. */
  107. onHide: function () {
  108. },
  109. /**
  110. * 生命周期函数--监听页面卸载
  111. */
  112. onUnload: function () {
  113. },
  114. /**
  115. * 页面相关事件处理函数--监听用户下拉动作
  116. */
  117. onPullDownRefresh: function () {
  118. },
  119. /**
  120. * 页面上拉触底事件的处理函数
  121. */
  122. onReachBottom: function () {
  123. },
  124. /**
  125. * 用户点击右上角分享
  126. */
  127. onShareAppMessage: function () {
  128. },
  129. strLen(s) {
  130. return (s ? s.length : 0);
  131. },
  132. loadIndexData: function () {
  133. const urls = urlDef.urls;
  134. //获取登录用户数据, 并加载首页数据
  135. util.apiPost(urls.get_cur_user, {}).then(rs => {
  136. // console.log(JSON.stringify(rs));
  137. let userItems = rs.attr.others;
  138. let orgItems = [];
  139. userItems.forEach(u => {
  140. let exists = false;
  141. orgItems.some(o => {
  142. if (o.orgId === u.orgId) {
  143. exists = true;
  144. return true;
  145. }
  146. });
  147. if (!exists) {
  148. orgItems.push({ orgId: u.orgId, orgName: u.orgName });
  149. }
  150. if (u.id === rs.id) { // 当前用户
  151. this.setData({ 'curStu': { studentId: u.studentId, orgId: u.orgId } });
  152. }
  153. if (this.strLen(u.imageUrl) === 0) {
  154. u.imageUrl = '/images/head.png';
  155. } else {
  156. u.imageUrl = urls.oss_file + 'image/' + u.imageUrl;
  157. }
  158. });
  159. this.setData({ 'userItems': userItems, 'orgItems': orgItems });
  160. let personId = rs.attr.personId;
  161. let orgId = rs.attr.orgId;
  162. let today = util.curTime().substring(0, 10).replace(/\//g, '-');
  163. // banner 图片
  164. util.apiPost(urls.get_advert, { '&q.use': 1, 'q.orgId': orgId, 'q.exceptStatus': 2 }).then(r => {
  165. // console.log('banner : ' + JSON.stringify(r));
  166. this.setData({ 'bannerList': r });
  167. });
  168. // 今日课程
  169. util.apiPost(urls.leave_get_classes_date, { 'q.studentId': personId, 'q.attenceDate': today }).then(r => {
  170. // console.log('今日课程 : ' + JSON.stringify(r));
  171. this.setData({ 'classList[0].num': (r ? r.length : 0) });
  172. })
  173. // 作业未提交
  174. util.apiPost(urls.my_homework, { 'q.studentId': personId, 'q.status': 0 }).then(r => {
  175. // console.log('作业未提交 : ' + JSON.stringify(r));
  176. this.setData({ 'classList[1].num': (r ? r.length : 0) });
  177. });
  178. // 出勤率
  179. util.apiPost(urls.get_attendance_rate, { 'q.studentId': personId }).then(r => {
  180. console.log('出勤率 : ' + JSON.stringify(r));
  181. let attendRate = 0, alreadyCount = 0, shouldCount = 0;
  182. if (r != null) {
  183. r.forEach(it => {
  184. if (it.isAttend === '1') {
  185. alreadyCount = it.num;
  186. } else if (it.isAttend === '0') {
  187. shouldCount = it.num;
  188. }
  189. });
  190. }
  191. let t = (alreadyCount + shouldCount);
  192. if (t > 0) {
  193. attendRate = alreadyCount * 100 / t;
  194. }
  195. console.log(attendRate)
  196. this.setData({ 'classList[2].num': attendRate.toFixed(2) + '%' });
  197. })
  198. });
  199. }
  200. })