WeChat.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. }
  204. });
  205. } else if (CC_WECHATGAME) {
  206. wx.shareAppMessage({
  207. title: shareObjct.title,
  208. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  209. query: 'uid=' + GameGlobal.user.uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  210. success: (res) => {
  211. console.log('分享成功');
  212. //判断是否分享到群
  213. if (res.hasOwnProperty('shareTickets')) {
  214. if (success) {
  215. success();
  216. }
  217. } else {
  218. // AlertManager.showShareFailAlert();
  219. console.log('分享的不是群');
  220. if (fail) {
  221. fail();
  222. }
  223. }
  224. },
  225. fail: () => {
  226. if (fail) {
  227. fail();
  228. }
  229. console.log('分享失败或取消');
  230. }
  231. });
  232. } else if (CC_QQPLAY) {
  233. BK.Share.share({
  234. qqImgUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/' + shareObjct.icon,
  235. summary: shareObjct.title,
  236. isToFriend: true,
  237. extendInfo: '',
  238. success: function(res) {
  239. BK.Console.log('分享成功', res.code, JSON.stringify(res.data));
  240. if(res.data.ret == 0) {
  241. success && success()
  242. }
  243. },
  244. fail: function(res) {
  245. BK.Console.log('分享失败', res.code, JSON.stringify(res.msg));
  246. fail && fail()
  247. },
  248. complete: function(res) {
  249. BK.Console.log('分享完成,不论成功失败');
  250. }
  251. });
  252. }
  253. }
  254. /// 跳转客服 是否为提现
  255. jumpCustomerServices(isDraw = false) {
  256. if (CC_QQPLAY || window.tt !== undefined) {
  257. AlertManager.showStoreQQaddGroup();
  258. } else if (CC_WECHATGAME) {
  259. if (isDraw) {
  260. wx.openCustomerServiceConversation({
  261. sessionFrom: 'draw',
  262. showMessageCard: false,
  263. });
  264. } else {
  265. wx.openCustomerServiceConversation({
  266. sessionFrom: 'shop',
  267. showMessageCard: true,
  268. sendMessageTitle: '商品',
  269. sendMessageImg: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_shop.png'
  270. });
  271. }
  272. }
  273. }
  274. static shareObjcts = [
  275. {'title': '猜猜他是谁?', 'icon': 'share_image_1.png'},
  276. {'title': '猜猜他是谁?', 'icon': 'share_image_2.png'},
  277. {'title': '看到这位明星了吗,签下这份合同,她就是你的人了~', 'icon': 'share_image_3.png'},
  278. {'title': '就、就算你是大明星,签约了你也是我的人!', 'icon': 'share_image_4.png'},
  279. {'title': '老板跟着小姨子跑路啦!本公司签约女团低价转让!', 'icon': 'share_image_5.png'},
  280. {'title': '你,还有你,我钦定你们两个。。。组CP!', 'icon': 'share_image_6.png'},
  281. {'title': '猜猜他是谁?', 'icon': 'share_image_7.png'},
  282. {'title': '猜猜他是谁?', 'icon': 'share_image_8.png'},
  283. {'title': '猜猜她是谁?', 'icon': 'share_image_9.png'}
  284. ]
  285. }
  286. module.exports = new WeChat();