WeChatSDKManager.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import HTTPS, { NetPost } from "../HTTPS";
  2. export class WeChatSDKManager {
  3. private loginData = {};
  4. private logincode = '';
  5. login(callBack: Function) {
  6. let wx = (window as any)["wx"];
  7. let self = this;
  8. wx.login({
  9. success: (login_res) => {
  10. self.logincode = login_res.code;
  11. console.log("得到登录凭证code");
  12. console.log(login_res.code);
  13. let sysInfo = wx.getSystemInfoSync();
  14. let width = sysInfo.screenWidth;
  15. let height = sysInfo.screenHeight;
  16. wx.getSetting({
  17. success(getSetting_res) {
  18. console.log("wx.getSetting获取用户配置成功");
  19. if (getSetting_res.authSetting['scope.userInfo']) {
  20. wx.getUserInfo({
  21. success: function (res) {
  22. console.log("得到用户信息数据");
  23. console.log(getSetting_res);
  24. self.loginData = res.userInfo;
  25. self.requestGameServer()
  26. callBack?.(res);
  27. }
  28. })
  29. } else {
  30. let button = wx.createUserInfoButton({
  31. type: 'text',
  32. text: '',
  33. style: {
  34. left: 0,
  35. top: 0,
  36. width: width,
  37. height: height,
  38. lineHeight: height,
  39. // backgroundColor: '#ff0000',
  40. color: '#000000',
  41. textAlign: 'center',
  42. fontSize: 18,
  43. borderRadius: 4
  44. }
  45. })
  46. // _disNode.active = true;
  47. button.onTap((res) => {
  48. // 用户同意授权后回调,通过回调可获取用户头像昵称信息
  49. console.log("用户点击后_得到用户信息数据");
  50. console.log(res);
  51. self.loginData = res.userInfo;
  52. self.requestGameServer()
  53. callBack?.(res);
  54. button.hide();
  55. })
  56. button.show();
  57. }
  58. },
  59. // 失败回调
  60. fail(res) {
  61. console.log("wx.getSetting获取用户配置失败");
  62. console.log(res);
  63. },
  64. // 结束回调(调用成功,失败都会执行)
  65. complete(res) {
  66. console.log("wx.getSetting获取用户配置结束");
  67. console.log(res);
  68. }
  69. })
  70. }
  71. });
  72. }
  73. requestGameServer() {
  74. HTTPS.Instance.post(NetPost.Login,
  75. {
  76. platform: 'wechatmini',
  77. code: this.logincode,
  78. avatar: this.loginData['avatarUrl'],
  79. nickname: this.loginData['nickName'],
  80. }).then((resp) => {
  81. console.error('999999999999');
  82. console.error(resp);
  83. HTTPS.Instance.token = resp.data.userinfo.token
  84. })
  85. }
  86. showShareMenu() {
  87. let wx = (window as any)["wx"];
  88. wx.showShareMenu({
  89. menus: ['shareAppMessage', "shareTimeline"],
  90. })
  91. wx.onShareAppMessage(function () {
  92. return {
  93. title: '你能挑战过吗?',
  94. imageUrl: ""
  95. }
  96. })
  97. if (wx.onShareTimeline != null) {
  98. wx.onShareTimeline(function () {
  99. return {
  100. title: '你能挑战过吗?',
  101. imageUrl: ""
  102. }
  103. })
  104. }
  105. }
  106. ///////////////////
  107. private rewardVideo2
  108. public show_video(callback: Function) {
  109. let wx = (window as any)["wx"];
  110. let id = "adunit-36fec0fa8e78f37b"
  111. if (id == '') {
  112. callback(true);
  113. return;
  114. }
  115. if (this.rewardVideo2 != null) {
  116. this.rewardVideo2.offClose(fun);
  117. }
  118. let rewardedVideoAd = wx.createRewardedVideoAd({
  119. adUnitId: id,
  120. });
  121. this.rewardVideo2 = rewardedVideoAd;
  122. rewardedVideoAd.load().then(() => {
  123. wx.showToast({
  124. title: "加载中,请稍后",
  125. icon: 'success',//图标,支持"success"、"loading" 
  126. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  127. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  128. success: function () { },
  129. fail: function () { },
  130. complete: function () { }
  131. })
  132. console.log('激励视频 广告加载成功');
  133. rewardedVideoAd.show();
  134. });
  135. rewardedVideoAd.onError(err => {
  136. console.log('激励视频 广告显示失败', err);
  137. wx.showToast({
  138. title: "请稍后再试",
  139. icon: 'fail',//图标,支持"success"、"loading" 
  140. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  141. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  142. success: function () { },
  143. fail: function () { },
  144. complete: function () { }
  145. })
  146. callback(false);
  147. })
  148. var fun = function (res) {
  149. if (res && res.isEnded) {
  150. console.log('res: ', res);
  151. callback(true);
  152. rewardedVideoAd.offClose(fun);
  153. } else {
  154. console.log('播放中途退出');
  155. callback(false);
  156. }
  157. }
  158. rewardedVideoAd.onClose(fun);
  159. }
  160. }