dialog-login.vue 4.1 KB

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