WxAuthor.js 3.7 KB

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