WxCommon.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. class WxCommon {
  2. constructor() {
  3. this.otherGameCount = 2;
  4. this.otherGameMap = new Map();
  5. this.otherGameMap.set(0, 'https://pub.dwstatic.com/wxgame/budingGame/share/xcxm.jpg');//布丁小程序码
  6. this.otherGameMap.set(1, 'https://pub.dwstatic.com/wxgame/budingGame/share/football.jpg');//勇闯世界杯小程序码
  7. }
  8. static getInstance() {
  9. if (!WxCommon.instance && CC_WECHATGAME) {
  10. WxCommon.instance = new WxCommon();
  11. }
  12. return WxCommon.instance;
  13. }
  14. ShowShareMenu() {
  15. wx.showShareMenu({
  16. withShareTicket: true,
  17. success: function (res) {
  18. },
  19. fail: function (res) {
  20. },
  21. complete: function (res) {
  22. }
  23. })
  24. }
  25. OnShareAppMessage(data, target, callback) {
  26. wx.onShareAppMessage(function (res) {
  27. // 用户点击了“转发”按钮
  28. if (res.from === 'button') {
  29. // 来自页面内转发按钮
  30. } else if (res.from === 'menu') {
  31. }
  32. return {
  33. title: UserInfo.shareDesc,
  34. imageUrl: UserInfo.shareUrl,
  35. success: function (res) {
  36. console.error(res);
  37. // 转发成功
  38. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  39. callback.call(target, res.shareTickets);//res.shareTickets不为空则是群
  40. }
  41. },
  42. fail: function (res) {
  43. // 转发失败
  44. console.error(res);
  45. },
  46. complete: function (res) {
  47. }
  48. }
  49. })
  50. }
  51. OnShow(data, target, callback) {
  52. var self = this;
  53. wx.onShow(function (obj) {
  54. cc.audioEngine.resumeAll();
  55. self.GetSystemInfo(null, null, null);
  56. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  57. callback.call(target, true);
  58. }
  59. })
  60. }
  61. //data结构 {titleData:titleData,imgurlData:imgurlData,queryData:queryData}
  62. ShareAppMessage(data, target, callback) {//微信分享
  63. wx.shareAppMessage({
  64. // title: '好游砀山麻将,你的贴身麻将馆,免费领房卡!',
  65. // imageUrl: 'https://images.haoyou998.com/mp/share/share.jpg',
  66. // query: 'key1=111&key2=222',
  67. title: data.titleData,
  68. imageUrl: data.imgurlData,
  69. query: data.queryData,
  70. success: function (res) {
  71. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  72. callback.call(target, res.shareTickets);//res.shareTickets不为空则是群
  73. }
  74. },
  75. fail: function (res) {
  76. console.error(res);
  77. },
  78. complete: function (res) {
  79. }
  80. });
  81. }
  82. GetSystemInfo(data, target, callback) {//获取系统信息
  83. if (cc.vv == undefined) {
  84. cc.vv = {};
  85. }
  86. wx.getSystemInfo({
  87. success: function (res) {
  88. if (res.version >= UserInfo.wxversion) {
  89. cc.vv.isRank = true;
  90. } else {
  91. cc.vv.isRank = false;
  92. // if (cc.vv.isAlreadyShow != true) {
  93. // }
  94. wx.showModal({
  95. title:'版本提示',
  96. content:'您的微信版本太低,将不支持排行榜,请更新最新版本微信客户端',
  97. showCancel:false,
  98. cancelText:'取消',
  99. cancelColor:'#000000',
  100. confirmText:'退出程序',
  101. confirmColor:'#3cc51f',
  102. success: function(res) {
  103. cc.vv.isAlreadyShow = true;
  104. wx.exitMiniProgram();
  105. }
  106. })
  107. }
  108. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  109. callback.call(target, res);
  110. //res.batteryLevel 电量,范围 1 - 100, number
  111. //res.wifiSignal wifi 信号强度,范围 0 - 4, number
  112. //res.version 微信版本号
  113. }
  114. },
  115. fail: function (res) {
  116. console.error(res);
  117. },
  118. complete: function (res) {
  119. }
  120. })
  121. }
  122. SetEnableDebug(type) {
  123. // wx.setEnableDebug({
  124. // enableDebug: type
  125. // })
  126. }
  127. PreviewImage(data, target, callback) {//预览图片
  128. wx.previewImage({
  129. urls:[data],
  130. success: function (res) {
  131. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  132. callback.call(target, true);
  133. }
  134. },
  135. fail: function (res) {
  136. },
  137. complete: function (res) {
  138. }
  139. });
  140. }
  141. SaveImageToPhotosAlbum(data, target, callback){//保存相册
  142. wx.downloadFile({
  143. url: data,
  144. success: function (res) {
  145. wx.saveImageToPhotosAlbum({
  146. filePath:res.tempFilePath,
  147. success: function (res) {
  148. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  149. callback.call(target, res.shareTickets);
  150. }
  151. },
  152. fail: function (res) {
  153. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  154. callback.call(target, res.shareTickets);
  155. }
  156. },
  157. complete: function (res) {
  158. }
  159. })
  160. },
  161. fail: function (res) {
  162. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  163. callback.call(target, res.shareTickets);
  164. }
  165. },
  166. })
  167. }
  168. RemoveUserCloudStorage(removeKey, target, callback) {
  169. if (UserInfo.platform != 1) {
  170. return;
  171. }
  172. if (typeof removeKey == "undefined" || removeKey == null || removeKey == "") {
  173. return;
  174. }
  175. wx.removeUserCloudStorage({
  176. keyList:removeKey,
  177. success: function (res) {
  178. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  179. callback.call(target, removeKey, true);
  180. }
  181. },
  182. fail: function (res) {
  183. if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
  184. callback.call(target, removeKey, false);
  185. }
  186. },
  187. complete: function (res) {
  188. removeKey = null;
  189. }
  190. })
  191. }
  192. }
  193. module.exports = WxCommon.getInstance();