WeChat.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. wx.getSetting({
  88. success: (res) => {
  89. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  90. if (res.authSetting['scope.userInfo']) {
  91. this._getUserInfo()
  92. } else {
  93. this._createBtn();
  94. }
  95. }
  96. })
  97. } else {
  98. this._getUserInfo()
  99. }
  100. }
  101. })
  102. wx.getSystemInfo({
  103. success: (res) => {
  104. Global.os = res.platform == "android" ? 2 : 1;
  105. this.wxWidth = res.windowWidth;
  106. this.wxHeight = res.windowHeight;
  107. }
  108. });
  109. }
  110. _getUserInfo() {
  111. wx.getUserInfo({
  112. withCredentials: true,
  113. success: (res) => {
  114. this._login(res);
  115. }
  116. })
  117. }
  118. _login(res) {
  119. // let loginApi = new LoginApi();
  120. LoginApi.login(this.js_code, res, this.cb);
  121. }
  122. /**
  123. * 创建一个微信环境下,获取登录信息的原生按钮
  124. */
  125. _createBtn() {
  126. let width = 167;
  127. let height = 88.5;
  128. let left = (this.wxWidth - width) / 2;
  129. let top = this.wxHeight - 380;
  130. console.log('top: ', top);
  131. let button = wx.createUserInfoButton({
  132. type: 'image',
  133. // text: '点击授权',
  134. image: 'https://pub.dwstatic.com/wxgame/allstar/share/startgame.png',
  135. style: {
  136. left: left,
  137. top: top,
  138. width: width,
  139. height: height,
  140. backgroundColor: '#ffffff',
  141. borderColor: '#ffffff',
  142. borderWidth: 0,
  143. borderRadius: 5,
  144. color: '#ffffff',
  145. textAlign: 'center',
  146. fontSize: 16,
  147. lineHeight: 88.5,
  148. },
  149. withCredentials: true
  150. })
  151. button.onTap((res) => {
  152. // console.log('res');
  153. // console.log(res);
  154. if (res.errMsg == 'getUserInfo:ok') {
  155. button.destroy();
  156. this._login(res);
  157. }
  158. })
  159. return button;
  160. }
  161. inviteFriendPromise() {
  162. return this._invite(ShareAction.ADD_FRIEND, '确认过眼神,你就是我要的C位', 'share1.jpg');
  163. }
  164. inviteArtistPromise() {
  165. let content = artistInvite[Math.floor(Math.random() * 5)];
  166. return this._invite(ShareAction.BECOME_ARTIST, content.title, content.image);
  167. }
  168. _invite(action, title, image) {
  169. return new Promise((resolve, reject) => {
  170. if (!CC_WECHATGAME) {
  171. reject();
  172. }
  173. wx.shareAppMessage({
  174. title: Global.user.nick + title,
  175. imageUrl: 'https://pub.dwstatic.com/wxgame/allstar/share/' + image,
  176. query: 'uid=' + Global.user.uid + '&shareType=' + action,
  177. success: resolve,
  178. fail: reject
  179. });
  180. });
  181. }
  182. }
  183. module.exports = new WeChat();