index.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import dayjs from 'dayjs'
  2. import AuthApi from '../../services/auth'
  3. import Navi from '../../utils/navi'
  4. /* eslint-disable no-param-reassign */
  5. /* eslint-disable no-nested-ternary */
  6. //@ts-ignore
  7. import Toast from 'tdesign-miniprogram/toast/index'
  8. import { delay } from '../../utils/util'
  9. // pages/login/index.ts
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. providerKey: '',
  16. loading: false,
  17. returnUrl: '',
  18. prepare: '',
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad(query: { returnUrl: string }) {
  24. if (query && query.returnUrl) {
  25. this.setData({
  26. returnUrl: query.returnUrl,
  27. })
  28. }
  29. },
  30. /**
  31. * 生命周期函数--监听页面初次渲染完成
  32. */
  33. onReady() {},
  34. /**
  35. * 生命周期函数--监听页面显示
  36. */
  37. onShow() {
  38. wx.clearStorageSync()
  39. const self = this
  40. this.setData({
  41. loading: true,
  42. prepare: 'preinit',
  43. })
  44. wx.login({
  45. async success(res) {
  46. if (res.code) {
  47. AuthApi.Authenticate({
  48. providerAccessCode: res.code,
  49. })
  50. .then((rsp) => {
  51. if (rsp.result) {
  52. if (rsp.result.providerKey) {
  53. self.setData({
  54. providerKey: rsp.result.providerKey,
  55. prepare: 'need',
  56. })
  57. } else if (rsp.result.signature) {
  58. self.onwork(rsp.result)
  59. }
  60. }
  61. self.setData({
  62. loading: false,
  63. })
  64. })
  65. .catch(() => {
  66. self.setData(
  67. {
  68. loading: false,
  69. },
  70. async () => {
  71. await delay(1000)
  72. self.onShow()
  73. },
  74. )
  75. })
  76. } else {
  77. self.setData({
  78. loading: false,
  79. })
  80. }
  81. },
  82. })
  83. },
  84. /**
  85. * 生命周期函数--监听页面隐藏
  86. */
  87. onHide() {},
  88. /**
  89. * 生命周期函数--监听页面卸载
  90. */
  91. onUnload() {},
  92. /**
  93. * 页面相关事件处理函数--监听用户下拉动作
  94. */
  95. onPullDownRefresh() {},
  96. /**
  97. * 页面上拉触底事件的处理函数
  98. */
  99. onReachBottom() {},
  100. /**
  101. * 用户点击右上角分享
  102. */
  103. onShareAppMessage() {},
  104. getPhoneNumber(
  105. e: WechatMiniprogram.CustomEvent<{ encryptedData: string; iv: string; errMsg: string }>,
  106. ) {
  107. console.log(e, this.data.providerKey)
  108. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  109. return
  110. }
  111. const self = this
  112. if (!this.data.providerKey || this.data.providerKey.length === 0) {
  113. this.setData({
  114. loading: true,
  115. prepare: 'login',
  116. })
  117. AuthApi.Entrance({
  118. seCode: this.data.providerKey,
  119. encryptedData: e.detail.encryptedData,
  120. iv: e.detail.iv,
  121. })
  122. .then((rsp2) => {
  123. if (rsp2.result) {
  124. self.onwork(rsp2.result)
  125. return
  126. }
  127. self.setData({
  128. loading: false,
  129. prepare: 'need',
  130. })
  131. })
  132. .catch(() => {
  133. Toast({
  134. context: this,
  135. selector: '#t-toast',
  136. message: '授权不成功,请重试',
  137. icon: '',
  138. duration: 1000,
  139. })
  140. self.setData({
  141. loading: false,
  142. prepare: 'need',
  143. })
  144. })
  145. return
  146. }
  147. this.setData({
  148. loading: true,
  149. })
  150. AuthApi.Entrance({
  151. seCode: this.data.providerKey,
  152. encryptedData: e.detail.encryptedData,
  153. iv: e.detail.iv,
  154. })
  155. .then((rsp) => {
  156. console.log(rsp)
  157. if (rsp.result) {
  158. this.onwork(rsp.result)
  159. }
  160. })
  161. .finally(() => {
  162. this.setData({
  163. loading: false,
  164. })
  165. })
  166. },
  167. onwork(info: OpenUserInfo) {
  168. info.expiration = dayjs().add(info.expiration, 'second') as any
  169. wx.setStorageSync('__uvx', JSON.stringify(info))
  170. let url = ''
  171. if (this.data.returnUrl && this.data.returnUrl.length > 0) {
  172. url = this.data.returnUrl
  173. }
  174. if (url.length > 0) {
  175. Navi.redirectTo({
  176. url,
  177. })
  178. return
  179. }
  180. const pages = getCurrentPages() //获取小程序页面栈
  181. // const beforePage = pages[pages.length - 2] //获取上个页面的实例对象
  182. // beforePage.research(keyword) //触发上个页面自定义的shuaxin方法
  183. if (pages.length - 2 > -1) {
  184. const beforePage = pages[pages.length - 2]
  185. if (beforePage.init) {
  186. beforePage.init()
  187. }
  188. wx.navigateBack({
  189. delta: 1,
  190. })
  191. return
  192. }
  193. Navi.switchTab({
  194. url: '/pages/mine/personal',
  195. })
  196. },
  197. })