WeChat.js 10 KB

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