WeChat.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const LoginApi = require('./LoginApi');
  2. const ShareAction = require('../utils/ShareAction');
  3. class WeChat {
  4. constructor() {
  5. this.wxWidth = 0;//微信小程序可用屏幕宽度
  6. this.wxHeight = 0;//微信小程序可用屏幕高度
  7. this.js_code = null;
  8. }
  9. /**
  10. * 检查登录态是否已失效
  11. * 回调函数会回传flag值(true:未失效, false:已失效)
  12. * @param {Function} cb 回调方法
  13. */
  14. checkLogin(cb) {
  15. CC_WECHATGAME && wx.checkSession({
  16. success () {
  17. //session_key 未失效
  18. cb && cb(true)
  19. },
  20. fail () {
  21. //session_key 已失效
  22. cb && cb(false)
  23. }
  24. })
  25. }
  26. /**
  27. * 使用存在localStorage中的登录信息进行静默登录
  28. * 不刷新 uid 和 token
  29. * @param {Function} cb 回调方法
  30. */
  31. loginStatic (cb) {
  32. let userStorage = cc.sys.localStorage.getItem('GlobalUser')
  33. if(userStorage) {
  34. Global.user = userStorage;
  35. cb && cb()
  36. } else {
  37. this.login(cb)
  38. }
  39. }
  40. /**
  41. * 调起一个新的微信登录
  42. * 刷新 token
  43. * @param {Function} cb 回调方法
  44. */
  45. login(cb) {
  46. this.cb = cb || function () { }
  47. if (!CC_WECHATGAME) {
  48. Global.showLoginPage = false;
  49. Global.user = {
  50. uid: 1,
  51. token: 'lucifer_test_token',
  52. nick: 'lucifer',
  53. avatarUrl: 'http://mpic.tiankong.com/f94/72b/f9472be4691ab43e3f6132bb8b63d00a/640.jpg',
  54. gender: 1
  55. }
  56. Global.channel = 'LuciferChannel';
  57. Global.os = 1;
  58. Global.ver = 1;
  59. this.cb();
  60. return;
  61. }
  62. this.isSupportInfoBtn = window.wx.createUserInfoButton ? true : false;
  63. window.wx.login({
  64. success: ({ code }) => {
  65. // console.log(code);
  66. this.js_code = code;
  67. //如果支持用户信息按钮
  68. if (this.isSupportInfoBtn) {
  69. window.wx.getSetting({
  70. success: (res) => {
  71. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  72. if (res.authSetting['scope.userInfo']) {
  73. this._getUserInfo()
  74. } else {
  75. this._createBtn();
  76. }
  77. }
  78. })
  79. } else {
  80. this._getUserInfo()
  81. }
  82. }
  83. })
  84. window.wx.getSystemInfo({
  85. success: (res) => {
  86. Global.os = res.platform == "android" ? 2 : 1;
  87. this.wxWidth = res.windowWidth;
  88. this.wxHeight = res.windowHeight;
  89. }
  90. });
  91. }
  92. _getUserInfo() {
  93. wx.getUserInfo({
  94. withCredentials: true,
  95. success: (res) => {
  96. this._login(res);
  97. }
  98. })
  99. }
  100. _login(res) {
  101. // let loginApi = new LoginApi();
  102. LoginApi.login(this.js_code, res, this.cb);
  103. }
  104. /**
  105. * 创建一个微信环境下,获取登录信息的原生按钮
  106. */
  107. _createBtn() {
  108. let width = 160;
  109. let height = 80;
  110. let left = (this.wxWidth - width) / 2;
  111. let top = (this.wxHeight - height) / 2;
  112. let button = window.wx.createUserInfoButton({
  113. type: 'text',
  114. text: '点击授权',
  115. style: {
  116. left: left,
  117. top: top,
  118. width: width,
  119. height: height,
  120. backgroundColor: '#B6ABAB',
  121. color: '#000000',
  122. textAlign: 'center',
  123. fontSize: 16,
  124. lineHeight: 80,
  125. borderRadius: 5
  126. }
  127. })
  128. button.onTap((res) => {
  129. // console.log('res');
  130. // console.log(res);
  131. if (res.errMsg == 'getUserInfo:ok') {
  132. button.destroy();
  133. this._login(res);
  134. }
  135. })
  136. return button;
  137. }
  138. inviteFriend() {
  139. if (!CC_WECHATGAME) {
  140. return;
  141. }
  142. window.wx.shareAppMessage({
  143. title: '偷偷分享给你一个小程序,福利满满,你懂的',
  144. imageUrl: 'http://t2.hddhhn.com/uploads/tu/201806/9999/eae9e74ec8.jpg',
  145. query: 'uid=' + Global.user.uid + '&shareType=' + ShareAction.ADD_FRIEND,
  146. });
  147. }
  148. }
  149. module.exports = new WeChat();