WeChat.js 7.8 KB

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