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.

371 lines
8.1 KiB

4 months ago
  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. if (activeUser.value?.id) {
  82. showPassword.value = true
  83. }
  84. }
  85. const back = () => {
  86. showPassword.value = false
  87. }
  88. const version = __APP_VERSION__
  89. </script>
  90. <template>
  91. <div class="login-box">
  92. <transition name="flip-card" mode="out-in">
  93. <div class="user-box" v-if="!showPassword">
  94. <p class="title">选择用户</p>
  95. <div class="user-list">
  96. <div class="user-info" v-for="user in userList" :key="user.id" @click="activeUser = user" :class="{ 'user-info-active': activeUser?.id === user.id }">
  97. <el-avatar :size="60" :src="circleUrl" />
  98. <span class="name">{{user.account}}</span>
  99. <el-icon v-show="activeUser?.id === user.id" color="#fff"><Select /></el-icon>
  100. </div>
  101. </div>
  102. <div class="next-box" :class="{'next-box-active': activeUser?.id}" @click="next">
  103. <el-icon><Right /></el-icon>
  104. </div>
  105. </div>
  106. <div class="password-box" v-else>
  107. <div class="user-info user-info-active">
  108. <el-avatar :size="40" :src="circleUrl" />
  109. <span class="name">{{activeUser!.account}}</span>
  110. </div>
  111. <p class="title">请输入4位PIN码</p>
  112. <div class="password-list">
  113. <div v-for="i in 4" :key="i" class="password-item" :class="{'password-item-fill': i <= password.length, 'password-error': passwordError}"></div>
  114. </div>
  115. <div class="pin-keypad">
  116. <div
  117. v-for="n in 9"
  118. :key="n"
  119. class="key"
  120. @click="inputPin(n.toString())"
  121. >
  122. {{ n }}
  123. </div>
  124. <div></div>
  125. <div class="key" @click="inputPin('0')">0</div>
  126. </div>
  127. <div class="next-box next-box-active" @click="back">
  128. <el-icon><Back /></el-icon>
  129. </div>
  130. </div>
  131. </transition>
  132. <div class="version">版本号v {{version}}</div>
  133. </div>
  134. </template>
  135. <style lang="less" scoped>
  136. @keyframes iconAnim {
  137. 0% { transform: scale(0.8); opacity: 0; }
  138. 100% { transform: scale(1); opacity: 1; }
  139. }
  140. @keyframes shake {
  141. 0%, 100% {
  142. transform: translateX(0);
  143. opacity: 1;
  144. }
  145. 25% {
  146. transform: translateX(-10px);
  147. opacity: 0.7;
  148. }
  149. 50% {
  150. transform: translateX(10px);
  151. opacity: 0.7;
  152. }
  153. 75% {
  154. transform: translateX(-7px);
  155. opacity: 0.7;
  156. }
  157. }
  158. .login-title {
  159. position: absolute;
  160. top: 10%;
  161. font-size: 50px;
  162. font-weight: bold;
  163. }
  164. .login-box {
  165. perspective: 1000px;
  166. width: 100%;
  167. height: 100%;
  168. background: #A0CEF2;
  169. display: flex;
  170. align-items: center;
  171. justify-content: center;
  172. position: relative;
  173. .user-box {
  174. width: 50%;
  175. height: 50%;
  176. background: #fff;
  177. border-radius: 30px;
  178. padding: 40px 30px;
  179. display: flex;
  180. flex-direction: column;
  181. align-items: center;
  182. .user-list {
  183. width: 100%;
  184. flex: 1;
  185. overflow: auto;
  186. .user-info {
  187. display: flex;
  188. align-items: center;
  189. padding: 20px;
  190. border-radius: 20px;
  191. .name {
  192. margin-left: 30px;
  193. font-size: 25px;
  194. font-weight: bold;
  195. }
  196. .el-icon {
  197. animation: iconAnim 0.3s ease forwards;
  198. font-size: 30px;
  199. margin-left: auto;
  200. }
  201. }
  202. .user-info-active {
  203. background: #3E8ED1;
  204. color: #fff;
  205. }
  206. }
  207. }
  208. .password-box {
  209. width: 50%;
  210. height: 50%;
  211. background: #fff;
  212. border-radius: 30px;
  213. padding: 40px 30px;
  214. display: flex;
  215. flex-direction: column;
  216. align-items: center;
  217. justify-content: space-between;
  218. .user-info {
  219. width: 50%;
  220. padding: 10px 0;
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. border-radius: 20px;
  225. background: #3E8ED1;
  226. color: #fff;
  227. .name {
  228. margin-left: 30px;
  229. font-size: 25px;
  230. font-weight: bold;
  231. }
  232. }
  233. }
  234. }
  235. .title {
  236. font-size: 30px;
  237. font-weight: bold;
  238. }
  239. .password-list {
  240. display: flex;
  241. justify-content: center;
  242. padding: 20px 0;
  243. .password-item {
  244. width: 30px;
  245. height: 30px;
  246. border-radius: 50%;
  247. background: #CDCFD4;
  248. margin: 0 10px;
  249. }
  250. .password-item-fill {
  251. background: #3E8ED1;
  252. }
  253. }
  254. .pin-keypad {
  255. display: grid;
  256. grid-template-columns: repeat(3, 1fr);
  257. gap: 10px;
  258. margin-top: 30px;
  259. .key {
  260. width: 100px;
  261. height: 80px;
  262. background-color: #fff;
  263. border-radius: 15px;
  264. display: flex;
  265. align-items: center;
  266. justify-content: center;
  267. font-size: 32px;
  268. color: #333;
  269. cursor: pointer;
  270. transition: all 0.3s;
  271. border: 1px solid #E7EDF1;
  272. box-shadow: 0 3px 8px #E7EDF1;
  273. &:active {
  274. transform: scale(0.95);
  275. }
  276. }
  277. }
  278. .error-box {
  279. flex: 1;
  280. display: flex;
  281. justify-content: center;
  282. align-items: center;
  283. font-size: 20px;
  284. color: #ff0000;
  285. }
  286. .password-error {
  287. animation: shake 0.3s ease-in-out 2;
  288. }
  289. .next-box {
  290. width: 80px;
  291. height: 80px;
  292. border-radius: 50%;
  293. background: #ddd;
  294. display: flex;
  295. justify-content: center;
  296. align-items: center;
  297. margin: 20px auto;
  298. .el-icon {
  299. font-size: 30px;
  300. color: #bbb
  301. }
  302. }
  303. .next-box-active {
  304. background: #3E8ED1;
  305. .el-icon {
  306. color: #fff;
  307. }
  308. }
  309. .flip-card-leave-active,
  310. .flip-card-enter-active {
  311. transition: all 0.5s ease;
  312. backface-visibility: hidden; /* 隐藏背面 */
  313. }
  314. .flip-card-enter-from,
  315. .flip-card-leave-to {
  316. transform: rotateY(90deg);
  317. opacity: 0;
  318. }
  319. .flip-card-enter-active .user-box,
  320. .flip-card-leave-active .user-box,
  321. .flip-card-enter-active .password-box,
  322. .flip-card-leave-active .password-box {
  323. position: absolute;
  324. backface-visibility: hidden;
  325. transition: transform 0.5s ease;
  326. }
  327. .flip-card-enter .user-box {
  328. transform: rotateY(-90deg);
  329. }
  330. .flip-card-leave-to .user-box {
  331. transform: rotateY(90deg);
  332. }
  333. .flip-card-enter .password-box {
  334. transform: rotateY(90deg);
  335. }
  336. .flip-card-leave-to .password-box {
  337. transform: rotateY(-90deg);
  338. }
  339. .version {
  340. position: absolute;
  341. bottom: 10px;
  342. color: rgba(0,0,0,0.25);
  343. }
  344. </style>