dialog-login.vue 4.4 KB

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