main.js 16 KB

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