public
function
index(Request
$request
){
$url
=
"https://api.weixin.qq.com/sns/jscode2session"
;
$params
[
'appid'
]=
'小程序的appid'
;
$params
[
'secret'
]=
'小程序的AppSecret'
;
$params
[
'js_code'
]=
$request
-> param(
'code'
);
$params
[
'grant_type'
]=
'authorization_code'
;
$user_phone
=
$request
-> param(
'user_phone'
);
$arr
= httpCurl(
$url
,
$params
,
'GET'
);
$arr
= json_decode(
$arr
,true);
if
(isset(
$arr
[
'errcode'
]) && !
empty
(
$arr
[
'errcode'
])){
return
json([
'code'
=>
'2'
,
'message'
=>
$arr
[
'errmsg'
],
"result"
=>null]);
}
$openid
=
$arr
[
'openid'
];
$session_key
=
$arr
[
'session_key'
];
$is_openid
= Db::table(
'user_info'
)->where(
'openid'
,
$openid
)->find();
if
(
$is_openid
){
$data
= Db::table(
'user_info'
)->where(
'openid'
,
$openid
)->find();
$time
= time() -
$data
[
'openid_time'
];
$time
=
$time
/ 3600;
if
(
$time
> 4){
return
json([
'sendsure'
=>
'0'
,
'message'
=>
'登录失败'
,]);
}
else
{
$update
= Db::table(
'user_info'
)->where(
'openid'
,
$openid
)->update([
'openid_time'
=> time()]);
if
(
$update
){
return
json([
'sendsure'
=>
'1'
,
'message'
=>
'登录成功'
,
'user_phone'
=>
$data
[
'user_phone'
]]);
}
else
{
return
json([
'sendsure'
=>
'0'
,
'message'
=>
'登录失败'
]);
}
}
}
else
{
if
(isset(
$user_phone
) && !
empty
(
$user_phone
)){
$update
= Db::table(
'user_info'
)
->where(
'user_phone'
,
$user_phone
)
->update([
'openid'
=>
$openid
,
'openid_time'
=> time(),
]);
if
(
$update
){
return
json([
'sendsure'
=>
'1'
,
'message'
=>
'登录成功'
,]);
}
}
else
{
return
json([
'sendsure'
=>
'0'
,
'message'
=>
'读取失败'
,]);
}
}
}
请发表评论