dialog-login.vue 4.2 KB

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