dialog-login.vue 4.1 KB

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