WeChat.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. const LoginApi = require('./LoginApi');
  2. const ShareAction = require('../utils/ShareAction');
  3. const artistInvite = [
  4. {
  5. title: '脱口秀主持人只能有一个...那就是你!',
  6. image: 'talent_1.png'
  7. },
  8. {
  9. title: '舞王争霸还差你,快来决一高下!',
  10. image: 'talent_2.png'
  11. },
  12. {
  13. title: '的演唱会等你来献唱~',
  14. image: 'talent_3.png'
  15. },
  16. {
  17. title: '新片开等你来主演!',
  18. image: 'talent_4.png'
  19. },
  20. {
  21. title: '的电竞战队需要你来carry!',
  22. image: 'talent_5.png'
  23. },
  24. ];
  25. class WeChat {
  26. constructor() {
  27. this.wxWidth = 0;//微信小程序可用屏幕宽度
  28. this.wxHeight = 0;//微信小程序可用屏幕高度
  29. this.js_code = null;
  30. }
  31. /**
  32. * 检查登录态是否已失效
  33. * 回调函数会回传flag值(true:未失效, false:已失效)
  34. * @param {Function} cb 回调方法
  35. */
  36. checkLogin(cb) {
  37. CC_WECHATGAME && wx.checkSession({
  38. success() {
  39. //session_key 未失效
  40. cb && cb(true)
  41. },
  42. fail() {
  43. //session_key 已失效
  44. cb && cb(false)
  45. }
  46. })
  47. }
  48. /**
  49. * 使用存在localStorage中的登录信息进行静默登录
  50. * 不刷新 uid 和 token
  51. * @param {Function} cb 回调方法
  52. */
  53. loginStatic(cb) {
  54. let userStorage = cc.sys.localStorage.getItem('GlobalUser')
  55. if (userStorage) {
  56. Global.user = userStorage;
  57. cb && cb()
  58. } else {
  59. this.login(cb)
  60. }
  61. }
  62. /**
  63. * 调起一个新的微信登录
  64. * 刷新 token
  65. * @param {Function} cb 回调方法
  66. */
  67. login(cb) {
  68. this.cb = cb || function () { }
  69. if (!CC_WECHATGAME) {
  70. Global.user = {
  71. uid: 1,
  72. token: 'lucifer_test_token',
  73. nick: 'lucifer',
  74. avatarUrl: 'http://mpic.tiankong.com/f94/72b/f9472be4691ab43e3f6132bb8b63d00a/640.jpg',
  75. gender: 1
  76. }
  77. this.cb();
  78. return;
  79. }
  80. this.isSupportInfoBtn = wx.createUserInfoButton ? true : false;
  81. wx.login({
  82. success: ({ code }) => {
  83. // console.log(code);
  84. this.js_code = code;
  85. //如果支持用户信息按钮
  86. if (this.isSupportInfoBtn) {
  87. this._createBtn();
  88. // 10月10日起微信禁止直接使用wx.getUserInfo获取用户信息
  89. // 需要先创建登录按钮
  90. // wx.getSetting({
  91. // success: (res) => {
  92. // // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  93. // if (res.authSetting['scope.userInfo']) {
  94. // this._getUserInfo()
  95. // } else {
  96. // }
  97. // }
  98. // })
  99. } else {
  100. this._getUserInfo()
  101. }
  102. }
  103. })
  104. wx.getSystemInfo({
  105. success: (res) => {
  106. Global.os = res.platform == "android" ? 2 : 1;
  107. this.wxWidth = res.windowWidth;
  108. this.wxHeight = res.windowHeight;
  109. }
  110. });
  111. }
  112. _getUserInfo() {
  113. wx.getUserInfo({
  114. withCredentials: true,
  115. success: (res) => {
  116. this._login(res);
  117. }
  118. })
  119. }
  120. _login(res) {
  121. // let loginApi = new LoginApi();
  122. LoginApi.login(this.js_code, res, this.cb);
  123. }
  124. /**
  125. * 创建一个微信环境下,获取登录信息的原生按钮
  126. */
  127. _createBtn() {
  128. let width = 167;
  129. let height = 88.5;
  130. let left = (this.wxWidth - width) / 2;
  131. let top = this.wxHeight - 380;
  132. console.log('top: ', top);
  133. let button = wx.createUserInfoButton({
  134. type: 'image',
  135. // text: '点击授权',
  136. image: 'https://pub.dwstatic.com/wxgame/allstar/share/startgame.png',
  137. style: {
  138. left: left,
  139. top: top,
  140. width: width,
  141. height: height,
  142. backgroundColor: '#ffffff',
  143. borderColor: '#ffffff',
  144. borderWidth: 0,
  145. borderRadius: 5,
  146. color: '#ffffff',
  147. textAlign: 'center',
  148. fontSize: 16,
  149. lineHeight: 88.5,
  150. },
  151. withCredentials: true
  152. })
  153. button.onTap((res) => {
  154. // console.log('res');
  155. // console.log(res);
  156. if (res.errMsg == 'getUserInfo:ok') {
  157. button.destroy();
  158. this._login(res);
  159. console.log(res);
  160. }
  161. })
  162. return button;
  163. }
  164. inviteFriendPromise() {
  165. return this._invite(ShareAction.ADD_FRIEND, '确认过眼神,你就是我要的C位', 'share1.jpg');
  166. }
  167. inviteArtistPromise() {
  168. let content = artistInvite[Math.floor(Math.random() * 5)];
  169. return this._invite(ShareAction.BECOME_ARTIST, content.title, content.image);
  170. }
  171. _invite(action, title, image) {
  172. return new Promise((resolve, reject) => {
  173. if (!CC_WECHATGAME) {
  174. reject();
  175. }
  176. wx.shareAppMessage({
  177. title: Global.user.nick + title,
  178. imageUrl: 'https://pub.dwstatic.com/wxgame/allstar/share/' + image,
  179. query: 'uid=' + Global.user.uid + '&shareType=' + action,
  180. success: resolve,
  181. fail: reject
  182. });
  183. });
  184. }
  185. }
  186. module.exports = new WeChat();