WxAuthor.js 2.8 KB

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