dialog-login.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 urlList = uni.getStorageSync('baseUrl');
  59. loginRequest(JSON.parse(urlList).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. if(currentUser.value.userName == null || currentUser.value.userName.trim() == ''
  88. ||currentUser.value.password == null || currentUser.value.password.trim() == ''){
  89. uni.showToast({
  90. icon: "none",
  91. title: "请用户名或密码",
  92. duration: 1000
  93. })
  94. }else{
  95. // 请求
  96. uni.request({
  97. method: 'POST',
  98. url: url + '/login',
  99. timeout: 10000,
  100. data: {
  101. username: currentUser.value.userName,
  102. password: currentUser.value.password
  103. },
  104. success: (res) => {
  105. if (res.data.code === 200) {
  106. uni.showToast({
  107. title: successMsg.value,
  108. icon: 'success',
  109. duration: 1000
  110. });
  111. // 保存token
  112. uni.setStorageSync('token', res.data.token);
  113. uni.setStorage({
  114. key: currentUser.value.userName,
  115. data: currentUser.value.password,
  116. });
  117. // 获取员工信息存入store,并跳转到控制面板
  118. getUserInfo(currentUser.value).then((res) => {
  119. if (res.code == 200) {
  120. userInfo.value = res.data;
  121. user.value.push(userInfo.value);
  122. store.userInfo = res.data
  123. // console.log(res)
  124. uni.redirectTo({
  125. url: '/pages/dashboard/index'
  126. });
  127. }
  128. })
  129. } else {
  130. uni.showToast({
  131. icon: 'none',
  132. title: res.data.msg,
  133. //显示持续时间为 2秒
  134. duration: 1000
  135. })
  136. }
  137. },
  138. fail: (err) => {
  139. console.log(err)
  140. uni.showToast({
  141. icon: "none",
  142. title: "请求异常,请联系管理员",
  143. duration: 1000
  144. })
  145. }
  146. })
  147. }
  148. }
  149. /** 暴露给父组件的方法 */
  150. defineExpose({
  151. open
  152. })
  153. </script>
  154. <style lang="scss">
  155. .dialog-body {
  156. .user-info-container {
  157. margin-top: 32rpx;
  158. align-items: center;
  159. .user-avatar {
  160. width: 120rpx;
  161. height: 120rpx;
  162. justify-content: center;
  163. align-items: center;
  164. border-radius: 120rpx;
  165. background-color: #F5F5F5;
  166. .label {
  167. font-size: 72rpx;
  168. }
  169. }
  170. .user-info {
  171. flex: 1;
  172. padding-left: 16rpx;
  173. .nickname {
  174. margin-bottom: 8rpx;
  175. .label {
  176. font-size: 40rpx;
  177. }
  178. }
  179. .current-process {
  180. .label {
  181. font-size: 32rpx;
  182. color: #666666;
  183. }
  184. }
  185. }
  186. }
  187. .input-control {
  188. border: 1px solid #666666;
  189. padding: 16rpx 24rpx;
  190. font-size: 32rpx;
  191. margin-top: 40rpx;
  192. }
  193. .login-btn {
  194. // background-color: #999999;
  195. background-color: #1684fc;
  196. margin-top: 32rpx;
  197. justify-content: center;
  198. padding: 16rpx 0;
  199. .label {
  200. font-size: 40rpx;
  201. color: #FFFFFF;
  202. }
  203. }
  204. }
  205. </style>