dialog-login.vue 4.4 KB

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