Skip to content

JS SDK 接入文档

资源获取

  • 获取接入游戏(产品)配置文件(此文件名固定,不可随意修改):access.config.js
  • 下载 JS SDK 下载

环境准备

  • 引入 JS SDK access-es.js 文件
  • 引入游戏配置文件 access.config.js(需要和 sdk 处于同级目录)

接口说明

引入

javascript
// @ts-ignore
import { Fastsdk } from "xxxx/config/access-es";

设置回调代理

javascript
const delegate = {
    onInitSuccess: function (initInfo: any): void {    
      console.log('初始化成功:' + JSON.stringify(initInfo))
    },
    onInitFailed: function (reason: string): void {
      console.log('初始化失败:' + reason)
    },
    onLoginSuccess: function (uInfo: any): void {
      // {
      //   "uid": "用户hoolai id"
      //    "channel": "",
      //    "accessToken": "",
      //    "optInfo": {
      //           "nickname":"昵称",
      //           "headimgurl":"头像",
      //           "sex":"性别",
      //           "language":"语言",
      //           "city":"城市",
      //           "province":"省",
      //           "country":"国家",
      //           "unionid":"平台ID",
      //        }
      // }
      //
      console.log('登录成功:' + JSON.stringify(uInfo))          
    },
    onLoginFailed: function (reason: string): void {
      console.log('登录失败:' + reason)
    },
    // 原支付成功返回订单id加密信息,string类型, 解密后为原始Hoolai订单号
    // 新回调数据返回string类型, 解密后数据格式
    //{
    //    "orderId": "xxxx",
    //    "callbackInfo": "xxxx",
    //    "amount": "xxxx",
    //    "itemName": "xxxx",
    //}
    onPaySuccess: function (info: string): void {
      // 支付成功返回加密订单ID,需要获取密钥及解密方式    
      console.log('支付成功', info)
    },
    onPayFailed: function (reason: string): void {
      console.log('支付失败:' + reason)    
    },
    onQuery: function (result: boolean, info: string): void {
      if (result) {
          //获取成功
          //{
          //    "uid": "用户uid"
          //}
          console.log('获取信息成功', info)
      } else {
          console.log('获取信息失败', info)
      }
    }
}
Fastsdk.configDelegate(delegate)

生命周期接口

  • 此生命周期在非首次会检测登录状态信息,如果存在会自动重定到向上次同渠道的地址
javascript
//

// vue 处理
onMounted(() => {
    Fastsdk.onMounted()      
})

// 普通js处理方式
$(document).ready(function() {
    Fastsdk.onMounted()
})

// 其他语言参考以上语言自行实现

初始化接口

javascript
Fastsdk.init()

登录接口

javascript
Fastsdk.login()

支付接口

javascript
//payInfo 为普通对象
// 参数说明
// productId: number类型,产品ID(sdk后台游戏id)
// channelId: number类型,渠道ID
// price: number类型,商品价格(必传)
// currency: string类型,货币(非必传)
// goodsId: string类型,商品ID(...)
// goodsName: string类型,商品名称(...)
// gameInfo: string类型,游戏透传参数,(必传)
// notifyUrl: string类型,支付发货地址,(非必传)
Fastsdk.pay(payInfo);

订单号加密

SDK 会添加一个加密后的订单号及透传参数返回,以便校验正确性。加密方式采用 AES,秘钥请联系 SDK 后端人员获取,解密代码参考如下:

java
public static String Decrypt(String sSrc, String sKey) throws Exception {
    try {
        if (sSrc == null || sSrc.length() == 0 || sSrc.trim().length() == 0) {
            return null;
        }
        if (sKey == null) {
            throw new Exception("decrypt key is null");
        }
        if (sKey.length() != 16) {
            throw new Exception("decrypt key length error");
        }
        byte[] raw = sKey.getBytes(charset);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] encrypted1 = hex2byte(sSrc);
        try {
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, charset);
            return originalString;
        } catch (Exception e) {
            throw new Exception("decrypt errot", e);
        }
    } catch (Exception ex) {
        throw new Exception("decrypt errot", ex);
    }
}

public static byte[] hex2byte(String strhex) {
    if (strhex == null) {
        return null;
    }
    int l = strhex.length();
    if (l % 2 == 1) {
        return null;
    }
    byte[] b = new byte[l / 2];
    for (int i = 0; i != l / 2; i++) {
        b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
                16);
    }
    return b;
}

用户信息查询接口

javascript
// queryInfo 为Object类型
// {
//   "productId": xxxx //number类型,游戏产品id
//   "type": "xxxx"    //string类型,查询类型Phone/RoleId (仅支持两种)
//   "flag": "xxxx".   //string类型, 查询标记值
//}
Fastsdk.queryInfo(queryInfo)

选服接口

javascript
// 支付之前需要调用
// serverId:服务器id
Fastsdk.selectServer(serverId)

Hoolai Access SDK