monitor.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. console.log("this.data.classIndex:", this.data.classIndex)
  191. }
  192. this.data.classArray.push(element.title)
  193. });
  194. this.setData({
  195. classArray: this.data.classArray,
  196. })
  197. }).catch(res => {
  198. console.log("查询班级error:", res)
  199. this.setData({
  200. classArray: ['暂无班级'],
  201. classId: ''
  202. })
  203. })
  204. },
  205. //根据地区,学校,班级获取deviceId
  206. getDeviceByRSC: function () {
  207. let params = {
  208. city: this.data.cityValue,
  209. classId: this.data.classId,
  210. province: this.data.provinceValue,//省份
  211. region: this.data.regionValue,//地区
  212. schoolId: this.data.schoolId,
  213. }
  214. httpUtil.wxGet(httpUtil.interfaces.getDevideIdByRSC, params).then((res: any) => {
  215. console.log("根据省市学校班级获取deviceId成功:", res)
  216. if (res.data.success) {
  217. this.setData({
  218. deviceId: res.data.data.deviceId
  219. })
  220. } else {
  221. this.setData({
  222. deviceId: ''
  223. })
  224. }
  225. }).catch((res) => {
  226. console.log("根据省市学校班级获取deviceId失败:", res)
  227. this.setData({
  228. deviceId: '暂无设备'
  229. })
  230. })
  231. },
  232. bindMultiPickerColumnChange: function (event: any) {
  233. //
  234. switch (Number(event.detail.column)) {
  235. case 0:
  236. //代表第一列
  237. this.setData({
  238. regionOneIndex: event.detail.value,
  239. })
  240. break;
  241. case 1:
  242. //代表第二列
  243. this.setData({
  244. regionTwoIndex: event.detail.value,
  245. })
  246. break;
  247. case 2:
  248. //代表第三列
  249. this.setData({
  250. regionThreeIndex: event.detail.value
  251. })
  252. break;
  253. }
  254. this.changeOrganizeInfo();
  255. },
  256. bindRegionChange: function (event: any) {
  257. this.setData({
  258. regionIndex: event.detail.value
  259. })
  260. },
  261. bindSchoolPicker: function (event: any) {
  262. this.setData({
  263. schoolIndex: event.detail.value,
  264. schoolId: this.data.schoolData[event.detail.value].id
  265. })
  266. this.changeClass();
  267. },
  268. bindClssPicker: function (event: any) {
  269. this.setData({
  270. classIndex: event.detail.value,
  271. classId: this.data.classData[event.detail.value].id
  272. })
  273. this.getDeviceByRSC()
  274. },
  275. //扫码界面(未处理回调)
  276. scanCode: function () {
  277. var that = this;
  278. wx.scanCode({ //扫描API
  279. success(res) { //扫描成功
  280. console.log(res) //输出回调信息
  281. that.setData({
  282. scanCodeMsg: res.result
  283. });
  284. wx.showToast({
  285. title: '扫码成功',
  286. icon: 'success',
  287. duration: 1000
  288. })
  289. },
  290. fail: (res: any) => {//接口调用失败的回调函数
  291. wx.showToast({
  292. title: '扫码失败',
  293. icon: 'success',
  294. duration: 1000
  295. })
  296. },
  297. })
  298. },
  299. //获取设备ID的输入
  300. bindDeviceInput(event: any) {
  301. this.setData({
  302. deviceId: event.detail.value
  303. })
  304. },
  305. //上课
  306. startMonitor: function () {
  307. if (!this.data.deviceId) {
  308. wx.showToast({
  309. title: '请输入设备ID',
  310. icon: 'none', //如果要纯文本,不要icon,将值设为'none'
  311. duration: 1000
  312. })
  313. return;
  314. }
  315. let params = {
  316. deviceId: this.data.deviceId,
  317. teacherId: httpUtil.httpData.userId,
  318. // title: ''
  319. }
  320. console.log("params:", params)
  321. httpUtil.wxPost(httpUtil.interfaces.createLess, params).then((res: any) => {
  322. console.log("创建文件成功(开始上课):", res)
  323. //开始上课
  324. if (res.data.success) {
  325. this.setData({
  326. pageState: 1,
  327. videoUrl: res.data.data.liveUrl,
  328. lessId: res.data.data.id,
  329. hasGetFile: true
  330. })
  331. this.triggerEvent('canChangeIndex', { canChange: false })
  332. this.getDocumentFile()
  333. }
  334. }).catch((res) => {
  335. console.log("创建文件失败(开始上课):", res)
  336. })
  337. },
  338. //#region 上课之后的方法
  339. //下课
  340. classOver: function () {
  341. //下课
  342. let that = this;
  343. wx.showModal({
  344. title: '',
  345. content: '确定下课么',
  346. success: function (res) {
  347. if (res.confirm) {
  348. console.log('点击确认回调')
  349. that.setData({
  350. pageState: 0,
  351. hasGetFile: false
  352. })
  353. that.triggerEvent('canChangeIndex', { canChange: true })
  354. } else {
  355. console.log('点击取消回调')
  356. }
  357. }
  358. })
  359. },
  360. //心跳获取文件库所有文件
  361. getDocumentFile: function () {
  362. let that = this;
  363. let params = {
  364. lessId: that.data.lessId,
  365. deviceId: that.data.deviceId,
  366. userId: httpUtil.httpData.userId,
  367. pageNo: 1,
  368. pageSize: 100000,
  369. }
  370. httpUtil.wxGet(httpUtil.interfaces.getDocumentFile, params).then((res: any) => {
  371. console.log("获取所有文件成功:", res)
  372. that.setData({
  373. imgList: res.data.data.list
  374. })
  375. let getfile = setInterval(function () {
  376. if (that.data.hasGetFile) {
  377. httpUtil.wxGet(httpUtil.interfaces.getDocumentFile, params).then((res: any) => {
  378. console.log("获取所有文件成功:", res)
  379. that.setData({
  380. imgList: res.data.data.list
  381. })
  382. }).catch((res) => {
  383. console.log("获取所有文件失败:", res)
  384. })
  385. } else {
  386. clearInterval(getfile)
  387. }
  388. }, 5000)
  389. }).catch((res) => {
  390. console.log("获取所有文件失败:", res)
  391. })
  392. }
  393. ////#endregion
  394. }
  395. })