WxAuthor.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. wx.reLaunch({
  91. url
  92. })
  93. }
  94. else {
  95. wx.showToast({
  96. icon: 'none',
  97. title: '登陆失败',
  98. duration: 2000
  99. })
  100. }
  101. })
  102. }
  103. getUrl() {
  104. let len = getCurrentPages().length
  105. if(len < 1) return ''
  106. let currentPage = getCurrentPages()[len - 1]
  107. let params = ''
  108. if (currentPage.options) {
  109. for (let k in currentPage.options) {
  110. params = params + k + '=' + currentPage.options[k] + '&'
  111. }
  112. params = params.slice(0, params.length - 1)
  113. }
  114. let current_url = "/" + currentPage['route']
  115. if (params) {
  116. current_url = current_url + '?' + params
  117. }
  118. return current_url
  119. }
  120. // 设置用户信息
  121. setUserInfo(userInfo, nickName) {
  122. this.target.globalData.userInfo = userInfo
  123. this.target.globalData.userInfo.nickname = nickName
  124. }
  125. //设置openid, unionid
  126. setId(openid, unionid) {
  127. this.target.globalData.userId.openid = openid
  128. this.target.globalData.userId.unionid = unionid
  129. var timeStamp = new Date().getTime();
  130. timeStamp = Math.floor(timeStamp);
  131. this.target.globalData.userId.timeStamp = timeStamp
  132. }
  133. }