<!-- 登录页面 -->
<template>
<view class="__logo">
<image class="__logo_img" src="../../static/logo.png" mode=""></image>
</view>
<view class="__form">
<uni-easyinput focus prefixIcon="person" v-model="formData.account" placeholder="账号" />
<view class="__fg"></view>
<uni-easyinput type="password" prefixIcon="locked-filled" v-model="formData.password" placeholder="密码" />
<view class="__fg"></view>
<view class="__login_btn" @click="handleLogin">登录</view>
</view>
</template>
<script setup>
import logic from "./logic.js";
const { formData, handleLogin } = logic();
</script>
<style lang="less" scoped>
.__logo {
position: relative;
display: flex;
align-items: center;
justify-content: center;
margin: 3rem auto;
.__logo_img {
width: 4rem;
height: 4rem;
text-align: center;
}
}
.__form {
padding: 2rem;
.__fg {
margin: 1rem 0;
}
.__login_btn {
margin: 1rem 0;
background-color: #5cadff;
border-radius: 0.3rem;
color: #fff;
font-size: 1rem;
padding: 0.5rem 1rem;
text-align: center;
}
}
</style>
<!-- logic.js -->
import { reactive, ref } from "vue";
import { publicKeyApi, loginApi } from "../../api/user-api.js";
import api_request from "../../lib/request.js";
import WxmpRsa from "wxmp-rsa";
import { onLoad } from "@dcloudio/uni-app";
export default function() {
const formData = reactive({ account: "", password: "" });
let pk = "";
const getPk = () => {
uni.request({
url: publicKeyApi,
success: ({ data }) => {
pk = data['data'];
}
})
};
getPk();
const handleLogin = () => {
console.log(formData, "==============");
const enc = new WxmpRsa();
enc.setPublicKey(pk || ''); //设置公钥
const pwd = enc.encryptLong(formData.password);
uni.showLoading({
title: "验证信息...",
icon: "none",
});
const { promise } = api_request.request(loginApi, "POST", { account: formData.account, password: pwd });
promise.then((res) => {
console.log(res);
}).catch((err) => {
const { message, errMsg, error } = err;
uni.showToast({ icon: "none", title: message || errMsg || error, duration: 3000 });
})
}
return { formData, handleLogin }
}
<!-- request.js -->
import { dataType } from "./tools.js";
const api_request = {
request: (url, method, parm, header) => {
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000; //获取当前时间戳
let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
let curRoute = "/pages/index/index";
if (routes.length > 0) {
curRoute = ("/" + routes[routes.length - 1].route).replaceAll("//", "/") // 获取当前页面路由,也就是最后一个打开的页面路由
}
if (!!header) {
header = { 'Content-Type': header }
}
var req = null;
//通过promise返回数据
var promise = new Promise(function(resolve, reject) {
req = uni.request({
method,
url,
header,
data: parm,
//请求成功
success: function(res) {
var { statusCode, data, errMsg } = res;
if (statusCode == 200) {
let result = res.data;
if (dataType(res.data) === "string") {
result = JSON.parse(res.data)
}
var { code, data, errMsg, message, description } = result;
const err = errMsg || message || description;
if (code === 200) {
resolve(data);
} else {
if (code == 401) {
console.error(err);
uni.redirectTo({ url: "/pages/login/login" });
} else if (code == 403) {
console.error(err);
reject({ error: "您没有权限操作", code: code });
} else {
console.error(err);
reject({ error: err, code: code });
}
}
} else if (statusCode == 401) {
// var origin = /^(https|http)?:\/\/[\w-.]+(:\d+)?/i.exec(url)[0];
console.error(errMsg);
uni.redirectTo({
url: "/pages/login/login",
fail: (err) => {
console.log(err);
}
});
} else if (statusCode == 403) {
console.error(errMsg);
reject({ error: "您没有权限操作", code: statusCode });
} else {
console.error(errMsg);
reject({ error: data || "服务器内部错误", code: statusCode });
}
},
fail(err) {
console.error(err);
reject({ error: '数据请求失败,请稍后重试', code: 500 });
}
})
})
//返回数据
return {
promise,
req
};
},
abort: (req) => req.abort(),
uploader: (url, filePath, name, file, formData) => {
var req = null;
//通过promise返回数据
var promise = new Promise(function(resolve, reject) {
req = uni.uploadFile({
url,
filePath,
file,
name,
formData, //Object HTTP 请求中其他额外的 form data
//请求成功
success: function(res) {
var { statusCode, data, errMsg } = res;
if (statusCode == 200) {
let result = res.data;
if (dataType(res.data) === "string") {
result = JSON.parse(res.data)
}
var { code, data, errMsg, message, description } = result;
const err = errMsg || message || description;
if (code === 200) {
resolve(data);
} else {
if (code == 401) {
// var origin = /^(https|http)?:\/\/[\w-.]+(:\d+)?/i.exec(url)[0];
console.error(err);
// new Login({
// callBack: () => {
// // reject({
// // error: "网络错误,请重试...",
// // code: code
// // });
// uni.redirectTo({
// url: curRoute
// });
// }
// });
uni.redirectTo({
url: "/pages/login/login"
});
} else if (code == 403) {
console.error(err);
reject({
error: "您没有权限操作",
code: code
});
} else {
console.error(err);
reject({
error: err,
code: code
});
}
}
} else if (statusCode == 401) {
// var origin = /^(https|http)?:\/\/[\w-.]+(:\d+)?/i.exec(url)[0];
console.error(errMsg);
// new Login({
// callBack: () => {
// // reject({
// // error: "网络错误,请重试...",
// // code: code
// // });
// uni.redirectTo({
// url: curRoute
// });
// }
// });
uni.redirectTo({
url: "/pages/login/login"
});
} else if (statusCode == 403) {
console.error(errMsg);
reject({
error: "您没有权限操作",
code: statusCode
});
} else {
console.error(errMsg);
reject({
error: data || "服务器内部错误",
code: statusCode
});
}
},
fail(err) {
console.error(err);
reject({
error: '数据请求失败,请稍后重试',
code: 500
});
}
})
})
//返回数据
return {
promise,
req
};
}
}
export default api_request
- 发布:2023-08-30 11:55
- 更新:2024-12-26 10:51
- 阅读:1054
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: mac pro m1 ventura 13.5.1
HBuilderX类型: 正式
HBuilderX版本号: 3.8.12
手机系统: Android
手机系统版本号: Android 14
手机厂商: 华为
手机机型: 华为P10
页面类型: vue
vue版本: vue3
打包方式: 云端
项目创建方式: HBuilderX
示例代码:
操作步骤:
直接云端打包
直接云端打包
预期结果:
能正常请求网络
能正常请求网络
实际结果:
不能请求网络
不能请求网络
bug描述:
网络请求在网页网和小程序端都没问题,打包成apk后,无法发送网络请求
2 个回复
2***@qq.com (作者)
我自己找到原因了,因为我自己又把uni.request封装了一遍,封装中使用了promise,而安卓环境下 是没有这个的,重新封装一下就行了
m***@163.com
大佬,具体怎么封装能分享一下吗?