Skip to content

Commit

Permalink
20180103
Browse files Browse the repository at this point in the history
  • Loading branch information
Tailyou committed Jan 3, 2018
1 parent 645c2cc commit 30169ab
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ object ShareLoginClient {
const val ACTION_LOGIN = "ACTION_LOGIN"
const val ACTION_SHARE = "ACTION_SHARE"

lateinit var sLoginListener: ILoginListener
lateinit var sShareListener: IShareListener
var sLoginListener: ILoginListener? = null
var sShareListener: IShareListener? = null

fun init(slc: ShareLoginConfig) {
if (slc.isDebug) {
Expand Down Expand Up @@ -59,7 +59,7 @@ object ShareLoginClient {
* 第三方登录
* @time 2017/6/6 13:52
*/
fun login(activity: Activity, @LoginPlatform type: String, loginListener: ILoginListener) {
fun login(activity: Activity, @LoginPlatform type: String, loginListener: ILoginListener?) {
ShareLoginClient.sLoginListener = loginListener
when (type) {
WEIBO -> toLogin(activity, SinaHandlerActivity::class.java)
Expand All @@ -86,7 +86,7 @@ object ShareLoginClient {
* 第三方分享
* @time 2017/6/7 9:51
*/
fun share(activity: Activity, @SharePlatform sharePlatform: String, shareContent: ShareContent, shareListener: IShareListener) {
fun share(activity: Activity, @SharePlatform sharePlatform: String, shareContent: ShareContent, shareListener: IShareListener?) {
ShareLoginClient.sShareListener = shareListener
when (sharePlatform) {
WEIBO_TIME_LINE -> toShare(activity, sharePlatform, shareContent, SinaHandlerActivity::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class QQHandlerActivity : Activity() {
super.onCreate(savedInstanceState)
if (intent != null && !intent.action.isNullOrBlank()) {
when (intent.action) {
ShareLoginClient.ACTION_LOGIN -> doLogin(ShareLoginClient.sLoginListener!!)
ShareLoginClient.ACTION_LOGIN -> doLogin(ShareLoginClient.sLoginListener)
ShareLoginClient.ACTION_SHARE -> {
if (intent.hasExtra(ShareLoginClient.SHARE_CONTENT) &&
intent.hasExtra(ShareLoginClient.SHARE_PLATFORM)) {
val shareContent = intent.extras.get(ShareLoginClient.SHARE_CONTENT) as ShareContent
val sharePlatform = intent.extras.getString(ShareLoginClient.SHARE_PLATFORM)
doShare(sharePlatform, shareContent, ShareLoginClient.sShareListener!!)
doShare(sharePlatform, shareContent, ShareLoginClient.sShareListener)
}
}
else -> {
Expand All @@ -52,26 +52,26 @@ class QQHandlerActivity : Activity() {
* @param loginListener
* @time 2017/6/6 13:46
*/
private fun doLogin(loginListener: ILoginListener) {
private fun doLogin(loginListener: ILoginListener?) {
mUIListener = object : IUiListener {
override fun onComplete(p0: Any?) {
try {
val jsonObject = p0 as JSONObject
val token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN)
val openId = jsonObject.getString(Constants.PARAM_OPEN_ID)
val expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN)
loginListener.onSuccess(token, openId, java.lang.Long.valueOf(expires)!!)
loginListener?.onSuccess(token, openId, java.lang.Long.valueOf(expires)!!)
} catch (e: Exception) {
e.printStackTrace()
}
}

override fun onCancel() {
loginListener.onCancel()
loginListener?.onCancel()
}

override fun onError(p0: UiError?) {
loginListener.onError(p0?.errorMessage)
loginListener?.onError(p0?.errorMessage)
}
}
val tencent = Tencent.createInstance(ShareLoginConfig.qqAppId, this.applicationContext)
Expand All @@ -86,18 +86,18 @@ class QQHandlerActivity : Activity() {
* 分享
* @time 2017/6/6 14:57
*/
private fun doShare(sharePlatform: String, shareContent: ShareContent, shareListener: IShareListener) {
private fun doShare(sharePlatform: String, shareContent: ShareContent, shareListener: IShareListener?) {
mUIListener = object : IUiListener {
override fun onComplete(p0: Any?) {
shareListener.onSuccess()
shareListener?.onSuccess()
}

override fun onCancel() {
shareListener.onCancel()
shareListener?.onCancel()
}

override fun onError(p0: UiError?) {
shareListener.onError(p0?.errorMessage)
shareListener?.onError(p0?.errorMessage)
}
}
val tencent = Tencent.createInstance(ShareLoginConfig.qqAppId, applicationContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ class SinaHandlerActivity : Activity(), WbShareCallback {
* @param loginListener
* @time 2017/6/6 13:46
*/
private fun doLogin(loginListener: ILoginListener) {
private fun doLogin(loginListener: ILoginListener?) {
mSsoHandler = SsoHandler(this)
mSsoHandler.authorizeClientSso(object : WbAuthListener {
override fun onSuccess(oauth2AccessToken: Oauth2AccessToken) {
if (oauth2AccessToken.isSessionValid) {
loginListener.onSuccess(oauth2AccessToken.token, oauth2AccessToken.uid, oauth2AccessToken.expiresTime / 1000000)
loginListener?.onSuccess(oauth2AccessToken.token, oauth2AccessToken.uid, oauth2AccessToken.expiresTime / 1000000)
} else {
loginListener.onError("登录失败")
loginListener?.onError("登录失败")
}
}

override fun onFailure(p0: WbConnectErrorMessage?) {
loginListener.onError(p0?.errorMessage)
loginListener?.onError(p0?.errorMessage)
}

override fun cancel() {
loginListener.onCancel()
loginListener?.onCancel()
}
})
}
Expand Down Expand Up @@ -122,17 +122,17 @@ class SinaHandlerActivity : Activity(), WbShareCallback {
}

override fun onWbShareSuccess() {
ShareLoginClient.sShareListener!!.onSuccess()
ShareLoginClient.sShareListener?.onSuccess()
finish()
}

override fun onWbShareCancel() {
ShareLoginClient.sShareListener!!.onCancel()
ShareLoginClient.sShareListener?.onCancel()
finish()
}

override fun onWbShareFail() {
ShareLoginClient.sShareListener!!.onError("分享失败")
ShareLoginClient.sShareListener?.onError("分享失败")
finish()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ class WechatHandlerActivity : Activity(), IWXAPIEventHandler {
* 解析分享响应
* @time 2017/6/7 14:55
*/
private fun parseShareResp(resp: BaseResp, listener: IShareListener) {
private fun parseShareResp(resp: BaseResp, listener: IShareListener?) {
when (resp.errCode) {
BaseResp.ErrCode.ERR_OK -> listener.onSuccess()
BaseResp.ErrCode.ERR_USER_CANCEL -> listener.onCancel()
BaseResp.ErrCode.ERR_AUTH_DENIED -> listener.onError("用户拒绝授权")
BaseResp.ErrCode.ERR_SENT_FAILED -> listener.onError("发送失败")
BaseResp.ErrCode.ERR_COMM -> listener.onError("一般错误")
else -> listener.onError("未知错误")
BaseResp.ErrCode.ERR_OK -> listener?.onSuccess()
BaseResp.ErrCode.ERR_USER_CANCEL -> listener?.onCancel()
BaseResp.ErrCode.ERR_AUTH_DENIED -> listener?.onError("用户拒绝授权")
BaseResp.ErrCode.ERR_SENT_FAILED -> listener?.onError("发送失败")
BaseResp.ErrCode.ERR_COMM -> listener?.onError("一般错误")
else -> listener?.onError("未知错误")
}
}

Expand Down Expand Up @@ -164,26 +164,26 @@ class WechatHandlerActivity : Activity(), IWXAPIEventHandler {
* 解析登录响应
* @time 2017/6/7 14:51
*/
private fun parseLoginResp(activity: Activity, resp: SendAuth.Resp, listener: ILoginListener) {
private fun parseLoginResp(activity: Activity, resp: SendAuth.Resp, listener: ILoginListener?) {
when (resp.errCode) {
BaseResp.ErrCode.ERR_OK -> getAccessTokenByCode(activity, resp.code, listener)
BaseResp.ErrCode.ERR_USER_CANCEL -> {
listener.onCancel()
listener?.onCancel()
LogUtil.d(TAG, "取消登录")
}
BaseResp.ErrCode.ERR_AUTH_DENIED -> {
listener.onError("用户拒绝授权")
listener?.onError("用户拒绝授权")
LogUtil.d(TAG, "用户拒绝授权")
}
else -> listener.onError("未知错误")
else -> listener?.onError("未知错误")
}
}

/**
* 根据登录成功后的code获取token
* @time 2017/6/7 14:50
*/
private fun getAccessTokenByCode(context: Context, code: String, listener: ILoginListener) {
private fun getAccessTokenByCode(context: Context, code: String, listener: ILoginListener?) {
val params = WeiboParameters(null)
params.put("appid", ShareLoginConfig.weiXinAppId)
params.put("secret", ShareLoginConfig.weiXinSecret)
Expand All @@ -197,14 +197,14 @@ class WechatHandlerActivity : Activity(), IWXAPIEventHandler {
val token = jsonObject.getString("access_token")
val openid = jsonObject.getString("openid")
val expires_in = jsonObject.getLong("expires_in")
listener.onSuccess(token, openid, expires_in)
listener?.onSuccess(token, openid, expires_in)
} catch (e: JSONException) {
e.printStackTrace()
}
}

override fun onWeiboException(e: WeiboException) {
listener.onError(e.message!!)
listener?.onError(e.message!!)
}
})
}
Expand Down

0 comments on commit 30169ab

Please sign in to comment.