dialog-login.vue 4.5 KB

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