WxAuthor.js 3.4 KB

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