|
@@ -1,22 +1,53 @@
|
|
|
package org.cocos2dx.javascript.presenter;
|
|
|
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
import android.os.Process;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.view.KeyEvent;
|
|
|
+import android.webkit.JavascriptInterface;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
+import com.alipay.sdk.app.PayTask;
|
|
|
+import com.edufound.mobile.wxapi.WXPayEntryActivity;
|
|
|
import com.google.gson.Gson;
|
|
|
+import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
|
|
|
+import com.mobile.auth.gatewayauth.TokenResultListener;
|
|
|
+import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
|
|
|
+import com.tencent.mm.opensdk.modelpay.PayReq;
|
|
|
+import com.tencent.mm.opensdk.openapi.IWXAPI;
|
|
|
+import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
|
|
|
|
|
import org.cocos2dx.javascript.AppActivity;
|
|
|
+import org.cocos2dx.javascript.bean.AliPayResult;
|
|
|
+import org.cocos2dx.javascript.util.Consts;
|
|
|
import org.cocos2dx.javascript.util.DeviceUtil;
|
|
|
+import org.cocos2dx.javascript.util.DeviceUuidFactory;
|
|
|
import org.cocos2dx.javascript.util.Logger;
|
|
|
import org.cocos2dx.javascript.util.PackageUtil;
|
|
|
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.cocos2dx.lib.Cocos2dxHelper.runOnGLThread;
|
|
|
|
|
|
public class MainPresenter {
|
|
|
private static AppActivity mActivity;
|
|
|
private static DeviceUtil mDeviceUtil;
|
|
|
private static PackageUtil mPackageUtil;
|
|
|
private static Gson mGson;
|
|
|
+ private static boolean autoLogin;
|
|
|
+ PhoneNumberAuthHelper mPhoneNumberAuthHelper;
|
|
|
+ private static String loginToken;
|
|
|
+ //微信api
|
|
|
+ private static IWXAPI WX_API;
|
|
|
+ //微信appid
|
|
|
+ public static String WX_APPID = "wx51acc19c8f7a0f6f";
|
|
|
+ public static String WX_SECRET = "e830d45f497025041269ef6221140c3d";
|
|
|
|
|
|
public MainPresenter(AppActivity activity) {
|
|
|
mActivity = activity;
|
|
@@ -25,8 +56,28 @@ public class MainPresenter {
|
|
|
Logger.e("getWidth:" + mDeviceUtil.getWidth(mActivity));
|
|
|
Logger.e("getHeight:" + mDeviceUtil.getHeight(mActivity));
|
|
|
mPackageUtil = new PackageUtil(activity);
|
|
|
+ new DeviceUuidFactory(Consts.mApplicatonContext);
|
|
|
+ mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(mActivity, mTokenResultListener);
|
|
|
+ mPhoneNumberAuthHelper.setAuthSDKInfo("BzFbe8Ud13a0fixI18bD2CRzO5U7iz4Xf7hS5aiftwGJLOBypQraiKdFd5IHgK9wTYW9Plr6+m8pH3cHqSjCOImr8HXf3NYTUCOyePsoUaKrX9qQGIbSZ7KvVMoyUY0FIUsA4hXiv873ZS6uIdMg5aU2Xx9hh+y0bAbsl0PnXQOShlZ/sdyRj8JiQibEel6LlvwFiy5f3l3ML3ojVvCNVhe6aH4f/Plx5hBcRRIf5Bv3oAY6FY6n6pNwNmCLUDRiy2A3bWp2gH6N6uTTaOZi308ylelFPZ17");
|
|
|
+ mPhoneNumberAuthHelper.checkEnvAvailable(2);
|
|
|
+ WX_API = WXAPIFactory.createWXAPI(Consts.mApplicatonContext, WX_APPID);
|
|
|
+ WX_API.registerApp(WX_APPID);
|
|
|
+// mPhoneNumberAuthHelper.getVerifyToken(5000);
|
|
|
+// handler.sendEmptyMessageDelayed(1, 5000);
|
|
|
}
|
|
|
|
|
|
+ Handler handler = new Handler(new Handler.Callback() {
|
|
|
+ @Override
|
|
|
+ public boolean handleMessage(@NonNull Message message) {
|
|
|
+ switch (message.what) {
|
|
|
+ case 1:
|
|
|
+ mPhoneNumberAuthHelper.getLoginToken(mActivity, 5000);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
public static String getVersionCode() {
|
|
|
return mDeviceUtil.getVersionCode(mActivity);
|
|
|
}
|
|
@@ -40,6 +91,7 @@ public class MainPresenter {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public static String getSystemAppsJson() {
|
|
|
return mGson.toJson(mPackageUtil.getSystemAppList()).toString();
|
|
|
}
|
|
@@ -83,6 +135,71 @@ public class MainPresenter {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ //返回一键登录初始化成功还是失败
|
|
|
+ public static String getAutoLoginSuccess() {
|
|
|
+ Logger.e("autoLogin:" + autoLogin);
|
|
|
+ return String.valueOf(autoLogin);
|
|
|
+ }
|
|
|
+
|
|
|
+ //返回deviceCode
|
|
|
+ public static String getLoginToken() {
|
|
|
+ Logger.e("getLoginToken:" + loginToken);
|
|
|
+ return loginToken;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //返回deviceCode
|
|
|
+ public static String getDeviceCode() {
|
|
|
+ Logger.e("调用getDeviceCode:" + DeviceUuidFactory.getUuid().toString());
|
|
|
+ return DeviceUuidFactory.getUuid().toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void WeChatPay(String json) {
|
|
|
+ Logger.e("WeChatPay:json-" + json);
|
|
|
+ if (!WX_API.isWXAppInstalled()) {
|
|
|
+ Toast.makeText(mActivity, "请安装微信", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Message msg = new Message();
|
|
|
+ msg.obj = json;
|
|
|
+ msg.what = 0xffbcd14;
|
|
|
+ resultHandler.sendMessageDelayed(msg, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void ALIPay(final String orderInfo) {
|
|
|
+ Logger.e("orderInfo:" + orderInfo);
|
|
|
+ Runnable payRunnable = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ PayTask alipay = new PayTask(mActivity);
|
|
|
+ Map<String, String> result = alipay.payV2(orderInfo, true);
|
|
|
+ Message msg = new Message();
|
|
|
+ msg.what = 0xffbcd15;
|
|
|
+ msg.obj = result;
|
|
|
+ resultHandler.sendMessage(msg);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Thread payThread = new Thread(payRunnable);
|
|
|
+ payThread.start();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @JavascriptInterface
|
|
|
+ public void toMiniProgram(String programId, String path) {
|
|
|
+ Logger.e("toMiniProgram:programId-" + programId + "--path:" + path);
|
|
|
+ if (!WX_API.isWXAppInstalled()) {
|
|
|
+ Toast.makeText(mActivity, "请安装微信", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
|
|
|
+ req.userName = programId; // 填小程序原始id
|
|
|
+ req.path = path; //拉起小程序页面的可带参路径,不填默认拉起小程序首页
|
|
|
+ req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版,体验版和正式版
|
|
|
+ WX_API.sendReq(req);
|
|
|
+ }
|
|
|
+
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
switch (keyCode) {
|
|
|
case KeyEvent.KEYCODE_BACK:
|
|
@@ -98,4 +215,114 @@ public class MainPresenter {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ TokenResultListener mTokenResultListener = new TokenResultListener() {
|
|
|
+ @Override
|
|
|
+ public void onTokenSuccess(String json) {
|
|
|
+ Logger.e("onTokenSuccess:" + json);
|
|
|
+ try {
|
|
|
+ JSONObject object = new JSONObject(json);
|
|
|
+ String code = object.getString("code");
|
|
|
+ autoLogin = true;
|
|
|
+ loginToken = object.getString("token");
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTokenFailed(String json) {
|
|
|
+ Logger.e("onTokenFailed:" + json);
|
|
|
+ try {
|
|
|
+ JSONObject object = new JSONObject(json);
|
|
|
+ int code = Integer.valueOf(object.getString("code"));
|
|
|
+ switch (code) {
|
|
|
+ case 700000:
|
|
|
+ case 700001:
|
|
|
+ case 700002:
|
|
|
+ case 700003:
|
|
|
+ case 700004:
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ autoLogin = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+// Toast.makeText(mActivity, "errorCode:" + object.getString("code") + "--errorMsg:" + object.getString("msg"), Toast.LENGTH_LONG).show();
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ public static Handler resultHandler = new Handler(new Handler.Callback() {
|
|
|
+ @Override
|
|
|
+ public boolean handleMessage(Message msg) {
|
|
|
+ switch (msg.what) {
|
|
|
+ case 0xffbcd13:
|
|
|
+ //微信回参回调
|
|
|
+ WXPayEntryActivity.payBack bean = (WXPayEntryActivity.payBack) msg.obj;
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("errorCode", bean.errorCode + "");
|
|
|
+ map.put("errStr", bean.errStr);
|
|
|
+ map.put("transaction", bean.transaction);
|
|
|
+ map.put("openId", bean.openId);
|
|
|
+ final String wechatCallBackJson = mGson.toJson(map);
|
|
|
+ mActivity.runOnGLThread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Cocos2dxJavascriptJavaBridge.evalString("PUBLIC.WeChatPayCallBack('" + wechatCallBackJson + "')");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 0xffbcd14:
|
|
|
+ //微信支付
|
|
|
+ try {
|
|
|
+ String jsonmessage = msg.obj.toString();
|
|
|
+ if (jsonmessage != null && jsonmessage.length() > 0) {
|
|
|
+ JSONObject json = new JSONObject(jsonmessage);
|
|
|
+ if (null != json) {
|
|
|
+ PayReq req = new PayReq();
|
|
|
+ req.appId = json.getString("appid");
|
|
|
+ req.partnerId = json.getString("partnerid");
|
|
|
+ req.prepayId = json.getString("prepayid");
|
|
|
+ req.nonceStr = json.getString("noncestr");
|
|
|
+ req.timeStamp = json.getString("timestamp");
|
|
|
+ req.packageValue = json.getString("package");
|
|
|
+ req.sign = json.getString("sign");
|
|
|
+ Logger.e("req-json:" + mGson.toJson(req));
|
|
|
+ WX_API.sendReq(req);
|
|
|
+ } else {
|
|
|
+ Logger.e("返回错误" + json.getString("retmsg"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Logger.e("服务器请求错误");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Logger.e("异常:" + e.getMessage());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 0xffbcd15:
|
|
|
+ //支付宝支付
|
|
|
+ AliPayResult payResult = new AliPayResult((Map<String, String>) msg.obj);
|
|
|
+ final String resultInfo = payResult.getResult();// 同步返回需要验证的信息
|
|
|
+ final String resultStatus = payResult.getResultStatus();
|
|
|
+ if (resultStatus.equals("4000")) {
|
|
|
+ Toast.makeText(mActivity, "未找到支付宝客户端", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ mActivity.runOnGLThread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Cocos2dxJavascriptJavaBridge.evalString("PUBLIC.AliPayCallback('" + resultInfo + "','" + resultStatus + "')");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
}
|