dialog-login.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 baseURL from '@/api/base/path.js'
  28. // 对话框
  29. const baseDialog = ref(null)
  30. const currentUser = ref({})
  31. const errorMsg = ref('用户名或密码错误')
  32. const successMsg = ref('登录成功')
  33. const userInfo = ref({})
  34. const user = ref([])
  35. // const { proxy } = getCurrentInstance()
  36. const open = (data) => {
  37. currentUser.value = data
  38. // console.log(dialog.value)
  39. baseDialog.value.open()
  40. }
  41. const handleLogin = () => {
  42. uni.showLoading({
  43. title: '加载中'
  44. });
  45. setTimeout(function(){
  46. uni.hideLoading();
  47. },3000)
  48. uni.request({
  49. method: 'POST',
  50. url: baseURL + '/login',
  51. data: {
  52. username: currentUser.value.userName,
  53. password: currentUser.value.password
  54. },
  55. success: (res) => {
  56. if (res.data.code === 200) {
  57. uni.showToast({
  58. title: successMsg.value,
  59. icon: 'success',
  60. duration: 2000
  61. });
  62. // 保存token
  63. uni.setStorageSync('token', res.data.token);
  64. uni.setStorage({
  65. key: currentUser.value.userName,
  66. data: currentUser.value.password,
  67. success: function() {
  68. }
  69. });
  70. getUserInfo(currentUser.value).then((res) => {
  71. if (res.code == 200) {
  72. userInfo.value = res.data;
  73. user.value.push(userInfo.value);
  74. uni.redirectTo({
  75. url: '/pages/dashboard/index?userName=' + userInfo.value
  76. .userName + '&userId=' + userInfo.value.userId
  77. });
  78. }
  79. })
  80. } else {
  81. uni.showToast({
  82. title: errorMsg.value,
  83. icon: 'error',
  84. //显示持续时间为 2秒
  85. duration: 2000
  86. })
  87. }
  88. }
  89. })
  90. }
  91. /** 暴露给父组件的方法 */
  92. defineExpose({
  93. open
  94. })
  95. </script>
  96. <style lang="scss">
  97. .dialog-body {
  98. .user-info-container {
  99. margin-top: 32rpx;
  100. align-items: center;
  101. .user-avatar {
  102. width: 120rpx;
  103. height: 120rpx;
  104. justify-content: center;
  105. align-items: center;
  106. border-radius: 120rpx;
  107. background-color: #F5F5F5;
  108. .label {
  109. font-size: 72rpx;
  110. }
  111. }
  112. .user-info {
  113. flex: 1;
  114. padding-left: 16rpx;
  115. .nickname {
  116. margin-bottom: 8rpx;
  117. .label {
  118. font-size: 40rpx;
  119. }
  120. }
  121. .current-process {
  122. .label {
  123. font-size: 32rpx;
  124. color: #666666;
  125. }
  126. }
  127. }
  128. }
  129. .input-control {
  130. border: 1px solid #666666;
  131. padding: 16rpx 24rpx;
  132. font-size: 32rpx;
  133. margin-top: 40rpx;
  134. }
  135. .login-btn {
  136. background-color: #999999;
  137. margin-top: 32rpx;
  138. justify-content: center;
  139. padding: 16rpx 0;
  140. .label {
  141. font-size: 40rpx;
  142. color: #FFFFFF;
  143. }
  144. }
  145. }
  146. </style>