import API from './api.js'
import STORE from '../store.js'
let version = ''
const TIMEOUT = 30000
const PARAMS = data => {
let _params = []
for (const k in data) {
_params.push(`${encodeURIComponent(k)}=${encodeURIComponent(data[k])}`)
}
return _params.join('&')
}
/**
* APP网络请求
* @param {Object} options
*/
function _AppNet(options) {
let _header = {
Accept: 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
}
const _method = options.method.toUpperCase()
const URL = (options.params && Object.keys(options.params).length !== 0 && _method === 'POST') ? `${options.url}?${PARAMS(options.params)}` : options.url
const _token = uni.getStorageSync('token')
if (_token && _token.length !== 0) {
_header['Authorization'] = `Bearer ${_token}`
}
// #ifdef APP-PLUS
_header['client-type'] = 'android'
if (version === '') {
plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
version = wgtinfo.version
})
}
_header['client-version'] = version
// #endif
// #ifdef MP-WEIXIN
_header['client-type'] = 'wechat_mini_program'
// #endif
// 是否显示加载loading
options.loading && uni.showLoading({ title: '加载中...', mask: true })
return uni.request({
url: API.root + URL,
data: options.data,
header: _header,
method: _method,
dataType: 'json',
timeout: TIMEOUT
}).then(data => {
options.loading && uni.hideLoading()
const [err, res] = data
if (err) {
options.error && _errorToast(-1)
return err
}
const _data = res.data
options.error && _errorToast(_data.code, _data.msg)
if (_data.code === 3) _forceLogout()
return _data
})
/**
* 网络请求错误提示
* @param {Object} code 错误代码
* @param {Object} msg 错误信息
*/
function _errorToast(code, msg) {
code !== 0 && code !== 3 && code !== 80103 && uni.showToast({ icon: 'none', title: msg || '网络异常,请稍候再试' })
}
// 强制用户退出
function _forceLogout() {
uni.showToast({
icon: 'none',
title: '登录状态失效,请重新登录',
success() {
uni.clearStorageSync()
setTimeout(() => {
// #ifdef APP-PLUS
uni.reLaunch({ url: '/pages/start/app/index'})
// #endif
// #ifdef MP-WEIXIN
uni.reLaunch({ url: '/pages/start/index'})
// #endif
}, 800)
}
})
}
}
export default {
$api: API.$api,
// #ifdef MP-WEIXIN
loginCode() {
return new Promise(resovle => {
uni.login({
success(res) { resovle(res.code || null) },
fail() { resovle(null) }
})
})
},
checkSession() {
return new Promise(resovle => {
uni.checkSession({
success() { resovle(true) },
fail() { resovle(false) }
})
})
},
wxMiniLogin(code) {
return new Promise(resovle => {
uni.showModal({
title: '温馨提示',
content: `微信授权登录后才能正常使用“${getApp().globalData.appName}”`,
cancelColor: '#de6352',
confirmColor: '#206ddb',
success(modal) {
if (modal.confirm) {
uni.getUserProfile({
lang: 'zh_CN',
desc: '展示并完善用户资料信息',
success(res) {
if (res.iv && res.encryptedData) {
STORE.commit('setUserInfo', {
avatar: res.userInfo.avatarUrl,
nickname: res.userInfo.nickName
})
resovle(_AppNet({
loading: true,
url: API.$api.login.wx_mini,
method: 'POST',
data: { code, iv: res.iv, encrypted_data: res.encryptedData }
}))
} else {
resovle(null)
}
},
fail() {
resovle(null)
}
})
} else {
resovle(null)
}
}
})
})
},
// #endif
get(url, options) {
return _AppNet({
url, method: 'GET',
data: (options && options.data) ? options.data : {},
error: options ? (options.error === false ? false : true) : true,
loading: options ? (options.loading === false ? false : true) : true
})
},
post(url, options) {
return _AppNet({
url, method: 'POST',
data: (options && options.data) ? options.data : {},
params: (options && options.params) ? options.params : {},
error: options ? (options.error === false ? false : true) : true,
loading: options ? (options.loading === false ? false : true) : true
})
}
} - 发布:2022-04-22 12:27
- 更新:2022-04-22 12:27
- 阅读:901
产品分类: uniapp/App
PC开发环境操作系统: Mac
PC开发环境操作系统版本号: 12.3.1
HBuilderX类型: 正式
HBuilderX版本号: 3.3.13
手机系统: Android
手机系统版本号: Android 9.0
手机厂商: 华为
手机机型: 华为 P10 plus
页面类型: vue
vue版本: vue2
打包方式: 离线
项目创建方式: HBuilderX
示例代码:
操作步骤:
同一份代码,离线打包apk
同一份代码,离线打包apk
预期结果:
安装到手机上正常访问网络
安装到手机上正常访问网络
实际结果:
安装到手机上无法联网
安装到手机上无法联网
bug描述:
同样的一份代码,在小程序、iOS模拟器、Android模拟器均可以正常访问网络,但根据相关文档在Android Studio(2021.1.1 Patch 3)配置好离线SDK工程打包Release包安装到手机上就无法联网