index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <view>
  3. <swiper class="student-swiper" :class="students && students.length > 1 ? 'show-dots' : ''" :indicator-dots="students && students.length > 1" indicator-color="#ffffff"
  4. indicator-active-color="#666666" previous-margin="30rpx" next-margin="30rpx"
  5. v-if="students && students.length > 0" @change="onSwiperChange">
  6. <swiper-item v-for="student in students" :key="student.id" @click="onSwiperClick(student.id)">
  7. <view class="student-card">
  8. <image class="student-avatar" :src="student.avatar"></image>
  9. <view class="student-info">
  10. <text class="student-name">{{ student.name }}</text>
  11. <text class="student-class">{{ student.className }}</text>
  12. </view>
  13. </view>
  14. </swiper-item>
  15. </swiper>
  16. <view class="container">
  17. <view class="function-list">
  18. <!-- 留言功能 -->
  19. <view class="function-item" @click="goToMessage">
  20. <view class="function-icon">
  21. <Icon name="icon-jinchu" size="60" color="#393f51" />
  22. <view class="badge" v-if="hasNewMessage"></view>
  23. </view>
  24. <text class="function-name">留言</text>
  25. </view>
  26. <view class="function-item" @click="goToInOut">
  27. <view class="function-icon">
  28. <Icon name="icon-duihuapaopao2" size="60" color="#393f51" />
  29. <view class="badge" v-if="hasNewRecord"></view>
  30. </view>
  31. <text class="function-name">进出</text>
  32. </view>
  33. <view class="function-item" @click="goToBzrDm">
  34. <view class="function-icon">
  35. <Icon name="icon-dianming" size="60" color="#8992ef"/>
  36. </view>
  37. <text class="function-name">班主任点名</text>
  38. </view>
  39. <view class="function-item" @click="goXuncha">
  40. <view class="function-icon">
  41. <Icon name="icon-xuncha" size="60" color="#475fab" />
  42. </view>
  43. <text class="function-name">巡查</text>
  44. </view>
  45. <view class="function-item" @click="goToXfjl">
  46. <view class="function-icon">
  47. <Icon name="icon-xiaofeijilu" size="60" color="#c48c21" />
  48. </view>
  49. <text class="function-name">消费记录</text>
  50. </view>
  51. <view class="function-item" >
  52. <view class="function-icon">
  53. </view>
  54. <text class="function-name"></text>
  55. </view>
  56. <view class="function-item" >
  57. <view class="function-icon">
  58. </view>
  59. <text class="function-name"></text>
  60. </view>
  61. <view class="function-item" >
  62. <view class="function-icon">
  63. </view>
  64. <text class="function-name"></text>
  65. </view>
  66. <view class="function-item" >
  67. <view class="function-icon">
  68. </view>
  69. <text class="function-name"></text>
  70. </view>
  71. <view class="function-item" >
  72. <view class="function-icon">
  73. </view>
  74. <text class="function-name"></text>
  75. </view>
  76. <view class="function-item" >
  77. <view class="function-icon">
  78. </view>
  79. <text class="function-name"></text>
  80. </view>
  81. <view class="function-item" >
  82. <view class="function-icon">
  83. </view>
  84. <text class="function-name"></text>
  85. </view>
  86. </view>
  87. </view>
  88. </view>
  89. <LoginPopup />
  90. <!-- 底部导航栏,登录后才显示 -->
  91. <Tabbar v-show="isLogin"/>
  92. </template>
  93. <script setup>
  94. import { ref, computed } from 'vue'
  95. import { useUserStore } from '@/store/modules/user'
  96. import websocketService from '@/utils/websocket'
  97. import Icon from '@/components/icon/index.vue';
  98. import { onShow, onHide, onLoad, onUnload } from '@dcloudio/uni-app'
  99. // 组件导入
  100. import Tabbar from '@/components/tabbar/index.vue'
  101. import LoginPopup from '@/components/login/LoginPopup.vue'
  102. const userStore = useUserStore();
  103. const hasNewMessage = ref(false);
  104. const currentStudentIndex = ref(0);
  105. const isLogin = ref(false);
  106. const studentIds = ref([]);
  107. const students = ref([
  108. {
  109. id: 0,
  110. name: '请先登录',
  111. className: '请先登录',
  112. avatar: '/static/logo.png'
  113. },
  114. ])
  115. const goXuncha = () => {
  116. if (!checkLogin()) {
  117. pleaseLogin()
  118. return
  119. }
  120. uni.navigateTo({
  121. url: '/pages/xuncha/mp_excelRcXcdjl_edit'
  122. })
  123. }
  124. const checkLogin = () => {
  125. const userInfo = uni.getStorageSync('userInfo')
  126. console.log('userInfo', userInfo)
  127. if (!userInfo || !userInfo.yhsbToken) {
  128. return false
  129. }
  130. return true
  131. }
  132. const pleaseLogin = () => {
  133. uni.showToast({
  134. title: '请先登录',
  135. icon: 'none'
  136. })
  137. userStore.showLoginPopup()
  138. }
  139. const onSwiperChange = (e) => {
  140. currentStudentIndex.value = e.detail.current;
  141. }
  142. const onSwiperClick = (studentId) => {
  143. if (studentId == 0) {
  144. pleaseLogin()
  145. }
  146. }
  147. const goToInOut = () => {
  148. if (checkLogin()) {
  149. // 获取当前学生ID
  150. const currentStudentId = students.value[currentStudentIndex.value].id;
  151. // 检查 studentIds 数组中是否存在当前学生ID
  152. const index = studentIds.value.indexOf(currentStudentId);
  153. if (index !== -1) {
  154. // 如果存在,才从数组中移除
  155. this.studentIds.splice(index, 1);
  156. }
  157. uni.navigateTo({
  158. url: '/pages/parent/in-out?role=parent&studentId=' + students.value[currentStudentIndex.value].id
  159. })
  160. } else {
  161. pleaseLogin()
  162. }
  163. }
  164. const goToMessage = () => {
  165. if (checkLogin()) {
  166. uni.navigateTo({
  167. url: '/pages/parent/message?role=parent'
  168. })
  169. } else {
  170. pleaseLogin()
  171. }
  172. }
  173. const goToBzrDm = () => {
  174. if (checkLogin()) {
  175. uni.navigateTo({
  176. url: '/pages/kqjl/kqjl_bzrDm_list'
  177. })
  178. } else {
  179. pleaseLogin()
  180. }
  181. }
  182. const goToXfjl = () => {
  183. if (checkLogin()) {
  184. uni.navigateTo({
  185. url: '/pages/xfjl/index'
  186. })
  187. } else {
  188. pleaseLogin()
  189. }
  190. }
  191. const hasNewRecord = computed(() => {
  192. // 获取当前选中的学生ID
  193. const currentStudentId = students[currentStudentIndex.value]?.id;
  194. // 如果当前没有选中学生,返回 false
  195. if (!currentStudentId) return false;
  196. // 检查当前学生ID是否在 studentIds 数组中
  197. return this.studentIds.includes(currentStudentId);
  198. })
  199. const openPopup = () => {
  200. // 获取用户的当前设置,判断是否点击了“总是保持以上,不在询问”
  201. uni.getSetting({
  202. withSubscriptions: true, // 是否获取用户订阅消息的订阅状态,默认false不返回
  203. success(res) {
  204. console.log('res.authSetting', res)
  205. if (res.subscriptionsSetting['zrGisuG52Prtk7eJGLb_zZy9EDnglEEtaEhTi3y0VDc'] == 'accept') {
  206. console.log('用户点击了“总是保持以上,不再询问”')
  207. } else {
  208. console.log('用户没有点击“总是保持以上,不再询问”则每次都会调起订阅消息')
  209. //因为没有选择总是保持,所以需要调起授权弹窗再次授权
  210. authorizationBtn();
  211. }
  212. }
  213. })
  214. }
  215. const authorizationBtn = () => {
  216. console.log('authorizationBtn')
  217. uni.requestSubscribeMessage({
  218. tmplIds: ['zrGisuG52Prtk7eJGLb_zZy9EDnglEEtaEhTi3y0VDc'],
  219. success(res) {
  220. console.log(res)
  221. if (res['zrGisuG52Prtk7eJGLb_zZy9EDnglEEtaEhTi3y0VDc'] == 'accept') {
  222. console.log("成功")
  223. } else {
  224. console.log("拒绝")
  225. }
  226. }
  227. })
  228. }
  229. const requestDeviceAuth = async () => {
  230. try {
  231. // 先检查是否已授权
  232. const voipList = await new Promise((resolve, reject) => {
  233. uni.getDeviceVoIPList({
  234. success: res => resolve(res.list),
  235. fail: err => reject(err)
  236. })
  237. })
  238. console.log('voipList', voipList)
  239. // 如果已授权,直接返回
  240. if (voipList.some(device => device.status === 1)) {
  241. return true
  242. }
  243. // 获取设备票据(需要后端接口支持)
  244. // const ticketData = await userApi.getDeviceTicket()
  245. // 请求设备授权
  246. await new Promise((resolve, reject) => {
  247. uni.requestDeviceVoIP({
  248. isGroup: true,
  249. groupId: 'gbfI8Bx4yzDQ-v0Ngv03WOw',
  250. success: res => {
  251. console.log('授权成功:', res)
  252. },
  253. fail: err => {
  254. console.error('授权失败:', err)
  255. if (err.errCode === 9) {
  256. uni.showModal({
  257. title: '提示',
  258. content: '请在设置中开启设备通话权限',
  259. success: () => {
  260. uni.openSetting()
  261. }
  262. })
  263. }
  264. reject(err)
  265. }
  266. })
  267. })
  268. return true
  269. } catch (error) {
  270. console.error('设备授权处理失败:', error)
  271. return false
  272. }
  273. }
  274. const getUserInfo = async () => {
  275. const userInfo = uni.getStorageSync('userInfo')
  276. const userMoreInfo = await userApi.getUserInfo(userInfo.userId)
  277. console.log('userMoreInfo', userMoreInfo)
  278. this.nickname = userMoreInfo.username
  279. this.avatarUrl = userMoreInfo.avatar
  280. this.students = userMoreInfo.students || []
  281. }
  282. const handleInit = (data) => {
  283. console.log('handleInit', data)
  284. studentIds.value = data.studentIds || [];
  285. }
  286. const handleNewMessage = (data) => {
  287. console.log('handleNewMessage', data)
  288. if (!studentIds.value.includes(data.studentId)) {
  289. studentIds.value.push(data.studentId);
  290. }
  291. }
  292. const handleNewRecord = (data) => {
  293. console.log('handleNewRecord', data)
  294. }
  295. // 如果是一开始没登陆,然后登录了 都进这个事件
  296. const handLogin = (data) => {
  297. isLogin.value = true
  298. // requestDeviceAuth()
  299. // getUserInfo()
  300. // websocketService.connect()
  301. uni.$on('init', handleInit)
  302. uni.$on('newMessage', handleNewMessage)
  303. uni.$on('newRecord', handleNewRecord)
  304. students.value[0] = {
  305. id: 1,
  306. name: '已登录',
  307. className: '已登录',
  308. avatar: '/static/logo.png'
  309. }
  310. }
  311. onLoad(async () => {
  312. // 监听登录事件
  313. uni.$on('login',handLogin)
  314. if (checkLogin()) {
  315. // 如果已经登录,则执行登录后的操作
  316. handLogin()
  317. } else {
  318. isLogin.value = false
  319. }
  320. })
  321. onUnload(async () => {
  322. uni.$off('newMessage', handleNewMessage)
  323. uni.$off('newRecord', handleNewRecord)
  324. // websocketService.disconnect()
  325. })
  326. </script>
  327. <style lang="scss">
  328. .wx-swiper-dots.wx-swiper-dots-horizontal {
  329. width: calc(100% - 90rpx);
  330. display: flex;
  331. justify-content: flex-end;
  332. display: none;
  333. }
  334. .show-dots .wx-swiper-dots.wx-swiper-dots-horizontal{
  335. display: flex;
  336. }
  337. .wx-swiper-dot {
  338. border: 2rpx solid #666666 !important;
  339. background: #ffffff !important;
  340. }
  341. .wx-swiper-dot-active {
  342. background: #666666 !important;
  343. }
  344. .container {
  345. padding: 30rpx;
  346. }
  347. .student-swiper {
  348. height: 250rpx;
  349. margin-top: 40rpx;
  350. margin-bottom: 40rpx;
  351. border-bottom: 2rpx solid #dcdcdc;
  352. }
  353. .student-card {
  354. box-sizing: border-box;
  355. display: flex;
  356. align-items: center;
  357. padding: 30rpx;
  358. background: #FFFFFF;
  359. border-radius: 12rpx;
  360. box-shadow: 0rpx 6rpx 15rpx rgba(0, 0, 0, .3);
  361. margin: 10rpx 15rpx;
  362. }
  363. .student-avatar {
  364. width: 124rpx;
  365. height: 124rpx;
  366. border-radius: 50%;
  367. margin-right: 20rpx;
  368. }
  369. .student-info {
  370. display: flex;
  371. flex-direction: column;
  372. }
  373. .student-name {
  374. font-size: 36rpx;
  375. font-weight: bold;
  376. color: #333333;
  377. margin-bottom: 8rpx;
  378. }
  379. .student-class {
  380. font-size: 30rpx;
  381. color: #666666;
  382. }
  383. .function-list {
  384. display: flex;
  385. flex-wrap: wrap;
  386. justify-content: space-between;
  387. gap: 30rpx;
  388. }
  389. .function-item {
  390. display: flex;
  391. flex-direction: column;
  392. align-items: center;
  393. width: 135rpx;
  394. font-size: 28.87 rpx;
  395. color: #333333;
  396. }
  397. .function-icon {
  398. position: relative;
  399. width: 100rpx;
  400. height: 100rpx;
  401. background: #ffffff;
  402. border: 2rpx solid #e9e9e9;
  403. box-shadow: 5rpx 5rpx 10rpx rgba(0, 0, 0, .3);
  404. border-radius: 10rpx;
  405. display: flex;
  406. align-items: center;
  407. justify-content: center;
  408. margin-bottom: 10rpx;
  409. }
  410. .function-icon image {
  411. width: 60rpx;
  412. height: 60rpx;
  413. }
  414. .badge {
  415. position: absolute;
  416. top: 1rpx;
  417. right: 1rpx;
  418. width: 20rpx;
  419. height: 20rpx;
  420. background: #eb6100;
  421. border-radius: 50%;
  422. }
  423. .function-name {
  424. font-size: 26rpx;
  425. color: #333333;
  426. }
  427. .login-btn {
  428. width: 100%;
  429. height: 88rpx;
  430. line-height: 88rpx;
  431. background: #666666;
  432. color: #fff;
  433. border-radius: 44rpx;
  434. font-size: 32rpx;
  435. border: none;
  436. }
  437. .login-btn::after {
  438. border: none;
  439. }
  440. .login-btn:active {
  441. opacity: 0.8;
  442. }
  443. </style>