liangliangSDK.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { sys } from "cc";
  2. export class LiangLiangSDK
  3. {
  4. static wx:any;
  5. static currentSDK = "1.0.2";
  6. static gameID = 10003;
  7. static gameName="HaiDaoBaoWei";
  8. static userID;
  9. static token;
  10. static _ok = false;
  11. static _initOK = false;
  12. static _loginOK = false;
  13. static _listOK = false;
  14. static _config ;
  15. private static _checkOK()
  16. {
  17. if(LiangLiangSDK._initOK &&LiangLiangSDK._loginOK && LiangLiangSDK._listOK )
  18. {
  19. LiangLiangSDK._ok = true;
  20. }
  21. }
  22. /**
  23. * 初始化
  24. * @param successcallback
  25. * @param failcallback
  26. */
  27. static Init(successcallback,failcallback)
  28. {
  29. this.wx=window["wx"];
  30. LiangLiangSDK.HttpRequest("https://www.zhiyuonline.com/api/getGameInfo",{"gameId": this.gameID,"version":this.currentSDK},"POST",(result)=>
  31. {
  32. console.log(result);
  33. LiangLiangSDK._config = result.data;
  34. LiangLiangSDK._initOK = true;
  35. LiangLiangSDK._checkOK();
  36. this.Login(()=>{
  37. console.log("登录成功");
  38. this.LoadIconDataList(()=>{
  39. console.log("加载互导数据成功:"+this._data);
  40. successcallback();
  41. },()=>{
  42. console.log("加载互导数据失败");
  43. successcallback();
  44. })
  45. },()=>{
  46. console.log("登录失败");
  47. failcallback();
  48. })
  49. },(fail)=>
  50. {
  51. console.log("fail : " + fail)
  52. failcallback();
  53. })
  54. }
  55. static Login(successcallback,failcallback)
  56. {
  57. let self = this;
  58. if(this.wx)
  59. {
  60. this.wx.login({
  61. success (res)
  62. {
  63. if (res.code)
  64. {
  65. let option=this.wx.getLaunchOptionsSync();
  66. if(option==null){
  67. throw new Error("调用 wx.getLaunchOptionsSync 获取数据为空!");
  68. }
  69. let channelId = 0;
  70. if (option.query && JSON.stringify(option.query) != "{}") {
  71. if (option.query.channel_id) {
  72. channelId = Number(option.query.channel_id);
  73. }
  74. }
  75. LiangLiangSDK.HttpRequest("https://www.zhiyuonline.com/user/login",{"gameId": this.gameID,"code":res.code,"channelId" : channelId},"POST",(result)=>
  76. {
  77. console.log(result);
  78. self.userID = result.data.userId;
  79. self.token = result.data.token;
  80. LiangLiangSDK._loginOK = true;
  81. LiangLiangSDK._checkOK();
  82. successcallback();
  83. },(fail)=>
  84. {
  85. console.log("fail : " + fail)
  86. failcallback();
  87. })
  88. }
  89. else
  90. {
  91. console.log('登录失败!' + res.errMsg)
  92. failcallback();
  93. }
  94. }
  95. })
  96. }
  97. else
  98. {
  99. LiangLiangSDK._loginOK = true;
  100. LiangLiangSDK._checkOK();
  101. }
  102. }
  103. static _data:any[];
  104. private static _errorIndex:number=0;
  105. private static _errorTotal:number=3;
  106. static LoadIconDataList(successcallback,failcallback)
  107. {
  108. this._LoadIconDataList(()=>{
  109. successcallback();
  110. },()=>{
  111. if(this._errorIndex<this._errorTotal){
  112. this.LoadIconDataList(successcallback,failcallback);
  113. this._errorIndex++;
  114. }else{
  115. failcallback();
  116. }
  117. });
  118. }
  119. private static _LoadIconDataList(successcallback,failcallback):void{
  120. if(this.wx!=null)
  121. {
  122. var url = "https://www.maoxingame.com/"+this.gameName+"/wx_server/xl_sdk_resources/xl_channel_app_config.json";
  123. cc.loader.load(url, function (err, data)
  124. {
  125. if(err)
  126. {
  127. console.error(err)
  128. failcallback();
  129. }
  130. else
  131. {
  132. LiangLiangSDK._listOK = true;
  133. LiangLiangSDK._checkOK();
  134. LiangLiangSDK._data = data;
  135. successcallback();
  136. }
  137. console.log(data);
  138. });
  139. }
  140. else
  141. {
  142. LiangLiangSDK._listOK = true;
  143. LiangLiangSDK._checkOK();
  144. LiangLiangSDK._data = [{
  145. app_id:"",
  146. skipIcon:true,
  147. name:"一个测试名字"
  148. }];
  149. }
  150. }
  151. static HttpRequest(url, data, method, success, fail)
  152. {
  153. /* if(CC_WECHATGAME1)
  154. {
  155. wx.request({
  156. url: url, //仅为示例,并非真实的接口地址
  157. data: data,
  158. header: {
  159. 'content-type': 'application/json' // 默认值
  160. },
  161. success (res) {
  162. console.log(res.data)
  163. },
  164. fail(res)
  165. {
  166. console.log("HttpRequest Fail" + res);
  167. }
  168. })
  169. }
  170. else*/
  171. {
  172. var xhr = new XMLHttpRequest();
  173. xhr.onreadystatechange = function () {
  174. if (xhr.readyState == 4) {
  175. var response = xhr.responseText;
  176. if (xhr.status >= 200 && xhr.status < 400) {
  177. var result = {};
  178. if(response)
  179. {
  180. try {
  181. result = JSON.parse(response)
  182. } catch (e) {
  183. console.error('json parse error ', response)
  184. if (fail)
  185. fail(e);
  186. }
  187. }
  188. if (success)
  189. success(result);
  190. } else {
  191. console.error('error ', response)
  192. if (fail)
  193. fail(response);
  194. }
  195. } else {
  196. }
  197. };
  198. xhr.timeout = 3000;
  199. xhr.ontimeout = function (event) {
  200. console.error('error ', event)
  201. if (fail)
  202. fail(event);
  203. }
  204. xhr.open(method, url, true);
  205. if (method == "POST") {
  206. xhr.open('POST', url);
  207. xhr.setRequestHeader("Content-Type", "application/json");
  208. xhr.send(JSON.stringify(data));
  209. } else {
  210. xhr.send();
  211. }
  212. }
  213. }
  214. static object2Query(obj)
  215. {
  216. var args = []
  217. for (var k in obj)
  218. args.push(k + "=" + obj[k])
  219. return args.join("&"); // 返回对象
  220. }
  221. static CpaReport(data)
  222. {
  223. var body:any = {};
  224. body.gameId = this.gameID;
  225. body.token = LiangLiangSDK.token;
  226. body.cpaId = data.game_id
  227. console.log("开始上报跳转数据" + JSON.stringify(body));
  228. LiangLiangSDK.HttpRequest("https://www.zhiyuonline.com/user/cpaClick",body, "POST",()=>{}, ()=>{});
  229. }
  230. static GetList(count:number):any[]
  231. {
  232. if(!LiangLiangSDK._ok) //如果服务器请求错误 则直接返回一个空的列表
  233. {
  234. return [];
  235. }
  236. var ret = [];
  237. for(var i = 0;i < count;i++)
  238. {
  239. ret.push(LiangLiangSDK._data[Math.floor(Math.random() * LiangLiangSDK._data.length * 0.99999)]);
  240. }
  241. return ret;
  242. }
  243. static CanJumpOut():boolean
  244. {
  245. if(!LiangLiangSDK._config)
  246. {
  247. return false;
  248. }
  249. if(this.currentSDK != LiangLiangSDK._config.version)
  250. {
  251. return false;
  252. }
  253. {
  254. if(!LiangLiangSDK._ok)
  255. {
  256. console.log("不允许跳转1")
  257. return false;
  258. }
  259. if(!LiangLiangSDK._data || LiangLiangSDK._data.length == 0)
  260. {
  261. console.log("不允许跳转2")
  262. return false;
  263. }
  264. console.log("允许跳转")
  265. return true
  266. }
  267. return false;
  268. }
  269. static CanWuChu():boolean
  270. {
  271. if(!LiangLiangSDK._config)
  272. {
  273. return false;
  274. }
  275. if(this.currentSDK != LiangLiangSDK._config.version)
  276. {
  277. return false;
  278. }
  279. if(!LiangLiangSDK._config.mistakeSwitch)
  280. {
  281. return false;
  282. }
  283. //上海forbidSwitch 为true
  284. if(LiangLiangSDK._config.forbidSwitch)
  285. {
  286. return false;
  287. }
  288. return true;
  289. }
  290. }