WxAuthor.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 params = {
  71. user_info: userInfo,
  72. act: "login",
  73. op: "wxauthen",
  74. relay_id: 100
  75. }
  76. getReq(this.target,params, res => {
  77. if (res.code == 200) {
  78. let url = this.getUrl()
  79. wx.reLaunch({
  80. url
  81. })
  82. }
  83. else {
  84. wx.showToast({
  85. icon: 'none',
  86. title: '登陆失败',
  87. duration: 2000
  88. })
  89. }
  90. })
  91. }
  92. getUrl() {
  93. let len = getCurrentPages().length
  94. if(len < 1) return ''
  95. let currentPage = getCurrentPages()[len - 1]
  96. let params = ''
  97. if (currentPage.options) {
  98. for (let k in currentPage.options) {
  99. params = params + k + '=' + currentPage.options[k] + '&'
  100. }
  101. params = params.slice(0, params.length - 1)
  102. }
  103. let current_url = "/" + currentPage['route']
  104. if (params) {
  105. current_url = current_url + '?' + params
  106. }
  107. return current_url
  108. }
  109. // 设置用户信息
  110. setUserInfo(userInfo, nickName) {
  111. this.target.globalData.userInfo = userInfo
  112. this.target.globalData.userInfo.nickname = nickName
  113. }
  114. //设置openid, unionid
  115. setId(openid, unionid) {
  116. this.target.globalData.userId.openid = openid
  117. this.target.globalData.userId.unionid = unionid
  118. }
  119. }