d
This commit is contained in:
202
app/src/main/java/com/fenghoo/seven/wxapi/WXEntryActivity.java
Normal file
202
app/src/main/java/com/fenghoo/seven/wxapi/WXEntryActivity.java
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* 官网地站:http://www.mob.com
|
||||
* 技术支持QQ: 4006852216
|
||||
* 官方微信:ShareSDK (如果发布新版本的话,我们将会第一时间通过微信将版本更新内容推送给您。如果使用过程中有任何问题,也可以通过微信与我们取得联系,我们将会在24小时内给予回复)
|
||||
*
|
||||
* Copyright (c) 2013年 mob.com. All rights reserved.
|
||||
*/
|
||||
|
||||
package com.fenghoo.seven.wxapi;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.fenghoo.seven.main.entity.mine.TreeUserEntity;
|
||||
import com.fenghoo.seven.main.entity.mine.WeChatLoginEntity;
|
||||
import com.fenghoo.seven.main.entity.mine.WeChatLoginResultEntity;
|
||||
import com.fenghoo.seven.network.ApiUtils;
|
||||
import com.fenghoo.seven.network.LifeTreeAPIException;
|
||||
import com.fenghoo.seven.network.http.ResponseBean;
|
||||
import com.fenghoo.seven.utils.checkVersionsUtils.ProfileSpUtils;
|
||||
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 cn.sharesdk.wechat.utils.WXAppExtendObject;
|
||||
import cn.sharesdk.wechat.utils.WXMediaMessage;
|
||||
import cn.sharesdk.wechat.utils.WechatHandlerActivity;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableEmitter;
|
||||
import io.reactivex.ObservableOnSubscribe;
|
||||
import io.reactivex.ObservableSource;
|
||||
import io.reactivex.annotations.NonNull;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.functions.Function;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* 微信客户端回调activity示例
|
||||
*/
|
||||
public class WXEntryActivity extends WechatHandlerActivity implements IWXAPIEventHandler {
|
||||
public static final String WEIXIN_APP_ID = "wxaa49732bd3a29bec";
|
||||
private static final String APP_SECRET = "038324288450b5dcb610915fea7fb1af";
|
||||
private static ObservableEmitter<ResponseBean<TreeUserEntity>> mE;
|
||||
private IWXAPI mWeixinAPI;
|
||||
/**
|
||||
* 微信登陆数据
|
||||
*/
|
||||
private WeChatLoginEntity mEntity;
|
||||
|
||||
public static Observable<ResponseBean<TreeUserEntity>> getObservable() {
|
||||
return Observable.create(new ObservableOnSubscribe<ResponseBean<TreeUserEntity>>() {
|
||||
@Override
|
||||
public void subscribe(@NonNull ObservableEmitter<ResponseBean<TreeUserEntity>> e) throws Exception {
|
||||
mE = e;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mWeixinAPI = WXAPIFactory.createWXAPI(this, WEIXIN_APP_ID, true);
|
||||
mWeixinAPI.handleIntent(this.getIntent(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
setIntent(intent);
|
||||
mWeixinAPI.handleIntent(intent, this);//必须调用此句话
|
||||
}
|
||||
|
||||
//微信发送的请求将回调到onReq方法
|
||||
@Override
|
||||
public void onReq(BaseReq req) {
|
||||
}
|
||||
|
||||
//发送到微信请求的响应结果
|
||||
@Override
|
||||
public void onResp(BaseResp resp) {
|
||||
switch (resp.errCode) {
|
||||
case BaseResp.ErrCode.ERR_OK:
|
||||
//发送成功
|
||||
SendAuth.Resp sendResp = (SendAuth.Resp) resp;
|
||||
if (sendResp != null) {
|
||||
String code = sendResp.code;
|
||||
getAccess_token(code);
|
||||
}
|
||||
break;
|
||||
case BaseResp.ErrCode.ERR_USER_CANCEL:
|
||||
//发送取消
|
||||
mE.onError(new LifeTreeAPIException(0, "取消登陆"));
|
||||
finish();
|
||||
break;
|
||||
case BaseResp.ErrCode.ERR_AUTH_DENIED:
|
||||
//发送被拒绝
|
||||
break;
|
||||
default:
|
||||
//发送返回
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取openid accessToken值用于后期操作
|
||||
*
|
||||
* @param code 请求码
|
||||
*/
|
||||
private void getAccess_token(final String code) {
|
||||
//获取微信资料
|
||||
ApiUtils.getWeChatApi()
|
||||
.access_token(WEIXIN_APP_ID, APP_SECRET, code, "authorization_code")
|
||||
.flatMap(new Function<WeChatLoginResultEntity, ObservableSource<WeChatLoginEntity>>() {
|
||||
@Override
|
||||
public ObservableSource<WeChatLoginEntity> apply(@NonNull WeChatLoginResultEntity entity) throws Exception {
|
||||
//存储两个id值
|
||||
mEntity = new WeChatLoginEntity();
|
||||
mEntity.setOpen_id(entity.getOpenid());
|
||||
mEntity.setUnion_id(entity.getUnionid());
|
||||
//获取微信用户信息
|
||||
return ApiUtils.getWeChatApi()
|
||||
.userInfo(entity.getAccess_token()
|
||||
, entity.getOpenid());
|
||||
}
|
||||
})
|
||||
//用户资料登陆
|
||||
.flatMap(new Function<WeChatLoginEntity, ObservableSource<ResponseBean<TreeUserEntity>>>() {
|
||||
@Override
|
||||
public ObservableSource<ResponseBean<TreeUserEntity>> apply(@NonNull WeChatLoginEntity entity) throws Exception {
|
||||
|
||||
if (ProfileSpUtils.getInstance().isLogin()) {
|
||||
//绑定微信
|
||||
return ApiUtils.getApi()
|
||||
.bindWx(ProfileSpUtils.getInstance()
|
||||
.getUserProfie()
|
||||
.getData().getUuid()
|
||||
, mEntity.getOpen_id()
|
||||
, entity.getNickname()
|
||||
, entity.getHeadimg()
|
||||
, entity.getSex()
|
||||
, mEntity.getUnion_id());
|
||||
} else {
|
||||
//用户微信登陆
|
||||
return ApiUtils.getApi()
|
||||
.wxLogin(mEntity.getOpen_id()
|
||||
, entity.getNickname()
|
||||
, entity.getHeadimg()
|
||||
, entity.getSex()
|
||||
, mEntity.getUnion_id());
|
||||
}
|
||||
}
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(new Consumer<ResponseBean<TreeUserEntity>>() {
|
||||
@Override
|
||||
public void accept(@NonNull ResponseBean<TreeUserEntity> treeUserEntityResponseBean) throws Exception {
|
||||
mE.onNext(treeUserEntityResponseBean);
|
||||
mE.onComplete();
|
||||
finish();
|
||||
}
|
||||
}, new Consumer<Throwable>() {
|
||||
@Override
|
||||
public void accept(@NonNull Throwable throwable) throws Exception {
|
||||
mE.onError(throwable);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理微信发出的向第三方应用请求app message
|
||||
* <p>
|
||||
* 在微信客户端中的聊天页面有“添加工具”,可以将本应用的图标添加到其中 此后点击图标,下面的代码会被执行。Demo仅仅只是打开自己而已,但你可
|
||||
* 做点其他的事情,包括根本不打开任何页面
|
||||
*/
|
||||
public void onGetMessageFromWXReq(WXMediaMessage msg) {
|
||||
Intent iLaunchMyself = getPackageManager().getLaunchIntentForPackage(
|
||||
getPackageName());
|
||||
startActivity(iLaunchMyself);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理微信向第三方应用发起的消息
|
||||
* <p>
|
||||
* 此处用来接收从微信发送过来的消息,比方说本demo在wechatpage里面分享
|
||||
* 应用时可以不分享应用文件,而分享一段应用的自定义信息。接受方的微信 客户端会通过这个方法,将这个信息发送回接收方手机上的本demo中,当作 回调。
|
||||
* <p>
|
||||
* 本Demo只是将信息展示出来,但你可做点其他的事情,而不仅仅只是Toast
|
||||
*/
|
||||
public void onShowMessageFromWXReq(WXMediaMessage msg) {
|
||||
if (msg != null && msg.mediaObject != null
|
||||
&& (msg.mediaObject instanceof WXAppExtendObject)) {
|
||||
WXAppExtendObject obj = (WXAppExtendObject) msg.mediaObject;
|
||||
Toast.makeText(this, obj.extInfo, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.fenghoo.seven.wxapi;
|
||||
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.fenghoo.seven.base.BaseActivity;
|
||||
import com.tencent.mm.opensdk.modelbase.BaseReq;
|
||||
import com.tencent.mm.opensdk.modelbase.BaseResp;
|
||||
import com.tencent.mm.opensdk.openapi.IWXAPI;
|
||||
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付回调页
|
||||
*/
|
||||
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
|
||||
|
||||
private IWXAPI api;
|
||||
private TextView mTextView;
|
||||
private Intent intent;
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
setIntent(intent);
|
||||
api.handleIntent(intent, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReq(BaseReq req) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResp(BaseResp resp) {
|
||||
if (resp.errCode == 0) {
|
||||
toast("支付成功");
|
||||
} else if (resp.errCode == -1) {
|
||||
toast("支付失败");
|
||||
} else if (resp.errCode == -2) {
|
||||
toast("取消支付");
|
||||
}
|
||||
/**
|
||||
* 用广播发出回掉状态
|
||||
*/
|
||||
intent = new Intent();
|
||||
intent.setAction("com.antiphon.BroadcastReceiver");
|
||||
intent.putExtra("payCode",resp.errCode);
|
||||
sendBroadcast(intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
initData();
|
||||
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
// api = WXAPIFactory.createWXAPI(this, PayUtils.APP_ID);
|
||||
api.handleIntent(getIntent(), this);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user