WxAuthor.js 3.0 KB

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