app网络请求封装修改
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
import request from '../utils/request'
|
||||
import request from '@/utils/request'
|
||||
// 登录方法
|
||||
export function login(data) {
|
||||
return request({
|
||||
url: '/auth/app/login', // 假设登录接口是 /login
|
||||
url: '/app/auth/login',
|
||||
method: 'POST',
|
||||
data: data
|
||||
})
|
||||
@ -15,6 +15,15 @@ export function sendRegisterSms(data) {
|
||||
data: data
|
||||
})
|
||||
}
|
||||
// 退出登录
|
||||
export function Logout(data) {
|
||||
return request({
|
||||
url: '/app/auth/logout',
|
||||
method: 'POST',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
9
api/common/qrcode.js
Normal file
9
api/common/qrcode.js
Normal 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
|
||||
})
|
||||
}
|
@ -49,7 +49,6 @@
|
||||
<!-- 按钮组 -->
|
||||
|
||||
<view class="popup-buttons">
|
||||
<!-- <button class="btn closeBtn" @click="sureBtn">取消</button> -->
|
||||
<button class="btn agreeBtn" @click="handleBtn">确定</button>
|
||||
</view>
|
||||
</view>
|
||||
@ -80,10 +79,7 @@
|
||||
data() {
|
||||
return {
|
||||
navBarHeight: 70 + uni.getSystemInfoSync().statusBarHeight,
|
||||
deviceList: Array(5).fill().map((_, i) => ({
|
||||
id: `022222${i}`,
|
||||
|
||||
})),
|
||||
deviceList:[],
|
||||
Options: [{
|
||||
text: '重命名',
|
||||
style: {
|
||||
|
@ -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'
|
||||
});
|
||||
}
|
||||
|
||||
},
|
||||
// 打开具体协议
|
||||
|
@ -27,6 +27,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {deviceBind} from "@/api/common/qrcode.js"
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@ -36,21 +37,25 @@
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
handleConnect() {
|
||||
async handleConnect() {
|
||||
if (this.isConnecting) {
|
||||
|
||||
} else {
|
||||
console.log('这里了么');
|
||||
// 开始连接逻辑
|
||||
uni.showLoading({
|
||||
mask: true,
|
||||
title: '加载中....'
|
||||
})
|
||||
this.isConnecting = true;
|
||||
//这里可以添加实际的连接操作
|
||||
setTimeout(() => {
|
||||
this.connectSuccess();
|
||||
}, 2000);
|
||||
try {
|
||||
// 调用绑定设备接口
|
||||
await deviceBind({
|
||||
//deviceImei: this.deviceId,
|
||||
deviceIMEI: 'FYS-YF-082-SB',
|
||||
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
}
|
||||
}
|
||||
},
|
||||
connectSuccess() {
|
||||
|
@ -11,14 +11,13 @@
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
// 这里可以添加数据绑定
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 扫描
|
||||
scanCode() {
|
||||
uni.scanCode({
|
||||
success: (res) => { // 改为箭头函数保持this指向
|
||||
success: (res) => {
|
||||
console.log('条码内容:' + res.result);
|
||||
// 跳转并传递扫描结果
|
||||
uni.navigateTo({
|
||||
|
@ -54,6 +54,9 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {
|
||||
Logout
|
||||
} from '@/api/common/login.js'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@ -61,18 +64,28 @@
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 不同意
|
||||
// 取消
|
||||
handleDisagree() {
|
||||
this.showAgreement = false,
|
||||
uni.showTabBar()
|
||||
uni.showTabBar()
|
||||
},
|
||||
// 同意
|
||||
// 确定
|
||||
handleAgree() {
|
||||
this.showAgreement = false
|
||||
uni.navigateTo({
|
||||
url: '/pages/common/login/index'
|
||||
// 退出登录
|
||||
Logout().then((res) => {
|
||||
this.showAgreement = false
|
||||
uni.showTabBar()
|
||||
// 1. 清除本地存储的 token
|
||||
uni.removeStorageSync('token');
|
||||
uni.showToast({
|
||||
title: '退出成功',
|
||||
icon: 'success'
|
||||
})
|
||||
uni.reLaunch({
|
||||
url: '/pages/common/login/index'
|
||||
});
|
||||
})
|
||||
uni.showTabBar()
|
||||
|
||||
},
|
||||
// 退出登录
|
||||
logout() {
|
||||
@ -80,19 +93,19 @@
|
||||
uni.hideTabBar()
|
||||
},
|
||||
// 用户协议
|
||||
userAgree(){
|
||||
userAgree() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/common/userAgreement/index'
|
||||
})
|
||||
},
|
||||
// 隐私协议
|
||||
privacyAgree(){
|
||||
privacyAgree() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/common/privacyAgreement/index'
|
||||
})
|
||||
},
|
||||
// 关于我们
|
||||
aboutUs(){
|
||||
aboutUs() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/common/aboutUs/index'
|
||||
})
|
||||
|
@ -1,42 +1,35 @@
|
||||
import axios from 'axios'
|
||||
const BASE_URL = 'http://192.168.2.23:8000'
|
||||
const BASE_URL = 'http://192.168.2.23:8001'
|
||||
// 创建 Axios 实例
|
||||
const service = axios.create({
|
||||
baseURL: BASE_URL,
|
||||
timeout: 10000
|
||||
baseURL: BASE_URL,
|
||||
timeout: 10000
|
||||
})
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
console.log('Full Request URL:', config.baseURL + config.url)
|
||||
const token = uni.getStorageSync('token')
|
||||
if (token) {
|
||||
|
||||
config.headers['Authorization'] = 'Bearer ' + token
|
||||
}
|
||||
config.headers['Content-Type'] = config.headers['Content-Type'] || 'application/json';
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
console.log('Request Error:', error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
config => {
|
||||
console.log('Full Request URL:', config.baseURL + config.url)
|
||||
const token = uni.getStorageSync('token')
|
||||
if (token) {
|
||||
config.headers['Authorization'] = 'Bearer ' + token
|
||||
}
|
||||
config.headers['Content-Type'] = config.headers['Content-Type'] || 'application/json';
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
console.log('Request Error:', error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
const res = 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
|
||||
}
|
||||
return response.data;
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error)
|
||||
return Promise.reject(error);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
export default service
|
||||
export default service
|
Reference in New Issue
Block a user