WxAuthor.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. }
  31. else {
  32. this.getSetting(true)
  33. }
  34. }
  35. else {
  36. return
  37. }
  38. })
  39. }
  40. getSetting(flag) {
  41. wx.getSetting({
  42. success: res => {
  43. if (res.authSetting['scope.userInfo']) {
  44. this.getUserInfo(flag)
  45. }
  46. else {
  47. return
  48. }
  49. }
  50. })
  51. }
  52. getUserInfo(flag) {
  53. let withCredentials = flag
  54. wx.getUserInfo({
  55. withCredentials,
  56. success: res => {
  57. this.setUserInfo(res.userInfo, res.userInfo.nickName)
  58. let userInfo = this.target.globalData.userInfo
  59. if (flag) {
  60. let { encryptedData, signature, iv } = res
  61. userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
  62. }
  63. else {
  64. userInfo = Object.assign({}, this.target.globalData.userId, userInfo)
  65. }
  66. this.wxauthen(userInfo)
  67. }
  68. })
  69. }
  70. wxauthen(userInfo) {
  71. let params = {
  72. user_info: userInfo,
  73. act: "login",
  74. op: "wxauthen"
  75. }
  76. getReq(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. let currentPage = getCurrentPages()[len - 1]
  95. let params = ''
  96. if (currentPage.options) {
  97. for (let k in currentPage.options) {
  98. params = params + k + '=' + currentPage.options[k] + '&'
  99. }
  100. params = params.slice(0, params.length - 1)
  101. }
  102. let current_url = "/" + currentPage['route']
  103. if (params) {
  104. current_url = current_url + '?' + params
  105. }
  106. return current_url
  107. }
  108. // 设置用户信息
  109. setUserInfo(userInfo, nickName) {
  110. this.target.globalData.userInfo = userInfo
  111. this.target.globalData.userInfo.nickname = nickName
  112. }
  113. //设置openid, unionid
  114. setId(openid, unionid) {
  115. this.target.globalData.userId.openid = openid
  116. this.target.globalData.userId.unionid = unionid
  117. }
  118. }