WxAuthor.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if(this.fGetSetting) {
  36. this.getSetting(false)
  37. }
  38. } else {
  39. if(this.fGetSetting) {
  40. this.getSetting(true)
  41. }
  42. }
  43. }
  44. })
  45. }
  46. getSetting(flag) {
  47. wx.getSetting({
  48. success: res => {
  49. if (res.authSetting['scope.userInfo']) {
  50. console.log('wx getSetting:',res)
  51. this.getUserInfo(flag)
  52. }
  53. }
  54. })
  55. }
  56. getUserInfo(flag) {
  57. let withCredentials = flag
  58. wx.getUserInfo({
  59. withCredentials,
  60. success: res => {
  61. this.setUserInfo(res.userInfo, res.userInfo.nickName)
  62. let userInfo = this.target.globalData.userInfo
  63. if (flag) {
  64. let { encryptedData, signature, iv } = res
  65. userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
  66. }
  67. else {
  68. userInfo = Object.assign({}, this.target.globalData.userId, userInfo)
  69. }
  70. this.wxauthen(userInfo)
  71. }
  72. })
  73. }
  74. wxauthen(userInfo) {
  75. let relay_id = this.target.globalData.relay_id
  76. let channel_num = this.target.globalData.channel_num
  77. let params = {
  78. user_info: userInfo,
  79. act: "login",
  80. op: "wxauthen",
  81. relay_id: relay_id,
  82. channel: channel_num
  83. }
  84. getReq(this.target,params, res => {
  85. if (res.code == 200) {
  86. this.target.globalData.member_id = res.datas.member_id
  87. this.target.globalData.hasmobile = res.datas.hasmobile
  88. let url = this.getUrl()
  89. wx.reLaunch({
  90. url
  91. })
  92. }
  93. else {
  94. wx.showToast({
  95. icon: 'none',
  96. title: '登陆失败',
  97. duration: 2000
  98. })
  99. }
  100. })
  101. }
  102. getUrl() {
  103. let len = getCurrentPages().length
  104. if(len < 1) return ''
  105. let currentPage = getCurrentPages()[len - 1]
  106. let params = ''
  107. if (currentPage.options) {
  108. for (let k in currentPage.options) {
  109. params = params + k + '=' + currentPage.options[k] + '&'
  110. }
  111. params = params.slice(0, params.length - 1)
  112. }
  113. let current_url = "/" + currentPage['route']
  114. if (params) {
  115. current_url = current_url + '?' + params
  116. }
  117. return current_url
  118. }
  119. // 设置用户信息
  120. setUserInfo(userInfo, nickName) {
  121. this.target.globalData.userInfo = userInfo
  122. this.target.globalData.userInfo.nickname = nickName
  123. }
  124. //设置openid, unionid
  125. setId(openid, unionid) {
  126. this.target.globalData.userId.openid = openid
  127. this.target.globalData.userId.unionid = unionid
  128. var timeStamp = new Date().getTime();
  129. timeStamp = Math.floor(timeStamp);
  130. this.target.globalData.userId.timeStamp = timeStamp
  131. }
  132. }