student_address_book.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import {pinyin} from '../../utils/hz2py_full.js'
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime } from '../../utils/util.js'
  3. import { AllStudentList } from '../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const app = getApp();
  6. const { globalData } = getApp()
  7. const { baseImgUrl, thumbnail } = globalData
  8. const TITLE_HEIGHT = 30
  9. Page({
  10. data: {
  11. baseImgUrl,
  12. thumbnail,
  13. isLoaded: false,
  14. toView: 'B',
  15. studentList: [],
  16. scrollTop: 10,
  17. HOT_NAME: '#',
  18. HOT_SINGER_LEN: 10,
  19. listHeight: [],
  20. currentIndex: 0,
  21. fixedTitle: '',
  22. fixedTop: 0,
  23. // studentName:'',
  24. nodata: {
  25. nodataObj: {
  26. image: '../../assets/no_search.png',
  27. text: '没有搜索结果'
  28. }
  29. },
  30. list: [
  31. // {
  32. // "vipId": "90782e09-c1a8-4f9d-9de8-337ef1adb2ef",
  33. // "studentName": "张某某",
  34. // index:'Z',
  35. // "school": "希望小学",
  36. // "grade": "一年级",
  37. // "imgUrl": "/2019/03/15/9bb54074bbe34f11bca5b8fca675bf43.jpg",
  38. // "checkedIn": 0,
  39. // "exited": 0
  40. // },
  41. // {
  42. // "vipId": "90782e09-c1a8-4f9d-9de8-337ef1adb2ef",
  43. // "studentName": "萧某某",
  44. // "school": "希望小学",
  45. // "grade": "四年级",
  46. // index: 'X',
  47. // "imgUrl": "/2019/03/15/9bb54074bbe34f11bca5b8fca675bf43.jpg",
  48. // "checkedIn": 1,
  49. // "exited": 1
  50. // },
  51. // {
  52. // "vipId": "90782e09-c1a8-4f9d-9de8-337ef1adb2ef",
  53. // "studentName": "李某某",
  54. // "school": "南山小学",
  55. // "grade": "一年级",
  56. // index: 'L',
  57. // "imgUrl": "/2019/03/15/9bb54074bbe34f11bca5b8fca675bf43.jpg",
  58. // "checkedIn": 1,
  59. // "exited": 0
  60. // },
  61. // {
  62. // "vipId": "90782e09-c1a8-4f9d-9de8-337ef1adb2ef",
  63. // "studentName": "王某某",
  64. // "school": "龙华小学",
  65. // "grade": "三年级",
  66. // index: 'W',
  67. // "imgUrl": "/2019/03/15/9bb54074bbe34f11bca5b8fca675bf43.jpg",
  68. // "checkedIn": 0,
  69. // "exited": 1
  70. // },
  71. ],
  72. footerData: {
  73. actIndex: 1,
  74. list: [
  75. {
  76. text: '任务',
  77. url: '../../assets/task_gray.png',
  78. actUrl: '../../assets/task_green.png',
  79. src: 'pages/task/task',
  80. id: 'task'
  81. },
  82. {
  83. text: '学生',
  84. url: '../../assets/student_gray.png',
  85. actUrl: '../../assets/student_green.png',
  86. src: 'pages/student_address_book/student_address_book',
  87. id: 'student_address_book'
  88. },
  89. {
  90. text: '我的',
  91. url: '../../assets/me_gray.png',
  92. actUrl: '../../assets/me_green.png',
  93. src: 'pages/me/me',
  94. id: 'me'
  95. }
  96. ]
  97. },
  98. },
  99. onLoad: function (options) {
  100. },
  101. /**
  102. * 生命周期函数--监听页面初次渲染完成
  103. */
  104. onReady: function () {
  105. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  106. for (const i in obj) {
  107. this[i] = obj[i]
  108. }
  109. },
  110. /**
  111. * 生命周期函数--监听页面显示
  112. */
  113. onShow: function () {
  114. this.getData()
  115. },
  116. /**
  117. * 生命周期函数--监听页面隐藏
  118. */
  119. onHide: function () {
  120. },
  121. /**
  122. * 生命周期函数--监听页面卸载
  123. */
  124. onUnload: function () {
  125. },
  126. /**
  127. * 页面相关事件处理函数--监听用户下拉动作
  128. */
  129. onPullDownRefresh: function () {
  130. },
  131. /**
  132. * 页面上拉触底事件的处理函数
  133. */
  134. onReachBottom: function () {
  135. },
  136. /**
  137. * 用户点击右上角分享
  138. */
  139. onShareAppMessage: function () {
  140. },
  141. /**
  142. * 重新搜索学生列表
  143. */
  144. blurCtl: function () {
  145. // const { navIndex, navList } = this.data
  146. // navList[navIndex]['pageNo'] = 1
  147. // this.setData({ navList })
  148. this.getData()
  149. },
  150. /**
  151. * 保存学生姓名
  152. */
  153. inputCtl: function (e) {
  154. const { value } = e.detail
  155. const { studentName } = this.data
  156. if (studentName == value) {
  157. return
  158. }
  159. this.setData({ studentName: value })
  160. },
  161. /**
  162. * 获取数据
  163. */
  164. getData: function (cb) {
  165. const continuousFn = { fn: this.getData, param: { ...arguments } }
  166. const { studentName } = this.data
  167. AllStudentList({ data: { studentName }, continuousFn }).then(res => {
  168. const { list = [] } = res.data
  169. const tempList = (res => {
  170. for (let i in res) {
  171. res[i]['index'] = pinyin.go(res[i].studentName).slice(0, 1).toUpperCase()
  172. }
  173. return res
  174. })(res.data.list || [])
  175. // console.log(this.normalizeSinger(list))
  176. this.setData({ studentList: this.normalizeSinger(list), isLoaded: true})
  177. this.calculateHeight()
  178. if (isFn(cb)) cb()
  179. }).catch(res => {
  180. this.setData({ isLoaded: true })
  181. if (isFn(cb)) cb()
  182. })
  183. },
  184. normalizeSinger(list) {
  185. let map = {
  186. hot: {
  187. title: this.data.HOT_NAME,
  188. items: []
  189. }
  190. }
  191. list.forEach((item, index) => {
  192. var parent = /^[A-Za-z]+$/;
  193. if (!parent.test(item.index)){
  194. map.hot.items.push({
  195. studentName: item.studentName,
  196. imgUrl: item.imgUrl,
  197. school: item.school,
  198. checkedIn: item.checkedIn,
  199. grade: item.grade,
  200. exited: item.exited,
  201. vipId: item.vipId
  202. })
  203. }
  204. const key = item.index
  205. if (!map[key]) {
  206. map[key] = {
  207. title: key,
  208. items: []
  209. }
  210. }
  211. map[key].items.push({
  212. studentName: item.studentName,
  213. imgUrl: item.imgUrl,
  214. school: item.school,
  215. checkedIn: item.checkedIn,
  216. grade: item.grade,
  217. exited: item.exited,
  218. vipId: item.vipId
  219. })
  220. })
  221. // 为了得到有序列表,我们需要处理 map
  222. let ret = []
  223. let hot = []
  224. for (let key in map) {
  225. let val = map[key]
  226. if (val.title.match(/[a-zA-Z]/)) {
  227. ret.push(val)
  228. } else if (val.title === this.data.HOT_NAME) {
  229. hot.push(val)
  230. }
  231. }
  232. ret.sort((a, b) => {
  233. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  234. })
  235. if (hot[0].items.length==0){
  236. return ret
  237. }else{
  238. return ret.concat(hot)
  239. }
  240. },
  241. // 点击首字母
  242. scrollToview(e) {
  243. const { id,idx} = e.currentTarget.dataset
  244. console.log(id, idx)
  245. this.setData({ currentIndex: idx, toView: id})
  246. wx.showToast({
  247. title: `${id}`,
  248. icon: 'none',
  249. duration:500
  250. })
  251. },
  252. // 滚动时触发事件
  253. viewScroll:function(e){
  254. var newY = e.detail.scrollTop;
  255. // this.scrollY(newY);
  256. },
  257. scrollY(newY) {
  258. const listHeight = this.data.listHeight
  259. // console.log(listHeight)
  260. // 当滚动到顶部,newY>0
  261. if (newY == 0 || newY < 0) {
  262. this.setData({
  263. currentIndex: 0,
  264. })
  265. return
  266. }
  267. // 在中间部分滚动
  268. for (let i = 0; i < listHeight.length-1; i++) {
  269. // debugger
  270. let height1 = listHeight[i]
  271. let height2 = listHeight[i + 1]
  272. console.log(listHeight,i)
  273. if (newY >= height1 && newY < height2) {
  274. // console.log(i)
  275. this.setData({
  276. currentIndex: i,
  277. })
  278. return
  279. }
  280. }
  281. // 当滚动到底部,且-newY大于最后一个元素的上限
  282. this.setData({
  283. currentIndex: listHeight.length - 2,
  284. })
  285. },
  286. calculateHeight() {
  287. var lHeight = [],
  288. that = this;
  289. let height = 0;
  290. lHeight.push(height);
  291. var query = wx.createSelectorQuery();
  292. query.selectAll('.list-group').boundingClientRect(function (rects) {
  293. var rect = rects,
  294. len = rect.length;
  295. for (let i = 0; i < len; i++) {
  296. height += rect[i].height;
  297. lHeight.push(height)
  298. }
  299. }).exec();
  300. var calHeight = setInterval(function () {
  301. if (lHeight != [0]) {
  302. that.setData({
  303. listHeight: lHeight
  304. });
  305. clearInterval(calHeight);
  306. }
  307. }, 1000)
  308. },
  309. })