WxSdk.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. declare var wx: any;
  2. import { find, Node, Size, UITransform, v3, Vec3 } from "cc";
  3. import BaseSdk from "./BaseSdk";
  4. import { MgrScope } from "./SdkInterface";
  5. const Scope = ({
  6. [MgrScope.FriendInteraction]: 'scope.WxFriendInteraction',
  7. [MgrScope.UserInfo]: 'scope.userInfo'
  8. })
  9. export default class WxSdk extends BaseSdk {
  10. getScope(str): string {
  11. return Scope[str];
  12. }
  13. getMenuButtonBoundingClientRect() {
  14. return wx.getMenuButtonBoundingClientRect()
  15. }
  16. openSetting(res){
  17. wx.openSetting(res)
  18. }
  19. getWindowInfo(){
  20. return wx.getWindowInfo()
  21. }
  22. recorderResume() {
  23. }
  24. recorderPause() {
  25. }
  26. private videoAd: any;
  27. private callback: any;
  28. private target: any;
  29. private bannerAd: any;
  30. private interstitialAd: any;
  31. private customAd: any;
  32. /**
  33. * 初始化
  34. * @param callback 初始化成功后回调
  35. * @param target 监听对象
  36. */
  37. public init(callback: Function, target: Object): void {
  38. console.log("WxSdk init");
  39. //转发
  40. wx.showShareMenu({
  41. withShareTicket: true,
  42. menus: ["shareAppMessage", "shareTimeline"]
  43. });
  44. }
  45. /**
  46. * 登录
  47. * @param callback 登录成功后回调
  48. * @param target 监听对象
  49. */
  50. public login(callback: Function, target: Object): void {
  51. console.log("WxSdk login");
  52. }
  53. /**
  54. * 登出
  55. */
  56. public logout(): void {
  57. console.log("WxSdk logout");
  58. }
  59. /**
  60. * 退出
  61. */
  62. public exit(): void {
  63. console.log("WxSdk exit");
  64. }
  65. /**
  66. * 切换账号
  67. */
  68. public switchLogin(): void {
  69. console.log("WxSdk switchLogin");
  70. }
  71. public setOpenScore(score: number): void {
  72. wx.postMessage({
  73. type: 'engine',
  74. event: 'setScore',
  75. score: score,
  76. });
  77. }
  78. public getOpenRank(): void {
  79. wx.postMessage({
  80. type: 'engine',
  81. event: 'getRank'
  82. });
  83. }
  84. /**
  85. * 数据上报
  86. * @param param 参数
  87. */
  88. public report(...param: any[]): void {
  89. console.log("WxSdk report");
  90. }
  91. /**
  92. * 数据上报
  93. * @param param 参数
  94. */
  95. public pay(...param: any[]): void {
  96. console.log("WxSdk pay");
  97. }
  98. showLoading(param={
  99. title: '加载中',
  100. }){
  101. wx.showLoading(param)
  102. }
  103. hideLoading(){
  104. wx.hideLoading()
  105. }
  106. /**
  107. * 播放广告
  108. */
  109. playRewardAd(callback: Function, target: any): void {
  110. this.callback = callback;
  111. this.target = target;
  112. if (this.adUnitId == 'adunit-') {
  113. this.callback.call(this.target, true);
  114. return
  115. }
  116. this.createRewardAd();
  117. // 用户触发广告后,显示激励视频广告
  118. this.videoAd.show().then(()=>{
  119. wx.hideLoading()
  120. }).catch(() => {
  121. // 失败重试
  122. this.videoAd.load()
  123. .then(() =>{
  124. this.videoAd.show().then(()=>{
  125. wx.hideLoading()
  126. }).catch(()=>{
  127. console.log('激励视频 广告显示失败')
  128. this.onRewardAdCallback(false);
  129. })
  130. } )
  131. .catch(err => {
  132. console.log('激励视频 广告显示失败')
  133. wx.hideLoading()
  134. this.onRewardAdCallback(false);
  135. })
  136. })
  137. }
  138. adUnitId = 'adunit-68f5c230e5dae73c'
  139. createRewardAd() {
  140. if (!this.videoAd) {
  141. this.videoAd = wx.createRewardedVideoAd({
  142. adUnitId: this.adUnitId
  143. })
  144. // this.videoAd.onLoad((res)=>{
  145. // wx.hideLoading()
  146. // })
  147. this.videoAd.onClose(res => {
  148. // 用户点击了【关闭广告】按钮
  149. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  150. wx.hideLoading()
  151. if (res && res.isEnded || res === undefined) {
  152. // 正常播放结束,可以下发游戏奖励
  153. // callback.call(target, true);
  154. this.onRewardAdCallback(true);
  155. }
  156. else {
  157. // 播放中途退出,不下发游戏奖励
  158. // callback.call(target, false);
  159. this.onRewardAdCallback(false);
  160. }
  161. })
  162. this.videoAd.onError(err => {
  163. // callback.call(target, false);
  164. // console.log(err, "1");
  165. wx.hideLoading()
  166. this.onRewardAdCallback(false);
  167. })
  168. }
  169. }
  170. onRewardAdCallback(success) {
  171. if (this.callback) {
  172. this.callback.call(this.target, success);
  173. if (success) {
  174. this.videoAd.load();
  175. }
  176. this.callback = null;
  177. }
  178. }
  179. /**
  180. * 显示banner广告
  181. */
  182. showBannerAd(show: boolean): void {
  183. if (show) {
  184. // 在适合的场景显示 Banner 广告
  185. this.createBannerAd();
  186. this.bannerAd.show();
  187. }
  188. else {
  189. this.bannerAd.hide();
  190. this.bannerAd.destroy();
  191. this.bannerAd = null;
  192. this.createBannerAd();
  193. }
  194. }
  195. createBannerAd() {
  196. if (!this.bannerAd) {
  197. let sysInfo = wx.getSystemInfoSync();
  198. // 创建 Banner 广告实例,提前初始化
  199. var width = 320;
  200. this.bannerAd = wx.createBannerAd({
  201. adUnitId: 'adunit-b03f9dc72e3592bd',
  202. adIntervals: 30, // 自动刷新频率不能小于30秒
  203. style: {
  204. left: sysInfo.screenWidth / 2 - width / 2,
  205. top: sysInfo.screenHeight,
  206. width: width
  207. }
  208. })
  209. this.bannerAd.onResize(res => {
  210. this.bannerAd.style.top = sysInfo.screenHeight - this.bannerAd.style.realHeight;
  211. })
  212. this.bannerAd.onError(err => {
  213. console.log(err)
  214. })
  215. }
  216. }
  217. createInterstitialAd() {
  218. // 创建插屏广告实例,提前初始化
  219. if (wx.createInterstitialAd && !this.interstitialAd) {
  220. this.interstitialAd = wx.createInterstitialAd({
  221. adUnitId: 'adunit-b03f9dc72e3592bd'
  222. })
  223. this.interstitialAd.onClose(res => {
  224. this.interstitialAd = null;
  225. this.createInterstitialAd();
  226. })
  227. }
  228. }
  229. showInterstitialAd() {
  230. // 在适合的场景显示插屏广告
  231. this.createInterstitialAd();
  232. if (this.interstitialAd) {
  233. this.interstitialAd.show().catch((err) => {
  234. console.error(err)
  235. })
  236. }
  237. }
  238. /**
  239. * 显示自定义广告
  240. */
  241. showCustomAd(show, top?, left?): void {
  242. // console.log(top, left);
  243. if (!this.customAd) {
  244. this.customAd = wx.createCustomAd({
  245. adIntervals: 30,
  246. adUnitId: 'adunit-b03f9dc72e3592bd',
  247. style: {
  248. fixed: true,
  249. left: left,
  250. top: top,
  251. },
  252. })
  253. }
  254. if (show) {
  255. this.customAd.show();
  256. }
  257. else {
  258. this.customAd.destroy();
  259. this.customAd = null;
  260. }
  261. }
  262. /**
  263. * 主动拉起转发
  264. */
  265. shareAppMessage(title: string = "", imageUrlId: string = "", imageUrl: string = "", query: any = null, shareCallback: any = null): void {
  266. title = title;
  267. imageUrlId = imageUrlId;
  268. imageUrl = imageUrl;
  269. var data = {
  270. title: title,
  271. imageUrlId: imageUrlId,
  272. imageUrl: imageUrl,
  273. query: ""
  274. };
  275. wx.shareAppMessage(data);
  276. }
  277. recorderStart(duration: number = 30): void {
  278. }
  279. recorderStop(): void {
  280. }
  281. shareRecord(title: string, desc: string, path: string, topics: string[]): void {
  282. }
  283. public goToSidebar(): void {
  284. }
  285. public checkHasSidebar(): boolean {
  286. return false;
  287. }
  288. public checkFromSidebar(): boolean {
  289. return false;
  290. }
  291. vibrateShort(param: any) {
  292. wx.vibrateShort({
  293. type: param.type,
  294. success: param.success,
  295. fail: param.fail,
  296. complete: param.complete
  297. });
  298. }
  299. posMessge(messge?) {
  300. let mess = { event: 'getRank', type: 'engine' }
  301. if (messge) {
  302. messge = { ...mess, ...messge }
  303. }
  304. messge = messge || mess
  305. console.log("messge")
  306. console.log(messge)
  307. console.log(wx.postMessage)
  308. console.log("messge__end")
  309. wx.postMessage(messge)
  310. // let env = wx
  311. // if (env) {
  312. // console.log('Message posted');
  313. // env.getOpenDataContext().postMessage({
  314. // event:'setValue',type:'engine'
  315. // });
  316. // }
  317. }
  318. authorize(str?: any, cb?) {
  319. wx.getSetting({
  320. success(res) {
  321. if (!res.authSetting[str]) {
  322. console.log("success_res ==>", str)
  323. wx.authorize({
  324. scope: str,
  325. success() {
  326. console.log("已经授权,直接获取用户信息==>", str)
  327. }
  328. })
  329. cb&&cb()
  330. } else {
  331. // if(str == 'scope.userInfo')
  332. }
  333. },
  334. });
  335. }
  336. getUserInfo(reportFun:any, node?: Node,btnCb?) {
  337. wx.getSetting({
  338. success(res) {
  339. if (res.authSetting['scope.userInfo'] === true) {
  340. wx.getUserInfo({
  341. success: (res) => {
  342. // 已经授权,直接获取用户信息
  343. // var userInfo = res.userInfo
  344. // var nickName = userInfo.nickName
  345. // var avatarUrl = userInfo.avatarUrl
  346. // var gender = userInfo.gender //性别 0:未知、1:男、2:女
  347. // var province = userInfo.province
  348. // var city = userInfo.city
  349. // var country = userInfo.country
  350. console.log("res=====>",res)
  351. reportFun(res)
  352. // export function reportUserInfo(avatar: string, nickname: string, gender: number): void;
  353. },
  354. });
  355. } else {
  356. let buttonPosition = {
  357. left: 0,
  358. top: 0,
  359. width:0,
  360. height:0
  361. }
  362. if(node){
  363. let uit = node?.getComponent(UITransform)
  364. const canvasNode = find('Canvas'); // 获取 Canvas 节点
  365. const ret = uit.getBoundingBoxToWorld(); // 转换为世界坐标
  366. let canvasSize = canvasNode.getComponent(UITransform).contentSize
  367. const systemInfo = wx.getSystemInfoSync();
  368. console.log("systemInfo==>",systemInfo)
  369. console.log("canvasSize==>",canvasSize)
  370. let k_x = systemInfo.windowWidth/canvasSize.width
  371. let k_y = systemInfo.windowHeight/canvasSize.height
  372. buttonPosition = {
  373. left: ret.x *k_x,
  374. top: systemInfo.windowHeight - (ret.y * k_y) - ret.height *k_y ,
  375. width:ret.width*k_x,
  376. height:ret.height * k_y,
  377. };
  378. }
  379. console.log("buttonPosition==>",buttonPosition)
  380. const button = wx.createUserInfoButton({
  381. type: "image",
  382. style: {
  383. left: buttonPosition.left,
  384. top: buttonPosition.top,
  385. width: buttonPosition.width,
  386. height: buttonPosition.height,
  387. // backgroundColor: "rgba(255, 255, 255, 0.5)",
  388. },
  389. });
  390. btnCb&&btnCb(button)
  391. button.onTap((res) => {
  392. if (res.errMsg.indexOf(':ok') > -1 && !!res.rawData) {
  393. // 获取用户信息
  394. // var userInfo = res.userInfo
  395. // var nickName = userInfo.nickName
  396. // var avatarUrl = userInfo.avatarUrl
  397. // var gender = userInfo.gender //性别 0:未知、1:男、2:女
  398. // var province = userInfo.province
  399. // var city = userInfo.city
  400. // var country = userInfo.country
  401. reportFun(res)
  402. }
  403. button.destroy();
  404. });
  405. }
  406. },
  407. });
  408. }
  409. }