add_teacher.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 { 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: 10,
  24. isAll: false,
  25. isAllType:1,
  26. isType:1,
  27. hiddenModel:false,
  28. HOT_NAME: '#',
  29. toView: 'A',
  30. scrollTop: 10,
  31. nodata: [
  32. {
  33. text: '暂无教师'
  34. },
  35. ],
  36. list: [
  37. // {
  38. // "teacherId": "asdasd",
  39. // "teacherTypeList": ["2", "3"],
  40. // "subject": "语文,数学",
  41. // "name": "王的大",
  42. // "imgUrl": "",
  43. // "phone": "",
  44. // "classSum": 10
  45. // }
  46. ]
  47. },
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad: function (options) {
  52. var windowHeight = wx.getSystemInfoSync().windowHeight - 70;
  53. var scrollHeight_ = windowHeight +"px";
  54. this.setData({scrollHeight_})
  55. this.setData(options)
  56. },
  57. /**
  58. * 生命周期函数--监听页面初次渲染完成
  59. */
  60. onReady: function () {
  61. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  62. for (const i in obj) {
  63. this[i] = obj[i]
  64. }
  65. },
  66. /**
  67. * 生命周期函数--监听页面显示
  68. */
  69. onShow: function () {
  70. this.setData({ pageNo: 1 })
  71. this.getAddTeacher()
  72. },
  73. /**
  74. * 生命周期函数--监听页面隐藏
  75. */
  76. onHide: function () {
  77. },
  78. /**
  79. * 生命周期函数--监听页面卸载
  80. */
  81. onUnload: function () {
  82. },
  83. /**
  84. * 页面相关事件处理函数--监听用户下拉动作
  85. */
  86. onPullDownRefresh: function () {
  87. },
  88. /**
  89. * 页面上拉触底事件的处理函数
  90. */
  91. onReachBottom: function () {
  92. },
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function () {
  97. if (app.sharePageDefaultCtl) {
  98. return app.sharePageDefaultCtl()
  99. }
  100. },
  101. /**
  102. * 跳转
  103. */
  104. navigateCtl:function(e){
  105. // const { parentVipList } = this.data
  106. var parentVipList = wx.getStorageSync('parentVipList')
  107. if (parentVipList.length>0) {
  108. app.navigateCtl(e);
  109. } else {
  110. wx.showToast({
  111. title: '请勾选孩子后再试',
  112. icon: 'none'
  113. })
  114. }
  115. },
  116. /**
  117. * 底部导航跳转
  118. */
  119. redirectCtl: app.redirectCtl,
  120. /**
  121. * 添加老师
  122. */
  123. addTeacher:function(){
  124. wx.setNavigationBarTitle({
  125. title: `添加老师`,
  126. })
  127. this.setData({ pageNo: 1, isAddTeacher: true,isAdd:true,list:[]})
  128. this.getAddTeacher()
  129. },
  130. /**
  131. * 提示框隐藏
  132. */
  133. allShowModel: function () {
  134. this.setData({ hiddenModel: false})
  135. },
  136. /**
  137. * 选择老师
  138. */
  139. selectTeacher:function(e){
  140. const { idx } = e.currentTarget.dataset
  141. const { navIdx } = this.data
  142. this.setData({ pageNo: 1, pageSize: 10, navIdx: idx })
  143. wx.showLoading({
  144. title: '加载中',
  145. })
  146. if(idx==0){
  147. this.getData()
  148. }else{
  149. this.teacherGetData()
  150. }
  151. },
  152. /**
  153. * 班级添加老师
  154. */
  155. addTeacherList:function(){
  156. const continuousFn = { fn: this.addTeacherList, param: { ...arguments } }
  157. const { classId, teacherIdList=[], className } = this.data
  158. if (teacherIdList.length==0){
  159. wx.showToast({
  160. title: '请添加教师',
  161. icon: 'none'
  162. })
  163. return
  164. }
  165. ConfirmClassTeacher({ data: { teacherIdList, classId }, continuousFn }).then(res => {
  166. wx.showToast({
  167. title: '添加教师成功',
  168. icon: 'none',
  169. duration:1000
  170. })
  171. setTimeout((function callback() {
  172. wx.navigateBack({
  173. delta: 1
  174. })
  175. }).bind(this), 500);
  176. if (isFn(cb)) cb()
  177. }).catch(res => {
  178. if (isFn(cb)) cb()
  179. })
  180. },
  181. /**
  182. * 选择学生
  183. */
  184. selectLiCtl: function (e) {
  185. const { index,key} = e.currentTarget.dataset
  186. const { isCheck,navIdx,teacherList } = this.data
  187. teacherList[index].items[key]['isCheck'] = teacherList[index].items[key].isCheck == '' ? 'active' : '';
  188. this.setData({ teacherList})
  189. this.caculate(teacherList)
  190. },
  191. /**
  192. * 计算学生总数和vip列表
  193. */
  194. studentCaculate: function (list) {
  195. let checkNum = 0
  196. const vipIds = [];
  197. for (let i in list) {
  198. if (list[i]['isCheck'] =='active') {
  199. checkNum++
  200. vipIds.push(list[i]['vipId'])
  201. }
  202. }
  203. this.setData({ checkNum, vipIds})
  204. },
  205. /**
  206. * 计算教师总数和vip列表
  207. */
  208. caculate: function (list) {
  209. let checkNum = 0
  210. const vipIds = [];
  211. const teacherIdList = [];
  212. for (let i in list) {
  213. for (let j in list[i].items){
  214. if (list[i].items[j]['isCheck'] =='active') {
  215. checkNum++
  216. vipIds.push(list[i].items[j]['vipId'])
  217. teacherIdList.push(list[i].items[j]['teacherId'])
  218. }
  219. }
  220. }
  221. this.setData({ checkNum, vipIds, teacherIdList})
  222. },
  223. /**
  224. * 获取添加老师列表
  225. */
  226. getAddTeacher: function (cb) {
  227. const continuousFn = { fn: this.getData, param: { ...arguments } }
  228. const { pageNo, pageSize, list = [],classId } = this.data;
  229. var that=this;
  230. ClassTeacherList({ data: {classId,type:1}, continuousFn }).then(res => {
  231. const { isCheck } = this.data;
  232. const temp = res.data.list;
  233. const tempList = (res => {
  234. for (let i in res) {
  235. // res[i]['isCheck'] = ''
  236. res[i]['subject'] = res[i].subject.split(',')
  237. res[i]['index'] = pinyin.go(res[i].name).slice(0, 1).toUpperCase()
  238. }
  239. return res
  240. })(temp || [])
  241. let arr = []
  242. if (pageNo == 1) {
  243. arr = [...tempList]
  244. } else {
  245. arr = [...list, ...tempList]
  246. }
  247. this.setData({
  248. teacherList: that.normalizeSinger(arr),
  249. pageNo: tempList.length == pageSize ? Number(pageNo) + 1 : pageNo,
  250. isAll: tempList.length < pageSize,
  251. isLoaded: true
  252. })
  253. if (isFn(cb)) cb()
  254. }).catch(res => {
  255. this.setData({ isLoaded: true })
  256. if (isFn(cb)) cb()
  257. })
  258. },
  259. /**
  260. * 字母导航
  261. */
  262. normalizeSinger(list) {
  263. const { navIdx}=this.data;
  264. let map = {
  265. hot: {
  266. title: this.data.HOT_NAME,
  267. items: []
  268. }
  269. }
  270. list.forEach((item, index) => {
  271. var parent = /^[A-Za-z]+$/;
  272. if (!parent.test(item.index)) {
  273. map.hot.items.push({
  274. name: item.name,
  275. phone: item.phone,
  276. subject: item.subject,
  277. imgUrl: item.imgUrl,
  278. teacherTypeList: item.teacherTypeList,
  279. teacherId: item.teacherId,
  280. isCheck: ''
  281. })
  282. }
  283. const key = item.index
  284. if (!map[key]) {
  285. map[key] = {
  286. title: key,
  287. items: []
  288. }
  289. }
  290. map[key].items.push({
  291. name: item.name,
  292. phone: item.phone,
  293. subject: item.subject,
  294. imgUrl: item.imgUrl,
  295. teacherTypeList: item.teacherTypeList,
  296. teacherId: item.teacherId,
  297. isCheck: ''
  298. })
  299. })
  300. // console.log(map)
  301. // 为了得到有序列表,我们需要处理 map
  302. let ret = []
  303. let hot = []
  304. for (let key in map) {
  305. let val = map[key]
  306. if (val.title.match(/[a-zA-Z]/)) {
  307. ret.push(val)
  308. } else if (val.title === this.data.HOT_NAME) {
  309. hot.push(val)
  310. }
  311. }
  312. ret.sort((a, b) => {
  313. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  314. })
  315. // console.log(hot, ret)
  316. if (hot[0].items.length == 0) {
  317. return ret
  318. } else {
  319. return ret.concat(hot)
  320. }
  321. },
  322. // 点击首字母
  323. scrollToview(e) {
  324. const { id, idx } = e.currentTarget.dataset
  325. console.log(id, idx)
  326. this.setData({ currentIndex: idx, toView: id })
  327. wx.showToast({
  328. title: `${id}`,
  329. icon: 'none',
  330. duration: 500
  331. })
  332. },
  333. // 滚动时触发事件
  334. viewScroll: function (e) {
  335. var newY = e.detail.scrollTop;
  336. // this.scrollY(newY);
  337. },
  338. scrollY(newY) {
  339. const listHeight = this.data.listHeight
  340. // console.log(listHeight)
  341. // 当滚动到顶部,newY>0
  342. if (newY == 0 || newY < 0) {
  343. this.setData({
  344. currentIndex: 0,
  345. })
  346. return
  347. }
  348. // 在中间部分滚动
  349. for (let i = 0; i < listHeight.length - 1; i++) {
  350. // debugger
  351. let height1 = listHeight[i]
  352. let height2 = listHeight[i + 1]
  353. // console.log(listHeight, i)
  354. if (newY >= height1 && newY < height2) {
  355. // console.log(i)
  356. this.setData({
  357. currentIndex: i,
  358. })
  359. return
  360. }
  361. }
  362. // 当滚动到底部,且-newY大于最后一个元素的上限
  363. this.setData({
  364. currentIndex: listHeight.length - 2,
  365. })
  366. },
  367. /**
  368. * 返回孩子信息
  369. */
  370. blockInfo:function(){
  371. const { classId} = this.data
  372. this.redirectCtl({ url: 'main', method: { classId} }, true)
  373. },
  374. calculateHeight() {
  375. var lHeight = [],
  376. that = this;
  377. let height = 0;
  378. lHeight.push(height);
  379. var query = wx.createSelectorQuery();
  380. query.selectAll('.list-group').boundingClientRect(function (rects) {
  381. var rect = rects,
  382. len = rect.length;
  383. for (let i = 0; i < len; i++) {
  384. height += rect[i].height;
  385. lHeight.push(height)
  386. }
  387. }).exec();
  388. var calHeight = setInterval(function () {
  389. if (lHeight != [0]) {
  390. that.setData({
  391. listHeight: lHeight
  392. });
  393. clearInterval(calHeight);
  394. }
  395. }, 1000)
  396. }
  397. })