app网络请求封装修改

This commit is contained in:
fengerli
2025-07-07 18:59:34 +08:00
parent 1128114e73
commit e69ff064d5
8 changed files with 145 additions and 85 deletions

View File

@ -53,7 +53,10 @@
</template>
<script>
import { login } from '@/api/login';
import {
login,
sendRegisterSms
} from '@/api/common/login.js';
export default {
data() {
return {
@ -63,14 +66,14 @@
code: "", //验证码
agreed: false,
isCounting: false,
countdown: 60,
countdown: 0,
isChecked: false,
showAgreement: false // 控制弹窗显示
showAgreement: false, // 控制弹窗显示
}
},
methods: {
// 获取验证码
getPhoneCode() {
async getPhoneCode() {
const phoneNumber = this.phone
const myreg = /^1[3456789]\d{9}$/;
if (!phoneNumber) {
@ -88,27 +91,31 @@
});
return false;
}
this.isCounting = true
this.countdown = 60
const timer = setInterval(() => {
this.countdown--
if (this.countdown <= 0) {
clearInterval(timer)
this.isCounting = false
}
}, 1000)
// 接口调用
// uni.request({
// ...
// })
try {
// 调用发送验证码接口
await sendRegisterSms({
phoneNumber: this.phone
})
//倒计时
this.countdown = 60
const timer = setInterval(() => {
this.countdown--
if (this.countdown <= 0) clearInterval(timer)
}, 1000)
uni.showToast({
title: '验证码已发送',
icon: 'none'
})
} catch (error) {
}
},
// 勾选同意
toggleCheck() {
this.isChecked = !this.isChecked
},
// 登录
handleLogin() {
async handleLogin() {
console.log('33333');
if (this.phone == '') {
uni.showToast({
title: '手机号不能为空',
@ -123,22 +130,51 @@
duration: 1000
})
return false
} else if (!this.isChecked) {
}
if (!this.isChecked) {
this.showAgreement = true
return false
}
// 接口调用
// uni.request({
// ...
// })
// uni.showToast({
// title: '验证通过了,走接口',
// icon: 'none',
// duration: 1000
// })
uni.switchTab({
url: '/pages/common/index/index'
})
try {
uni.showLoading({
title: '登录中...'
})
console.log('44444');
// 调用登录接口
const res = await login({
phonenumber: this.phone,
smsCode: this.code,
tenantId: '894078' //租户ID
})
console.log(res, 'res44444');
if (res.code == 200) {
uni.hideLoading()
uni.setStorageSync('token', res.data.access_token) // 缓存token
uni.showToast({
title: '登录成功',
icon: 'success'
})
// 跳转到首页
setTimeout(() => {
uni.switchTab({
url: '/pages/common/index/index'
})
}, 800)
} else {
uni.showToast({
title: res.msg,
icon: 'none'
})
}
} catch (error) {
uni.hideLoading()
uni.showToast({
title: error.msg,
icon: 'none'
});
}
},
// 打开具体协议