monitor.ts 15 KB

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