gaoluyang
2025-08-11 df1992a1c3ca982b1432aaf41668bb8437d02a2d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<template>
  <view class="normal-login-container">
    <view class="logo-content">
      <text>账号密码登录</text>
    </view>
    <view class="login-form-content">
      <view class="input-item flex align-center">
                <up-input prefixIcon="account" placeholder="请输入账号" border="bottom"
                                    @blur="getUserLoginFacotryList"
                                    maxlength="30" v-model="loginForm.username" clearable></up-input>
      </view>
      <view class="input-item flex align-center">
                <up-input prefixIcon="lock" placeholder="请输入密码" border="bottom" maxlength="20" v-model="loginForm.password" clearable type="password"></up-input>
      </view>
            <view class="input-item flex align-center select-container">
                <up-icon name="tags" size="18"></up-icon>
                <up-picker-data
                    v-model="loginForm.factoryId"
                    title="请选择公司"
                    :options="factoryList"
                    valueKey="id"
                    labelKey="name">
                </up-picker-data>
            </view>
      <view>
        <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
      </view>
    </view>
        <!-- 记住密码选项 -->
        <view class="remember-password">
            <up-checkbox
                :customStyle="{marginBottom: '8px'}"
                label="记住密码"
                name="agree"
                usedAlone
                v-model:checked="rememberPassword"
            >
            </up-checkbox>
        </view>
  </view>
</template>
 
<script setup>
import modal from '@/plugins/modal'
import { userLoginFacotryList} from '@/api/login'
import { ref, onMounted } from "vue";
import useUserStore from '@/store/modules/user'
import { getWxCode } from '@/utils/geek';
import { wxLogin } from '@/api/oauth';
import { setToken } from '@/utils/auth';
const userStore = useUserStore()
const useWxLogin = ref(false); // 是否使用微信登录
const rememberPassword = ref(false); // 记住密码
const loginForm = ref({
  username: "",
  password: "",
    factoryId: "",
    currentFatoryName: "",
});
const factoryList = ref([]) // 公司列表
 
// 保存密码到本地存储
function savePassword() {
  if (rememberPassword.value) {
    uni.setStorageSync('remembered_username', loginForm.value.username);
    uni.setStorageSync('remembered_password', loginForm.value.password);
    uni.setStorageSync('remember_password', true);
  } else {
    uni.removeStorageSync('remembered_username');
    uni.removeStorageSync('remembered_password');
    uni.setStorageSync('remember_password', false);
  }
}
 
// 从本地存储加载密码
function loadPassword() {
  const remembered = uni.getStorageSync('remember_password');
  if (remembered) {
    rememberPassword.value = true;
    const savedUsername = uni.getStorageSync('remembered_username');
    const savedPassword = uni.getStorageSync('remembered_password');
    if (savedUsername) {
      loginForm.value.username = savedUsername;
    }
    if (savedPassword) {
      loginForm.value.password = savedPassword;
    }
  }
}
 
if (useWxLogin.value) {
  getWxCode().then(res => {
    console.log(res);
    wxLogin('miniapp',res).then(res => {
      if(res.token != null){
        setToken(res.token);
        loginSuccess()
      }
    });
  })
}
 
function getUserLoginFacotryList() {
    if(loginForm.value.username){
        userLoginFacotryList({userName:loginForm.value.username}).then(res => {
            // 检查res.data是否为数组
            if (res.data && Array.isArray(res.data)) {
                // 重新组装数据格式:deptId变成id,deptName变成name
                factoryList.value = res.data.map(item => ({
                    id: item.deptId,
                    name: item.deptName
                }))
            } else {
                // 如果res.data不是数组,设置为空数组
                factoryList.value = []
            }
        }).catch(error => {
            modal.msgError('获取公司列表失败:', error)
            factoryList.value = []
        })
    }else {
        factoryList.value = []
    }
}
 
async function handleLogin() {
  if (loginForm.value.username === "") {
    modal.msgError("请输入您的账号")
  } else if (loginForm.value.password === "") {
    modal.msgError("请输入您的密码")
  } else if (loginForm.value.factoryId === "") {
    modal.msgError("请选择公司")
  } else {
    modal.loading("登录中,请耐心等待...")
    pwdLogin()
  }
};
// 密码登录
async function pwdLogin() {
  userStore.loginCheckFactory(loginForm.value).then(() => {
    modal.closeLoading()
    // 登录成功后保存密码
    savePassword();
    loginSuccess()
  }).catch(() => {
        modal.closeLoading()
  })
};
 
function loginSuccess(result) {
  // 设置用户信息
  userStore.getInfo().then(res => {
    uni.switchTab({
      url: '/pages/index'
    });
  })
}
// 页面加载时检查是否有保存的密码
onMounted(() => {
    loadPassword();
    getUserLoginFacotryList()
});
</script>
 
<style lang="scss">
page {
  background-color: #ffffff;
}
 
.normal-login-container {
  width: 100%;
    height: 100vh;
 
  .logo-content {
    width: 90%;
        font-weight: 400;
        font-size: 30px;
        color: #333333;
        margin: 80px 0 0 30px;
 
    image {
      border-radius: 4px;
    }
 
    .title {
      margin-left: 10px;
    }
  }
    .u-checkbox {
        margin-left: 34px;
    }
 
  .login-form-content {
    text-align: center;
    margin: 58px auto;
        padding: 0 30px;
 
        .input-item {
       margin: 30px auto;
       height: 45px;
 
       .icon {
         font-size: 38rpx;
         margin-left: 10px;
         color: #999;
       }
 
       .input {
         width: 100%;
         font-size: 14px;
         line-height: 20px;
         text-align: left;
         padding-left: 15px;
       }
     }
        .select-container {
            flex: 1;
            border-bottom: 1px solid #e5e5e5;
            padding: 6px 9px;
            
            :deep(.up-select) {
                border: none;
                background: transparent;
                
                .up-select__label {
                    font-size: 14px;
                    color: #333;
                }
                
                .up-select__value {
                    font-size: 14px;
                    color: #333;
                }
            }
        }
 
    .login-btn {
      margin-top: 60px;
      height: 50px;
            background: linear-gradient( 140deg, #00BAFF 0%, #006CFB 100%);
            box-shadow: 0px 4px 10px 0px rgba(3,88,185,0.2);
            border-radius: 40px 40px 40px 40px;
    }
 
    .xieyi {
      color: #333;
      margin-top: 20px;
    }
 
    .login-code {
      height: 38px;
      float: right;
 
      .login-code-img {
        height: 38px;
        position: absolute;
        margin-left: 10px;
        width: 200rpx;
      }
    }
  }
}
</style>