Skip to content

Commit

Permalink
增加第三方登陆返回的accessToken等信息
Browse files Browse the repository at this point in the history
  • Loading branch information
liujinwei committed Sep 27, 2018
1 parent a39f081 commit 0921698
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 114 deletions.
47 changes: 31 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ allprojects {
```
dependencies {
...
compile 'com.github.arvinljw:SocialHelper:v1.0.9'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.github.arvinljw:SocialHelper:v1.1.0'
}
```

*注:如果在该module中使用了v7包,那么可使用exclude命令移除本库的引用避免重复,gson也是一样,大体方法如下*

```
compile ('com.github.arvinljw:SocialHelper:v1.0.9'){
exclude group: 'com.android.support'
}
```
*注:如果在该module中只引用了这三个第三方的包

#### 使用

Expand Down Expand Up @@ -95,9 +91,21 @@ AndroidManifest.xml配置
*注:其中需要注意的是,qq配置中<data android:scheme="tencentqqAppId"/>
qqAppId换成您申请的qqAppId即可*

微信还需要在packageName.wxapi的包下创建WXEntryActivity,具体实现可在[这里查看](app/src/main/java/net/arvin/socialhelper/sample/wxapi/WXEntryActivity.java)
微信还需要在packageName.wxapi的包下创建WXEntryActivity,该Activity可继承**WxHelperActivity**,并传入SocialHelper的实例

```
public class WXEntryActivity extends WXHelperActivity {
@Override
protected SocialHelper getSocialHelper() {
return SocialUtil.INSTANCE.socialHelper;
}
}
```

具体实现可在[这里查看](app/src/main/java/net/arvin/socialhelper/sample/wxapi/WXEntryActivity.java)

拷贝过去即可,注意路径,其中主要的处理是在其onResp的时候判断是登录还是分享做出相应的处理,通知本库触发回调
WxHelperActivity主要处理的是在其onResp的时候判断是登录还是分享做出相应的处理,通知本库触发回调

**2、获取实例**

Expand All @@ -110,9 +118,14 @@ socialHelper = new SocialHelper.Builder()
.setWxAppSecret("wxAppSecret")
.setWbAppId("wbAppKey")
.setWbRedirectUrl("wbRedirectUrl")
.setNeedLoginResult(true)
.build();
```

其中setNeedLoginResult表示是否带有第三方登陆后的AccessToken等信息,默认是false。

**返回的AccessToken等信息分别在ThirdInfoEntity里的qqInfo,wxInfo,wbInfo中的loginResultEntity中。**

**3、调用相关方法**

这里提供了QQ、微信和微博的相应的登录和分享方法
Expand Down Expand Up @@ -160,7 +173,6 @@ public interface SocialShareCallback extends SocialCallback{

分享成功就只有一个回调,便于提示。


**c、ShareEntity**

这个参数是分享的重点,对于QQ、微信和微博对应着QQShareEntity、WXShareEntity以及WBShareEntity
Expand Down Expand Up @@ -240,6 +252,14 @@ protected void onDestroy() {

### Release Log

**v1.1.0:**

* 增加第三方登陆返回的accessToken等信息
* 封装微信登陆和分享回调类WxHelperActivity
* 将微信的广播注册方式换成本地广播
* 将v7和gson包只是编译,需要自己引入,避免重复使用
* 调整相关包版本以及编译版本等

**v1.0.9:**

* 分享回调增加分享类型
Expand Down Expand Up @@ -295,11 +315,6 @@ protected void onDestroy() {

若是有什么好的建议或者问题,请多多指教,感激不尽。

待做事项

* 参数解释以及对应样式的整理
* 完善demo的代码

### License

```
Expand Down
12 changes: 5 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ android {
}

dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
api 'com.android.support:appcompat-v7:'.concat(projectSupportVersion)
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
api project(':socialhelper')
// api ('com.github.arvinljw:SocialHelper:v1.0.9'){
// exclude group: 'com.android.support'
// }
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:appcompat-v7:'.concat(projectSupportVersion)
implementation 'com.google.code.gson:gson:2.8.2'
implementation project(':socialhelper')
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,18 @@
package net.arvin.socialhelper.sample.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import net.arvin.socialhelper.SocialHelper;
import net.arvin.socialhelper.WXHelperActivity;
import net.arvin.socialhelper.sample.utils.SocialUtil;

/**
* Created by arvinljw on 17/7/6 14:43
* Function:
* Desc:
*/
public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
private IWXAPI api;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String wxAppId = SocialUtil.INSTANCE.socialHelper.getBuilder().getWxAppId();
api = WXAPIFactory.createWXAPI(this, wxAppId, true);
api.registerApp(wxAppId);

api.handleIntent(getIntent(), this);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}

