Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
S
starlight
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王雪伟
starlight
Commits
d0f3857e
Commit
d0f3857e
authored
Apr 12, 2022
by
王雪伟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[提交人]:王雪伟
[提交简述] :加入登录工具类 [实现方案] :
parent
9bd644f6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
288 additions
and
0 deletions
+288
-0
FaceBookLoginUtil.kt
cms/src/main/java/com/zxhl/cms/utils/FaceBookLoginUtil.kt
+140
-0
GoogleLoginUtil.kt
cms/src/main/java/com/zxhl/cms/utils/GoogleLoginUtil.kt
+148
-0
No files found.
cms/src/main/java/com/zxhl/cms/utils/FaceBookLoginUtil.kt
0 → 100644
View file @
d0f3857e
package
com.zxhl.cms.utils
import
android.app.Activity
import
android.content.Intent
import
android.os.Handler
import
android.util.Log
import
com.facebook.*
import
com.facebook.login.LoginManager
import
com.facebook.login.LoginResult
import
com.zxhl.cms.common.Constant
import
com.zxhl.cms.net.ApiClient
import
com.zxhl.cms.net.RxSchedulers
import
com.zxhl.cms.net.SettingPreference
import
com.zxhl.cms.net.callback.BaseObserver
import
com.zxhl.cms.net.model.other.WxBindEntity
/**
* @author (wangXuewei)
* @datetime 2022-03-26 11:04 GMT+8
* @detail :
*/
class
FaceBookLoginUtil
{
private
val
TAG
=
"FaceBookLoginUtil"
private
var
mActivity
:
Activity
;
private
var
mLoginCallBack
:
LoginCallBack
?
=
null
;
private
var
callbackManager
:
CallbackManager
?
=
null
;
constructor
(
mActivity
:
Activity
)
{
this
.
mActivity
=
mActivity
}
interface
LoginCallBack
{
fun
onLoginSuccess
()
fun
onLoginError
(
errorMsg
:
String
)
}
fun
init
()
{
callbackManager
=
CallbackManager
.
Factory
.
create
()
LoginManager
.
getInstance
().
registerCallback
(
callbackManager
,
object
:
FacebookCallback
<
LoginResult
>
{
override
fun
onSuccess
(
loginResult
:
LoginResult
)
{
//延时500毫秒获取信息,不然获取不到
val
handler
=
Handler
()
handler
.
postDelayed
(
{
val
accessToken
=
loginResult
.
accessToken
val
profile
=
Profile
.
getCurrentProfile
()
if
(
profile
!=
null
)
{
bindFaceBookLogin
(
accessToken
.
userId
,
profile
.
name
,
profile
.
getProfilePictureUri
(
200
,
200
).
toString
()
)
}
else
{
mLoginCallBack
?.
onLoginError
(
"Success_未获取到登录信息"
)
}
},
1000
)
}
override
fun
onCancel
()
{
mLoginCallBack
?.
onLoginError
(
"FaceBookLoginCancel"
)
}
override
fun
onError
(
exception
:
FacebookException
)
{
mLoginCallBack
?.
onLoginError
(
"FaceBookLogin error:"
+
exception
.
message
)
}
})
}
/**
* faceBook登录
*/
fun
faceBookLogin
(
callBack
:
LoginCallBack
)
{
mLoginCallBack
=
callBack
;
val
accessToken
=
AccessToken
.
getCurrentAccessToken
()
val
profile
=
Profile
.
getCurrentProfile
()
if
(
accessToken
!=
null
&&
!
accessToken
.
isExpired
&&
profile
!=
null
)
{
bindFaceBookLogin
(
accessToken
.
userId
,
profile
.
name
,
profile
.
getProfilePictureUri
(
200
,
200
).
toString
()
)
}
else
{
LoginManager
.
getInstance
()
.
logInWithReadPermissions
(
mActivity
,
listOf
(
"public_profile"
));
}
}
/**
* Activity 重写
* */
fun
onActivityResult
(
requestCode
:
Int
,
resultCode
:
Int
,
data
:
Intent
?)
{
callbackManager
?.
onActivityResult
(
requestCode
,
resultCode
,
data
)
}
/**
* faceBook 绑定后端服务器
* */
private
fun
bindFaceBookLogin
(
userId
:
String
?,
userName
:
String
?,
picture
:
String
?)
{
ApiClient
.
homeApi
.
authFaceBookLogin
(
userId
,
userName
,
picture
)
.
compose
(
RxSchedulers
.
observableIO2Main
())
.
subscribe
(
object
:
BaseObserver
<
WxBindEntity
>()
{
override
fun
onSuccess
(
result
:
WxBindEntity
?)
{
SettingPreference
.
saveToken
(
result
?.
token
)
UserDataUtils
.
updateUserInfo
(
null
)
Constant
.
Switch
.
isLogin
=
true
mLoginCallBack
?.
onLoginSuccess
()
}
override
fun
onFailure
(
e
:
Throwable
?,
code
:
String
?,
errorMsg
:
String
?)
{
// callBack.loginSuccess(lottery)
mLoginCallBack
?.
onLoginError
(
"net code:$code msg:$errorMsg"
)
}
})
}
/**
* 退出登录
*/
fun
signOut
()
{
}
/**
* 断开链接
*/
fun
revokeAccess
()
{
}
}
\ No newline at end of file
cms/src/main/java/com/zxhl/cms/utils/GoogleLoginUtil.kt
0 → 100644
View file @
d0f3857e
package
com.zxhl.cms.utils
import
android.app.Activity
import
android.content.Intent
import
android.util.Log
import
com.google.android.gms.auth.api.signin.GoogleSignIn
import
com.google.android.gms.auth.api.signin.GoogleSignInAccount
import
com.google.android.gms.auth.api.signin.GoogleSignInClient
import
com.google.android.gms.auth.api.signin.GoogleSignInOptions
import
com.google.android.gms.common.api.ApiException
import
com.google.android.gms.tasks.OnCompleteListener
import
com.google.android.gms.tasks.Task
import
com.zxhl.cms.common.Constant
import
com.zxhl.cms.net.ApiClient
import
com.zxhl.cms.net.RxSchedulers
import
com.zxhl.cms.net.SettingPreference
import
com.zxhl.cms.net.callback.BaseObserver
import
com.zxhl.cms.net.model.other.WxBindEntity
/**
* @author (wangXuewei)
* @datetime 2022-03-25 14:04 GMT+8
* @detail :
*/
class
GoogleLoginUtil
{
private
val
TAG
=
"GoogleLoginUtil"
private
val
RC_SIGN_IN
=
9009
private
val
googleClientId
=
"535211515800-fcblld827b8ojq7dccuohmeo199205ce.apps.googleusercontent.com"
private
var
mActivity
:
Activity
;
private
var
mGoogleSignInClient
:
GoogleSignInClient
?
=
null
;
private
var
mLoginCallBack
:
LoginCallBack
?
=
null
;
constructor
(
mActivity
:
Activity
)
{
this
.
mActivity
=
mActivity
}
interface
LoginCallBack
{
fun
onLoginSuccess
()
fun
onLoginError
(
errorMsg
:
String
)
}
fun
init
()
{
val
gso
=
GoogleSignInOptions
.
Builder
(
GoogleSignInOptions
.
DEFAULT_SIGN_IN
)
.
requestEmail
()
.
requestIdToken
(
googleClientId
)
.
requestId
()
.
requestProfile
()
.
build
()
mGoogleSignInClient
=
GoogleSignIn
.
getClient
(
mActivity
,
gso
);
}
/**
* google登录
*/
fun
googleLogin
(
callBack
:
LoginCallBack
)
{
mLoginCallBack
=
callBack
;
val
account
=
GoogleSignIn
.
getLastSignedInAccount
(
mActivity
)
if
(
account
!=
null
)
{
bindGoogle
(
account
.
id
,
account
.
displayName
,
account
.
photoUrl
.
toString
())
}
else
{
mActivity
.
runOnUiThread
{
val
signInIntent
=
mGoogleSignInClient
?.
signInIntent
mActivity
.
startActivityForResult
(
signInIntent
,
RC_SIGN_IN
)
}
}
}
/**
* Activity 重写
* */
fun
onActivityResult
(
requestCode
:
Int
,
resultCode
:
Int
,
data
:
Intent
?)
{
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if
(
requestCode
===
RC_SIGN_IN
)
{
// The Task returned from this call is always completed, no need to attach
// a listener.
val
task
:
Task
<
GoogleSignInAccount
>
=
GoogleSignIn
.
getSignedInAccountFromIntent
(
data
)
handleSignInResult
(
task
)
}
}
private
fun
handleSignInResult
(
completedTask
:
Task
<
GoogleSignInAccount
>)
{
try
{
// Signed in successfully, show authenticated UI.
val
account
=
completedTask
.
getResult
(
ApiException
::
class
.
java
)
if
(
account
!=
null
)
{
bindGoogle
(
account
.
id
,
account
.
displayName
,
account
.
photoUrl
.
toString
())
}
else
{
mLoginCallBack
?.
onLoginError
(
"code:-2 msg:account is null"
)
}
}
catch
(
e
:
ApiException
)
{
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
mLoginCallBack
?.
onLoginError
(
"code:${e.statusCode} msg:${e.message}"
)
}
}
/**
* 绑定后端服务器
* */
private
fun
bindGoogle
(
userId
:
String
?,
userName
:
String
?,
picture
:
String
?)
{
ApiClient
.
homeApi
.
authGoogleLogin
(
userId
,
userName
,
picture
)
.
compose
(
RxSchedulers
.
observableIO2Main
())
.
subscribe
(
object
:
BaseObserver
<
WxBindEntity
>()
{
override
fun
onSuccess
(
result
:
WxBindEntity
?)
{
SettingPreference
.
saveToken
(
result
?.
token
)
UserDataUtils
.
updateUserInfo
(
null
)
Constant
.
Switch
.
isLogin
=
true
mLoginCallBack
?.
onLoginSuccess
()
}
override
fun
onFailure
(
e
:
Throwable
?,
code
:
String
?,
errorMsg
:
String
?)
{
// callBack.loginSuccess(lottery)
mLoginCallBack
?.
onLoginError
(
"net code:$code msg:$errorMsg"
)
}
})
}
/**
* 退出登录
*/
fun
signOut
()
{
mGoogleSignInClient
?.
signOut
()
?.
addOnCompleteListener
(
mActivity
)
{
// ...
//it.result
}
}
/**
* 断开链接
*/
fun
revokeAccess
()
{
mGoogleSignInClient
?.
revokeAccess
()
?.
addOnCompleteListener
(
mActivity
)
{
// ...
}
}
}
\ No newline at end of file
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