123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <template>
- <dialog-base ref="baseDialog" title="账号登录">
- <template v-if="currentUser.nickname">
- <view class="user-info-container uni-row">
- <view class="user-avatar uni-row"><text class="label">{{ currentUser.nickname }}</text></view>
- <view class="user-info">
- <view class="nickname"><text class="label">{{ currentUser.nickname }}</text></view>
- <view class="current-process"><text class="label">热处理</text></view>
- </view>
- </view>
- </template>
- <template v-else>
- <input class="input-control" v-model="currentUser.userName" placeholder="员工编码" />
- </template>
- <input class="input-control" :password="true" v-model="currentUser.password" placeholder='密码' />
- <view class="login-btn uni-row" @click="debounce(handleLogin,300)"><text class="label">登录</text></view>
- </dialog-base>
- </template>
- <script setup>
- import {
- ref,
- getCurrentInstance
- } from 'vue'
- import {
- onLoad
- } from '@dcloudio/uni-app'
- import {
- getUserInfo
- } from '@/api/login/index.js'
- import {
- getUserProcess
- } from '@/api/process/process.js'
-
- import {
- store
- } from '@/store/index.js'
- import { debounce } from '@/utils/common.js'
- // 对话框
- const baseDialog = ref(null)
- const currentUser = ref({})
- const errorMsg = ref('用户名或密码错误')
- const successMsg = ref('登录成功')
- const userInfo = ref({})
- const user = ref([])
- // const URL = ref('')
- // const { proxy } = getCurrentInstance()
- onLoad(() => {
- // URL.value = baseURL;
- })
- function init() {
- }
- const open = (data) => {
- currentUser.value = data
- // console.log(dialog.value)
- baseDialog.value.open()
- }
- const handleLogin = () => {
- // 获取服务器地址
- const urlList = uni.getStorageSync('baseUrl');
-
- loginRequest(JSON.parse(urlList).baseUrl)
- /*
- uni.getStorage({
- key: 'baseUrl',
- success: function(res) {
- URL.value = res.data;
- // 加载
- uni.showLoading({
- title: '加载中'
- });
- setTimeout(function() {
- uni.hideLoading();
- }, 3000)
- loginRequest(URL.value);
- console.log(URL.value)
- },
- fail: function() {
- uni.setStorageSync('baseUrl', URL.value);
- loginRequest(URL.value);
- console.log(URL.value)
- setTimeout(function() {
- uni.hideLoading();
- }, 3000)
- }
- });
- */
- }
- function loginRequest(url) {
- if(currentUser.value.userName == null || currentUser.value.userName.trim() == ''
- ||currentUser.value.password == null || currentUser.value.password.trim() == ''){
- uni.showToast({
- icon: "none",
- title: "请用户名或密码",
- duration: 1000
- })
- }else{
- // 请求
- uni.request({
- method: 'POST',
- url: url + '/login',
- timeout: 10000,
- data: {
- username: currentUser.value.userName,
- password: currentUser.value.password
- },
- success: (res) => {
- if (res.data.code === 200) {
- uni.showToast({
- title: successMsg.value,
- icon: 'success',
- duration: 1000
- });
- // 保存token
- uni.setStorageSync('token', res.data.token);
- uni.setStorage({
- key: currentUser.value.userName,
- data: currentUser.value.password,
- });
- // 获取员工信息存入store,并跳转到控制面板
- getUserInfo(currentUser.value).then((res) => {
- if (res.code == 200) {
- userInfo.value = res.data;
- user.value.push(userInfo.value);
- store.userInfo = res.data
- // console.log(res)
- uni.redirectTo({
- url: '/pages/dashboard/index'
- });
- }
- })
- } else {
- uni.showToast({
- icon: 'none',
- title: res.data.msg,
- //显示持续时间为 2秒
- duration: 1000
- })
- }
- },
- fail: (err) => {
- console.log(err)
- uni.showToast({
- icon: "none",
- title: "请求异常,请联系管理员",
- duration: 1000
- })
- }
- })
- }
- }
- /** 暴露给父组件的方法 */
- defineExpose({
- open
- })
- </script>
- <style lang="scss">
- .dialog-body {
- .user-info-container {
- margin-top: 32rpx;
- align-items: center;
- .user-avatar {
- width: 120rpx;
- height: 120rpx;
- justify-content: center;
- align-items: center;
- border-radius: 120rpx;
- background-color: #F5F5F5;
- .label {
- font-size: 72rpx;
- }
- }
- .user-info {
- flex: 1;
- padding-left: 16rpx;
- .nickname {
- margin-bottom: 8rpx;
- .label {
- font-size: 40rpx;
- }
- }
- .current-process {
- .label {
- font-size: 32rpx;
- color: #666666;
- }
- }
- }
- }
- .input-control {
- border: 1px solid #666666;
- padding: 16rpx 24rpx;
- font-size: 32rpx;
- margin-top: 40rpx;
- }
- .login-btn {
- // background-color: #999999;
- background-color: #1684fc;
- margin-top: 32rpx;
- justify-content: center;
- padding: 16rpx 0;
- .label {
- font-size: 40rpx;
- color: #FFFFFF;
- }
- }
- }
- </style>
|