WeChat.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. const LoginApi = require('./LoginApi');
  2. const ShareAction = require('../utils/ShareAction');
  3. const AlertManager = require('../utils/AlertManager');
  4. const {GameNotificationKey, WechatShareType } = require('../utils/GameEnum');
  5. const SkillApi = require("../net/SkillApi");
  6. class WeChat {
  7. constructor() {
  8. this.wxWidth = 0;//微信小程序可用屏幕宽度
  9. this.wxHeight = 0;//微信小程序可用屏幕高度
  10. this.js_code = null;
  11. this.anonymousCode = null;
  12. this.shareArray = WeChat.shareObjcts;
  13. }
  14. /**
  15. * 检查登录态是否已失效
  16. * 回调函数会回传flag值(true:未失效, false:已失效)
  17. * @param {Function} cb 回调方法
  18. */
  19. checkLogin(cb) {
  20. if (window.tt != undefined) {
  21. tt.checkSession({
  22. success() {
  23. //session_key 未失效
  24. cb && cb()
  25. },
  26. fail() {
  27. //session_key 已失效
  28. cb && cb(true)
  29. }
  30. })
  31. } else if (CC_WECHATGAME) {
  32. wx.checkSession({
  33. success() {
  34. //session_key 未失效
  35. cb && cb()
  36. },
  37. fail() {
  38. //session_key 已失效
  39. cb && cb(true)
  40. }
  41. })
  42. }
  43. }
  44. /**
  45. * 使用存在localStorage中的登录信息进行静默登录
  46. * 不刷新 uid 和 token
  47. * @param {Function} cb 回调方法
  48. */
  49. loginStatic(cb) {
  50. let userStorage = cc.sys.localStorage.getItem('GlobalUser')
  51. if (userStorage) {
  52. GameGlobal.user = userStorage;
  53. cb && cb()
  54. } else {
  55. this.login(cb)
  56. }
  57. }
  58. /**
  59. * 调起一个新的微信登录
  60. * 刷新 token
  61. * @param {Function} cb 回调方法
  62. */
  63. login(cb) {
  64. this.cb = cb || function () { }
  65. if (!CC_WECHATGAME && window.tt == undefined) {
  66. GameGlobal.user = {
  67. uid: 1,
  68. token: 'lucifer_test_token',
  69. nick: 'lucifer',
  70. avatarUrl: 'http://mpic.tiankong.com/f94/72b/f9472be4691ab43e3f6132bb8b63d00a/640.jpg',
  71. gender: 1
  72. }
  73. this.cb();
  74. return;
  75. }
  76. if (window.tt != undefined) {
  77. var self = this;
  78. tt.login({
  79. success (res) {
  80. self.js_code = res.code;
  81. self.anonymousCode = res.anonymousCode;
  82. self._getUserInfo();
  83. },
  84. fail (res) {
  85. self.cb && self.cb(true);
  86. }
  87. });
  88. } else if (CC_WECHATGAME) {
  89. this.isSupportInfoBtn = wx.createUserInfoButton ? true : false;
  90. wx.login({
  91. success: ({ code }) => {
  92. this.js_code = code;
  93. //如果支持用户信息按钮
  94. if (this.isSupportInfoBtn) {
  95. wx.getSetting({
  96. success: (res) => {
  97. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  98. if (res.authSetting['scope.userInfo']) {
  99. this._getUserInfo();
  100. } else {
  101. this._createBtn();
  102. }
  103. }
  104. })
  105. } else {
  106. this._getUserInfo();
  107. }
  108. }
  109. })
  110. wx.getSystemInfo({
  111. success: (res) => {
  112. GameGlobal.os = res.platform == "android" ? 2 : 1;
  113. this.wxWidth = res.windowWidth;
  114. this.wxHeight = res.windowHeight;
  115. }
  116. });
  117. }
  118. }
  119. _getUserInfo() {
  120. if (window.tt != undefined) {
  121. var self = this;
  122. tt.getUserInfo({
  123. withCredentials: true,
  124. success (res) {
  125. self._login(res);
  126. },
  127. fail (res) {
  128. self.cb && self.cb(true);
  129. }
  130. });
  131. } else if (CC_WECHATGAME) {
  132. wx.getUserInfo({
  133. withCredentials: true,
  134. success: (res) => {
  135. this._login(res);
  136. }
  137. })
  138. }
  139. }
  140. _login(res) {
  141. if (window.tt != undefined) {
  142. LoginApi.ttLogin(this.js_code, this.anonymousCode, res, this.cb);
  143. } else if (CC_WECHATGAME) {
  144. LoginApi.login(this.js_code, res, this.cb);
  145. }
  146. }
  147. /**
  148. * 创建一个微信环境下,获取登录信息的原生按钮
  149. */
  150. _createBtn() {
  151. let width = 167;
  152. let height = 88.5;
  153. let left = (this.wxWidth - width) / 2;
  154. let top = this.wxHeight - 380;
  155. console.log('top: ', top);
  156. let button = wx.createUserInfoButton({
  157. type: 'image',
  158. // text: '点击授权',
  159. image: 'https://pub.dwstatic.com/wxgame/taptapstar/share/startgame.png',
  160. style: {
  161. left: left,
  162. top: top,
  163. width: width,
  164. height: height,
  165. backgroundColor: '#ffffff',
  166. borderColor: '#ffffff',
  167. borderWidth: 0,
  168. borderRadius: 5,
  169. color: '#ffffff',
  170. textAlign: 'center',
  171. fontSize: 16,
  172. lineHeight: 88.5,
  173. },
  174. withCredentials: true
  175. })
  176. button.onTap((res) => {
  177. if (res.errMsg == 'getUserInfo:ok') {
  178. button.destroy();
  179. this._login(res);
  180. }
  181. })
  182. return button;
  183. }
  184. shareAction(type, success, fail) {
  185. if (CC_WECHATGAME || window.tt != undefined) {
  186. GameGlobal.isIgnoreShareStatus = false;
  187. GameGlobal.clickShare = true;
  188. GameGlobal.gameShareType = type;
  189. }
  190. let randomIndex = parseInt(Math.random()*(WeChat.shareObjcts.length),10);
  191. let shareObjct = WeChat.shareObjcts[randomIndex];
  192. if (window.tt != undefined) {
  193. tt.shareAppMessage({
  194. title: shareObjct.title,
  195. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  196. query: 'uid=' + GameGlobal.user.uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  197. success: (res) => {
  198. console.log('分享成功');
  199. success && success();
  200. if (GameGlobal.clickShare) {
  201. GameEvent.fire(GameNotificationKey.ShowShareAction, GameGlobal.gameShareType, true);
  202. GameGlobal.clickShare = false;
  203. GameGlobal.gameShareType = WechatShareType.None;
  204. GameGlobal.isIgnoreShareStatus = false;
  205. SkillApi.report(2, (responseData) => {
  206. },(error) => {
  207. });
  208. }
  209. },
  210. fail: () => {
  211. console.log('分享失败或取消');
  212. fail && fail();
  213. if (GameGlobal.clickShare) {
  214. GameEvent.fire(GameNotificationKey.ShowShareAction, GameGlobal.gameShareType, false);
  215. GameGlobal.clickShare = false;
  216. GameGlobal.gameShareType = WechatShareType.None;
  217. GameGlobal.isIgnoreShareStatus = false;
  218. }
  219. }
  220. });
  221. } else if (CC_WECHATGAME) {
  222. wx.shareAppMessage({
  223. title: shareObjct.title,
  224. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  225. query: 'uid=' + GameGlobal.user.uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  226. success: (res) => {
  227. console.log('分享成功');
  228. //判断是否分享到群
  229. if (res.hasOwnProperty('shareTickets')) {
  230. if (success) {
  231. success();
  232. }
  233. } else {
  234. // AlertManager.showShareFailAlert();
  235. console.log('分享的不是群');
  236. if (fail) {
  237. fail();
  238. }
  239. }
  240. },
  241. fail: () => {
  242. if (fail) {
  243. fail();
  244. }
  245. console.log('分享失败或取消');
  246. }
  247. });
  248. } else if (CC_QQPLAY) {
  249. BK.Share.share({
  250. qqImgUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/' + shareObjct.icon,
  251. summary: shareObjct.title,
  252. isToFriend: true,
  253. extendInfo: '',
  254. success: function(res) {
  255. BK.Console.log('分享成功', res.code, JSON.stringify(res.data));
  256. if(res.data.ret == 0) {
  257. success && success()
  258. }
  259. },
  260. fail: function(res) {
  261. BK.Console.log('分享失败', res.code, JSON.stringify(res.msg));
  262. fail && fail()
  263. },
  264. complete: function(res) {
  265. BK.Console.log('分享完成,不论成功失败');
  266. }
  267. });
  268. }
  269. }
  270. /// 跳转客服 是否为提现
  271. jumpCustomerServices(isDraw = false) {
  272. if (CC_QQPLAY || window.tt !== undefined) {
  273. AlertManager.showStoreQQaddGroup();
  274. } else if (CC_WECHATGAME) {
  275. if (isDraw) {
  276. wx.openCustomerServiceConversation({
  277. sessionFrom: 'draw',
  278. showMessageCard: false,
  279. });
  280. } else {
  281. wx.openCustomerServiceConversation({
  282. sessionFrom: 'shop',
  283. showMessageCard: true,
  284. sendMessageTitle: '商品',
  285. sendMessageImg: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_shop.png'
  286. });
  287. }
  288. }
  289. }
  290. static shareObjcts = [
  291. {'title': '猜猜他是谁?', 'icon': 'share_image_1.png'},
  292. {'title': '猜猜他是谁?', 'icon': 'share_image_2.png'},
  293. {'title': '看到这位明星了吗,签下这份合同,她就是你的人了~', 'icon': 'share_image_3.png'},
  294. {'title': '就、就算你是大明星,签约了你也是我的人!', 'icon': 'share_image_4.png'},
  295. {'title': '老板跟着小姨子跑路啦!本公司签约女团低价转让!', 'icon': 'share_image_5.png'},
  296. {'title': '你,还有你,我钦定你们两个。。。组CP!', 'icon': 'share_image_6.png'},
  297. {'title': '猜猜他是谁?', 'icon': 'share_image_7.png'},
  298. {'title': '猜猜他是谁?', 'icon': 'share_image_8.png'},
  299. {'title': '猜猜她是谁?', 'icon': 'share_image_9.png'}
  300. ]
  301. }
  302. module.exports = new WeChat();