social.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * @author uu
  3. * @file 排行榜组件
  4. * @description 用户点击查看排行榜才检查授权,如果此时用户没有授权则进入授权界面
  5. */
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. display: cc.Sprite,
  10. groupDisplay: cc.Sprite,
  11. _isShow: false,
  12. // score: 0
  13. },
  14. isSharing: false,
  15. init(c) {
  16. let self = this
  17. this._controller = c
  18. wx.showShareMenu({
  19. withShareTicket: true
  20. })
  21. wx.onShareAppMessage(function () {
  22. return {
  23. title: "开局只是个农民,现在已经做到宰相",
  24. // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
  25. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
  26. }
  27. })
  28. // this.display.node.width = window.width
  29. //this.display.node.height = window.height
  30. //this.display.node.getComponent(cc.WXSubContextView).enabled = false;
  31. // this.tex = new cc.Texture2D();
  32. //TODO: 微信小游戏导致音乐自动关闭 处理失败
  33. // 监听
  34. wx.onAudioInterruptionEnd(() => {
  35. c.musicMgr.pauseBg()
  36. c.musicMgr.resumeBg()
  37. })
  38. wx.onShow((options) => {
  39. // console.log(options)
  40. if (options.scene == 1044) {
  41. wx.postMessage({
  42. message: 'group',
  43. shareTicket: options.shareTicket
  44. })
  45. c.openGroupRank()
  46. this.display.node.active = false
  47. c.totalRank.active = false
  48. }
  49. cc.director.resume()
  50. // if (!self.hasShared && self.isSharing && self._controller.game._status == 1) {
  51. // // TODO 分享成功
  52. // self.onItemShareSuccess()
  53. // }
  54. })
  55. wx.onHide(() => {
  56. cc.director.pause()
  57. })
  58. // 获取最高官阶
  59. this.getHighestLevel()
  60. },
  61. onItemShareSuccess() {
  62. this.hasShared = true
  63. this._controller.game.fakeShareSuccess()
  64. },
  65. getHighestLevel() {
  66. let highLevel = wx.getStorageSync('highLevel')
  67. return highLevel
  68. },
  69. getHighestScore() {
  70. let score = wx.getStorageSync('highScore')
  71. return score
  72. },
  73. // --------------- share ----------------
  74. onShareButton() {
  75. var self = this;
  76. wx.shareAppMessage({
  77. title: "我终于当上了" + this._controller.scoreMgr.levelData[this._controller.scoreMgr.level - 1].name + ",不服来战",
  78. // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
  79. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
  80. })
  81. },
  82. onUsualShareButton() {
  83. wx.shareAppMessage({
  84. title: "开局只是个农民,现在已经做到宰相",
  85. // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
  86. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
  87. })
  88. },
  89. onItemShareButton() {
  90. if (!this.hasShared) {
  91. this.isSharing = true
  92. } else {
  93. // 提示玩家 当前局已分享
  94. return
  95. }
  96. wx.shareAppMessage({
  97. title: "开局只是个农民,现在已经做到宰相",
  98. // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
  99. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
  100. })
  101. },
  102. onShakePhone() {
  103. wx.vibrateShort()
  104. },
  105. // ---------------分数上传---------------
  106. onGameOver(level, score) {
  107. //上传分数
  108. //打开开放域
  109. this.score = score
  110. let highLevel = level
  111. let highScore = score
  112. let self = this
  113. highLevel = wx.getStorageSync('highLevel')
  114. highLevel = parseInt(highLevel)
  115. if (highLevel) {
  116. highLevel = highLevel < level ? level : highLevel
  117. } else {
  118. highLevel = level
  119. }
  120. highScore = wx.getStorageSync('highScore')
  121. if (highScore) {
  122. highScore = parseInt(highScore)
  123. highScore = highScore < score ? score : highScore
  124. } else {
  125. highScore = score
  126. }
  127. var highLevelName = this._controller.gameData.json.levelData[highLevel - 1].name
  128. wx.setStorageSync('highLevel', highLevel + '')
  129. wx.setStorageSync('highScore', highScore + '')
  130. self._controller.scoreMgr.failHighScore.string = "您的最高分:" + (highScore + '')
  131. var kvDataList = new Array()
  132. kvDataList.push({
  133. key: "highLevel",
  134. value: highLevelName,
  135. }, {
  136. key: "highScore",
  137. value: highScore + '',
  138. })
  139. wx.setUserCloudStorage({
  140. "KVDataList": kvDataList,
  141. success: () => {
  142. // self.showRank()
  143. },
  144. fail: (res) => {
  145. // console.log(res)
  146. }
  147. })
  148. },
  149. showRank() {
  150. wx.postMessage({
  151. message: 'Show'
  152. })
  153. this.display.node.active = true
  154. this._isShow = true
  155. },
  156. // switchRankType() {
  157. // wx.postMessage({
  158. // message: 'switchRank'
  159. // })
  160. // this._isShow = true
  161. // },
  162. closeRank() {
  163. this.display.node.active = false
  164. wx.postMessage({
  165. message: 'Hide'
  166. })
  167. this._isShow = false
  168. },
  169. showGroupRank() {
  170. wx.postMessage({
  171. message: 'Show'
  172. })
  173. this.groupDisplay.node.active = true
  174. this._isShow = true
  175. },
  176. // switchRankType() {
  177. // wx.postMessage({
  178. // message: 'switchRank'
  179. // })
  180. // this._isShow = true
  181. // },
  182. closeGroupRank() {
  183. this.groupDisplay.node.active = false
  184. wx.postMessage({
  185. message: 'Hide'
  186. })
  187. this._isShow = false
  188. },
  189. createImage(sprite, url) {
  190. let image = wx.createImage();
  191. image.onload = function () {
  192. let texture = new cc.Texture2D();
  193. texture.initWithElement(image);
  194. texture.handleLoadedTexture();
  195. sprite.spriteFrame = new cc.SpriteFrame(texture);
  196. };
  197. image.src = url;
  198. },
  199. update() {
  200. if (this._isShow) {
  201. if (this.display.node.active) {
  202. this.display.node.getComponent(cc.WXSubContextView).update()
  203. }
  204. if (this.groupDisplay.node.active) {
  205. this.groupDisplay.node.getComponent(cc.WXSubContextView).update()
  206. }
  207. }
  208. },
  209. // 控制打开广告
  210. onReviveButton(type) {
  211. // 广告位
  212. let self = this
  213. this.adType = type //0表示加倍 1表示复活 2表示炸弹
  214. if (this.audioAd) {
  215. this.audioAd.show().catch(() => {
  216. // 失败重试
  217. this.audioAd.load()
  218. .then(() => this.audioAd.show())
  219. .catch(err => {
  220. console.log('激励视频 广告显示失败', err.errMsg)
  221. if (self.adType == 1) {
  222. self._controller.game.onSkipRevive()
  223. } else if (self.adType == 2) {
  224. self._controller.scoreMgr.onLevelUpButton()
  225. } else {
  226. self.onItemShareSuccess()
  227. }
  228. })
  229. })
  230. return
  231. }
  232. this.audioAd = wx.createRewardedVideoAd({
  233. adUnitId: 'adunit-482148cfeb243378'
  234. })
  235. this.audioAd.show().catch(() => {
  236. // 失败重试
  237. this.audioAd.load()
  238. .then(() => this.audioAd.show())
  239. .catch(err => {
  240. self.fakeShare()
  241. })
  242. })
  243. this.audioAd.onError(err => {
  244. self.fakeShare()
  245. })
  246. this.audioAd.onClose((res) => {
  247. if (self.adType == 1) {
  248. if (res && res.isEnded || res === undefined) {
  249. self._controller.game.showReviveSuccess()
  250. } else {
  251. self._controller.game.askRevive()
  252. }
  253. } else if (self.adType == 0) {
  254. if (res && res.isEnded || res === undefined) {
  255. self._controller.scoreMgr.onLevelUpButton(2)
  256. }
  257. } else {
  258. if (res && res.isEnded || res === undefined) {
  259. self.onItemShareSuccess()
  260. }
  261. }
  262. })
  263. },
  264. fakeShare() {
  265. let self = this
  266. wx.shareAppMessage({
  267. title: "我已经玩到" + this._controller.scoreMgr.score + "分了,邀请你来挑战",
  268. // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
  269. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
  270. })
  271. if (this.adType) {
  272. self._controller.game.showReviveSuccess()
  273. } else {
  274. self._controller.scoreMgr.onLevelUpButton(2)
  275. }
  276. },
  277. openBannerAdv() {
  278. // 创建 Banner 广告实例,提前初始化
  279. // let screenWidth = wx.getSystemInfoSync().screenWidth
  280. // let bannerHeight = screenWidth / 350 * 120
  281. // let screenHeight = wx.getSystemInfoSync().screenHeight - 108
  282. // let adUnitIds = [
  283. // 'adunit-510a4ec39065ef96',
  284. // 'adunit-29b0fa7a2db8e8cb',
  285. // 'adunit-4020bb9ea439e6a5'
  286. // ]
  287. // if (this.bannerAd) {
  288. // this.bannerAd.destroy()
  289. // }
  290. // this.bannerAd = wx.createBannerAd({
  291. // adUnitId: adUnitIds[Math.floor(Math.random() * 3)],
  292. // style: {
  293. // left: 0,
  294. // top: screenHeight,
  295. // width: 620,
  296. // }
  297. // })
  298. // // 在适合的场景显示 Banner 广告
  299. // this.bannerAd.onLoad(() => {
  300. // // console.log('banner 广告加载成功')
  301. // })
  302. // this.bannerAd.onError((e) => {
  303. // console.log('banner 广告加载失败', e)
  304. // })
  305. // this.bannerAd.show()
  306. // .then()
  307. },
  308. navToMiniprogram(event, custom) {
  309. console.log(custom)
  310. wx.navigateToMiniProgram({
  311. appId: custom
  312. })
  313. },
  314. closeBannerAdv() {
  315. if (this.bannerAd) {
  316. this.bannerAd.hide()
  317. }
  318. }
  319. });