package com.zhaoyang.driver.wxapi;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.graphics.Bitmap;
|
import android.os.Bundle;
|
import android.util.Log;
|
import android.widget.Toast;
|
import com.zhaoyang.driver.base.BaseEvent;
|
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.modelmsg.SendMessageToWX;
|
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
|
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
|
import com.tencent.mm.opensdk.openapi.IWXAPI;
|
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
|
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
import com.umeng.socialize.weixin.view.WXCallbackActivity;
|
|
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.ThreadMode;
|
|
import java.net.URLEncoder;
|
|
//
|
//import com.example.beiDouThird.dto.ResWechatToken;
|
//import com.example.beiDouThird.dto.WechatJson;
|
//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.modelmsg.SendMessageToWX;
|
//import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
|
//import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
|
//import com.tencent.mm.opensdk.openapi.IWXAPI;
|
//import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
|
//import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
|
|
/**
|
* 微信相关操作,比如登陆
|
*
|
* @author 唐浩
|
*/
|
public class WXEntryActivity extends WXCallbackActivity implements IWXAPIEventHandler {
|
private static final String TAG = WXEntryActivity.class.getSimpleName();
|
private static IWXAPI api;
|
private BaseResp mResp = null;
|
public static String WX_APP_ID = "wx97c33c052b2222f7";
|
// private String WX_APP_SECRET = "24652a2c90268579c8ccd927edc5a5e4";
|
private int type;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
EventBus.getDefault().register(this);
|
initApi();
|
|
}
|
|
private void initApi() {
|
api = WXAPIFactory.createWXAPI(this, WX_APP_ID, false);
|
api.handleIntent(getIntent(), this);
|
api.registerApp(WX_APP_ID);
|
}
|
|
@Override
|
protected void onStart() {
|
super.onStart();
|
api.handleIntent(getIntent(), this);
|
}
|
|
// 微信发送请求到第三方应用时,会回调到该方法
|
@Override
|
public void onReq(BaseReq req) {
|
finish();
|
}
|
|
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
|
@Override
|
public void onResp(BaseResp resp) {
|
Log.i(TAG, "onResp: 微信获得授权");
|
Log.i(TAG, "onResp: "+resp.toString());
|
String result = "";
|
if (resp != null) {
|
mResp = resp;
|
}
|
switch (resp.errCode) {
|
case BaseResp.ErrCode.ERR_OK:
|
switch (resp.getType()) {
|
case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
|
//微信分享
|
// 只是做了简单的finish操作
|
break;
|
case ConstantsAPI.COMMAND_ADD_CARD_TO_EX_CARD_PACKAGE:
|
default:
|
// String code = ((SendAuth.Resp) resp).code;
|
// MapUtil.CustomerHashMap map = MapUtil.getInstance()
|
// .append("appid", WX_APP_ID)
|
// .append("secret", WX_APP_SECRET)
|
// .append("code", code)
|
// .append("grant_type", "authorization_code");
|
// HttpMethods.getInstance()
|
// .mApi
|
// .post("https://api.weixin.qq.com/sns/oauth2/access_token", map)
|
// .map(new Function<String, ResWechatToken>() {
|
// @Override
|
// public ResWechatToken apply(String s) throws Exception {
|
// if (TextUtils.isEmpty(s)) {
|
// return null;
|
// }
|
// return HttpMethods.mGson.fromJson(s, ResWechatToken.class);
|
// }
|
// })
|
// .subscribeOn(Schedulers.io())
|
// .observeOn(AndroidSchedulers.mainThread())
|
// .subscribe(new Consumer<ResWechatToken>() {
|
// @Override
|
// public void accept(ResWechatToken resWechatToken) throws Exception {
|
// Log.i(TAG, "accept: resWechatToken = " + resWechatToken);
|
// getUserInfo(resWechatToken);
|
// }
|
// }, new Consumer<Throwable>() {
|
// @Override
|
// public void accept(Throwable throwable) throws Exception {
|
// Log.e(TAG, "accept: " + throwable.getMessage());
|
// }
|
// })
|
// ;
|
break;
|
}
|
|
finish();
|
|
break;
|
case BaseResp.ErrCode.ERR_USER_CANCEL:
|
result = "发送取消";
|
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
|
finish();
|
break;
|
case BaseResp.ErrCode.ERR_AUTH_DENIED:
|
result = "发送被拒绝";
|
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
|
finish();
|
break;
|
default:
|
result = "发送返回";
|
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
|
finish();
|
break;
|
}
|
}
|
|
|
|
// /**
|
// * 通过拼接的用户信息url获取用户信息
|
// */
|
// private void getUserInfo(ResWechatToken data) {
|
//
|
// val map: HashMap<String, Any> = HashMap()
|
// map["type"] = "1"
|
// HttpManager.getInstance().post(Api.getParamInfo, map, object : MyObserver(this) {
|
// override fun success(responseString: String?) {
|
// var banner: BannerBean = Gson().fromJson(responseString, BannerBean::class.java)
|
// }
|
//
|
// })
|
// HashMap<String,Object> maps = new HashMap<>();
|
// maps.put("access_token",data.getAccess_token());
|
// maps.put("openid",data.getOpenid());
|
//
|
// HttpMethods.getInstance()
|
// .mApi
|
// .post("https://api.weixin.qq.com/sns/userinfo", map)
|
// .map(new Function<String, WechatJson>() {
|
// @Override
|
// public WechatJson apply(String s) throws Exception {
|
// Log.e(TAG, "apply: " + s);
|
// if (TextUtils.isEmpty(s)) return null;
|
// return HttpMethods.mGson.fromJson(s, WechatJson.class);
|
// }
|
// })
|
// .subscribeOn(Schedulers.io())
|
// .observeOn(AndroidSchedulers.mainThread())
|
// .subscribe(new Consumer<WechatJson>() {
|
// @Override
|
// public void accept(WechatJson stringHttpResult) throws Exception {
|
// EventBus.getDefault().post(new EventWechatLogin(stringHttpResult));
|
// }
|
// }, new Consumer<Throwable>() {
|
// @Override
|
// public void accept(Throwable throwable) throws Exception {
|
// }
|
// })
|
// ;
|
// }
|
|
@Override
|
protected void onNewIntent(Intent intent) {
|
super.onNewIntent(intent);
|
setIntent(intent);
|
api.handleIntent(intent, this);
|
finish();
|
}
|
|
private String urlEnodeUTF8(String str) {
|
String result = str;
|
try {
|
result = URLEncoder.encode(str, "UTF-8");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
* 登录微信
|
*/
|
public static void loginWithWechat(Context context) {
|
IWXAPI wxapi = WXAPIFactory.createWXAPI(context, WXEntryActivity.WX_APP_ID, true);
|
wxapi.registerApp(WXEntryActivity.WX_APP_ID);
|
SendAuth.Req req = new SendAuth.Req();
|
req.scope = "snsapi_userinfo";
|
req.state = "youliao";
|
wxapi.sendReq(req);
|
}
|
|
/***
|
* 微信分享
|
* @param context context
|
* @param appId appId
|
* @param webUrl 填写网页的url
|
* @param title 填写网页标题
|
* @param content /填写网页描述
|
* @param bitmap 填写网页位图
|
*/
|
public static void shareWeb(Context context, String appId, String webUrl, String title
|
, String content, Bitmap bitmap) {
|
// 通过appId得到IWXAPI这个对象
|
IWXAPI wxapi = WXAPIFactory.createWXAPI(context, appId);
|
// 检查手机或者模拟器是否安装了微信
|
if (!wxapi.isWXAppInstalled()) {
|
Toast.makeText(context, "您还没有安装微信", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
WXWebpageObject webpageObject = new WXWebpageObject();
|
webpageObject.webpageUrl = webUrl;
|
WXMediaMessage msg = new WXMediaMessage(webpageObject);
|
msg.title = title;
|
msg.description = content;
|
// 如果没有位图,可以传null,会显示默认的图片
|
msg.setThumbImage(bitmap);
|
SendMessageToWX.Req req = new SendMessageToWX.Req();
|
// transaction用于唯一标识一个请求(可自定义)
|
req.transaction = "webpage";
|
// 上文的WXMediaMessage对象
|
req.message = msg;
|
// SendMessageToWX.Req.WXSceneSession是分享到好友会话
|
// SendMessageToWX.Req.WXSceneTimeline是分享到朋友圈
|
req.scene = SendMessageToWX.Req.WXSceneSession;
|
// 向微信发送请求
|
wxapi.sendReq(req);
|
}
|
|
|
// public void WXPay(OrderPayBean orderInfo) {
|
// PayReq request = new PayReq();
|
// request.appId = orderInfo.getAppid();
|
// request.partnerId = orderInfo.getPartnerId();
|
// request.prepayId = orderInfo.getPrepayId();
|
// request.packageValue = orderInfo.getPackageX();
|
// request.nonceStr = orderInfo.getNonceStr();
|
// request.timeStamp = orderInfo.getTimeStamp();
|
// request.sign = orderInfo.getSign();
|
// Boolean Pay = api.sendReq(request);
|
// Toast.makeText(this, "发起支付", Toast.LENGTH_SHORT).show();
|
// }
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
EventBus.getDefault().unregister(this);
|
}
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
public void onMainEvent(BaseEvent eventEntity) {
|
if (eventEntity.getCode() == BaseEvent.WEIXIN_BACK){
|
// onBackPressed();
|
}
|
}
|
|
}
|