authorize.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { CACHE_USERINFO, CACHE_TOKEN, CACHE_EXPIRES_TIME } from '../../config.js'
  2. import Util from '../../utils/util.js'
  3. import { getLogo } from '../../api/api.js'
  4. import { login } from '../../api/user.js'
  5. let app = getApp()
  6. Component({
  7. properties: {
  8. // 当前授权窗口是否隐藏
  9. isHidden: {
  10. type: Boolean,
  11. value: true
  12. },
  13. // 是否自动登录
  14. isAuto: {
  15. type: Boolean,
  16. value: true
  17. },
  18. // 是否跳转到首页
  19. isGoIndex: {
  20. type: Boolean,
  21. value: true
  22. }
  23. }, // properties
  24. data: {
  25. errorCount: 0,
  26. errorMaxNum: 3,
  27. code: null, // wx login code
  28. canIUseGetUserProfile: false, // 判断是否为最新获取用户信息函数
  29. canIUseGetUserInfo: false
  30. }, // data
  31. attached() {
  32. this.get_logo_url()
  33. this.setAuthStatus()
  34. if (wx.getUserProfile) {
  35. this.setData({
  36. canIUseGetUserProfile: true
  37. })
  38. }
  39. if (wx.getUserInfo) {
  40. this.setData({
  41. canIUseGetUserInfo: true
  42. })
  43. }
  44. }, // attached
  45. methods: {
  46. close() {
  47. wx.hideLoading()
  48. let pages = getCurrentPages()
  49. let currPage = pages[pages.length - 1]
  50. if (this.data.isGoIndex) {
  51. wx.switchTab({
  52. url: '/pages/index/index'
  53. })
  54. } else {
  55. this.setData({
  56. isHidden: true
  57. })
  58. if (currPage && currPage.data.isHidden != undefined) {
  59. currPage.setData({
  60. isHidden: true
  61. })
  62. }
  63. }
  64. }, // close()
  65. get_logo_url: function () {
  66. var that = this
  67. if (wx.getStorageSync('logo_url')) {
  68. return this.setData({
  69. logo_url: wx.getStorageSync('logo_url')
  70. })
  71. }
  72. getLogo().then(res => {
  73. wx.setStorageSync('logo_url', res.data.logo_url)
  74. that.setData({
  75. logo_url: res.data.logo_url
  76. })
  77. })
  78. }, // get_logo_url()
  79. //检测登录状态并执行自动登录
  80. setAuthStatus() {
  81. var that = this
  82. Util.checkWxLogin()
  83. .then(res => {
  84. let pages = getCurrentPages()
  85. let currPage = pages[pages.length - 1]
  86. if (currPage && currPage.data.isHidden != undefined) {
  87. currPage.setData({
  88. isHidden: true
  89. })
  90. }
  91. that.triggerEvent('onLoadFun', app.globalData.userInfo)
  92. })
  93. .catch(err => {
  94. console.log(err)
  95. })
  96. }, // setAuthStatus()
  97. wxSilentLogin: function () {
  98. return new Promise((resolve, reject) => {
  99. wx.login({
  100. success(res) {
  101. resolve(res.code)
  102. },
  103. fail(err) {
  104. reject(err)
  105. }
  106. })
  107. })
  108. }, // wxSilentLogin
  109. // 获取用户信息
  110. agreeAndAuth() {
  111. let that = this
  112. wx.showLoading({
  113. title: '正在登录中'
  114. })
  115. let p1 = this.wxSilentLogin()
  116. let p2 = Util.wxGetUserProfile()
  117. Promise.all([p1, p2])
  118. .then(res => {
  119. let userInfo = res[1]
  120. userInfo.code = res[0]
  121. // console.log(userInfo);
  122. that.loginAuth(userInfo)
  123. })
  124. .catch(err => {
  125. wx.hideLoading()
  126. console.log('err:', err)
  127. })
  128. }, // agreeAndAuth
  129. // 从后台获取详细信息
  130. loginAuth: function (userInfo) {
  131. let that = this
  132. userInfo.spread_spid = app.globalData.spid //获取推广人ID
  133. userInfo.spread_code = app.globalData.code //获取推广人分享二维码ID
  134. userInfo.ch = app.globalData.channel // 注册渠道
  135. // 发送到后台
  136. login(userInfo)
  137. .then(res => {
  138. app.globalData.token = res.data.token
  139. app.globalData.isLog = true
  140. app.globalData.userInfo = res.data.userInfo
  141. app.globalData.expiresTime = res.data.expires_time
  142. wx.setStorageSync(CACHE_TOKEN, res.data.token)
  143. wx.setStorageSync(CACHE_EXPIRES_TIME, res.data.expires_time)
  144. wx.setStorageSync(CACHE_USERINFO, JSON.stringify(res.data.userInfo))
  145. if (res.data.cache_key) {
  146. wx.setStorage({
  147. key: 'cache_key',
  148. data: res.data.cache_key
  149. })
  150. }
  151. //取消登录提示
  152. wx.hideLoading()
  153. //关闭登录弹出窗口
  154. that.setData({
  155. isHidden: true,
  156. errorCount: 0
  157. })
  158. //执行登录完成回调
  159. that.triggerEvent('onLoadFun', app.globalData.userInfo)
  160. })
  161. .catch(err => {
  162. wx.hideLoading()
  163. that.data.errorCount++
  164. that.setData({
  165. errorCount: that.data.errorCount
  166. })
  167. if (that.data.errorCount >= that.data.errorMaxNum) {
  168. Util.Tips({
  169. title: err
  170. })
  171. }
  172. })
  173. }, // loginAuth()
  174. // 点击打开用户协议
  175. onTabRegular: function () {
  176. wx.navigateTo({
  177. url: `/pages/user_license/license`
  178. })
  179. } //onTabRegular
  180. } // methods
  181. })