A8000
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
6.6 KiB

  1. <script setup lang="ts">
  2. import { ref, onMounted, onUnmounted } from 'vue'
  3. import { getUserList } from '@/services/Index/user-manage'
  4. import type { User } from '@/types/Index'
  5. import { createWebSocket, DeviceContextStateMessage } from '@/websocket/socket.ts'
  6. import { getServerInfo } from '@/utils/getServerInfo.ts'
  7. import router from '@/router/router.ts'
  8. import { isBoardParamInited, login } from '@/services'
  9. import circleUrl from '@/assets/avatar.png'
  10. import { eMessage } from '@/pages/Index/utils'
  11. const stateUrl = getServerInfo('/api/v1/app/ws/state')
  12. const wsState = createWebSocket(stateUrl.wsUrl)
  13. const handleDeviceContextState = (data: DeviceContextStateMessage['data']) => {
  14. if (data.loginFlag) {
  15. sessionStorage.setItem('token', JSON.stringify(data.loginUser))
  16. router.push('/index')
  17. }
  18. }
  19. //用户列表
  20. const userList = ref<User[]>([])
  21. //获取用户列表
  22. const getUserListData = async () => {
  23. const res = await getUserList()
  24. userList.value = res.data
  25. }
  26. onMounted(() => {
  27. wsState.subscribe<DeviceContextStateMessage>(
  28. 'DeviceContext',
  29. handleDeviceContextState,
  30. )
  31. wsState.connect()
  32. getUserListData()
  33. })
  34. onUnmounted(() => {
  35. wsState.unsubscribe<DeviceContextStateMessage>(
  36. 'DeviceContext',
  37. handleDeviceContextState,
  38. )
  39. })
  40. const activeUser = ref<User>()
  41. const password = ref('')
  42. const inputPin = (pin: string) => {
  43. if (password.value.length >= 4) {
  44. return
  45. }
  46. if (password.value.length < 4) {
  47. password.value += pin
  48. }
  49. if (password.value.length === 4) {
  50. // 输入完成,执行登录操作
  51. submitPin()
  52. }
  53. }
  54. const passwordError = ref(false)
  55. const submitPin = async () => {
  56. let resData = await isBoardParamInited()
  57. if (!resData.data) {
  58. //设备正在初始化
  59. eMessage.error('设备正在初始化,请稍候重试')
  60. return
  61. }
  62. const params = {
  63. id: activeUser.value?.id,
  64. password: password.value,
  65. }
  66. const res = await login(params)
  67. if (res.success) {
  68. sessionStorage.setItem('token', JSON.stringify(res.data))
  69. await router.push('/index')
  70. } else {
  71. passwordError.value = true // 触发抖动动画
  72. // 动画结束后重置状态(2次抖动共0.6秒)
  73. setTimeout(() => {
  74. passwordError.value = false
  75. }, 600)
  76. password.value = ''
  77. }
  78. }
  79. const showPassword = ref(false)
  80. const next = () => {
  81. showPassword.value = true
  82. }
  83. const back = () => {
  84. showPassword.value = false
  85. }
  86. </script>
  87. <template>
  88. <div class="login-box">
  89. <p class="login-title">登录</p>
  90. <div class="user-box" v-if="!showPassword">
  91. <p class="title">选择用户</p>
  92. <div class="user-list">
  93. <div class="user-info" v-for="user in userList" :key="user.id" @click="activeUser = user" :class="{ 'user-info-active': activeUser?.id === user.id }">
  94. <el-avatar :size="60" :src="circleUrl" />
  95. <span class="name">{{user.account}}</span>
  96. <el-icon v-show="activeUser?.id === user.id" color="#fff"><Select /></el-icon>
  97. </div>
  98. </div>
  99. <div class="next-box" :class="{'next-box-active': activeUser?.id}" @click="next">
  100. <el-icon><Right /></el-icon>
  101. </div>
  102. </div>
  103. <div class="password-box" v-else>
  104. <p class="title">请输入4位PIN码</p>
  105. <div class="password-list">
  106. <div v-for="i in 4" :key="i" class="password-item" :class="{'password-item-fill': i <= password.length, 'password-error': passwordError}"></div>
  107. </div>
  108. <div class="pin-keypad">
  109. <div
  110. v-for="n in 9"
  111. :key="n"
  112. class="key"
  113. @click="inputPin(n.toString())"
  114. >
  115. {{ n }}
  116. </div>
  117. <div></div>
  118. <div class="key" @click="inputPin('0')">0</div>
  119. </div>
  120. <div class="next-box next-box-active" @click="back">
  121. <el-icon><Back /></el-icon>
  122. </div>
  123. </div>
  124. </div>
  125. </template>
  126. <style lang="less" scoped>
  127. @keyframes iconAnim {
  128. 0% { transform: scale(0.8); opacity: 0; }
  129. 100% { transform: scale(1); opacity: 1; }
  130. }
  131. @keyframes shake {
  132. 0%, 100% {
  133. transform: translateX(0);
  134. opacity: 1;
  135. }
  136. 25% {
  137. transform: translateX(-10px);
  138. opacity: 0.7;
  139. }
  140. 50% {
  141. transform: translateX(10px);
  142. opacity: 0.7;
  143. }
  144. 75% {
  145. transform: translateX(-7px);
  146. opacity: 0.7;
  147. }
  148. }
  149. .login-title {
  150. position: absolute;
  151. top: 10%;
  152. font-size: 50px;
  153. font-weight: bold;
  154. }
  155. .login-box {
  156. width: 100%;
  157. height: 100%;
  158. background: #A0CEF2;
  159. display: flex;
  160. align-items: center;
  161. justify-content: center;
  162. position: relative;
  163. .user-box {
  164. width: 50%;
  165. height: 50%;
  166. background: #fff;
  167. border-radius: 30px;
  168. padding: 40px 30px;
  169. display: flex;
  170. flex-direction: column;
  171. align-items: center;
  172. .user-list {
  173. width: 100%;
  174. flex: 1;
  175. overflow: auto;
  176. .user-info {
  177. display: flex;
  178. align-items: center;
  179. padding: 20px;
  180. border-radius: 20px;
  181. .name {
  182. margin-left: 30px;
  183. font-size: 25px;
  184. font-weight: bold;
  185. }
  186. .el-icon {
  187. animation: iconAnim 0.3s ease forwards;
  188. font-size: 30px;
  189. margin-left: auto;
  190. }
  191. }
  192. .user-info-active {
  193. background: #3E8ED1;
  194. color: #fff;
  195. }
  196. }
  197. }
  198. .password-box {
  199. width: 50%;
  200. height: 50%;
  201. background: #fff;
  202. border-radius: 30px;
  203. padding: 40px 30px;
  204. display: flex;
  205. flex-direction: column;
  206. align-items: center;
  207. justify-content: space-between;
  208. }
  209. }
  210. .title {
  211. font-size: 30px;
  212. font-weight: bold;
  213. }
  214. .password-list {
  215. display: flex;
  216. justify-content: center;
  217. padding: 20px 0;
  218. .password-item {
  219. width: 30px;
  220. height: 30px;
  221. border-radius: 50%;
  222. background: #CDCFD4;
  223. margin: 0 10px;
  224. }
  225. .password-item-fill {
  226. background: #3E8ED1;
  227. }
  228. }
  229. .pin-keypad {
  230. display: grid;
  231. grid-template-columns: repeat(3, 1fr);
  232. gap: 10px;
  233. margin-top: 30px;
  234. .key {
  235. width: 100px;
  236. height: 80px;
  237. background-color: #fff;
  238. border-radius: 15px;
  239. display: flex;
  240. align-items: center;
  241. justify-content: center;
  242. font-size: 32px;
  243. color: #333;
  244. cursor: pointer;
  245. transition: all 0.3s;
  246. border: 1px solid #E7EDF1;
  247. box-shadow: 0 3px 8px #E7EDF1;
  248. &:active {
  249. transform: scale(0.95);
  250. }
  251. }
  252. }
  253. .error-box {
  254. flex: 1;
  255. display: flex;
  256. justify-content: center;
  257. align-items: center;
  258. font-size: 20px;
  259. color: #ff0000;
  260. }
  261. .password-error {
  262. animation: shake 0.3s ease-in-out 2;
  263. }
  264. .next-box {
  265. width: 80px;
  266. height: 80px;
  267. border-radius: 50%;
  268. background: #ddd;
  269. display: flex;
  270. justify-content: center;
  271. align-items: center;
  272. margin: 20px auto;
  273. .el-icon {
  274. font-size: 30px;
  275. color: #bbb
  276. }
  277. }
  278. .next-box-active {
  279. background: #3E8ED1;
  280. .el-icon {
  281. color: #fff;
  282. }
  283. }
  284. </style>