forked from java110/WechatOwnerService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava110Request.js
More file actions
142 lines (122 loc) · 3.05 KB
/
Copy pathjava110Request.js
File metadata and controls
142 lines (122 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import coreUtil from '../utils/CoreUtil.js'
import app from '../constant/AppConstant.js'
import {getDateYYYYMMDDHHMISS} from '../utils/DateUtil.js'
import url from '../constant/url.js'
import mapping from '../constant/MappingConstant.js'
import {checkSession} from '../auth/Java110Auth.js'
import {
wechatRefreshToken
} from '../auth/H5Login.js'
import {
doLogin
} from '../auth/MpWeixinLogin.js'
import {
doLoginOwnerByKey
}
from '../auth/AppLogin.js'
import {debug,warn,info,error} from '../utils/LogUtil.js'
/**
* 获取请求头信息
* add by 吴学文 QQ:928255095
**/
export function getHeaders() {
let _wAppId = uni.getStorageSync(mapping.W_APP_ID);
return {
"app-id": app.appId,
"transaction-id": coreUtil.wxuuid(),
"req-time": getDateYYYYMMDDHHMISS(),
"sign": '1234567',
"user-id": '-1',
"cookie": '_java110_token_=' + wx.getStorageSync('token'),
"Accept": '*/*',
"w-app-id": _wAppId
};
}
/**
* http 请求 加入是否登录判断
*/
export function request(_reqObj) {
debug('java110Request','request',_reqObj);
//这里判断只有在 post 方式时 放加载框
if (_reqObj.hasOwnProperty("method") && "POST" == _reqObj.method) {
uni.showLoading({
title: '加载中',
mask: true
});
_reqObj.complete = function() {
uni.hideLoading();
}
}
let _headers = getHeaders();
if (_reqObj.hasOwnProperty('header')) {
let _header = _reqObj.header;
for (let key in _headers) {
_header.key = _headers[key];
}
} else {
_reqObj.header = _headers;
}
//白名单直接跳过检查登录
if (url.NEED_NOT_LOGIN_URL.includes(_reqObj.url)) {
_reqObj.communityId = mapping.HC_TEST_COMMUNITY_ID;
uni.request(_reqObj);
return;
}
//校验是否登录,如果没有登录跳转至温馨提示页面
checkSession().then(function() {
//有回话 跳转至相应页面
//重写token
_reqObj.header.cookie = '_java110_token_=' + wx.getStorageSync('token');
uni.request(_reqObj);
}, function(error) { //回话过期
// #ifdef H5
//先微信登录
wechatRefreshToken();
// #endif
//小程序登录
// #ifdef MP-WEIXIN
doLogin();
// #endif
// #ifdef APP-PLUS
//查询临时钥匙
let _key = wx.getStorageSync(mapping.OWNER_KEY);
if (_key) {
doLoginOwnerByKey(_key);
} else {
uni.navigateTo({
url: '/pages/showlogin/showlogin'
});
return;
}
// #endif
});
}
/**
*
* 不用鉴权的 HTTP 请求
* @param {Object} _reqObj 请求参数
*/
export function requestNoAuth(_reqObj) {
debug('java110Request','requestNoAuth',_reqObj);
//这里判断只有在 post 方式时 放加载框
if (_reqObj.hasOwnProperty("method") && "POST" == _reqObj.method) {
uni.showLoading({
title: '加载中',
mask: true
});
_reqObj.complete = function() {
uni.hideLoading();
}
}
let _headers = getHeaders();
if (_reqObj.hasOwnProperty('header')) {
let _header = _reqObj.header;
for (let key in _headers) {
_header.key = _headers[key];
}
} else {
_reqObj.header = _headers;
}
_reqObj.header.cookie = '_java110_token_=' + wx.getStorageSync('token');
uni.request(_reqObj);
}