Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
goods
Project
Project
Details
Activity
Releases
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
bp
goods
Commits
f7c2e2f3
Commit
f7c2e2f3
authored
Sep 29, 2021
by
jianghaiming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update:修改登陆配置
parent
f56a963d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
230 additions
and
76 deletions
+230
-76
UserException.php
application/exception/custom/UserException.php
+2
-1
UserService.php
application/services/user/UserService.php
+218
-75
application.ini
conf/application.ini
+10
-0
No files found.
application/exception/custom/UserException.php
View file @
f7c2e2f3
...
...
@@ -9,7 +9,7 @@ use App\Exception\BaseException;
class
UserException
extends
BaseException
{
protected
$base_code
=
Code
::
USUR
;
const
APP_ID_NULL
=
14
;
protected
$cus
=
[
0
=>
'code 无效'
,
1
=>
'获取openid失败'
,
...
...
@@ -25,5 +25,6 @@ class UserException extends BaseException
11
=>
'头像昵称更新失败'
,
12
=>
'绑定手机号保存失败'
,
13
=>
'请求太频繁,稍后重试'
,
self
::
APP_ID_NULL
=>
'AppId 不能为空'
,
];
}
\ No newline at end of file
application/services/user/UserService.php
View file @
f7c2e2f3
...
...
@@ -9,9 +9,8 @@ use Api\PhpUtils\Http\Request;
use
App\Services\user\Weixin\WxBizDataCrypt
;;
use
App\Exception\BaseException
;
use
App\Exception\custom\UserException
;
use
App\Exception\custom\CodeSpecialException
;
use
Api\PhpUtils\Validate\Validate
;
use
Api\PhpServices\Idgen\Idgen
;
use
Api\PhpUtils\Redis\RedisUtil
;
use
Api\PhpServices\JwUser\JwUser
;
use
App\Services\common\CommonService
;
...
...
@@ -20,72 +19,40 @@ use Api\PhpUtils\Lock\FrequencyLockUtil;
class
UserService
{
private
$code
;
private
$grant_type
=
'authorization_code'
;
private
$loginUrl
=
'https://api.weixin.qq.com/sns/jscode2session'
;
private
static
$code
=
''
;
private
static
$grantType
=
'authorization_code'
;
private
static
$loginUrl
=
'https://api.weixin.qq.com/sns/jscode2session'
;
private
static
$appid
=
''
;
private
static
$secret
=
""
;
private
static
$redis
=
""
;
private
static
$keyCode
=
""
;
private
static
$openid
=
""
;
/**
* 微信静默登陆
* @param $params
* @return \Api\PhpUtils\Mysql\MysqlBase|mixed
* @throws UserException
*/
public
static
function
wechatLogin
(
$params
)
{
$code
=
!
empty
(
$params
[
'code'
])
?
$params
[
'code'
]
:
''
;
//小程序授权code
$
openid
=
!
empty
(
$params
[
'openid'
])
?
$params
[
'openid'
]
:
''
;
//小程序授权code
self
::
$code
=
!
empty
(
$params
[
'code'
])
?
$params
[
'code'
]
:
''
;
//小程序授权code
$
appid
=
!
empty
(
$params
[
'appid'
])
?
$params
[
'appid'
]
:
''
;
//登陆来源
$inviteUserId
=
!
empty
(
$params
[
'invite_user_id'
])
?
intval
(
$params
[
'invite_user_id'
])
:
0
;
//邀请用户id
$activityType
=
!
empty
(
$params
[
'activity_type'
])
?
intval
(
$params
[
'activity_type'
])
:
0
;
//活动类型
if
(
empty
(
$code
)
&&
empty
(
$openid
))
{
FileLog
::
error
(
"wechatLogin:获取参数为空"
,
json_encode
(
$params
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
throw
new
UserException
([
'cus'
=>
0
]);
}
$redis
=
RedisUtil
::
getInstance
(
'cache'
);
$key
=
"wx_wechat_login_"
.
$code
;
$keyCode
=
"wx_wechat_login_code_"
.
$code
;
$lock
=
FrequencyLockUtil
::
isLocked
(
$key
,
1
,
120
);
if
(
$lock
)
{
sleep
(
1
);
$user
=
$redis
->
get
(
$keyCode
);
if
(
!
empty
(
$user
))
{
$user
=
json_decode
(
$user
,
true
);
}
FileLog
::
error
(
"wechatLogin: lock"
,
json_encode
(
$user
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
FileLog
::
error
(
"wechatLogin: lock--params"
,
json_encode
(
$params
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
return
$user
;
}
//需要授权微信
//if (empty($openid)) {
$appid
=
\Yaf\Registry
::
get
(
'config'
)
->
wechat
->
appid
;
$secret
=
\Yaf\Registry
::
get
(
'config'
)
->
wechat
->
secret
;
$grant_type
=
'authorization_code'
;
$loginUrl
=
'https://api.weixin.qq.com/sns/jscode2session'
;
//请求接口,获取用户openid
$params
=
[
'appid'
=>
$appid
,
'secret'
=>
$secret
,
'js_code'
=>
$code
,
'grant_type'
=>
$grant_type
,
];
$begin
=
microtime
(
true
);
$response
=
(
new
Request
())
->
post
(
$loginUrl
,
$params
);
$end
=
microtime
(
true
);
$totalTime
=
round
((
$end
-
$begin
),
4
)
*
1000
;
if
(
$response
[
'code'
]
==
0
)
{
$response
=
$response
[
'response'
];
//验证登陆参数
self
::
checkWechatLoginParams
(
$params
);
$lock
=
self
::
getWechatLoginLock
(
$params
);
if
(
!
empty
(
$lock
)){
return
$lock
;
}
//mon打点
$env
=
\Yaf\Application
::
app
()
->
environ
()
??
"dev"
;
MonUtil
::
proxyMon
(
$loginUrl
,
$response
[
'code'
],
'weixin_'
.
$env
,
$totalTime
);
if
(
empty
(
$response
)
||
array_key_exists
(
'errcode'
,
$response
))
{
//throw new \Exception('获取openid失败:' . $response['errcode']);
FileLog
::
error
(
"wechatLogin:获取openid失败"
,
json_encode
(
$params
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
throw
new
UserException
([
'cus'
=>
1
]);
}
//设置小程序配置
self
::
setWechatConfig
(
$appid
);
//获取小程序用户信息
$response
=
self
::
getJscode2session
();
//检查系统是已注册
$openid
=
!
empty
(
$response
[
'openid'
])
?
$response
[
'openid'
]
:
''
;
//}
$user
=
UserWechatBind
::
getRecordMaster
([
'openid'
=>
$openid
]);
//如果系统不存在,用户信息,则注册新用户
if
(
empty
(
$user
)
&&
!
empty
(
$response
))
{
...
...
@@ -95,11 +62,12 @@ class UserService
'session_key'
=>
!
empty
(
$response
[
'session_key'
])
?
$response
[
'session_key'
]
:
''
,
//默认7200有效期
'invite_user_id'
=>
$inviteUserId
,
'activity_type'
=>
$activityType
,
'create_time'
=>
date
(
"Y-m-d H:i:s"
)
'create_time'
=>
date
(
"Y-m-d H:i:s"
),
'appid'
=>
$appid
,
'wechat_app_id'
=>
self
::
$appid
,
];
$sessionKey
=
!
empty
(
$response
[
'session_key'
])
?
$response
[
'session_key'
]
:
''
;
//$newWechatId = UserWechatBind::save($insert);
$newWechatId
=
UserWechatBind
::
insertDuplicate
(
$insert
,
[
'session_key'
=>
$sessionKey
,
'create_time'
=>
date
(
"Y-m-d H:i:s"
)]);
if
(
!
$newWechatId
)
{
...
...
@@ -128,10 +96,93 @@ class UserService
}
$user
[
'third_session'
]
=
self
::
generate3rdSession
(
$openid
);
$redis
->
set
(
$keyCode
,
json_encode
(
$user
),
120
);
self
::
$redis
->
set
(
self
::
$keyCode
,
json_encode
(
$user
),
120
);
return
$user
;
}
/**
* 验证登陆参数
* @param $params
* @throws UserException
*/
public
static
function
checkWechatLoginParams
(
$params
)
{
if
(
empty
(
self
::
$code
))
{
FileLog
::
error
(
"wechatLogin:获取参数为空"
,
json_encode
(
$params
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
throw
new
UserException
([
'cus'
=>
0
]);
}
if
(
empty
(
$params
[
'appid'
]))
{
throw
new
UserException
([
'cus'
=>
UserException
::
APP_ID_NULL
]);
}
}
/**
* 初始化微信配置
* @param $wechatFrom
*/
public
static
function
setWechatConfig
(
$wechatFrom
)
{
//需要授权微信
self
::
$appid
=
\Yaf\Registry
::
get
(
'config'
)
->
wechat
->
appid
->
$wechatFrom
;
self
::
$secret
=
\Yaf\Registry
::
get
(
'config'
)
->
wechat
->
secret
->
$wechatFrom
;
}
/**
* 重复提交
* @param $code
* @return bool|mixed
*/
public
static
function
getWechatLoginLock
(
$params
){
self
::
$redis
=
RedisUtil
::
getInstance
(
'cache'
);
$key
=
"wx_wechat_login_"
.
self
::
$code
;
self
::
$keyCode
=
"wx_wechat_login_code_"
.
self
::
$code
;
$lock
=
FrequencyLockUtil
::
isLocked
(
$key
,
1
,
120
);
if
(
$lock
)
{
sleep
(
1
);
$user
=
self
::
$redis
->
get
(
self
::
$keyCode
);
if
(
!
empty
(
$user
))
{
$user
=
json_decode
(
$user
,
true
);
}
FileLog
::
error
(
"wechatLogin: lock"
,
json_encode
(
$user
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
FileLog
::
error
(
"wechatLogin: lock--params"
,
json_encode
(
$params
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
return
$user
;
}
return
false
;
}
/**
* 获取微信用户信息 openid
* @param $code
* @return array|mixed
* @throws UserException
*/
public
static
function
getJscode2session
(){
//请求接口,获取用户openid
$params
=
[
'appid'
=>
self
::
$appid
,
'secret'
=>
self
::
$secret
,
'js_code'
=>
self
::
$code
,
'grant_type'
=>
self
::
$grantType
,
];
$begin
=
microtime
(
true
);
$response
=
(
new
Request
())
->
post
(
self
::
$loginUrl
,
$params
);
$end
=
microtime
(
true
);
$totalTime
=
round
((
$end
-
$begin
),
4
)
*
1000
;
if
(
$response
[
'code'
]
==
0
)
{
$response
=
$response
[
'response'
];
}
//mon打点
$env
=
\Yaf\Application
::
app
()
->
environ
()
??
"dev"
;
MonUtil
::
proxyMon
(
self
::
$loginUrl
,
$response
[
'code'
],
'weixin_'
.
$env
,
$totalTime
);
if
(
empty
(
$response
)
||
array_key_exists
(
'errcode'
,
$response
))
{
//throw new \Exception('获取openid失败:' . $response['errcode']);
FileLog
::
error
(
"wechatLogin:获取openid失败"
,
json_encode
(
$params
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
throw
new
UserException
([
'cus'
=>
1
]);
}
return
$response
;
}
/**
* 绑定手机号
* @param $params
...
...
@@ -140,12 +191,93 @@ class UserService
* @throws \App\Exception\custom\CodeSpecialException
* @throws \App\Exception\custom\InterfaceException
*/
public
static
function
bindPhone
(
$params
)
public
static
function
bindPhone
(
$params
){
self
::
$openid
=
!
empty
(
$params
[
'openid'
])
?
$params
[
'openid'
]
:
''
;
$phone
=
!
empty
(
$params
[
'mobile'
])
?
$params
[
'mobile'
]
:
''
;
$wechatFrom
=
!
empty
(
$params
[
'appid'
])
?
$params
[
'appid'
]
:
''
;
self
::
setWechatConfig
(
$wechatFrom
);
if
(
!
empty
(
$phone
)){
$userInfo
=
self
::
delivererBindPhone
(
$params
);
}
else
{
$userInfo
=
self
::
pinBindPhone
(
$params
);
}
return
$userInfo
;
}
/**
* 配送员绑定手机号
* @param $params
* @return \Api\PhpUtils\Mysql\MysqlBase
* @throws UserException
* @throws \App\Exception\custom\CodeSpecialException
* @throws \App\Exception\custom\InterfaceException
*/
public
static
function
delivererBindPhone
(
$params
){
$mobile
=
!
empty
(
$params
[
'mobile'
])
?
$params
[
'mobile'
]
:
''
;
$is_mobile
=
(
new
Validate
)
->
isMobile
(
$mobile
);
if
(
empty
(
$is_mobile
))
{
throw
new
UserException
([
'cus'
=>
14
]);
}
//短信验证是否有效
$check
=
self
::
checkPhoneCode
(
$params
);
if
(
!
empty
(
$check
)
&&
$check
[
'status'
]
==
'failed'
)
{
throw
new
BaseException
([
'code'
=>
$check
[
'code'
],
'msg'
=>
$check
[
'reason'
]]);
}
$jwUser
=
(
new
JwUser
())
->
getUserInfo
([
'mobile'
=>
$mobile
]);
$userId
=
''
;
if
(
!
empty
(
$jwUser
[
'data'
]))
{
$data
=
$jwUser
[
'data'
];
$userId
=
!
empty
(
$data
[
'userId'
])
?
$data
[
'userId'
]
:
''
;
}
$userInfo
=
UserWechatBind
::
getRecordMaster
([
'openid'
=>
self
::
$openid
]);
CommonService
::
activateDeliverer
([
'user_id'
=>
$userId
,
'phone'
=>
$mobile
]);
$userInfo
=
array_merge
(
$userInfo
,
self
::
saveWechatPhone
(
$mobile
,
$userInfo
))
;
return
$userInfo
;
}
/**
* 验证手机验证码
* @param array $params
* @return array
* @throws CodeSpecialException
*/
public
static
function
checkPhoneCode
(
$params
=
[])
{
$url
=
config
(
'interface'
,
'service.user.check_sms_code'
);
if
(
!
$url
){
throw
new
CodeSpecialException
(
"failed"
);
}
$params
=
[
'mobile'
=>
!
empty
(
$params
[
'mobile'
])
?
$params
[
'mobile'
]
:
''
,
'appid'
=>
!
empty
(
$params
[
'appid'
])
?
$params
[
'appid'
]
:
'hubble'
,
'code'
=>
!
empty
(
$params
[
'mobile_code'
])
?
$params
[
'mobile_code'
]
:
''
,
'key'
=>
'f16bdd46292480b4b82c841d90a6ba02'
,
'partner'
=>
'merchant-b'
,
'scene'
=>
'login'
,
];
$sms
=
(
new
Request
())
->
get
(
$url
,
$params
);
if
(
!
$sms
){
throw
new
CodeSpecialException
(
"timeout"
);
}
if
(
!
empty
(
$sms
)
&&
$sms
[
'code'
]
==
0
)
{
return
$sms
;
}
else
{
return
$sms
;
}
}
/**
* 拼一拼绑定手机号
* @param $params
* @return \Api\PhpUtils\Mysql\MysqlBase
* @throws UserException
* @throws \App\Exception\custom\CodeSpecialException
* @throws \App\Exception\custom\InterfaceException
*/
public
static
function
pinBindPhone
(
$params
)
{
$openid
=
!
empty
(
$params
[
'openid'
])
?
$params
[
'openid'
]
:
''
;
$encryptedData
=
!
empty
(
$params
[
'encryptedData'
])
?
$params
[
'encryptedData'
]
:
''
;
$iv
=
!
empty
(
$params
[
'iv'
])
?
$params
[
'iv'
]
:
''
;
$appid
=
\Yaf\Registry
::
get
(
'config'
)
->
wechat
->
appid
;
if
(
!
$encryptedData
)
{
FileLog
::
error
(
"bindPhone:encryptedData 为空"
,
json_encode
(
$params
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
...
...
@@ -157,7 +289,7 @@ class UserService
throw
new
UserException
([
'cus'
=>
4
]);
}
$userInfo
=
UserWechatBind
::
getRecordMaster
([
'openid'
=>
$openid
]);
$userInfo
=
UserWechatBind
::
getRecordMaster
([
'openid'
=>
self
::
$openid
]);
$sessionKey
=
!
empty
(
$userInfo
[
'session_key'
])
?
$userInfo
[
'session_key'
]
:
''
;
$openid
=
isset
(
$userInfo
[
'openid'
])
&&
$userInfo
[
'openid'
]
?
$userInfo
[
'openid'
]
:
''
;
if
(
empty
(
$userInfo
)
||
!
$sessionKey
||
!
$openid
)
{
...
...
@@ -167,7 +299,7 @@ class UserService
//解密
$decryptData
=
[];
$wXBizDataCrypt
=
new
WxBizDataCrypt
(
$appid
,
$sessionKey
);
$wXBizDataCrypt
=
new
WxBizDataCrypt
(
self
::
$appid
,
$sessionKey
);
$errCode
=
$wXBizDataCrypt
->
decryptData
(
$encryptedData
,
$iv
,
$decryptData
);
if
(
$errCode
)
{
...
...
@@ -190,7 +322,11 @@ class UserService
FileLog
::
error
(
"bindPhone:该手机号已绑定其他用户不能重复绑定--user"
,
json_encode
(
$user
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
throw
new
UserException
([
'cus'
=>
9
]);
}
$userInfo
=
array_merge
(
$userInfo
,
self
::
saveWechatPhone
(
$phoneNumber
,
$userInfo
))
;
return
$userInfo
;
}
public
static
function
saveWechatPhone
(
$phoneNumber
,
$userInfo
){
$jwUser
=
(
new
JwUser
())
->
getUserInfo
([
'mobile'
=>
$phoneNumber
]);
$userId
=
''
;
if
(
!
empty
(
$jwUser
[
'data'
]))
{
...
...
@@ -210,12 +346,7 @@ class UserService
throw
new
UserException
([
'cus'
=>
12
]);
}
if
(
!
$phoneNumber
)
{
FileLog
::
error
(
"bindPhone:该手机号已绑定其他用户不能重复绑定--user"
,
json_encode
(
$user
,
JSON_UNESCAPED_UNICODE
),
''
,
'jianghaiming@yidian-inc.com'
);
throw
new
UserException
([
'cus'
=>
8
]);
}
$isNewUser
=
CommonService
::
isNewUser
([
'openid'
=>
$openid
]);
$isNewUser
=
CommonService
::
isNewUser
([
'openid'
=>
self
::
$openid
]);
if
(
!
empty
(
$isNewUser
)
&&
$isNewUser
[
'is_new_user'
]
==
1
)
{
$userInfo
[
'is_pop_up'
]
=
1
;
}
else
{
...
...
@@ -228,7 +359,11 @@ class UserService
return
$userInfo
;
}
/**
* 绑定头像
* @param $params
* @return \Api\PhpUtils\Mysql\MysqlBase
*/
public
static
function
userWechatBind
(
$params
)
{
...
...
@@ -246,6 +381,14 @@ class UserService
$data
[
'phone'
]
=
$params
[
'phone'
];
}
if
(
!
empty
(
$params
[
'wechat_app_id'
]))
{
$data
[
'wechat_app_id'
]
=
$params
[
'wechat_app_id'
];
}
if
(
!
empty
(
$params
[
'appid'
]))
{
$data
[
'appid'
]
=
$params
[
'appid'
];
}
$userList
=
UserWechatBind
::
getRecords
(
$data
);
return
$userList
;
}
...
...
conf/application.ini
View file @
f7c2e2f3
...
...
@@ -22,6 +22,16 @@ wechat.appid = "wx4e0d92499185fb74";
wechat.secret = "731bcf2f7ebb1ebddb677618c2008b25";
wechat.appid.merchant-pub-pin
=
"wx4e0d92499185fb74"
wechat.secret.merchant-pub-pin
=
"731bcf2f7ebb1ebddb677618c2008b25"
wechat.appid.merchant-pub-deliverer
=
"wx91042f2a75f8b994"
wechat.secret.merchant-pub-deliverer
=
"09d7ea5472d871b4b1a0f3100bdfe1c9"
wechat.appid.merchant-pub-idle
=
"wx4e0d92499185fb74"
wechat.secret.merchant-pub-idle
=
"731bcf2f7ebb1ebddb677618c2008b25"
[exception]
debug
=
false
exception.user.code
=
-1
...
...
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