dialog-login.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <dialog-base ref="baseDialog" title="账号登录">
  3. <template v-if="currentUser.nickname">
  4. <view class="user-info-container uni-row">
  5. <view class="user-avatar uni-row"><text class="label">{{ currentUser.nickname }}</text></view>
  6. <view class="user-info">
  7. <view class="nickname"><text class="label">{{ currentUser.nickname }}</text></view>
  8. <view class="current-process"><text class="label">热处理</text></view>
  9. </view>
  10. </view>
  11. </template>
  12. <template v-else>
  13. <input class="input-control" v-model="currentUser.userName" placeholder="员工编码" />
  14. </template>
  15. <input class="input-control" :password="true" v-model="currentUser.password" placeholder='密码' />
  16. <view class="login-btn uni-row" @click="handleLogin"><text class="label">登录</text></view>
  17. </dialog-base>
  18. </template>
  19. <script setup>
  20. import {
  21. ref,
  22. getCurrentInstance
  23. } from 'vue'
  24. import {
  25. getUserInfo
  26. } from '@/api/login/index.js'
  27. import { getUserProcess } from '@/api/process/process.js'
  28. import baseURL from '@/api/base/path.js'
  29. import { store } from '@/store/index.js'
  30. // 对话框
  31. const baseDialog = ref(null)
  32. const currentUser = ref({})
  33. const errorMsg = ref('用户名或密码错误')
  34. const successMsg = ref('登录成功')
  35. const userInfo = ref({})
  36. const user = ref([])
  37. // const { proxy } = getCurrentInstance()
  38. const open = (data) => {
  39. currentUser.value = data
  40. // console.log(dialog.value)
  41. baseDialog.value.open()
  42. }
  43. const handleLogin = () => {
  44. uni.showLoading({
  45. title: '加载中'
  46. });
  47. setTimeout(function(){
  48. uni.hideLoading();
  49. },3000)
  50. uni.request({
  51. method: 'POST',
  52. url: baseURL + '/login',
  53. data: {
  54. username: currentUser.value.userName,
  55. password: currentUser.value.password
  56. },
  57. success: (res) => {
  58. if (res.data.code === 200) {
  59. uni.showToast({
  60. title: successMsg.value,
  61. icon: 'success',
  62. duration: 2000
  63. });
  64. // 保存token
  65. uni.setStorageSync('token', res.data.token);
  66. uni.setStorage({
  67. key: currentUser.value.userName,
  68. data: currentUser.value.password,
  69. });
  70. // 获取员工工序信息
  71. getUserProcess().then(res => {
  72. store.userProcessInfo = res.data;
  73. })
  74. // 获取员工信息存入store,并跳转到控制面板
  75. getUserInfo(currentUser.value).then((res) => {
  76. if (res.code == 200) {
  77. userInfo.value = res.data;
  78. user.value.push(userInfo.value);
  79. store.userInfo = res.data
  80. console.log(store.userInfo)
  81. uni.redirectTo({
  82. url: '/pages/dashboard/index'
  83. });
  84. }
  85. })
  86. } else {
  87. uni.showToast({
  88. title: errorMsg.value,
  89. icon: 'error',
  90. //显示持续时间为 2秒
  91. duration: 2000
  92. })
  93. }
  94. }
  95. })
  96. }
  97. /** 暴露给父组件的方法 */
  98. defineExpose({
  99. open
  100. })
  101. </script>
  102. <style lang="scss">
  103. .dialog-body {
  104. .user-info-container {
  105. margin-top: 32rpx;
  106. align-items: center;
  107. .user-avatar {
  108. width: 120rpx;
  109. height: 120rpx;
  110. justify-content: center;
  111. align-items: center;
  112. border-radius: 120rpx;
  113. background-color: #F5F5F5;
  114. .label {
  115. font-size: 72rpx;
  116. }
  117. }
  118. .user-info {
  119. flex: 1;
  120. padding-left: 16rpx;
  121. .nickname {
  122. margin-bottom: 8rpx;
  123. .label {
  124. font-size: 40rpx;
  125. }
  126. }
  127. .current-process {
  128. .label {
  129. font-size: 32rpx;
  130. color: #666666;
  131. }
  132. }
  133. }
  134. }
  135. .input-control {
  136. border: 1px solid #666666;
  137. padding: 16rpx 24rpx;
  138. font-size: 32rpx;
  139. margin-top: 40rpx;
  140. }
  141. .login-btn {
  142. // background-color: #999999;
  143. background-color: #1684fc;
  144. margin-top: 32rpx;
  145. justify-content: center;
  146. padding: 16rpx 0;
  147. .label {
  148. font-size: 40rpx;
  149. color: #FFFFFF;
  150. }
  151. }
  152. }
  153. </style>