auth.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // component/auth/auth.js
  2. const api = require('../../config/api')
  3. const util = require('../../utils/util.js');
  4. Component({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. show: false,
  10. canIUse: wx.canIUse('button.open-type.getUserInfo')
  11. },
  12. attached() {
  13. this.callback = () => {
  14. this.setData({
  15. show: !getApp().globalData.loginProps.isLogin
  16. })
  17. // let pages = getCurrentPages()
  18. // let currPage = pages[pages.length - 1].route
  19. // if (currPage === 'pages/discover/discover') {
  20. // this.setData({bottom: '50px'})
  21. // } else {
  22. // this.setData({bottom: 0})
  23. // }
  24. }
  25. this.callback()
  26. getApp().addLoginListener(this.callback)
  27. },
  28. detached() {
  29. getApp().removeLoginListener(this.callback)
  30. },
  31. methods: {
  32. quitHandle: function () {
  33. getApp().setLoginProps(true)
  34. },
  35. getCode: function () {
  36. return new Promise(r => {
  37. wx.login({
  38. success: function (res) {
  39. if (res.code) {
  40. r(res.code)
  41. }
  42. }
  43. });
  44. })
  45. },
  46. authLogin() {
  47. this.triggerEvent('login')
  48. },
  49. //获取用户信息接口
  50. getUserProfile: function () {
  51. return new Promise((resovle, reject) => {
  52. wx.getUserProfile({
  53. desc: "用于完善用户资料",
  54. //异步请求:回调函数中调用下一个函数
  55. success(res) {
  56. resovle(res)
  57. },
  58. fail(err) {
  59. wx.showModal({
  60. title: "登录失败",
  61. content: "网络异常,请重试",
  62. })
  63. reject(err)
  64. },
  65. });
  66. })
  67. },
  68. bindGetUserInfo: async function (e) {
  69. // let UserProfile = await this.getUserProfile()
  70. let UserProfile ={}
  71. console.error(UserProfile)
  72. return new Promise(async (resolve, reject) => {
  73. let code = await wxLogin()
  74. UserProfile.code = code
  75. // UserProfile.userInfo.signature = UserProfile.signature
  76. // UserProfile.userInfo.rawData = UserProfile.rawData
  77. // UserProfile.userInfo.encryptedData = UserProfile.encryptedData
  78. console.log(UserProfile)
  79. util.request(api.AuthLoginByWeixin, UserProfile, 'POST', 'application/json').then(res => {
  80. if (res.errno === 0) {
  81. //存储用户信息
  82. res.data.userInfo.userId = res.data.userId
  83. wx.setStorageSync('userInfo', res.data.userInfo);
  84. wx.setStorageSync('token', res.data.token);
  85. wx.setStorageSync('userId', res.data.userId);
  86. wx.setStorageSync('isLogin', true);
  87. getApp().setLoginProps(true)
  88. this.authLogin()
  89. } else {
  90. // util.showErrorToast(res.errmsg)
  91. // wx.showModal({
  92. // title: '提示',
  93. // content: res.errmsg,
  94. // showCancel: false
  95. // });
  96. }
  97. });
  98. // wx.getUserInfo({
  99. // withCredentials: true,
  100. // success(res) {
  101. // console.log(res)
  102. // //登录远程服务器
  103. // util.request(api.AuthLoginByWeixin, UserProfile, 'POST', 'application/json').then(res => {
  104. // if (res.errno === 0) {
  105. // //存储用户信息
  106. // res.data.userInfo.userId = res.data.userId
  107. // wx.setStorageSync('userInfo', res.data.userInfo);
  108. // wx.setStorageSync('token', res.data.token);
  109. // wx.setStorageSync('userId', res.data.userId);
  110. // wx.setStorageSync('isLogin', true);
  111. // getApp().setLoginProps(true)
  112. // this.authLogin()
  113. // } else {
  114. // // util.showErrorToast(res.errmsg)
  115. // // wx.showModal({
  116. // // title: '提示',
  117. // // content: res.errmsg,
  118. // // showCancel: false
  119. // // });
  120. // }
  121. // });
  122. // },
  123. // fail(err) {
  124. // wx.hideLoading()
  125. // reject(err)
  126. // }
  127. // })
  128. })
  129. // let code = await this.getCode()
  130. // //登录远程服务器
  131. // util.request(api.AuthLoginByWeixin, {
  132. // code: code,
  133. // userInfo: e.detail
  134. // }, 'POST', 'application/json').then(res => {
  135. // if (res.errno === 0) {
  136. // //存储用户信息
  137. // res.data.userInfo.userId = res.data.userId
  138. // wx.setStorageSync('userInfo', res.data.userInfo);
  139. // wx.setStorageSync('token', res.data.token);
  140. // wx.setStorageSync('userId', res.data.userId);
  141. // wx.setStorageSync('isLogin', true);
  142. // getApp().setLoginProps(true)
  143. // this.authLogin()
  144. // } else {
  145. // // util.showErrorToast(res.errmsg)
  146. // // wx.showModal({
  147. // // title: '提示',
  148. // // content: res.errmsg,
  149. // // showCancel: false
  150. // // });
  151. // }
  152. // });
  153. },
  154. }
  155. })
  156. function wxLogin() {
  157. return new Promise((resovle, reject) => {
  158. wx.login({
  159. success(res) {
  160. resovle(res.code)
  161. },
  162. fail(err) {
  163. wx.showModal({
  164. title: "登录失败",
  165. content: "网络异常,请重试",
  166. })
  167. reject(err)
  168. },
  169. })
  170. })
  171. }