student_select_class.js 6.6 KB

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