index.vue 11 KB

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