WxAuthor.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const getReq = require('../config.js').getReq
  2. const api = require('../config.js').host
  3. const wxauthen = require('../config.js').wxauthen
  4. var ald = require('./ald-stat.js')
  5. export default class WxAuthor {
  6. fGetSetting = true
  7. constructor(app, getsetting) {
  8. this.target = app
  9. this.init(getsetting)
  10. }
  11. // 首先wx.login
  12. init(getsetting) {
  13. this.target.globalData.fMinistart = false
  14. this.fGetSetting = getsetting
  15. wx.login({
  16. success: res => {
  17. console.log('wx.login code:', res)
  18. this.ministart(res.code)
  19. }
  20. })
  21. }
  22. // 拿到code 请求act=login op=ministart
  23. ministart(code) {
  24. let params = {
  25. act: "login",
  26. op: "ministart",
  27. code
  28. }
  29. getReq(this.target, params, res => {
  30. this.target.globalData.fMinistart = true
  31. console.log('ministart', res)
  32. if (res.code == 200) {
  33. let { openid, unionid, share_title, share_image } = res.datas
  34. console.log('getReq 0', openid, unionid);
  35. if (share_title && share_image) {
  36. this.setShare(share_title, share_image)
  37. }
  38. if (openid && unionid) {
  39. this.setId(openid, unionid)
  40. wx.aldstat.sendOpenid(openid)
  41. wx.aldPushSendOpenid(openid)
  42. if (this.fGetSetting) {
  43. this.getSetting(false)
  44. }
  45. }
  46. else {
  47. if (this.fGetSetting) {
  48. this.getSetting(true)
  49. }
  50. }
  51. }
  52. })
  53. }
  54. getSetting(flag) {
  55. wx.getSetting({
  56. success: res => {
  57. if (res.authSetting['scope.userInfo']) {
  58. console.log('wx getSetting:', res)
  59. this.getUserInfo(flag)
  60. }
  61. }
  62. })
  63. }
  64. getUserInfo(flag) {
  65. let withCredentials = flag
  66. wx.getUserInfo({
  67. withCredentials,
  68. success: res => {
  69. this.setUserInfo(res.userInfo, res.userInfo.nickName)
  70. let userInfo = this.target.globalData.userInfo
  71. if (flag) {
  72. let { encryptedData, signature, iv } = res
  73. userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
  74. }
  75. else {
  76. userInfo = Object.assign({}, this.target.globalData.userId, userInfo)
  77. }
  78. wxauthen(this.target, userInfo,
  79. (res) => {
  80. let url = this.target.getUrl()
  81. if (url == '') {
  82. url = "pages/home/home";
  83. }
  84. console.log('url:', url)
  85. if (url != '/pages/index/index' && url != "pages/home/home") {
  86. wx.reLaunch({
  87. url
  88. })
  89. }
  90. },
  91. (err) => {
  92. wx.showToast({
  93. icon: 'none',
  94. title: '授权登陆失败',
  95. duration: 2000
  96. })
  97. }
  98. )
  99. }
  100. })
  101. }
  102. // 设置用户信息
  103. setUserInfo(userInfo, nickName) {
  104. this.target.globalData.userInfo = userInfo
  105. this.target.globalData.userInfo.nickname = nickName
  106. }
  107. //设置openid, unionid
  108. setId(openid, unionid) {
  109. this.target.globalData.userId.openid = openid
  110. this.target.globalData.userId.unionid = unionid
  111. var timeStamp = new Date().getTime();
  112. timeStamp = Math.floor(timeStamp);
  113. this.target.globalData.userId.timeStamp = timeStamp
  114. }
  115. //设置title, image
  116. setShare(title, image) {
  117. this.target.globalData.share_title = title;
  118. this.target.globalData.share_image = image;
  119. }
  120. }