@Override
public void onReq(BaseReq baseReq) {
}
public class WXEntryActivity extends WXHelperActivity {

@Override
public void onResp(BaseResp baseResp) {
//登录
Log.d("WXEntryActivity", baseResp.errCode + baseResp.errStr);
if (baseResp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
String code = ((SendAuth.Resp) baseResp).code;
SocialUtil.INSTANCE.socialHelper.sendAuthBackBroadcast(this, code);
} else {
SocialUtil.INSTANCE.socialHelper.sendAuthBackBroadcast(this, null);
}
} else if (baseResp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
SocialUtil.INSTANCE.socialHelper.sendShareBackBroadcast(this, true);
} else {
SocialUtil.INSTANCE.socialHelper.sendShareBackBroadcast(this, false);
}
}
onBackPressed();
protected SocialHelper getSocialHelper() {
return SocialUtil.INSTANCE.socialHelper;
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ org.gradle.jvmargs=-Xmx1536m

projectApplicationId = net.arvin.socialhelper.sample

projectCompileSdkVersion = 27
projectCompileSdkVersion = 28

projectMinSdkVersion = 14
projectTargetSdkVersion = 27
projectTargetSdkVersion = 28

projectSupportVersion = 27.1.1
projectSupportVersion = 28.0.0
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed May 09 20:18:08 CST 2018
#Thu Sep 27 14:54:57 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4 changes: 2 additions & 2 deletions socialhelper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ android {

dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
api 'com.android.support:appcompat-v7:'.concat(projectSupportVersion)
compileOnly 'com.android.support:appcompat-v7:'.concat(projectSupportVersion)
api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:5.1.4'
api 'com.sina.weibo.sdk:core:4.2.7:openDefaultRelease@aar'
api 'com.google.code.gson:gson:2.8.2'
compileOnly 'com.google.code.gson:gson:2.8.2'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.arvin.socialhelper;

/**
* Created by arvinljw on 2018/9/27 15:26
* Function:
* Desc:
*/
public interface INeedLoginResult {
void setNeedLoginResult(boolean needLoginResult);

boolean isNeedLoginResult();
}
46 changes: 33 additions & 13 deletions socialhelper/src/main/java/net/arvin/socialhelper/QQHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@
* Function:
* Desc:
*/
final class QQHelper implements ISocial {
final class QQHelper implements ISocial, INeedLoginResult {
private Activity activity;
private Tencent tencent;
private String appId;

private SocialLoginCallback loginCallback;
private IUiListener loginListener;
private QQLoginResultEntity loginResult;
private IUiListener userInfoListener;
private QQInfoEntity qqInfo;

private boolean needLoginResult;

private SocialShareCallback shareCallback;
private IUiListener shareListener;

Expand Down Expand Up @@ -115,31 +118,48 @@ private void getUserInfo() {
return;
}

initUserInfoListener();

UserInfo info = new UserInfo(activity, tencent.getQQToken());
info.getUserInfo(userInfoListener);
}

private IUiListener userInfoListener = new NormalUIListener(activity, loginCallback) {
@Override
public void onComplete(Object o) {
try {
qqInfo = new Gson().fromJson(o.toString(), QQInfoEntity.class);
if (loginCallback != null) {
loginCallback.loginSuccess(createThirdInfo());
private void initUserInfoListener() {
userInfoListener = new NormalUIListener(activity, loginCallback) {
@Override
public void onComplete(Object o) {
try {
qqInfo = new Gson().fromJson(o.toString(), QQInfoEntity.class);
if (isNeedLoginResult()) {
qqInfo.setLoginResultEntity(loginResult);
}
if (loginCallback != null) {
loginCallback.loginSuccess(createThirdInfo());
}
tencent.logout(activity);
} catch (Exception e) {
e.printStackTrace();
}
tencent.logout(activity);
} catch (Exception e) {
e.printStackTrace();
}
}
};
};
}

@Override
public ThirdInfoEntity createThirdInfo() {
return ThirdInfoEntity.createQQThirdInfo(loginResult.getPfkey(), tencent.getOpenId(), qqInfo.getNickname(),
SocialUtil.getQQSex(qqInfo.getGender()), qqInfo.getFigureurl_qq_2(), qqInfo);
}

@Override
public void setNeedLoginResult(boolean needLoginResult) {
this.needLoginResult = needLoginResult;
}

@Override
public boolean isNeedLoginResult() {
return needLoginResult;
}

/**
* qq有个坑,采用onActivityResult的方式,如果留在qq的话,home键退出之后无法正确回调
*/
Expand Down
Loading

0 comments on commit 0921698

Please sign in to comment.