chsdk_inside.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. declare namespace chsdk {
  2. /**日志等级*/
  3. enum loglevel {
  4. OFF = 0,
  5. ERROR = 1,
  6. WARN = 2,
  7. DEBUG = 3,
  8. INFO = 4,
  9. ALL = 5
  10. }
  11. /**上报类型*/
  12. export enum reportType {
  13. /**默认不上报*/
  14. off = 0,
  15. /**使用ch服务器*/
  16. ch = 1,
  17. /**上报到应用平台(需在各平台后台设置上报事件,微信,抖音)*/
  18. platform = 2,
  19. /**上面两个都选*/
  20. ch__platform = 3
  21. }
  22. /**平台*/
  23. export enum pf {
  24. /**测试*/
  25. web = "web",
  26. /**微信*/
  27. wx = "wx",
  28. /**抖音*/
  29. tt = "tt"
  30. }
  31. /**获取token*/
  32. export function getToken(): string;
  33. export function init_inside(gid: string, loglevel: number, serverIP: number | string, isLocal?: boolean, report?: reportType): Promise<{
  34. code: number;
  35. err?: string;
  36. data?: any;
  37. }>;
  38. /**
  39. * 获取基本功能完成接口地址
  40. * @param action 接口字符串如 '/user/shareList'
  41. * @returns
  42. */
  43. export function getUrl(action: string): string;
  44. /**
  45. * 获取上报功能完成接口地址
  46. * @param action 接口字符串如 '/user/shareList'
  47. * @returns
  48. */
  49. export function getReportUrl(action: string): string;
  50. export function check_req_time(req_type: string): {
  51. code: number;
  52. err: string;
  53. } | null;
  54. export function makePostTokenRequest(url: string, body?: any): Promise<{
  55. code: number;
  56. err?: string;
  57. data?: any;
  58. }>;
  59. export function base64_encode(input: string): string;
  60. export function base64_decode(input: string): string;
  61. /**
  62. * md5加密
  63. */
  64. export function md5HashStr(str: string): string;
  65. /**
  66. * 一次http请求
  67. * @param url 请求地址
  68. * @param method 请求方式
  69. * @param data
  70. * @param timeout
  71. * @param responseType
  72. * @param headers
  73. * @returns
  74. */
  75. export function do_request(url: string, method: 'POST' | 'GET' | 'PUT', data?: any, timeout?: number, responseType?: XMLHttpRequestResponseType, headers?: {
  76. [key in string]: string;
  77. }): Promise<any>;
  78. /**验证权限功能
  79. * key 某个功能权限
  80. * 返回 是否验证成功
  81. */
  82. export function verify_option(key: string): boolean;
  83. /**获取一个唯一标识的字符串 */
  84. export function guid(): string;
  85. /**判断是否有名字,没有返回玩家*/
  86. export function getDefNickName(info: any): string;
  87. /**
  88. * 主动记录分数到缓存
  89. * @param rankType 排行榜类型
  90. * @param update 更新类型
  91. * @param type 0 个人排行 1地区排行
  92. * @param score 分数
  93. */
  94. export function recordCacheScore(rankField: string, update: number, type: 0 | 1, score: number): void;
  95. /**
  96. * 获取缓存中的分数
  97. * @param rankField
  98. * @param update
  99. * @param type
  100. * @returns
  101. */
  102. export function getCacheScore(rankField: string, update: number, type: 0 | 1): number;
  103. /**清空所有缓存分数*/
  104. export function cleanCacheScore(): void;
  105. /**清空所有缓存排行榜数据*/
  106. export function cleanCacheRank(): void;
  107. /**当前平台是否有分享功能*/
  108. export function canShareAppMessage(): boolean;
  109. /**
  110. * 主动拉起分享转发
  111. */
  112. export function shareAppMessage(title?: string, imageUrlId?: string, imageUrl?: string, message?: string): void;
  113. /**
  114. * 主动拉起分享转发并等待是错分享成功
  115. */
  116. export function shareAppMessageAsync(title?: string, imageUrlId?: string, imageUrl?: string, message?: string): Promise<boolean>;
  117. /**设置被动分享参数,具体参看平台的参数设置*/
  118. export function setOnShareAppMessage(res: {}): void;
  119. export function getQuery(): any;
  120. /**
  121. * @description 上传从分享进入(无需主动调用,除非需要在message里加入游戏特别数据)
  122. * @param openid 服务器拿到的玩家oid 如果为空从平台拿
  123. * @param message 分享的自定义数据
  124. */
  125. export function sendShare(openid?: string | null, message?: string | null): Promise<{
  126. code: number;
  127. err?: string;
  128. data?: any;
  129. }>;
  130. /**
  131. * @description 获取从自己分享进入游戏的玩家列表
  132. */
  133. export function getShareList(): Promise<{
  134. code: number;
  135. err?: string;
  136. data?: {
  137. gid: string;
  138. head: string;
  139. hid: number;
  140. ip: string;
  141. loginTime: number;
  142. nickName: string;
  143. openId: string;
  144. option: string;
  145. pf: string;
  146. registerTime: number;
  147. userId: number;
  148. msg?: string;
  149. }[];
  150. }>;
  151. /**播放奖励广告
  152. * @param scene 奖励目的场景
  153. playRewardAd("复活")
  154. .then(success => {
  155. if (success) {
  156. console.log("广告播放成功,用户获得奖励");
  157. // 在这里执行成功后的操作
  158. } else {
  159. console.log("广告播放失败,用户未获得奖励");
  160. // 处理广告播放失败的逻辑
  161. }
  162. })
  163. .catch(error => {
  164. console.error("发生未处理的错误: ", error);
  165. });
  166. */
  167. export function playRewardAd(scene: string): Promise<boolean>;
  168. /**播放插屏广告*/
  169. export function playInsterAd(scene: string): Promise<boolean>;
  170. /**设置广告配置*/
  171. export function setConf(pf: pf, conf: {
  172. adUnitId: string;
  173. multiton: boolean;
  174. inster_unitId?: string;
  175. tmplIds?: string[];
  176. }): void;
  177. /**当前平台是否有侧边栏功能*/
  178. export function checkHasSidebar(): boolean;
  179. /**抖音是否从侧边栏进入游戏,微信是否从我的小程序进入游戏*/
  180. export function checkFromSidebar(): boolean;
  181. /**抖音进入侧边栏*/
  182. export function goToSidebar(): boolean;
  183. /**开始录屏*/
  184. export function recorderStart(duration?: number): void;
  185. /**结束录屏*/
  186. export function recorderStop(): void;
  187. /**分享录屏*/
  188. export function shareRecord(title?: string, desc?: string, path?: string, topics?: string[]): void;
  189. /**创建反馈按钮
  190. * @param op 按纽坐标大小
  191. * 例:feed_node.active = ch.sdk.createFeedbackButton(ch.util.getBtnOp(feed_node));
  192. * 如果对应平台有此功能(目前只有wx)返回true,没有返回false
  193. * 成功记得 destoryFeedbackButton();*/
  194. export function createFeedbackButton(op: {
  195. left: number;
  196. top: number;
  197. width: number;
  198. height: number;
  199. }): boolean;
  200. /**销毁反馈按钮*/
  201. export function destoryFeedbackButton(): void;
  202. /**复制到剪切板*/
  203. export function setClipboardData(content: string): void;
  204. /**短震动(15ms)*/
  205. export function vibrateShort(): void;
  206. /**长震动(400ms)*/
  207. export function vibrateLong(): void;
  208. /**界面提示信息*/
  209. export function showToast(title: string, duration?: number, icon?: 'success' | 'error' | 'fail' | 'loading' | 'none'): boolean;
  210. /**获取菜单坐标范围*/
  211. export function getMenuButtonBoundingClientRect(): any;
  212. export function showLoading(title?: string): boolean;
  213. export function hideLoading(): void;
  214. export function showModal(title?: string, content?: string, confirmText?: string, showCancel?: boolean, cancelText?: string): Promise<{
  215. confirm: boolean;
  216. cancel: boolean;
  217. }>;
  218. export function openSetting(scope: string | 'scope.userInfo' | 'scope.userLocation'): Promise<boolean>;
  219. /**加载图片
  220. * cocos里不要直接调用,使用 ch.util.loadImage(url);
  221. */
  222. export function loadImage(imgURL: string): Promise<any>;
  223. /**当前平台是否有关注功能*/
  224. export function canAwemeUserProfile(): boolean;
  225. /**检查是否已经关注*/
  226. export function checkFollowAwemeState(): Promise<boolean>;
  227. /**打开关注返回关注结果*/
  228. export function openAwemeUserProfile(): Promise<boolean>;
  229. /**当前平台是否有订阅功能*/
  230. export function canSubscribeMessage(): boolean;
  231. /**订阅消息 参数为空的话使用配置里的模板id*/
  232. export function requestSubscribeMessage(tmplIds?: string[]): Promise<boolean>;
  233. class ch_log {
  234. private static _instance;
  235. static getInstance(): ch_log;
  236. private _log_level;
  237. set_log_level(loglevel: loglevel): void;
  238. private _no;
  239. private _log;
  240. private _info;
  241. private _debug;
  242. private _trace;
  243. private _warn;
  244. private _error;
  245. private _log_start;
  246. private _log_end;
  247. get log(): (message?: any, ...optionalParams: any[]) => void;
  248. get info(): (message?: any, ...optionalParams: any[]) => void;
  249. get debug(): (message?: any, ...optionalParams: any[]) => void;
  250. get trace(): (message?: any, ...optionalParams: any[]) => void;
  251. get warn(): (message?: any, ...optionalParams: any[]) => void;
  252. get error(): (message?: any, ...optionalParams: any[]) => void;
  253. /**记录开始计时*/
  254. get log_start(): (label?: string) => void;
  255. /** 打印范围内时间消耗*/
  256. get log_end(): (label?: string) => void;
  257. }
  258. /**日志打印*/
  259. export const log: ch_log;
  260. /**事件*/
  261. export interface EventsMap {
  262. [event: string]: (...args: any[]) => void;
  263. }
  264. export type EventNames<Map extends EventsMap> = keyof Map & string;
  265. export type OmitIndex<T> = {
  266. [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
  267. };
  268. export type EventParams<Map extends EventsMap, Ev extends EventNames<Map>> = Parameters<Map[Ev]>;
  269. class EventModel<CT extends OmitIndex<EventsMap>> {
  270. private _handlersMap;
  271. /** 事件键 */
  272. key: {
  273. [k in EventNames<CT>]: k;
  274. };
  275. /**
  276. * 监听事件
  277. * @param type_ 事件类型
  278. * @param callback_ 触发回调
  279. * @param target_ 事件目标对象
  280. * @param once_b_ 是否触发单次
  281. * @returns 触发回调
  282. */
  283. on<T extends EventNames<CT>, T2 extends (...event_: EventParams<CT, T>) => void>(type_: T, callback_: T2, target_?: any, once_?: boolean): typeof callback_ | null;
  284. once<T extends EventNames<CT>, T2 extends (...event_: EventParams<CT, T>) => void>(type_: T, callback_: T2, target_?: any): void;
  285. /**
  286. * 取消监听事件
  287. * @param type_ 事件类型
  288. * @param callback_ 触发回调
  289. * @param target_ 事件目标对象
  290. * @returns 触发回调
  291. */
  292. off<T extends EventNames<CT>, T2 extends (...event_: EventParams<CT, T>) => void>(type_: T, callback_?: T2, target_?: any): void;
  293. /**
  294. * 清除某个事件队列
  295. * @param type_ 事件名,为空的话清除所有
  296. */
  297. clearAll<T extends EventNames<CT>>(type_?: T): void;
  298. /**
  299. * 派发事件
  300. * @param type_ 事件类型
  301. * @param args_ 事件参数
  302. */
  303. emit<T extends EventNames<CT>, T2 extends EventParams<CT, T>>(type_: T, ...args_: T2): void;
  304. private _emit;
  305. }
  306. /**sdk事件*/
  307. export interface event_sdk {
  308. show(): void;
  309. hide(): void;
  310. onShare(success: boolean): void;
  311. }
  312. export const sdk_event: EventModel<event_sdk>;
  313. export function get_new_event<CT>(): EventModel<CT>;
  314. /**日期时间*/
  315. class ch_date {
  316. private static _instance;
  317. static getInstance(): ch_date;
  318. private _day_s;
  319. private _diff;
  320. updateServerTime(s: number): void;
  321. now(): number;
  322. getTime(): number;
  323. getDayStartTime(s: number): number;
  324. getDayEndTime(e: number): number;
  325. getWeekEndTime(e: number): number;
  326. getMonthEndTime(e: number): number;
  327. /**两个时间相隔天数*/
  328. getDiffDayNum(e: number, t: number): number;
  329. /**判定两个时间是否是同一天*/
  330. isSameDate(timestamp1: number, timestamp2: number): boolean;
  331. /**
  332. * 单位毫秒格式化
  333. * - $H: 替换为小时,补全空位(02:00:00)
  334. * - $h: 替换为小时,不补全(2:00:00)
  335. * - $M: 替换为分钟,补全空位(00:02:00)
  336. * - $m: 替换为分钟,不补全(00:2:00)
  337. * - $S: 替换为秒,补全空位(00:00:02)
  338. * - $s: 替换为秒,不补全(0:00:2)
  339. */
  340. ms_format(ms_n_: number, format_s_?: string): string;
  341. }
  342. export const date: ch_date;
  343. /**本地存储*/
  344. class ch_storage {
  345. private static _instance;
  346. static getInstance(): ch_storage;
  347. /**
  348. * 缓存变量存储
  349. * @param key
  350. * @param value
  351. * @param user_id 区别用户
  352. */
  353. set(key: string, value: any, user_id?: string | null): any;
  354. /**
  355. * 获取缓存变量区别用户
  356. * @param {*} key
  357. * @returns
  358. */
  359. private get;
  360. /** 获取指定关键字的string*/
  361. getString(key: string, user_id?: string | null): string | null;
  362. /** 获取指定关键字的数值 */
  363. getNumber(key: string, user_id?: string | null, defaultValue?: number): number;
  364. /** 获取指定关键字的布尔值*/
  365. getBoolean(key: string, user_id?: string | null): boolean;
  366. /** 获取指定关键字的JSON对象*/
  367. getJson(key: string, user_id?: string | null): any;
  368. /** 获取指定关键字的JSON对象*/
  369. getObject(key: string, user_id?: string | null): any;
  370. /**
  371. * 删除缓存变量
  372. * @param {*} key
  373. */
  374. remove(key: string, user_id?: string | null): any;
  375. /** 清空整个本地存储 */
  376. clear(): void;
  377. }
  378. /**本地存储*/
  379. export const storage: ch_storage;
  380. export { };
  381. }