WeChat.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. // console.log('res');
  134. // console.log(res);
  135. if (res.errMsg == 'getUserInfo:ok') {
  136. button.destroy();
  137. this._login(res);
  138. }
  139. })
  140. return button;
  141. }
  142. inviteFriend(action, success, fail) {
  143. if (!CC_WECHATGAME) {
  144. return;
  145. }
  146. wx.shareAppMessage({
  147. title: '偷偷分享给你一个小程序,福利满满,你懂的',
  148. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share1.jpg',
  149. query: 'uid=' + Global.user.uid + '&shareType=' + action,
  150. success: success || function () { },
  151. fail: fail || function () { }
  152. });
  153. }
  154. shareAction(success, fail) {
  155. if (CC_WECHATGAME) {
  156. let randomIndex = parseInt(Math.random()*(WeChat.shareObjcts.length),10);
  157. let shareObjct = WeChat.shareObjcts[randomIndex];
  158. wx.shareAppMessage({
  159. title: shareObjct.title,
  160. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  161. query: 'shareType=' + ShareAction.NONE,
  162. success: (res) => {
  163. console.log('分享成功');
  164. //判断是否分享到群
  165. if (res.hasOwnProperty('shareTickets')) {
  166. if (success) {
  167. success();
  168. }
  169. } else {
  170. AlertManager.showShareFailAlert();
  171. console.log('分享的不是群');
  172. }
  173. SkillApi.report(2, (responseData) => {
  174. },(error) => {
  175. });
  176. },
  177. fail: (res) => {
  178. if (fail) {
  179. fail();
  180. }
  181. console.log('分享失败或取消');
  182. }
  183. });
  184. }
  185. }
  186. jumpCustomerServices() {
  187. if (CC_WECHATGAME) {
  188. wx.openCustomerServiceConversation({
  189. sessionFrom: 'shop',
  190. showMessageCard: true,
  191. sendMessageTitle: '商品',
  192. sendMessageImg: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_1.png'
  193. });
  194. }
  195. }
  196. static shareObjcts = [
  197. {'title': '就算你是大明星,签约了你就是我的人了!给我站住!', 'icon': 'share_1.png'},
  198. {'title': '签了我乌鸦哥,就等于签了整个东星,你老板你话事!', 'icon': 'share_2.png'},
  199. {'title': '今天买下宋仲基,明天签约赵丽颖,就差一个人合作这一票!', 'icon': 'share_3.png'},
  200. {'title': '我今天就是要让蔡徐坤给我打工,我话讲完,谁赞成?谁反对?', 'icon': 'share_4.png'},
  201. {'title': '老板。。。你签了我。。。今天打算让我做什么呀?', 'icon': 'share_5.png'},
  202. {'title': '什么?你把我买了?彭于晏和吴彦祖是我新同事?', 'icon': 'share_6.png'},
  203. ]
  204. }
  205. module.exports = new WeChat();