| package com.secondworld.univeralsdk; | 
|   | 
| import android.app.Activity; | 
| import android.content.Context; | 
| import android.content.SharedPreferences; | 
| import android.os.Bundle; | 
|   | 
| import com.snowfish.cn.ganga.helper.SFOnlineHelper; | 
| import com.snowfish.cn.ganga.helper.SFOnlineInitListener; | 
| import com.snowfish.cn.ganga.helper.SFOnlineLoginListener; | 
| import com.snowfish.cn.ganga.helper.SFOnlinePayResultListener; | 
| import com.snowfish.cn.ganga.helper.SFOnlineUser; | 
|   | 
| import org.json.JSONException; | 
| import org.json.JSONObject; | 
|   | 
| import java.util.HashMap; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * Created by Administrator on 2018/9/18 0018. | 
|  */ | 
|   | 
| public class YJPlatformUtil | 
| { | 
|     private static SFOnlineUser USER = null; | 
|     private final static String PAY_CALLBACK_URL = ""; | 
|     private final static String LOGIN_CHECK_URL = ""; | 
|     private static final String TAG = "YJPlatformUtil"; | 
|   | 
|     private static YJPlatformUtil m_Instance; | 
|   | 
|     public static YJPlatformUtil getInstance() | 
|     { | 
|         if (m_Instance == null) | 
|         { | 
|             m_Instance = new YJPlatformUtil(); | 
|         } | 
|         return m_Instance; | 
|     } | 
|   | 
|     private Map<String, Object> m_Message = new HashMap<>(); | 
|     public boolean payProcessing = false; | 
|   | 
|     public void onCreate(final Activity activity, Bundle savedInstanceState) | 
|     { | 
|         SFOnlineHelper.onCreate(activity, new SFOnlineInitListener() | 
|         { | 
|             @Override | 
|             public void onResponse(String s, String s1) | 
|             { | 
|                 m_Message.clear(); | 
|                 if (s.equalsIgnoreCase("success")) | 
|                 { | 
|                     m_Message.put("code", CodeA2U.FreePlatformInitOk); | 
|                     UniversalUtil.sendMessageToUnity(m_Message); | 
|                 } | 
|                 else if (s.equalsIgnoreCase("fail")) | 
|                 { | 
|                     m_Message.put("code", CodeA2U.FreePlatformInitFail); | 
|                     UniversalUtil.sendMessageToUnity(m_Message); | 
|                 } | 
|             } | 
|         }); | 
|   | 
|         SFOnlineHelper.setLoginListener(activity, new SFOnlineLoginListener() | 
|         { | 
|             @Override | 
|             public void onLogout(Object o) | 
|             { | 
|                 m_Message.clear(); | 
|                 //需要把游戏切换回登陆前的场景,并重新弹出登录框等操作 | 
|                 m_Message.put("code", CodeA2U.FreePlatformLogoutOk); | 
|                 UniversalUtil.sendMessageToUnity(m_Message); | 
|             } | 
|   | 
|             @Override | 
|             public void onLoginSuccess(SFOnlineUser sfOnlineUser, Object o) | 
|             { | 
|                 m_Message.clear(); | 
|                 USER = sfOnlineUser; | 
|   | 
|                 //根据回调获取用户信息 | 
|                 String timestamp = USER.getToken(); | 
|                 String token = USER.getToken(); | 
|                 String userId = USER.getChannelUserId(); | 
|                 try | 
|                 { | 
|                     JSONObject _info = new JSONObject(); | 
|                     _info.put("account", userId); | 
|                     _info.put("token", token); | 
|                     _info.put("timeStamp", timestamp); | 
|                     _info.put("account_id", userId); | 
|                     _info.put("yjSdkId", USER.getChannelId()); | 
|                     _info.put("yjAppId", USER.getProductCode()); | 
|                     m_Message.put("code", CodeA2U.FreePlatformLoginOk); | 
|                     m_Message.put("info", _info); | 
|                     UniversalUtil.sendMessageToUnity(m_Message); | 
|                 } catch (JSONException e) | 
|                 { | 
|                     e.printStackTrace(); | 
|                 } | 
|             } | 
|   | 
|             @Override | 
|             public void onLoginFailed(String s, Object o) | 
|             { | 
|                 m_Message.clear(); | 
|                 m_Message.put("code", CodeA2U.FreePlatformLoginFail); | 
|                 UniversalUtil.sendMessageToUnity(m_Message); | 
|             } | 
|         }); | 
|     } | 
|   | 
|     public void login(final Activity activity) | 
|     { | 
|         String _appid = UniversalUtil.getMetaString(activity, "yj_appid"); | 
|         if (_appid.equals("yybgame")) | 
|         { | 
|             SharedPreferences _sp = activity.getPreferences(Context.MODE_PRIVATE); | 
|             String _loginType = _sp.getString(StaticDefine.LS_KEY_TENCENT_LOGINPARAM, ""); | 
|             if(_loginType.length() > 0) | 
|             { | 
|                 SFOnlineHelper.login(activity, _loginType); | 
|             } | 
|         } | 
|         else | 
|         { | 
|             SFOnlineHelper.login(activity, "Login"); | 
|         } | 
|     } | 
|   | 
|     public void loginTencent(final Activity activity, String param) | 
|     { | 
|         SFOnlineHelper.login(activity, param); | 
|   | 
|         SharedPreferences _sp = activity.getPreferences(Context.MODE_PRIVATE); | 
|         SharedPreferences.Editor _editor = _sp.edit(); | 
|         _editor.putString(StaticDefine.LS_KEY_TENCENT_LOGINPARAM, param); | 
|         _editor.apply(); | 
|     } | 
|   | 
|     public void logout(final Activity activity) | 
|     { | 
|         SFOnlineHelper.logout(activity, "LoginOut"); | 
|     } | 
|   | 
|     public void charge(final Activity activity, String productName, float price, String extData) | 
|     { | 
|         SFOnlineHelper.charge(activity, | 
|                               productName, | 
|                               (int) (price * 100), | 
|                               1, extData, | 
|                               PAY_CALLBACK_URL, | 
|                               new SFOnlinePayResultListener() | 
|                               { | 
|                                   @Override | 
|                                   public void onFailed(String s) | 
|                                   { | 
|                                       m_Message.clear(); | 
|                                       //根据回调获取支付订单信息 | 
|                                       LogUtil.i(TAG, "Pay Fail Info: " + s); | 
|                                       m_Message.put("code", CodeA2U.FreePlatformPayFail); | 
|                                       UniversalUtil.sendMessageToUnity(m_Message); | 
|                                       payProcessing = false; | 
|                                   } | 
|   | 
|                                   @Override | 
|                                   public void onSuccess(String s) | 
|                                   { | 
|                                       m_Message.clear(); | 
|                                       //根据回调获取支付订单信息 | 
|                                       LogUtil.i(TAG, "Pay Success Info: " + s); | 
|                                       m_Message.put("code", CodeA2U.FreePlatformPayOk); | 
|                                       UniversalUtil.sendMessageToUnity(m_Message); | 
|                                       payProcessing = false; | 
|                                   } | 
|   | 
|                                   @Override | 
|                                   public void onOderNo(String s) | 
|                                   { | 
|                                       LogUtil.i(TAG, "Pay Order No: " + s); | 
|                                   } | 
|                               }); | 
|         payProcessing = true; | 
|     } | 
|   | 
|     public void payExtend(final Activity activity, | 
|                           String productName, | 
|                           String productID, | 
|                           float price, | 
|                           String extData) | 
|     { | 
|         SFOnlineHelper.payExtend(activity, (int) (price * 100), | 
|                                  productName, productID, extData, 1, "", PAY_CALLBACK_URL, | 
|                                  new SFOnlinePayResultListener() | 
|                                  { | 
|                                      @Override | 
|                                      public void onFailed(String s) | 
|                                      { | 
|                                          m_Message.clear(); | 
|                                          //根据回调获取支付订单信息 | 
|                                          LogUtil.i(TAG, "Pay Fail Info: " + s); | 
|                                          m_Message.put("code", CodeA2U.FreePlatformPayFail); | 
|                                          UniversalUtil.sendMessageToUnity(m_Message); | 
|                                          payProcessing = false; | 
|                                      } | 
|   | 
|                                      @Override | 
|                                      public void onSuccess(String s) | 
|                                      { | 
|                                          m_Message.clear(); | 
|                                          //根据回调获取支付订单信息 | 
|                                          LogUtil.i(TAG, "Pay Success Info: " + s); | 
|                                          m_Message.put("code", CodeA2U.FreePlatformPayOk); | 
|                                          UniversalUtil.sendMessageToUnity(m_Message); | 
|                                          payProcessing = false; | 
|                                      } | 
|   | 
|                                      @Override | 
|                                      public void onOderNo(String s) | 
|                                      { | 
|                                          LogUtil.i(TAG, "Pay Order No: " + s); | 
|                                      } | 
|                                  }); | 
|         payProcessing = true; | 
|     } | 
|   | 
|     public void pay(final Activity activity, | 
|                     String productName, | 
|                     String productID, | 
|                     float price, | 
|                     String extData) | 
|     { | 
|         SFOnlineHelper.pay(activity, | 
|                            (int) (price * 100), | 
|                            productName, | 
|                            1, | 
|                            extData, | 
|                            PAY_CALLBACK_URL, | 
|                            new SFOnlinePayResultListener() | 
|                            { | 
|                                @Override | 
|                                public void onFailed(String s) | 
|                                { | 
|                                    m_Message.clear(); | 
|                                    //根据回调获取支付订单信息 | 
|                                    LogUtil.i(TAG, "Pay Fail Info: " + s); | 
|                                    m_Message.put("code", CodeA2U.FreePlatformPayFail); | 
|                                    UniversalUtil.sendMessageToUnity(m_Message); | 
|                                    payProcessing = false; | 
|                                } | 
|   | 
|                                @Override | 
|                                public void onSuccess(String s) | 
|                                { | 
|                                    m_Message.clear(); | 
|                                    //根据回调获取支付订单信息 | 
|                                    LogUtil.i(TAG, "Pay Success Info: " + s); | 
|                                    m_Message.put("code", CodeA2U.FreePlatformPayOk); | 
|                                    UniversalUtil.sendMessageToUnity(m_Message); | 
|                                    payProcessing = false; | 
|                                } | 
|   | 
|                                @Override | 
|                                public void onOderNo(String s) | 
|                                { | 
|                                    LogUtil.i(TAG, "Pay Order No: " + s); | 
|                                } | 
|                            }); | 
|         payProcessing = true; | 
|     } | 
|   | 
|     public void createRole(final Activity activity, | 
|                            String serverID, | 
|                            String serverName, | 
|                            String roleID, | 
|                            String roleName, | 
|                            String roleLevel, | 
|                            String FamilyName, | 
|                            String createTime, | 
|                            String vipLevel, | 
|                            String surplusMoney) | 
|     { | 
|         SFOnlineHelper.setRoleData(activity, roleID, roleName, roleLevel, "1", "默认分区"); | 
|         String _info = createGameData(serverID, serverName, roleID, roleName, roleLevel, FamilyName, | 
|                                       createTime, "", vipLevel, surplusMoney); | 
|         SFOnlineHelper.setData(activity, "createrole", _info); //  创建新角色时调用       必接 | 
|     } | 
|   | 
|     public void enterWorld(Activity activity, | 
|                            String serverID, | 
|                            String serverName, | 
|                            String roleID, | 
|                            String roleName, | 
|                            String roleLevel, | 
|                            String FamilyName, | 
|                            String vipLevel, | 
|                            String surplusMoney) | 
|     { | 
|         SFOnlineHelper.setRoleData(activity, roleID, roleName, roleLevel, "1", "默认分区"); | 
|         String _info = createGameData(serverID, serverName, roleID, roleName, roleLevel, FamilyName, | 
|                                       "", "", vipLevel, surplusMoney); | 
|         SFOnlineHelper.setData(activity, "enterServer", _info); //  创建新角色时调用       必接 | 
|     } | 
|   | 
|     public void levelUp(final Activity activity, | 
|                         String serverID, | 
|                         String serverName, | 
|                         String roleID, | 
|                         String roleName, | 
|                         String roleLevel, | 
|                         String FamilyName, | 
|                         String createTime, | 
|                         String levelUpTime, | 
|                         String vipLevel, | 
|                         String surplusMoney) | 
|     { | 
|         String _info = createGameData(serverID, serverName, roleID, roleName, roleLevel, FamilyName, | 
|                                       createTime, levelUpTime, vipLevel, surplusMoney); | 
|         SFOnlineHelper.setData(activity, "levelup", _info); //  创建新角色时调用       必接 | 
|     } | 
|   | 
|     private String createGameData(String serverID, | 
|                                   String serverName, | 
|                                   String roleID, | 
|                                   String roleName, | 
|                                   String roleLevel, | 
|                                   String FamilyName, | 
|                                   String createTime, | 
|                                   String levelUpTime, | 
|                                   String vipLevel, | 
|                                   String surplusMoney) | 
|     { | 
|         try | 
|         { | 
|             JSONObject roleInfo = new JSONObject(); | 
|             roleInfo.put("roleId", roleID);         //当前登录的玩家角色ID,必须为数字 | 
|             roleInfo.put("roleName", roleName);  //当前登录的玩家角色名,不能为空,不能为null | 
|             roleInfo.put("roleLevel", roleLevel);   //当前登录的玩家角色等级,必须为数字,且不能为0,若无,传入1 | 
|             roleInfo.put("zoneId", serverID);       //当前登录的游戏区服ID,必须为数字,且不能为0,若无,传入1 | 
|             roleInfo.put("zoneName", serverName);//当前登录的游戏区服名称,不能为空,不能为null | 
|             roleInfo.put("balance", surplusMoney);   //用户游戏币余额,必须为数字,若无,传入0 | 
|             roleInfo.put("vip", vipLevel);            //当前用户VIP等级,必须为数字,若无,传入1 | 
|             roleInfo.put("partyName", FamilyName);//当前角色所属帮派,不能为空,不能为null,若无,传入“无帮派” | 
|             if (createTime.length() > 0) | 
|             { | 
|                 roleInfo.put("roleCTime", createTime);     //单位为秒,创建角色的时间 | 
|             } | 
|             if (levelUpTime.length() > 0) | 
|             { | 
|                 roleInfo.put("roleLevelMTime", levelUpTime);    //单位为秒,角色等级变化时间 | 
|             } | 
|             return roleInfo.toString(); | 
|         } catch (JSONException ex) | 
|         { | 
|             ex.printStackTrace(); | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     public void SendRegisterEvent() | 
|     { | 
|     } | 
| } |