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

@ -1,8 +1,8 @@
import request from '../utils/request' import request from '@/utils/request'
// 登录方法 // 登录方法
export function login(data) { export function login(data) {
return request({ return request({
url: '/auth/app/login', // 假设登录接口是 /login url: '/app/auth/login',
method: 'POST', method: 'POST',
data: data data: data
}) })
@ -15,6 +15,15 @@ export function sendRegisterSms(data) {
data: data data: data
}) })
} }
// 退出登录
export function Logout(data) {
return request({
url: '/app/auth/logout',
method: 'POST',
data: data
})
}

9
api/common/qrcode.js Normal file
View File

@ -0,0 +1,9 @@
import request from '@/utils/request'
// APP设备入库绑定
export function deviceBind(data) {
return request({
url: '/api/app/device/bind',
method: 'POST',
data: data
})
}

View File

@ -49,7 +49,6 @@
<!-- 按钮组 --> <!-- 按钮组 -->
<view class="popup-buttons"> <view class="popup-buttons">
<!-- <button class="btn closeBtn" @click="sureBtn">取消</button> -->
<button class="btn agreeBtn" @click="handleBtn">确定</button> <button class="btn agreeBtn" @click="handleBtn">确定</button>
</view> </view>
</view> </view>
@ -80,10 +79,7 @@
data() { data() {
return { return {
navBarHeight: 70 + uni.getSystemInfoSync().statusBarHeight, navBarHeight: 70 + uni.getSystemInfoSync().statusBarHeight,
deviceList: Array(5).fill().map((_, i) => ({ deviceList:[],
id: `022222${i}`,
})),
Options: [{ Options: [{
text: '重命名', text: '重命名',
style: { style: {

View File

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

View File

@ -27,6 +27,7 @@
</template> </template>
<script> <script>
import {deviceBind} from "@/api/common/qrcode.js"
export default { export default {
data() { data() {
return { return {
@ -36,21 +37,25 @@
} }
}, },
methods: { methods: {
handleConnect() { async handleConnect() {
if (this.isConnecting) { if (this.isConnecting) {
} else { } else {
console.log('这里了么');
// 开始连接逻辑 // 开始连接逻辑
uni.showLoading({ uni.showLoading({
mask: true, mask: true,
title: '加载中....' title: '加载中....'
}) })
this.isConnecting = true; this.isConnecting = true;
//这里可以添加实际的连接操作 try {
setTimeout(() => { // 调用绑定设备接口
this.connectSuccess(); await deviceBind({
}, 2000); //deviceImei: this.deviceId,
deviceIMEI: 'FYS-YF-082-SB',
})
} catch (error) {
}
} }
}, },
connectSuccess() { connectSuccess() {

View File

@ -11,14 +11,13 @@
export default { export default {
data() { data() {
return { return {
// 这里可以添加数据绑定
} }
}, },
methods: { methods: {
// 扫描 // 扫描
scanCode() { scanCode() {
uni.scanCode({ uni.scanCode({
success: (res) => { // 改为箭头函数保持this指向 success: (res) => {
console.log('条码内容:' + res.result); console.log('条码内容:' + res.result);
// 跳转并传递扫描结果 // 跳转并传递扫描结果
uni.navigateTo({ uni.navigateTo({

View File

@ -54,6 +54,9 @@
</template> </template>
<script> <script>
import {
Logout
} from '@/api/common/login.js'
export default { export default {
data() { data() {
return { return {
@ -61,18 +64,28 @@
} }
}, },
methods: { methods: {
// 不同意 // 取消
handleDisagree() { handleDisagree() {
this.showAgreement = false, this.showAgreement = false,
uni.showTabBar() uni.showTabBar()
}, },
// 同意 // 确定
handleAgree() { handleAgree() {
// 退出登录
Logout().then((res) => {
this.showAgreement = false this.showAgreement = false
uni.navigateTo({
url: '/pages/common/login/index'
})
uni.showTabBar() uni.showTabBar()
// 1. 清除本地存储的 token
uni.removeStorageSync('token');
uni.showToast({
title: '退出成功',
icon: 'success'
})
uni.reLaunch({
url: '/pages/common/login/index'
});
})
}, },
// 退出登录 // 退出登录
logout() { logout() {

View File

@ -1,5 +1,5 @@
import axios from 'axios' import axios from 'axios'
const BASE_URL = 'http://192.168.2.23:8000' const BASE_URL = 'http://192.168.2.23:8001'
// 创建 Axios 实例 // 创建 Axios 实例
const service = axios.create({ const service = axios.create({
baseURL: BASE_URL, baseURL: BASE_URL,
@ -11,7 +11,6 @@ service.interceptors.request.use(
console.log('Full Request URL:', config.baseURL + config.url) console.log('Full Request URL:', config.baseURL + config.url)
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
if (token) { if (token) {
config.headers['Authorization'] = 'Bearer ' + token config.headers['Authorization'] = 'Bearer ' + token
} }
config.headers['Content-Type'] = config.headers['Content-Type'] || 'application/json'; config.headers['Content-Type'] = config.headers['Content-Type'] || 'application/json';
@ -26,17 +25,11 @@ service.interceptors.request.use(
// 响应拦截器 // 响应拦截器
service.interceptors.response.use( service.interceptors.response.use(
response => { response => {
const res = response.data return response.data;
if (res.code !== 200 && res.code !== 0) {
console.log('API Error:', res.message || 'Error')
return Promise.reject(new Error(res.message || 'Error'))
} else {
return res
}
}, },
error => { error => {
return Promise.reject(error) return Promise.reject(error);
} }
) );
export default service export default service