monitor.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // pages/teacher/monitor/monitor.ts
  2. import { ConstsData } from "../../../utils/const"
  3. import { httpUtil } from "../../../utils/restful";
  4. Component({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. regionOne: Array<String>(),
  10. regionTwo: Array<String>(),
  11. regionThree: Array<String>(),
  12. regionOneIndex: 0,
  13. regionTwoIndex: 0,
  14. regionThreeIndex: 0,
  15. region: [
  16. Array<String>(),
  17. Array<String>(),
  18. Array<String>()
  19. ],
  20. regionIndex: [0, 0, 0],
  21. schoolArray: [['暂无学校']],
  22. schoolIndex: 0,
  23. classArray: ['暂无班级'],
  24. classIndex: 0,
  25. provinceValue: '',//省份value
  26. regionValue: '',//地区value
  27. cityValue: '',
  28. schoolData: [{ 'id': '' }],
  29. schoolId: '',
  30. classData: [{ 'id': '' }],
  31. classId: '',
  32. initFirst: true,
  33. deviceId: '',
  34. //0=未上课 1=正在上课
  35. pageState: 0,
  36. hiddenInput: false,
  37. //#region 开始上课用到的数据
  38. //0是正常,1是error
  39. videostatus: 0,
  40. videoUrl: '',
  41. lessId: '',
  42. hasGetFile: false,
  43. imgList: Array<String>(),
  44. //#endregion
  45. //弹窗
  46. windowHidden: true,
  47. windowRegion: 'test',
  48. windowSchool: 'test',
  49. windowClass: 'test'
  50. },
  51. lifetimes: {
  52. attached: function () {
  53. // 在组件实例被从页面节点树添加时执行
  54. this.getMyInfo();
  55. this.setData({
  56. pageState: ConstsData.AppData.monitorsPageIndex
  57. })
  58. // console.log("monitorsPageIndex:", ConstsData.AppData.monitorsPageIndex)
  59. },
  60. detached: function () {
  61. // 在组件实例被从页面节点树移除时执行
  62. this.setData({
  63. hasGetFile: false
  64. })
  65. },
  66. },
  67. methods: {
  68. getMyInfo: function () {
  69. if (ConstsData.AppData.myInfoData.id == 0) {
  70. //没有用户信息,请求接口
  71. httpUtil.wxGet(httpUtil.interfaces.getUserById, null).then((res: any) => {
  72. ConstsData.AppData.myInfoData = res.data.data
  73. //获取地区和学校班级等信息
  74. this.changeOrganizeInfo()
  75. }).catch((res) => {
  76. console.log("获取我的信息失败:", res)
  77. })
  78. } else {
  79. //有用户信息,直接设置地区啥的
  80. this.changeOrganizeInfo()
  81. }
  82. if (ConstsData.AppData.monitorsPageIndex == 1) {
  83. this.setData({
  84. hasGetFile: true
  85. })
  86. this.getDocumentFile()
  87. }
  88. },
  89. changeOrganizeInfo: function () {
  90. //切换地区
  91. this.setData({
  92. regionOne: [],
  93. regionTwo: [],
  94. regionThree: []
  95. })
  96. ConstsData.AppData.organizeInfo.data.children.forEach((item, index) => {
  97. if (this.data.initFirst) {
  98. if (item.value == ConstsData.AppData.myInfoData.province) {
  99. //如果是当前的
  100. this.setData({
  101. regionOneIndex: index
  102. })
  103. }
  104. }
  105. this.data.regionOne.push(item.title)
  106. })
  107. ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children.forEach((item, index) => {
  108. if (this.data.initFirst) {
  109. if (item.value == ConstsData.AppData.myInfoData.city) {
  110. this.setData({
  111. regionTwoIndex: index
  112. })
  113. }
  114. }
  115. this.data.regionTwo.push(item.title)
  116. })
  117. ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children[this.data.regionTwoIndex].children.forEach((item, index) => {
  118. if (this.data.initFirst) {
  119. if (item.value == ConstsData.AppData.myInfoData.region) {
  120. this.setData({
  121. regionThreeIndex: index
  122. })
  123. }
  124. }
  125. this.data.regionThree.push(item.title)
  126. })
  127. this.setData({
  128. region: [this.data.regionOne, this.data.regionTwo, this.data.regionThree],
  129. regionIndex: [this.data.regionOneIndex, this.data.regionTwoIndex, this.data.regionThreeIndex],
  130. provinceValue: ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].value.toString(),
  131. cityValue: ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children[this.data.regionTwoIndex].value.toString(),
  132. regionValue: ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children[this.data.regionTwoIndex].children[this.data.regionThreeIndex].value.toString(),
  133. initFirst: false
  134. })
  135. //切换学校
  136. this.changeSchool();
  137. },
  138. //查询学校
  139. changeSchool: function () {
  140. //查询学校
  141. let params = {
  142. city: this.data.cityValue,
  143. province: this.data.provinceValue,
  144. region: this.data.regionValue,
  145. type: 1,
  146. }
  147. httpUtil.wxGet(httpUtil.interfaces.getSchoolOrClass, params).then((res: any) => {
  148. this.setData({
  149. schoolArray: [],
  150. schoolIndex: 0,
  151. schoolData: res.data.data,
  152. schoolId: res.data.data[0].id
  153. })
  154. let newArray: string[] = [];
  155. res.data.data.forEach((element: any, index: any) => {
  156. if (ConstsData.AppData.myInfoData.schoolId == element.id) {
  157. this.setData({
  158. schoolIndex: index,
  159. schoolId: this.data.schoolData[index].id
  160. })
  161. }
  162. newArray.push(element.title)
  163. });
  164. this.data.schoolArray.push(newArray)
  165. this.setData({
  166. schoolArray: this.data.schoolArray
  167. })
  168. //切换班级
  169. this.changeClass()
  170. }).catch(res => {
  171. console.log("查询学校error:", res)
  172. this.setData({
  173. schoolArray: [['暂无学校']],
  174. schoolId: '',
  175. classArray: ['暂无班级'],
  176. classId: ''
  177. })
  178. })
  179. },
  180. //查询班级
  181. changeClass: function () {
  182. let params = {
  183. "city": this.data.cityValue,
  184. "father": this.data.schoolId,//学校ID
  185. "province": this.data.provinceValue,
  186. "region": this.data.regionValue,
  187. "type": 2,
  188. }
  189. httpUtil.wxGet(httpUtil.interfaces.getSchoolOrClass, params).then((res: any) => {
  190. this.setData({
  191. classArray: [],
  192. classIndex: 0
  193. })
  194. this.setData({
  195. classData: res.data.data,
  196. classId: res.data.data[0].id
  197. })
  198. console.log("classData:", this.data.classData)
  199. res.data.data.forEach((element: { id: number; title: string; }, index: number) => {
  200. if (element.id == ConstsData.AppData.myInfoData.classId) {
  201. this.setData({
  202. classIndex: index,
  203. classId: res.data.data[index].id
  204. })
  205. this.getDeviceByRSC()
  206. console.log("this.data.classIndex:", this.data.classIndex)
  207. }
  208. this.data.classArray.push(element.title)
  209. });
  210. this.setData({
  211. classArray: this.data.classArray,
  212. })
  213. }).catch(res => {
  214. console.log("查询班级error:", res)
  215. this.setData({
  216. classArray: ['暂无班级'],
  217. classId: ''
  218. })
  219. })
  220. },
  221. //根据地区,学校,班级获取deviceId
  222. getDeviceByRSC: function () {
  223. let params = {
  224. city: this.data.cityValue,
  225. classId: this.data.classId,
  226. province: this.data.provinceValue,//省份
  227. region: this.data.regionValue,//地区
  228. schoolId: this.data.schoolId,
  229. }
  230. httpUtil.wxGet(httpUtil.interfaces.getDevideIdByRSC, params).then((res: any) => {
  231. console.log("根据省市学校班级获取deviceId成功:", res)
  232. if (res.data.success) {
  233. this.setData({
  234. deviceId: res.data.data.deviceId
  235. })
  236. } else {
  237. this.setData({
  238. deviceId: ''
  239. })
  240. }
  241. }).catch((res) => {
  242. console.log("根据省市学校班级获取deviceId失败:", res)
  243. this.setData({
  244. deviceId: '暂无设备'
  245. })
  246. })
  247. },
  248. bindMultiPickerColumnChange: function (event: any) {
  249. //
  250. switch (Number(event.detail.column)) {
  251. case 0:
  252. //代表第一列
  253. this.setData({
  254. regionOneIndex: event.detail.value,
  255. })
  256. break;
  257. case 1:
  258. //代表第二列
  259. this.setData({
  260. regionTwoIndex: event.detail.value,
  261. })
  262. break;
  263. case 2:
  264. //代表第三列
  265. this.setData({
  266. regionThreeIndex: event.detail.value
  267. })
  268. break;
  269. }
  270. this.changeOrganizeInfo();
  271. },
  272. bindRegionChange: function (event: any) {
  273. this.setData({
  274. regionIndex: event.detail.value
  275. })
  276. },
  277. bindSchoolPicker: function (event: any) {
  278. this.setData({
  279. schoolIndex: event.detail.value,
  280. schoolId: this.data.schoolData[event.detail.value].id
  281. })
  282. this.changeClass();
  283. },
  284. bindClssPicker: function (event: any) {
  285. this.setData({
  286. classIndex: event.detail.value,
  287. classId: this.data.classData[event.detail.value].id
  288. })
  289. this.getDeviceByRSC()
  290. },
  291. //扫码界面(未处理回调)
  292. scanCode: function () {
  293. var that = this;
  294. wx.scanCode({ //扫描API
  295. success(res) { //扫描成功
  296. console.log("扫码返回:", res.result) //输出回调信息
  297. let a = JSON.parse(res.result)
  298. console.log("res.result.deviceId:", a.deviceId)
  299. httpUtil.wxGet(httpUtil.interfaces.getDeviceById + a.deviceId, null).then((res: any) => {
  300. console.log("获取设备信息成功:", res)
  301. that.setData({
  302. deviceId: a.deviceId,
  303. windowRegion: res.data.data.provinceCity,
  304. windowSchool: res.data.data.schoolName,
  305. windowClass: res.data.data.className,
  306. hiddenInput: true,
  307. windowHidden: false,
  308. })
  309. }).catch((res) => {
  310. console.log("获取设备信息失败:", res)
  311. })
  312. // that.setData({
  313. // scanCodeMsg: res.result
  314. // });
  315. wx.showToast({
  316. title: '扫码成功',
  317. icon: 'success',
  318. duration: 1000
  319. })
  320. },
  321. fail: (res: any) => {//接口调用失败的回调函数
  322. console.log("扫码失败res:", res)
  323. wx.showToast({
  324. title: '扫码失败',
  325. icon: 'success',
  326. duration: 1000
  327. })
  328. },
  329. })
  330. },
  331. //获取设备ID的输入
  332. bindDeviceInput(event: any) {
  333. this.setData({
  334. deviceId: event.detail.value
  335. })
  336. },
  337. //上课
  338. startMonitor: function () {
  339. if (!this.data.deviceId) {
  340. wx.showToast({
  341. title: '请输入设备ID',
  342. icon: 'none', //如果要纯文本,不要icon,将值设为'none'
  343. duration: 1000
  344. })
  345. return;
  346. }
  347. let params = {
  348. deviceId: this.data.deviceId,
  349. teacherId: httpUtil.httpData.userId,
  350. // title: ''
  351. }
  352. console.log("params:", params)
  353. httpUtil.wxPost(httpUtil.interfaces.createLess, params).then((res: any) => {
  354. console.log("创建文件成功(开始上课):", res)
  355. //开始上课
  356. if (res.data.success) {
  357. this.setData({
  358. pageState: 1,
  359. videoUrl: res.data.data.liveUrl,
  360. lessId: res.data.data.id,
  361. hasGetFile: true,
  362. windowHidden: true
  363. })
  364. ConstsData.AppData.monitorsPageIndex = 1
  365. // this.triggerEvent('canChangeIndex', { canChange: false })
  366. this.getDocumentFile()
  367. }
  368. }).catch((res) => {
  369. console.log("创建文件失败(开始上课):", res)
  370. })
  371. },
  372. //#region 上课之后的方法
  373. //下课
  374. classOver: function () {
  375. //下课
  376. let that = this;
  377. wx.showModal({
  378. title: '',
  379. content: '确定下课么',
  380. success: function (res) {
  381. if (res.confirm) {
  382. console.log('点击确认回调')
  383. that.setData({
  384. pageState: 0,
  385. hasGetFile: false,
  386. hiddenInput: false
  387. })
  388. ConstsData.AppData.monitorsPageIndex = 0
  389. // that.triggerEvent('canChangeIndex', { canChange: true })
  390. } else {
  391. console.log('点击取消回调')
  392. }
  393. }
  394. })
  395. },
  396. //心跳获取文件库所有文件
  397. getDocumentFile: function () {
  398. let that = this;
  399. let params = {
  400. lessId: '',
  401. deviceId: '',
  402. userId: '',
  403. pageNo: 1,
  404. pageSize: 100000
  405. }
  406. if (ConstsData.AppData.monitorsPageGetDocumentParams.lessId) {
  407. params = {
  408. lessId: that.data.lessId,
  409. deviceId: that.data.deviceId,
  410. userId: httpUtil.httpData.userId,
  411. pageNo: 1,
  412. pageSize: 100000,
  413. }
  414. ConstsData.AppData.monitorsPageGetDocumentParams = params
  415. } else {
  416. params = ConstsData.AppData.monitorsPageGetDocumentParams
  417. }
  418. httpUtil.wxGet(httpUtil.interfaces.getDocumentFile, params).then((res: any) => {
  419. console.log("获取所有文件成功:", res)
  420. let showarr: String[] = []
  421. res.data.data.list.forEach((item: any) => {
  422. if (item.type == 0) {
  423. showarr.push(item)
  424. }
  425. })
  426. that.setData({
  427. imgList: showarr
  428. })
  429. let getfile = setInterval(function () {
  430. if (that.data.hasGetFile) {
  431. httpUtil.wxGet(httpUtil.interfaces.getDocumentFile, params).then((res: any) => {
  432. console.log("获取所有文件成功:", res)
  433. let showarr: any[] = []
  434. res.data.data.list.forEach((item: any) => {
  435. if (item.type == 0) {
  436. showarr.push(item)
  437. }
  438. })
  439. that.setData({
  440. imgList: showarr
  441. })
  442. }).catch((res) => {
  443. console.log("获取所有文件失败:", res)
  444. })
  445. } else {
  446. clearInterval(getfile)
  447. }
  448. }, 5000)
  449. }).catch((res) => {
  450. console.log("获取所有文件失败:", res)
  451. })
  452. },
  453. ////#endregion
  454. hideWindow: function () {
  455. this.setData({
  456. hiddenInput: false,
  457. windowHidden: true
  458. })
  459. return true;
  460. },
  461. }
  462. })