kuaishou.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * 2021/4/14版本
  3. */
  4. class kuaishou {
  5. ////////////////////////////
  6. // 类成员
  7. ///////////////////////////
  8. public static readonly _instance = new kuaishou()
  9. /** 奖励视频广告对象 */
  10. private rewardedVideoAd: any = null;
  11. /** 全局唯一的录屏管理器 */
  12. private mediaRecorder: any = null;
  13. /**用于存储成功录制的视频数据 */
  14. private video: any = null;
  15. /** 奖励视频广告id */
  16. private _rewardedVideoAdUnitId = "2300001739_01";
  17. /**快手平台appId */
  18. private appId = "ks685954522284992897";
  19. /**app secret */
  20. private app_secret = ''
  21. /**关注错误码 */
  22. ksError: string = ""
  23. ////////////////////////////
  24. // get、set访问器
  25. ///////////////////////////
  26. public set rewardedVideoAdUnitId(adUnitId: string) {
  27. this._rewardedVideoAdUnitId = adUnitId;
  28. }
  29. /**获取平台基础信息 */
  30. getSystemInfo() {
  31. kwaigame.getSystemInfo({
  32. response: (result) => {
  33. console.log("获取系统信息:" + JSON.stringify(result));
  34. }
  35. });
  36. }
  37. /**同步获取启动参数 */
  38. getLaunchOptionsSync() {
  39. kwaigame.getLaunchOptionsSync()
  40. }
  41. /**初始化, 在游戏启动时调用 */
  42. gameInit() {
  43. kwaigame.init({ "appId": this.appId });
  44. }
  45. /**在合适时机调用,移除快手小游戏加载页面,将游戏界面展示出来。 */
  46. readyGo() {
  47. kwaigame.readyGo()
  48. }
  49. /**登录接口,
  50. * 通过该接口获取的 gameUserId 是快手小游戏的唯一用户id,
  51. * gameToken 是使用快手小游戏服务器API的唯一验证 token。 */
  52. KSlogin(cb: Function) {
  53. kwaigame.login({
  54. success: (result: { gameUserId: string, gameToken: string }) => {
  55. console.log("登录成功:" + JSON.stringify(result));
  56. if (cb) {
  57. cb(result)
  58. }
  59. },
  60. fail: (error: { code, msg: string }) => {
  61. console.log("登录失败:" + JSON.stringify(error));
  62. }
  63. });
  64. }
  65. /**向用户发起授权请求
  66. * 1.如果尚未发起过授权请求,则弹窗询问用户是否同意授权游戏使用某项功能或获取用户的某些数据;
  67. * 2.如果用户之前已经同意授权,则不会出现弹窗,直接返回成功
  68. * 3.如果用户之前已经拒绝授权,则不会再次出现弹窗,直接返回失败。
  69. * scpoe:需要获取权限的scope
  70. */
  71. authorize(cb: Function, failCb: Function) {
  72. // this.login()
  73. kwaigame.authorize({
  74. scope: "scope.userInfo",/**用户信息 */
  75. success: (result) => {
  76. console.log("获取用户信息成功:" + JSON.stringify(result));
  77. cb(result);
  78. },
  79. fail: (error: { code, msg: string }) => {
  80. console.log("授权获取用户信息失败: " + JSON.stringify(error));
  81. failCb();
  82. },
  83. complete: () => {
  84. /**申请授权完成的回调(无论成功失败都会被调用) */
  85. console.log("授权获取用户信息完成");
  86. }
  87. });
  88. }
  89. /**获取用户信息
  90. * 如果在未授权的情况下请求用户信息,会返回默认数据(空昵称、默认头像)
  91. * 1.userName string 昵称
  92. * 2.userHead string 头像
  93. * 3.gender string 性别,M-男,F-女
  94. * 4.userCity string 城市
  95. * 5.age int 年龄
  96. */
  97. KSgetUserInfo(cb: Function) {
  98. kwaigame.getUserInfo({
  99. success: (result: { userName: string, userHead: string, gender: string, userCity, age }) => {
  100. console.log("获取用户信息成功:" + JSON.stringify(result));
  101. cb(result)
  102. },
  103. fail: (error: { code, msg: string }) => {
  104. console.log("获取用户信息失败: " + JSON.stringify(error));
  105. },
  106. complete: () => {
  107. console.log("获取用户信息完成");
  108. }
  109. });
  110. }
  111. /**
  112. * @description 是否支持改API
  113. * @param api
  114. */
  115. private static isSupportedAPI(api: any) {
  116. // kwaigame.isSupport(kwaigame.Support.features.RewardVideo);
  117. // kwaigame.isSupportAsync(kwaigame.Support.features.RewardVideo, {response: (result) => {
  118. // console.log("support reward video: " + result);
  119. // }});
  120. }
  121. /**获取全局激励视频广告组件 */
  122. showRewardedVideoAd(cb: Function) {
  123. let param: any = {};
  124. param.adUnitId = this._rewardedVideoAdUnitId;
  125. this.rewardedVideoAd = kwaigame.createRewardedVideoAd(param);
  126. if (this.rewardedVideoAd) {
  127. this.rewardedVideoAd.show({
  128. success: () => {
  129. console.log("激励视频播放成功");
  130. },
  131. fail: (result) => {
  132. console.log("激励视频播放失败: " + JSON.stringify(result));
  133. }
  134. })
  135. console.log("激励广告组件获取成功");
  136. this.rewardedVideoAd.onClose((result) => {
  137. console.log("激励视频关闭回调: " + JSON.stringify(result));
  138. });
  139. this.rewardedVideoAd.onReward((result) => {
  140. console.log("激励视频奖励回调: " + JSON.stringify(result));
  141. cb()
  142. });
  143. } else {
  144. console.log("激励广告组件获取失败");
  145. }
  146. }
  147. /**
  148. * @description 创建全局录屏组件
  149. */
  150. registerRecordScreenEvent() {
  151. // 创建录屏对象
  152. this.video = null
  153. this.mediaRecorder = kwaigame.createMediaRecorder()
  154. if (this.mediaRecorder === null) {
  155. console.log("返回 null 时表示当前 APP 版本不支持录屏")
  156. return
  157. } else {
  158. // 注册回调, 不关心的回调可以不注册
  159. let that = this
  160. this.mediaRecorder.onStart(function () {
  161. console.log("录频开始成功")
  162. })
  163. this.mediaRecorder.onStop(function (data) {
  164. // 每次成功录制都会生成一个 videoID
  165. that.video = data.videoID
  166. console.log(`录频停止成功 ${JSON.stringify(data)} `)
  167. })
  168. this.mediaRecorder.onError(function (error) {
  169. console.log(`录频过程中发生错误 ${JSON.stringify(error)} `)
  170. })
  171. }
  172. }
  173. /**
  174. * 开始录屏
  175. */
  176. public startRecordScreen() {
  177. // 开始录屏, start 和 stop 调用需要配对,每次 start , stop 都会生成一段视频, 并从 onStop() 回调
  178. this.video = null
  179. if (this.mediaRecorder) {
  180. this.mediaRecorder.start();
  181. }
  182. }
  183. /**
  184. * 暂停录屏
  185. */
  186. public pauseRecordScreen() {
  187. if (this.mediaRecorder) {
  188. this.mediaRecorder.pause();
  189. }
  190. }
  191. /**恢复录屏 */
  192. public resumeRecordScreen() {
  193. if (this.mediaRecorder) {
  194. this.mediaRecorder.resume();
  195. }
  196. }
  197. /**
  198. * 停止录屏
  199. */
  200. public stopRecordScreen() {
  201. if (this.mediaRecorder) {
  202. // 成功 stop 后会触发 onStop() 回调
  203. this.mediaRecorder.stop()
  204. }
  205. }
  206. /**发布录屏到快手。 */
  207. publishVideo(cb?: Function) {
  208. // 发布录屏至快手
  209. if (this.mediaRecorder) {
  210. if (this.video === null) {
  211. // 无视频可以发布
  212. return
  213. }
  214. let that = this
  215. this.mediaRecorder.publishVideo({
  216. video: that.video, // 指定需要发布的视频片段,不指定则默认发布最后一次 start , stop 之间生成的视频
  217. callback: (error) => {
  218. if (error) {
  219. console.log(`发布录屏失败: ${JSON.stringify(error)}`)
  220. } else {
  221. console.log("发布录屏成功")
  222. cb()
  223. }
  224. }
  225. });
  226. }
  227. }
  228. /**查看关注官⽅帐号状态
  229. * errorCode:1 表示成功
  230. * errorMsg:错误信息
  231. * hasFollow:是否关注
  232. */
  233. checkFollowState() {
  234. kwaigame.checkFollowState({
  235. accountType: "CPServiceAccount",
  236. callback: (result: { errorCode: number, errorMsg: string, hasFollow: boolean }) => {
  237. console.log(JSON.stringify(result));
  238. this.ksError = result.errorMsg
  239. }
  240. })
  241. }
  242. /**打开官方账号 */
  243. openUserProfile() {
  244. kwaigame.openUserProfile({
  245. accountType: "CPServiceAccount",
  246. callback: (result) => {
  247. console.log(JSON.stringify(result));
  248. }
  249. })
  250. }
  251. }
  252. export const KS = kuaishou._instance