Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
5
51business
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
方斌
51business
Commits
9720531a
Commit
9720531a
authored
Nov 19, 2019
by
李尚科
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix user_sid
parent
9066950e
Pipeline
#16686
passed with stage
in 13 seconds
Changes
1
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
64 deletions
+67
-64
helper.js
app/extend/helper.js
+67
-64
No files found.
app/extend/helper.js
View file @
9720531a
...
...
@@ -7,19 +7,19 @@ module.exports = {
// 获取 Token
get_jwt
()
{
const
{
ctx
}
=
this
;
le
t
bearerToken
=
ctx
.
request
.
header
.
authorization
;
cons
t
bearerToken
=
ctx
.
request
.
header
.
authorization
;
if
(
!
bearerToken
)
{
ctx
.
failed
(
'error auth'
);
}
return
bearerToken
&&
bearerToken
.
replace
(
"Bearer "
,
""
);
return
bearerToken
&&
bearerToken
.
replace
(
'Bearer '
,
''
);
},
// 校验 Token
async
verify_token
(
ctx
)
{
le
t
token
=
this
.
get_jwt
(
ctx
);
le
t
decode_res
=
await
ctx
.
service
.
jwt
.
decode_token
(
token
);
ctx
.
logger
.
info
(
'decode_res'
,
decode_res
)
le
t
token_black
=
await
this
.
app
.
memcache
.
get
(
'auth_token_'
+
decode_res
.
data
.
user_id
);
cons
t
token
=
this
.
get_jwt
(
ctx
);
cons
t
decode_res
=
await
ctx
.
service
.
jwt
.
decode_token
(
token
);
ctx
.
logger
.
info
(
'decode_res'
,
decode_res
)
;
cons
t
token_black
=
await
this
.
app
.
memcache
.
get
(
'auth_token_'
+
decode_res
.
data
.
user_id
);
ctx
.
logger
.
info
(
'token_black'
,
token_black
,
token
);
if
(
token_black
==
token
)
{
ctx
.
failed
(
'token 已失效'
);
...
...
@@ -40,11 +40,11 @@ module.exports = {
return
ret
.
digest
(
'hex'
);
},
//发送请求 注意params和options都为对象
//
发送请求 注意params和options都为对象
async
send_request
(
url
,
params
,
input_options
)
{
const
{
ctx
}
=
this
;
/***
/**
*
example_options = {
//是否开启请求各阶段的时间测量
timing: false,
...
...
@@ -67,7 +67,7 @@ module.exports = {
}
***/
le
t
default_options
=
{
cons
t
default_options
=
{
timeout
:
3000
,
timing
:
true
,
method
:
'POST'
,
...
...
@@ -75,12 +75,12 @@ module.exports = {
dataType
:
'json'
,
};
le
t
options
=
input_options
?
Object
.
assign
({},
default_options
,
input_options
)
:
default_options
;
cons
t
options
=
input_options
?
Object
.
assign
({},
default_options
,
input_options
)
:
default_options
;
options
.
data
=
params
;
//测试接口:个税获取话题列表
//var url = 'https://b.jianbing.com/app/geshui/social/get_topics';
var
resp
=
await
ctx
.
curl
(
url
,
options
);
//
测试接口:个税获取话题列表
//
var url = 'https://b.jianbing.com/app/geshui/social/get_topics';
const
resp
=
await
ctx
.
curl
(
url
,
options
);
return
resp
;
},
...
...
@@ -119,17 +119,17 @@ module.exports = {
},
aes256_cbc_decrypt_weixin
(
key
,
crypted
)
{
le
t
aesKey
=
Buffer
.
from
(
key
+
'='
,
'base64'
);
cons
t
aesKey
=
Buffer
.
from
(
key
+
'='
,
'base64'
);
const
cipherEncoding
=
'base64'
;
const
clearEncoding
=
'utf8'
;
const
cipher
=
crypto
.
createDecipheriv
(
'aes-256-cbc'
,
aesKey
,
aesKey
.
slice
(
0
,
16
));
cipher
.
setAutoPadding
(
false
);
// 是否取消自动填充 不取消
le
t
decoded
=
cipher
.
update
(
crypted
,
cipherEncoding
,
clearEncoding
)
+
cipher
.
final
(
clearEncoding
);
cons
t
decoded
=
cipher
.
update
(
crypted
,
cipherEncoding
,
clearEncoding
)
+
cipher
.
final
(
clearEncoding
);
return
{
noncestr
:
decoded
.
substring
(
0
,
16
),
msg_len
:
decoded
.
substring
(
16
,
20
),
msg
:
decoded
.
substring
(
20
,
decoded
.
lastIndexOf
(
"}"
)
+
1
)
}
msg
:
decoded
.
substring
(
20
,
decoded
.
lastIndexOf
(
'}'
)
+
1
),
}
;
},
toInt
(
str
)
{
...
...
@@ -139,19 +139,19 @@ module.exports = {
},
//校验经纬度 部分存入的gps地址经纬度混乱,输出使用时进行校验修复
//longitude 经线 latitude 纬度 纬度从南到北,范围为-90 - 90
//示例:北纬N29°57′28.20″ 东经E119°42′32.30″ gps:29.9578340000,119.7089730000
//
校验经纬度 部分存入的gps地址经纬度混乱,输出使用时进行校验修复
//
longitude 经线 latitude 纬度 纬度从南到北,范围为-90 - 90
//
示例:北纬N29°57′28.20″ 东经E119°42′32.30″ gps:29.9578340000,119.7089730000
checkGps
(
gps
)
{
if
(
!
gps
||
gps
.
indexOf
(
','
)
===
-
1
)
{
return
''
;
}
le
t
gps_arr
=
gps
.
split
(
','
);
cons
t
gps_arr
=
gps
.
split
(
','
);
if
(
Math
.
abs
(
gps_arr
[
0
])
>=
90
)
{
gps_arr
.
reverse
();
gps
=
gps_arr
.
join
(
','
);
}
;
}
return
gps
;
},
...
...
@@ -162,20 +162,20 @@ module.exports = {
if
(
!
gps
||
gps
.
indexOf
(
','
)
===
-
1
)
{
return
''
;
}
le
t
gps_arr
=
gps
.
split
(
','
);
cons
t
gps_arr
=
gps
.
split
(
','
);
if
(
Math
.
abs
(
gps_arr
[
0
])
>=
90
)
{
gps_arr
.
reverse
();
gps
=
gps_arr
.
join
(
','
);
}
;
}
le
t
data
=
{
cons
t
data
=
{
ak
:
'3TBenWOhPygtFFazaR5kSibU'
,
pois
:
0
,
output
:
'json'
,
location
:
gps
,
};
const
{
ctx
}
=
this
;
const
resp
=
await
ctx
.
curl
(
'http://api.map.baidu.com/geocoder/v2/'
,
{
timeout
:
3000
,
dataType
:
'json'
,
method
:
'GET'
,
data
:
data
});
const
resp
=
await
ctx
.
curl
(
'http://api.map.baidu.com/geocoder/v2/'
,
{
timeout
:
3000
,
dataType
:
'json'
,
method
:
'GET'
,
data
});
const
ret
=
resp
.
data
;
if
(
ret
.
status
!=
0
)
{
...
...
@@ -186,8 +186,8 @@ module.exports = {
async
getGPS
(
address
,
city
)
{
console
.
info
(
address
);
const
baidu_url
=
`https://api.map.baidu.com/geocoder/v2/`
;
const
result
=
await
this
.
send_request
(
baidu_url
,
{
address
:
address
,
output
:
'json'
,
ak
:
'3TBenWOhPygtFFazaR5kSibU'
,
city
:
city
},
{
method
:
'GET'
});
const
baidu_url
=
'https://api.map.baidu.com/geocoder/v2/'
;
const
result
=
await
this
.
send_request
(
baidu_url
,
{
address
,
output
:
'json'
,
ak
:
'3TBenWOhPygtFFazaR5kSibU'
,
city
},
{
method
:
'GET'
});
console
.
info
(
result
);
const
ret
=
result
.
data
;
if
(
ret
&&
ret
.
status
===
0
&&
ret
.
result
&&
ret
.
result
.
location
)
{
...
...
@@ -196,23 +196,23 @@ module.exports = {
return
{
lng
:
0
,
lat
:
0
};
},
//根据IP地址来处理业务的展示与否
//
根据IP地址来处理业务的展示与否
async
parseIp
()
{
le
t
ip
=
this
.
getClientIP
();
cons
t
ip
=
this
.
getClientIP
();
//
//ip = '122.224.130.226';
//
ip = '122.224.130.226';
if
(
!
ip
)
{
return
''
;
}
le
t
data
=
{
cons
t
data
=
{
ak
:
'3TBenWOhPygtFFazaR5kSibU'
,
output
:
'json'
,
ip
,
};
const
{
ctx
}
=
this
;
const
resp
=
await
ctx
.
curl
(
'https://api.map.baidu.com/location/ip'
,
{
timeout
:
3000
,
dataType
:
'json'
,
method
:
'GET'
,
data
:
data
});
const
resp
=
await
ctx
.
curl
(
'https://api.map.baidu.com/location/ip'
,
{
timeout
:
3000
,
dataType
:
'json'
,
method
:
'GET'
,
data
});
const
ret
=
resp
.
data
;
if
(
ret
.
status
!=
0
)
{
...
...
@@ -231,12 +231,12 @@ module.exports = {
return
true
;
},
randomsort
(
a
,
b
)
{
//
数组随机排序
return
Math
.
random
()
>
.
5
?
-
1
:
1
;
//
通过随机产生0到1的数,然后判断是否大于0.5从而影响排序,产生随机性的效果。
randomsort
(
a
,
b
)
{
//
数组随机排序
return
Math
.
random
()
>
0.5
?
-
1
:
1
;
//
通过随机产生0到1的数,然后判断是否大于0.5从而影响排序,产生随机性的效果。
},
debug
(
data
,
mark
=
''
)
{
var
str
=
''
;
let
str
=
''
;
for
(
let
i
=
0
;
i
<=
60
;
i
++
)
{
str
+=
mark
;
}
...
...
@@ -254,40 +254,41 @@ module.exports = {
},
unique
(
arr
)
{
var
res
=
arr
.
filter
(
function
(
item
,
index
,
array
)
{
const
res
=
arr
.
filter
(
function
(
item
,
index
,
array
)
{
return
array
.
indexOf
(
item
)
===
index
;
});
return
res
;
},
//校验身份证
//
校验身份证
verify_id_card
(
id_card
)
{
const
id_card_reg
=
/
(
^
\d{15}
$
)
|
(
^
\d{18}
$
)
|
(
^
\d{17}(\d
|X|x
)
$
)
/
;
if
(
id_card_reg
.
test
(
id_card
)
===
false
)
{
//
身份证号码校验
if
(
id_card_reg
.
test
(
id_card
)
===
false
)
{
//
身份证号码校验
return
false
;
}
/*1、从第一位到第十七位的系数分别为:
7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2
/*
1、从第一位到第十七位的系数分别为:
7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2
将这17位数字和系数相乘的结果相加。 */
const
arr
=
[
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
];
const
arr
=
[
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
];
let
sum
=
0
;
for
(
let
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
sum
+=
parseInt
(
id_card
.
charAt
(
i
))
*
arr
[
i
];
}
//2、用加出来和除以11,看余数,
//
2、用加出来和除以11,看余数,
const
c
=
sum
%
11
;
//3、分别对应的最后一位身份证的号码为:1-0-X-9-8-7-6-5-4-3-2
const
ch
=
[
'1'
,
'0'
,
'X'
,
'9'
,
'8'
,
'7'
,
'6'
,
'5'
,
'4'
,
'3'
,
'2'
];
//
3、分别对应的最后一位身份证的号码为:1-0-X-9-8-7-6-5-4-3-2
const
ch
=
[
'1'
,
'0'
,
'X'
,
'9'
,
'8'
,
'7'
,
'6'
,
'5'
,
'4'
,
'3'
,
'2'
];
const
code
=
ch
[
c
];
let
last
=
id_card
.
charAt
(
17
);
last
=
last
==
'x'
?
'X'
:
last
;
return
last
==
code
;
},
//随机化原数组
//
随机化原数组
shuffle
(
array
)
{
var
m
=
array
.
length
,
t
,
i
;
let
m
=
array
.
length
,
t
,
i
;
// 如果还剩有元素…
while
(
m
)
{
// 随机选取一个元素…
...
...
@@ -304,7 +305,7 @@ module.exports = {
getClientIP
()
{
const
{
ctx
}
=
this
;
const
ips
=
ctx
.
request
.
header
[
'x-forwarded-for'
];
const
ipList
=
ips
?
ips
.
split
(
','
)
:
[
''
];
const
ipList
=
ips
?
ips
.
split
(
','
)
:
[
''
];
const
ip
=
ipList
[
0
];
return
ip
;
},
...
...
@@ -334,9 +335,9 @@ module.exports = {
}
},
/**判断是否是手机号**/
/**
判断是否是手机号**/
isPhoneNumber
(
tel
)
{
var
reg
=
/^0
?
1
[
3|4|5|6|7|8|9
][
0-9
]\d{8}
$/
;
const
reg
=
/^0
?
1
[
3|4|5|6|7|8|9
][
0-9
]\d{8}
$/
;
return
reg
.
test
(
tel
);
},
...
...
@@ -344,7 +345,7 @@ module.exports = {
verify_real_name
(
real_name
)
{
const
real_name_reg
=
/^
[\u
4E00-
\u
9FA5
]{1,10}
$/
;
if
(
!
real_name_reg
.
test
(
real_name
))
{
//
姓名校验
if
(
!
real_name_reg
.
test
(
real_name
))
{
//
姓名校验
return
0
;
}
if
(
real_name
.
length
<
2
)
{
...
...
@@ -353,7 +354,7 @@ module.exports = {
return
1
;
},
//固定位数前补充 0 操作
//
固定位数前补充 0 操作
PrefixInteger
(
num
,
length
)
{
return
(
Array
(
length
).
join
(
'0'
)
+
num
).
slice
(
-
length
);
},
...
...
@@ -371,7 +372,7 @@ module.exports = {
});
});
let
ret_json
=
{};
await
ret_p
.
then
((
resolve
,
reject
)
=>
{
ret_json
=
resolve
});
await
ret_p
.
then
((
resolve
,
reject
)
=>
{
ret_json
=
resolve
;
});
return
ret_json
;
},
...
...
@@ -381,7 +382,7 @@ module.exports = {
return
xml_params
;
},
//获取手机号码归属地
//
获取手机号码归属地
async
getPhoneCity
(
phone
)
{
const
{
ctx
}
=
this
;
...
...
@@ -408,16 +409,16 @@ module.exports = {
return
location
;
},
//延时函数
//
延时函数
async
sleep
(
time
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
setTimeout
(
function
()
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
setTimeout
(
function
()
{
resolve
(
'ok'
);
},
time
);
});
},
//格式化地址
//
格式化地址
formatUrl
(
url
,
baseUrl
)
{
if
(
!
baseUrl
)
return
url
;
if
(
url
.
indexOf
(
'http'
)
===
0
)
return
url
;
...
...
@@ -436,7 +437,9 @@ module.exports = {
decodeUserSid
(
code
)
{
if
(
!
code
)
return
code
;
const
rand
=
'OU1WjLvZCrRJ7Yo0gE2XDjuuaSAUuaH1bhHPuMymcdfEeKz0igRhXQkMuLTm1'
;
code
=
code
.
substr
(
5
);
const
begin
=
code
.
slice
(
0
,
1
);
let
rtn
=
''
;
const
codelen
=
rand
.
slice
(
0
,
11
);
...
...
@@ -469,20 +472,20 @@ module.exports = {
let
temp
=
String
(
Math
.
floor
(
code
/
randKey
)).
replace
(
'.'
,
''
);
temp
=
temp
.
slice
(
-
ext
);
le
t
arrExtTemp
=
codeExt
.
split
(
''
);
le
t
arrExt
=
temp
.
split
(
''
);
cons
t
arrExtTemp
=
codeExt
.
split
(
''
);
cons
t
arrExt
=
temp
.
split
(
''
);
for
(
const
v
of
arrExt
)
{
rtn
+=
arrExtTemp
[
v
];
}
le
t
arrNumsTemp
=
codeNums
.
split
(
''
);
le
t
arrNums
=
String
(
code
).
split
(
''
);
cons
t
arrNumsTemp
=
codeNums
.
split
(
''
);
cons
t
arrNums
=
String
(
code
).
split
(
''
);
for
(
const
v
of
arrNums
)
{
rtn
+=
arrNumsTemp
[
v
];
}
rtn
=
begin
+
rtn
;
return
rtn
;
}
}
,
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment