select_class.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // pages/select_class/select_class.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber } from '../../utils/util.js'
  3. import { TeacherSelectClassList, ConfirmTeacherClass} from '../../utils/api.js'
  4. import { pinyin } from '../../utils/hz2py_full.js'
  5. const { navigateTo, redirectTo, navigateBack } = routers()
  6. const { globalData, saveUserInfo, getWxloginCode, checkStatus } = getApp()
  7. const { baseImgUrl, thumbnail } = globalData
  8. const app = getApp()
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. list:[],
  15. HOT_NAME: '#',
  16. toView: 'B',
  17. scrollTop: 10,
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. var windowHeight = wx.getSystemInfoSync().windowHeight - 50;
  24. var scrollHeight_ = windowHeight +"px";
  25. this.setData({scrollHeight_})
  26. this.setData(options)
  27. },
  28. /**
  29. * 生命周期函数--监听页面初次渲染完成
  30. */
  31. onReady: function () {
  32. },
  33. /**
  34. * 生命周期函数--监听页面显示
  35. */
  36. onShow: function () {
  37. this.getClassList()
  38. },
  39. /**
  40. * 生命周期函数--监听页面隐藏
  41. */
  42. onHide: function () {
  43. },
  44. /**
  45. * 生命周期函数--监听页面卸载
  46. */
  47. onUnload: function () {
  48. },
  49. /**
  50. * 页面相关事件处理函数--监听用户下拉动作
  51. */
  52. onPullDownRefresh: function () {
  53. },
  54. /**
  55. * 页面上拉触底事件的处理函数
  56. */
  57. onReachBottom: function () {
  58. },
  59. /**
  60. * 用户点击右上角分享
  61. */
  62. onShareAppMessage: function () {
  63. },
  64. /**
  65. * 取消
  66. */
  67. cancel: function () {
  68. wx.navigateBack({})
  69. },
  70. /**
  71. * 选择班级
  72. */
  73. slectClass:function(e){
  74. const { idx, id,key} = e.currentTarget.dataset;
  75. const {list}=this.data;
  76. list[idx].items[key]['isCheck'] = list[idx].items[key].isCheck == '' ? 'active' : '';
  77. this.setData({ list })
  78. },
  79. /**
  80. * 老师选择加入班级列表
  81. */
  82. getClassList: function (cb) {
  83. const continuousFn = { fn: this.getClassList, param: { ...arguments } }
  84. const { teacherId } = this.data;
  85. TeacherSelectClassList({ data: { teacherId }, continuousFn }).then(res => {
  86. const {list}=res.data;
  87. const tempList = (res => {
  88. for (let i in res) {
  89. res[i]['index'] = pinyin.go(res[i].className).slice(0, 1).toUpperCase()
  90. }
  91. return res
  92. })(list || [])
  93. this.setData({ list: this.normalizeSinger(list)})
  94. if (isFn(cb)) cb()
  95. }).catch(res => {
  96. if (isFn(cb)) cb()
  97. })
  98. },
  99. /**
  100. * 确认选择班级
  101. */
  102. sureConfirmTeacherClass: function (cb) {
  103. const continuousFn = { fn: this.sureConfirmTeacherClass, param: { ...arguments } }
  104. const { teacherId, list } = this.data;
  105. var classIdList=[];
  106. for(var i in list){
  107. for (var j in list[i].items){
  108. if (list[i].items[j].isCheck=='active'){
  109. classIdList.push(list[i].items[j].classId)
  110. }
  111. }
  112. }
  113. ConfirmTeacherClass({ data: { teacherId, classIdList }, continuousFn }).then(res => {
  114. wx.showToast({
  115. title: '加入成功',
  116. icon: 'none'
  117. })
  118. this.cancel()
  119. if (isFn(cb)) cb()
  120. }).catch(res => {
  121. if (isFn(cb)) cb()
  122. })
  123. },
  124. /**
  125. * 字母导航
  126. */
  127. normalizeSinger(list) {
  128. let map = {
  129. hot: {
  130. title: this.data.HOT_NAME,
  131. items: []
  132. }
  133. }
  134. list.forEach((item, index) => {
  135. var parent = /^[A-Za-z]+$/;
  136. if (!parent.test(item.index)) {
  137. map.hot.items.push({
  138. classId: item.classId,
  139. className: item.className,
  140. isDefault: item.isDefault,
  141. studentSum: item.studentSum,
  142. teacherSum: item.teacherSum,
  143. isCheck: ''
  144. })
  145. }
  146. const key = item.index
  147. if (!map[key]) {
  148. map[key] = {
  149. title: key,
  150. items: []
  151. }
  152. }
  153. map[key].items.push({
  154. classId: item.classId,
  155. className: item.className,
  156. isDefault: item.isDefault,
  157. studentSum: item.studentSum,
  158. teacherSum: item.teacherSum,
  159. isCheck: ''
  160. })
  161. })
  162. // console.log(map)
  163. // 为了得到有序列表,我们需要处理 map
  164. let ret = []
  165. let hot = []
  166. for (let key in map) {
  167. let val = map[key]
  168. if (val.title.match(/[a-zA-Z]/)) {
  169. ret.push(val)
  170. } else if (val.title === this.data.HOT_NAME) {
  171. hot.push(val)
  172. }
  173. }
  174. ret.sort((a, b) => {
  175. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  176. })
  177. console.log(hot, ret)
  178. if (hot[0].items.length == 0) {
  179. return ret
  180. } else {
  181. return ret.concat(hot)
  182. }
  183. },
  184. // 点击首字母
  185. scrollToview(e) {
  186. const { id, idx } = e.currentTarget.dataset
  187. console.log(id, idx)
  188. this.setData({ currentIndex: idx, toView: id })
  189. wx.showToast({
  190. title: `${id}`,
  191. icon: 'none',
  192. duration: 500
  193. })
  194. },
  195. // 滚动时触发事件
  196. viewScroll: function (e) {
  197. var newY = e.detail.scrollTop;
  198. // this.scrollY(newY);
  199. },
  200. scrollY(newY) {
  201. const listHeight = this.data.listHeight
  202. // console.log(listHeight)
  203. // 当滚动到顶部,newY>0
  204. if (newY == 0 || newY < 0) {
  205. this.setData({
  206. currentIndex: 0,
  207. })
  208. return
  209. }
  210. // 在中间部分滚动
  211. for (let i = 0; i < listHeight.length - 1; i++) {
  212. // debugger
  213. let height1 = listHeight[i]
  214. let height2 = listHeight[i + 1]
  215. console.log(listHeight, i)
  216. if (newY >= height1 && newY < height2) {
  217. // console.log(i)
  218. this.setData({
  219. currentIndex: i,
  220. })
  221. return
  222. }
  223. }
  224. // 当滚动到底部,且-newY大于最后一个元素的上限
  225. this.setData({
  226. currentIndex: listHeight.length - 2,
  227. })
  228. },
  229. calculateHeight() {
  230. var lHeight = [],
  231. that = this;
  232. let height = 0;
  233. lHeight.push(height);
  234. var query = wx.createSelectorQuery();
  235. query.selectAll('.list-group').boundingClientRect(function (rects) {
  236. var rect = rects,
  237. len = rect.length;
  238. for (let i = 0; i < len; i++) {
  239. height += rect[i].height;
  240. lHeight.push(height)
  241. }
  242. }).exec();
  243. var calHeight = setInterval(function () {
  244. if (lHeight != [0]) {
  245. that.setData({
  246. listHeight: lHeight
  247. });
  248. clearInterval(calHeight);
  249. }
  250. }, 1000)
  251. },
  252. })