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
127733f3
Commit
127733f3
authored
Sep 24, 2021
by
jianghaiming
Browse files
Options
Browse Files
Download
Plain Diff
update:修改登陆
parents
b00b5b3f
8f0dc1ea
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
210 additions
and
63 deletions
+210
-63
UserException.php
application/exception/custom/UserException.php
+1
-0
CommonService.php
application/services/common/CommonService.php
+15
-0
UserService.php
application/services/user/UserService.php
+190
-63
application.ini
conf/application.ini
+4
-0
No files found.
application/exception/custom/UserException.php
View file @
127733f3
...
...
@@ -25,5 +25,6 @@ class UserException extends BaseException
11
=>
'头像昵称更新失败'
,
12
=>
'绑定手机号保存失败'
,
13
=>
'请求太频繁,稍后重试'
,
14
=>
'手机号格式错误'
,
];
}
\ No newline at end of file
application/services/common/CommonService.php
View file @
127733f3
...
...
@@ -33,6 +33,21 @@ class CommonService
}
return
[];
}
public
static
function
activateDeliverer
(
$params
)
{
$url
=
config
(
'interface'
,
'order.deliverer.activate_deliverer'
);
if
(
empty
(
$url
)){
throw
new
InterfaceException
([
'cus'
=>
0
]);
}
$res
=
(
new
Request
())
->
post
(
$url
,
$params
);
if
(
$res
[
'code'
]
==
0
&&
isset
(
$res
[
'response'
]))
{
return
!
empty
(
$res
[
'response'
][
'result'
])
?
$res
[
'response'
][
'result'
]
:
[];
}
return
[];
}
/**
* 通过生活号id获取生活号信息
...
...
application/services/user/UserService.php
View file @
127733f3
...
...
@@ -8,9 +8,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
;
...
...
@@ -19,64 +18,44 @@ 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
self
::
$code
=
!
empty
(
$params
[
'code'
])
?
$params
[
'code'
]
:
''
;
//小程序授权code
$openid
=
!
empty
(
$params
[
'openid'
])
?
$params
[
'openid'
]
:
''
;
//小程序授权code
$wechatFrom
=
!
empty
(
$params
[
'wechat_from'
])
?
$params
[
'wechat_from'
]
:
1
;
//登陆来源
$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'
);
if
(
empty
(
self
::
$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
;
$lock
=
self
::
getWechatLoginLock
(
$params
);
if
(
!
empty
(
$lock
)){
return
$lock
;
}
//需要授权微信
//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
,
];
$response
=
(
new
Request
())
->
post
(
$loginUrl
,
$params
);
if
(
$response
[
'code'
]
==
0
)
{
$response
=
$response
[
'response'
];
}
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
]);
}
//检查系统是已注册
$openid
=
!
empty
(
$response
[
'openid'
])
?
$response
[
'openid'
]
:
''
;
//}
//设置小程序配置
self
::
setWechatConfig
(
$wechatFrom
);
//获取小程序用户信息
$response
=
self
::
getJscode2session
();
//检查系统是已注册
$openid
=
!
empty
(
$response
[
'openid'
])
?
$response
[
'openid'
]
:
''
;
$user
=
UserWechatBind
::
getRecordMaster
([
'openid'
=>
$openid
]);
//如果系统不存在,用户信息,则注册新用户
...
...
@@ -91,7 +70,6 @@ class UserService
];
$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
)
{
...
...
@@ -119,8 +97,80 @@ class UserService
$newWechatId
=
UserWechatBind
::
save
(
$insert
,[
'openid'
=>
$openid
]);
}
$user
[
'third_session'
]
=
self
::
generate3rdSession
(
$openid
);
$redis
->
set
(
$keyCode
,
json_encode
(
$user
),
120
);
return
$user
;
self
::
$redis
->
set
(
self
::
$keyCode
,
json_encode
(
$user
),
120
);
return
$user
;
}
/**
* 初始化微信配置
* @param $wechatFrom
*/
public
static
function
setWechatConfig
(
$wechatFrom
)
{
//需要授权微信
if
(
$wechatFrom
==
2
)
{
self
::
$appid
=
\Yaf\Registry
::
get
(
'config'
)
->
deliverer
->
wechat
->
appid
;
self
::
$secret
=
\Yaf\Registry
::
get
(
'config'
)
->
deliverer
->
wechat
->
secret
;
}
else
{
self
::
$appid
=
\Yaf\Registry
::
get
(
'config'
)
->
wechat
->
appid
;
self
::
$secret
=
\Yaf\Registry
::
get
(
'config'
)
->
wechat
->
secret
;
}
}
/**
* 重复提交
* @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
;
}
/**
...
...
@@ -131,9 +181,83 @@ 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'
]
:
''
;
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'
]]);
}
$userInfo
=
UserWechatBind
::
getRecordMaster
([
'openid'
=>
self
::
$openid
]);
$userInfo
=
array_merge
(
$userInfo
,
self
::
saveWechatPhone
(
$mobile
,
$userInfo
))
;
CommonService
::
activateDeliverer
([
'user_id'
=>
$userInfo
[
'user_id'
],
'phone'
=>
$userInfo
[
'phone'
]]);
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
;
...
...
@@ -148,7 +272,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
)
{
...
...
@@ -181,7 +305,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'
]))
{
...
...
@@ -201,14 +329,9 @@ 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
;
$userInfo
[
'is_pop_up'
]
=
1
;
}
else
{
$userInfo
[
'is_pop_up'
]
=
2
;
}
...
...
@@ -219,7 +342,11 @@ class UserService
return
$userInfo
;
}
/**
* 绑定头像
* @param $params
* @return \Api\PhpUtils\Mysql\MysqlBase
*/
public
static
function
userWechatBind
(
$params
)
{
...
...
conf/application.ini
View file @
127733f3
...
...
@@ -22,6 +22,10 @@ wechat.appid = "wx4e0d92499185fb74";
wechat.secret = "731bcf2f7ebb1ebddb677618c2008b25";
deliverer.wechat.appid
=
"wx91042f2a75f8b994"
;
deliverer.wechat.secret = "09d7ea5472d871b4b1a0f3100bdfe1c9";
[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