child_list.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // pages/child_list/child_list.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber } from '../../utils/util.js'
  3. import { pinyin } from '../../utils/hz2py_full.js'
  4. import { StudentList, MyInfo, ClassTeacherList, ChangeClass, ConfirmClassTeacher, DetachTeacher} from '../../utils/api.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. baseImgUrl,
  15. thumbnail,
  16. statusBar: app.globalData.statusBar,
  17. isLoaded: false,
  18. isChild:false,
  19. searchKey: '',
  20. checkNum:0,
  21. isCheck:false,
  22. pageNo: 1,
  23. pageSize: 50,
  24. isAll: false,
  25. isAllType:1,
  26. isType:1,
  27. isAdd:false,
  28. isAddTeacher:false,
  29. hiddenModel:false,
  30. navList: ['学生','教师'],
  31. navIdx:0,
  32. HOT_NAME: '#',
  33. toView: 'A',
  34. scrollTop: 10,
  35. nodata: [
  36. {
  37. text: '暂无学生'
  38. },
  39. {
  40. text: '暂无教师'
  41. },
  42. ],
  43. list: []
  44. },
  45. /**
  46. * 生命周期函数--监听页面加载
  47. */
  48. onLoad: function (options) {
  49. var windowHeight = wx.getSystemInfoSync().windowHeight - 190;
  50. var scrollHeight_ = windowHeight +"px";
  51. this.setData({scrollHeight_})
  52. this.setData(options)
  53. // console.log(options)
  54. if (options.className){
  55. wx.setNavigationBarTitle({
  56. title: `${options.className}`,
  57. })
  58. }
  59. },
  60. /**
  61. * 生命周期函数--监听页面初次渲染完成
  62. */
  63. onReady: function () {
  64. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  65. for (const i in obj) {
  66. this[i] = obj[i]
  67. }
  68. },
  69. /**
  70. * 生命周期函数--监听页面显示
  71. */
  72. onShow: function () {
  73. const { navIdx } = this.data;
  74. this.setData({ pageNo: 1 })
  75. if (navIdx == 0) {
  76. this.getData();
  77. } else {
  78. this.teacherGetData()
  79. }
  80. },
  81. /**
  82. * 生命周期函数--监听页面隐藏
  83. */
  84. onHide: function () {
  85. },
  86. /**
  87. * 生命周期函数--监听页面卸载
  88. */
  89. onUnload: function () {
  90. },
  91. /**
  92. * 页面相关事件处理函数--监听用户下拉动作
  93. */
  94. onPullDownRefresh: function () {
  95. const { navIdx, isAdd, isAddTeacher} = this.data
  96. if (navIdx==0&&!isAdd){
  97. this.setData({ pageNo: 1 })
  98. this.getData(res => {
  99. wx.stopPullDownRefresh()
  100. })
  101. }
  102. // else if (isAddTeacher && navIdx == 1){
  103. // this.setData({ pageNo: 1 })
  104. // this.getAddTeacher(res => {
  105. // wx.stopPullDownRefresh()
  106. // })
  107. // }
  108. },
  109. /**
  110. * 页面上拉触底事件的处理函数
  111. */
  112. onReachBottom: function () {
  113. const { isAll, pageNo, navIdx, isAddTeacher } = this.data
  114. if (isAll) {
  115. return
  116. }
  117. wx.showLoading({
  118. title: '加载中',
  119. })
  120. if (navIdx==0){
  121. this.getData()
  122. }
  123. // else if (isAddTeacher && navIdx==1){
  124. // this.getAddTeacher()
  125. // }
  126. },
  127. /**
  128. * 用户点击右上角分享
  129. */
  130. onShareAppMessage: function () {
  131. if (app.sharePageDefaultCtl) {
  132. return app.sharePageDefaultCtl()
  133. }
  134. },
  135. /**
  136. * 跳转
  137. */
  138. navigateCtl:function(e){
  139. // const { parentVipList } = this.data
  140. var parentVipList = wx.getStorageSync('parentVipList')
  141. if (parentVipList.length>0) {
  142. app.navigateCtl(e);
  143. } else {
  144. wx.showToast({
  145. title: '请勾选孩子后再试',
  146. icon: 'none'
  147. })
  148. }
  149. },
  150. /**
  151. * 底部导航跳转
  152. */
  153. redirectCtl: app.redirectCtl,
  154. /**
  155. * 添加老师
  156. */
  157. addTeacher:function(){
  158. wx.setNavigationBarTitle({
  159. title: `添加老师`,
  160. })
  161. this.setData({ pageNo: 1, isAddTeacher: true,isAdd:true,list:[]})
  162. this.getAddTeacher()
  163. },
  164. /**
  165. * 提示框打开
  166. */
  167. addTeacherShow:function(e){
  168. const {id}=e.currentTarget.dataset
  169. this.setData({ hiddenModel: true, teacherId:id })
  170. },
  171. /**
  172. * 提示框隐藏
  173. */
  174. allShowModel: function () {
  175. this.setData({ hiddenModel: false})
  176. },
  177. /**
  178. * 选择老师
  179. */
  180. selectTeacher:function(e){
  181. const { idx } = e.currentTarget.dataset
  182. const { navIdx } = this.data
  183. this.setData({ pageNo: 1, pageSize: 10, navIdx: idx })
  184. wx.showLoading({
  185. title: '加载中',
  186. })
  187. if(idx==0){
  188. this.getData()
  189. }else{
  190. this.teacherGetData()
  191. }
  192. },
  193. /**
  194. * 获取老师列表
  195. */
  196. teacherGetData: function (cb) {
  197. const continuousFn = { fn: this.teacherGetData, param: { ...arguments } }
  198. const { classId} = this.data;
  199. var that=this
  200. ClassTeacherList({ data: { classId }, continuousFn }).then(res => {
  201. wx.hideLoading()
  202. const {list} = res.data;
  203. const tempList = (res => {
  204. for (let i in res) {
  205. res[i]['subject'] = res[i].subject.split(',')
  206. res[i]['index'] = pinyin.go(res[i].name).slice(0, 1).toUpperCase()
  207. }
  208. return res
  209. })(list || [])
  210. // console.log(list)
  211. this.setData({
  212. teacherList: that.normalizeSinger(list),
  213. isLoaded: true
  214. })
  215. if (isFn(cb)) cb()
  216. }).catch(res => {
  217. this.setData({ isLoaded: true })
  218. if (isFn(cb)) cb()
  219. })
  220. },
  221. /**
  222. * 班级里移除老师
  223. */
  224. deleteTeacher: function (cb) {
  225. const continuousFn = { fn: this.deleteTeacher, param: { ...arguments } }
  226. const { classId, teacherId} = this.data;
  227. DetachTeacher({ data: { classId, teacherId}, continuousFn }).then(res => {
  228. wx.showToast({
  229. title: '移除教师成功',
  230. icon: 'none',
  231. duration:1000
  232. })
  233. this.teacherGetData()
  234. this.setData({ hiddenModel:false })
  235. if (isFn(cb)) cb()
  236. }).catch(res => {
  237. this.setData({ isLoaded: true })
  238. if (isFn(cb)) cb()
  239. })
  240. },
  241. /**
  242. * 添加学生更换班级确认
  243. */
  244. addStudentShow:function(){
  245. wx.setNavigationBarTitle({
  246. title: `添加学生`,
  247. })
  248. this.setData({ isAdd: true, isAllType: 2, isLoaded: false, pageNo: 1, list: [], searchKey:''})
  249. this.getData();
  250. },
  251. /**
  252. * 添加学生更换班级确认
  253. */
  254. addStudent: function (cb) {
  255. const continuousFn = { fn: this.addStudent, param: { ...arguments } }
  256. const { classId, vipIds=[], className}=this.data
  257. if (vipIds.length == 0) {
  258. wx.showToast({
  259. title: '请添加学生',
  260. icon: 'none'
  261. })
  262. return
  263. }
  264. ChangeClass({ data: { vipIds, classId}, continuousFn }).then(res => {
  265. wx.showToast({
  266. title: '添加学生成功',
  267. icon: 'none'
  268. })
  269. wx.setNavigationBarTitle({
  270. title: `${className}`,
  271. })
  272. this.setData({ isAdd: false, isAllType: 1, searchKey: '', list: [], isLoaded: false, pageNo: 1, checkNum:0})
  273. this.getData();
  274. if (isFn(cb)) cb()
  275. }).catch(res => {
  276. if (isFn(cb)) cb()
  277. })
  278. },
  279. /**
  280. * 班级添加老师
  281. */
  282. addTeacherList:function(){
  283. const continuousFn = { fn: this.addTeacherList, param: { ...arguments } }
  284. const { classId, teacherIdList=[], className } = this.data
  285. if (teacherIdList.length==0){
  286. wx.showToast({
  287. title: '请添加教师',
  288. icon: 'none'
  289. })
  290. return
  291. }
  292. ConfirmClassTeacher({ data: { teacherIdList, classId }, continuousFn }).then(res => {
  293. wx.showToast({
  294. title: '添加教师成功',
  295. icon: 'none'
  296. })
  297. wx.setNavigationBarTitle({
  298. title: `${className}`,
  299. })
  300. this.setData({ isAdd: false, teacherList: [], isLoaded: false, isAddTeacher: false, pageNo: 1, checkNum: 0 })
  301. this.teacherGetData();
  302. if (isFn(cb)) cb()
  303. }).catch(res => {
  304. if (isFn(cb)) cb()
  305. })
  306. },
  307. /**
  308. * 选择续费学生
  309. */
  310. selectPay:function(){
  311. this.setData({ isChild: true, isAllType: 1, pageNo: 1, searchKey:''});
  312. this.getData();
  313. },
  314. /**
  315. * 续费搜索学生列表
  316. */
  317. payBlurCtl: function () {
  318. const { searchKey } = this.data
  319. if (!searchKey) {
  320. wx.showToast({
  321. title: '搜索内容不能为空',
  322. icon: 'none'
  323. })
  324. return
  325. }
  326. this.setData({ pageNo: 1, isAllType:1 });
  327. this.getData()
  328. },
  329. /**
  330. * 重新搜索学生列表
  331. */
  332. cancel:function(){
  333. this.setData({ isAllType: 0, isChild: false, pageNo: 1 })
  334. this.getData();
  335. },
  336. /**
  337. * 保存学生姓名
  338. */
  339. inputCtl: function (e) {
  340. const { value } = e.detail
  341. const { searchKey } = this.data
  342. if (searchKey == value) {
  343. return
  344. }
  345. this.setData({ searchKey: value })
  346. },
  347. /**
  348. * 重新搜索学生列表
  349. */
  350. blurCtl: function () {
  351. const { searchKey } = this.data
  352. this.setData({ pageNo: 1 });
  353. this.getData()
  354. },
  355. /**
  356. * 选择学生
  357. */
  358. selectLiCtl: function (e) {
  359. const { index,key} = e.currentTarget.dataset
  360. const { list, isCheck,navIdx,teacherList } = this.data
  361. console.log(navIdx,teacherList)
  362. if(navIdx==0){
  363. list[index]['isCheck'] = list[index].isCheck == '' ? 'active' : '';
  364. this.setData({ list})
  365. this.studentCaculate(list)
  366. }else{
  367. teacherList[index].items[key]['isCheck'] = teacherList[index].items[key].isCheck == '' ? 'active' : '';
  368. this.setData({ teacherList})
  369. this.caculate(teacherList)
  370. }
  371. },
  372. /**
  373. * 计算学生总数和vip列表
  374. */
  375. studentCaculate: function (list) {
  376. let checkNum = 0
  377. const vipIds = [];
  378. for (let i in list) {
  379. if (list[i]['isCheck'] =='active') {
  380. checkNum++
  381. vipIds.push(list[i]['vipId'])
  382. }
  383. }
  384. this.setData({ checkNum, vipIds})
  385. },
  386. /**
  387. * 计算教师总数和vip列表
  388. */
  389. caculate: function (list) {
  390. let checkNum = 0
  391. const vipIds = [];
  392. const teacherIdList = [];
  393. for (let i in list) {
  394. for (let j in list[i].items){
  395. if (list[i].items[j]['isCheck'] =='active') {
  396. checkNum++
  397. vipIds.push(list[i].items[j]['vipId'])
  398. teacherIdList.push(list[i].items[j]['teacherId'])
  399. }
  400. }
  401. }
  402. this.setData({ checkNum, vipIds, teacherIdList})
  403. },
  404. /**
  405. * 获取学生数据
  406. */
  407. getData: function (cb) {
  408. const continuousFn = { fn: this.getData, param: { ...arguments } }
  409. const { searchKey, isAllType, pageNo, pageSize, list=[], isAdd, classId}=this.data;
  410. var that=this;
  411. StudentList({ data: { searchKey, type: isAllType,classId,pageNo,pageSize}, continuousFn }).then(res => {
  412. wx.hideLoading();
  413. // const {isCheck } = this.data;
  414. const {uploadHomeworkSum,studentSum,correctSum } = res.data;
  415. const temp = res.data.list;
  416. const tempList = (res => {
  417. for (let i in res) {
  418. // res[i]['isCheck'] = ''
  419. res[i]['index'] = pinyin.go(res[i].childName).slice(0, 1).toUpperCase()
  420. if (res[i].childName.length>5){
  421. res[i].childName = res[i].childName.slice(0, 5)+ '...'
  422. }else{
  423. res[i].childName = res[i].childName
  424. }
  425. }
  426. return res
  427. })(temp || [])
  428. let arr = []
  429. if (pageNo == 1) {
  430. arr = [...tempList]
  431. } else {
  432. arr = [...list, ...tempList]
  433. }
  434. this.setData({
  435. uploadHomeworkSum,studentSum,correctSum,
  436. list: arr,
  437. studentList:that.normalizeSinger(arr),
  438. pageNo: tempList.length == pageSize ? Number(pageNo) + 1 : pageNo,
  439. isAll: tempList.length < pageSize,
  440. isLoaded: true
  441. })
  442. if (isFn(cb)) cb()
  443. }).catch(res => {
  444. this.setData({ isLoaded: true })
  445. if (isFn(cb)) cb()
  446. })
  447. },
  448. /**
  449. * 获取添加老师列表
  450. */
  451. getAddTeacher: function (cb) {
  452. const continuousFn = { fn: this.getData, param: { ...arguments } }
  453. const { pageNo, pageSize, list = [],classId } = this.data;
  454. var that=this;
  455. ClassTeacherList({ data: {classId,type:1}, continuousFn }).then(res => {
  456. const { isCheck } = this.data;
  457. const temp = res.data.list;
  458. const tempList = (res => {
  459. for (let i in res) {
  460. // res[i]['isCheck'] = ''
  461. res[i]['subject'] = res[i].subject.split(',')
  462. res[i]['index'] = pinyin.go(res[i].name).slice(0, 1).toUpperCase()
  463. }
  464. return res
  465. })(temp || [])
  466. let arr = []
  467. if (pageNo == 1) {
  468. arr = [...tempList]
  469. } else {
  470. arr = [...list, ...tempList]
  471. }
  472. this.setData({
  473. teacherList: that.normalizeSinger(arr),
  474. pageNo: tempList.length == pageSize ? Number(pageNo) + 1 : pageNo,
  475. isAll: tempList.length < pageSize,
  476. isLoaded: true
  477. })
  478. if (isFn(cb)) cb()
  479. }).catch(res => {
  480. this.setData({ isLoaded: true })
  481. if (isFn(cb)) cb()
  482. })
  483. },
  484. /**
  485. * 字母导航
  486. */
  487. normalizeSinger(list) {
  488. // console.log(list)
  489. const { navIdx}=this.data;
  490. let map = {
  491. hot: {
  492. title: this.data.HOT_NAME,
  493. items: []
  494. }
  495. }
  496. if (navIdx==0){
  497. list.forEach((item, index) => {
  498. // console.log(item)
  499. var parent = /^[A-Za-z]+$/;
  500. if (!parent.test(item.index)) {
  501. map.hot.items.push({
  502. childName: item.childName,
  503. classId: item.classId,
  504. className: item.className,
  505. grade: item.grade,
  506. imgUrl: item.imgUrl,
  507. serviceType: item.serviceType,
  508. vipId: item.vipId,
  509. phone:item.phone,
  510. homeworkStatus:item.homeworkStatus
  511. })
  512. }
  513. const key = item.index
  514. if (!map[key]) {
  515. map[key] = {
  516. title: key,
  517. items: []
  518. }
  519. }
  520. map[key].items.push({
  521. childName: item.childName,
  522. classId: item.classId,
  523. className: item.className,
  524. grade: item.grade,
  525. imgUrl: item.imgUrl,
  526. serviceType: item.serviceType,
  527. vipId: item.vipId,
  528. phone: item.phone,
  529. homeworkStatus:item.homeworkStatus
  530. })
  531. })
  532. }else if(navIdx == 1){
  533. list.forEach((item, index) => {
  534. var parent = /^[A-Za-z]+$/;
  535. if (!parent.test(item.index)) {
  536. map.hot.items.push({
  537. name: item.name,
  538. phone: item.phone,
  539. subject: item.subject,
  540. imgUrl: item.imgUrl,
  541. teacherTypeList: item.teacherTypeList,
  542. teacherId: item.teacherId,
  543. isCheck: ''
  544. })
  545. }
  546. const key = item.index
  547. if (!map[key]) {
  548. map[key] = {
  549. title: key,
  550. items: []
  551. }
  552. }
  553. map[key].items.push({
  554. name: item.name,
  555. phone: item.phone,
  556. subject: item.subject,
  557. imgUrl: item.imgUrl,
  558. teacherTypeList: item.teacherTypeList,
  559. teacherId: item.teacherId,
  560. isCheck: ''
  561. })
  562. })
  563. }
  564. // console.log(map)
  565. // 为了得到有序列表,我们需要处理 map
  566. let ret = []
  567. let hot = []
  568. for (let key in map) {
  569. let val = map[key]
  570. if (val.title.match(/[a-zA-Z]/)) {
  571. ret.push(val)
  572. } else if (val.title === this.data.HOT_NAME) {
  573. hot.push(val)
  574. }
  575. }
  576. ret.sort((a, b) => {
  577. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  578. })
  579. // console.log(hot, ret)
  580. if (hot[0].items.length == 0) {
  581. return ret
  582. } else {
  583. return ret.concat(hot)
  584. }
  585. },
  586. // 点击首字母
  587. scrollToview(e) {
  588. const { id, idx } = e.currentTarget.dataset
  589. console.log(id, idx)
  590. this.setData({ currentIndex: idx, toView: id })
  591. wx.showToast({
  592. title: `${id}`,
  593. icon: 'none',
  594. duration: 500
  595. })
  596. },
  597. // 滚动时触发事件
  598. viewScroll: function (e) {
  599. var newY = e.detail.scrollTop;
  600. // this.scrollY(newY);
  601. },
  602. scrollY(newY) {
  603. const listHeight = this.data.listHeight
  604. // console.log(listHeight)
  605. // 当滚动到顶部,newY>0
  606. if (newY == 0 || newY < 0) {
  607. this.setData({
  608. currentIndex: 0,
  609. })
  610. return
  611. }
  612. // 在中间部分滚动
  613. for (let i = 0; i < listHeight.length - 1; i++) {
  614. // debugger
  615. let height1 = listHeight[i]
  616. let height2 = listHeight[i + 1]
  617. // console.log(listHeight, i)
  618. if (newY >= height1 && newY < height2) {
  619. // console.log(i)
  620. this.setData({
  621. currentIndex: i,
  622. })
  623. return
  624. }
  625. }
  626. // 当滚动到底部,且-newY大于最后一个元素的上限
  627. this.setData({
  628. currentIndex: listHeight.length - 2,
  629. })
  630. },
  631. /**
  632. * 返回孩子信息
  633. */
  634. blockInfo:function(){
  635. const { classId} = this.data
  636. this.redirectCtl({ url: 'main', method: { classId} }, true)
  637. },
  638. calculateHeight() {
  639. var lHeight = [],
  640. that = this;
  641. let height = 0;
  642. lHeight.push(height);
  643. var query = wx.createSelectorQuery();
  644. query.selectAll('.list-group').boundingClientRect(function (rects) {
  645. var rect = rects,
  646. len = rect.length;
  647. for (let i = 0; i < len; i++) {
  648. height += rect[i].height;
  649. lHeight.push(height)
  650. }
  651. }).exec();
  652. var calHeight = setInterval(function () {
  653. if (lHeight != [0]) {
  654. that.setData({
  655. listHeight: lHeight
  656. });
  657. clearInterval(calHeight);
  658. }
  659. }, 1000)
  660. }
  661. })