WeChat.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. Global.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. Global.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. Global.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. if (CC_WECHATGAME) {
  142. Global.clickShare = true;
  143. Global.gameShareType = type;
  144. let randomIndex = parseInt(Math.random()*(WeChat.shareObjcts.length),10);
  145. let shareObjct = WeChat.shareObjcts[randomIndex];
  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. }
  173. }
  174. /// 跳转客服 是否为提现
  175. jumpCustomerServices(isDraw = false) {
  176. if (CC_WECHATGAME) {
  177. if (isDraw) {
  178. wx.openCustomerServiceConversation({
  179. sessionFrom: 'draw',
  180. showMessageCard: false,
  181. });
  182. } else {
  183. wx.openCustomerServiceConversation({
  184. sessionFrom: 'shop',
  185. showMessageCard: true,
  186. sendMessageTitle: '商品',
  187. sendMessageImg: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_shop.png'
  188. });
  189. }
  190. }
  191. }
  192. static shareObjcts = [
  193. {'title': '猜猜他是谁?', 'icon': 'share_image_1.png'},
  194. {'title': '猜猜他是谁?', 'icon': 'share_image_2.png'},
  195. {'title': '看到这位明星了吗,签下这份合同,她就是你的人了~', 'icon': 'share_image_3.png'},
  196. {'title': '就、就算你是大明星,签约了你也是我的人!', 'icon': 'share_image_4.png'},
  197. {'title': '老板跟着小姨子跑路啦!本公司签约女团低价转让!', 'icon': 'share_image_5.png'},
  198. {'title': '你,还有你,我钦定你们两个。。。组CP!', 'icon': 'share_image_6.png'},
  199. {'title': '猜猜他是谁?', 'icon': 'share_image_7.png'},
  200. {'title': '猜猜他是谁?', 'icon': 'share_image_8.png'},
  201. {'title': '猜猜她是谁?', 'icon': 'share_image_9.png'}
  202. ]
  203. }
  204. module.exports = new WeChat();