index.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // let pages = getCurrentPages(); //获取所有页面栈实例列表
  105. // let nowPage = pages[pages.length - 1]; //当前页页面实例
  106. // return {
  107. // title: share.title,
  108. // path: `/${nowPage.route}`,
  109. // imageUrl: share.imageUrl,
  110. // success(res) {
  111. // console.log('success(res)==', res);
  112. // },
  113. // fail(res) {
  114. // console.log('fail(res)==', res);
  115. // }
  116. // }
  117. // },
  118. getPhoneNumber(
  119. e: WechatMiniprogram.CustomEvent<{ encryptedData: string; iv: string; errMsg: string }>,
  120. ) {
  121. console.log(e, this.data.providerKey)
  122. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  123. return
  124. }
  125. const self = this
  126. if (!this.data.providerKey || this.data.providerKey.length === 0) {
  127. this.setData({
  128. loading: true,
  129. prepare: 'login',
  130. })
  131. AuthApi.Entrance({
  132. seCode: this.data.providerKey,
  133. encryptedData: e.detail.encryptedData,
  134. iv: e.detail.iv,
  135. })
  136. .then((rsp2) => {
  137. if (rsp2.result) {
  138. self.onwork(rsp2.result)
  139. return
  140. }
  141. self.setData({
  142. loading: false,
  143. prepare: 'need',
  144. })
  145. })
  146. .catch(() => {
  147. Toast({
  148. context: this,
  149. selector: '#t-toast',
  150. message: '授权不成功,请重试',
  151. icon: '',
  152. duration: 1000,
  153. })
  154. self.setData({
  155. loading: false,
  156. prepare: 'need',
  157. })
  158. })
  159. return
  160. }
  161. this.setData({
  162. loading: true,
  163. })
  164. AuthApi.Entrance({
  165. seCode: this.data.providerKey,
  166. encryptedData: e.detail.encryptedData,
  167. iv: e.detail.iv,
  168. })
  169. .then((rsp) => {
  170. console.log(rsp)
  171. if (rsp.result) {
  172. this.onwork(rsp.result)
  173. }
  174. })
  175. .finally(() => {
  176. this.setData({
  177. loading: false,
  178. })
  179. })
  180. },
  181. onwork(info: OpenUserInfo) {
  182. info.expiration = dayjs().add(info.expiration, 'second') as any
  183. wx.setStorageSync('__uvx', JSON.stringify(info))
  184. let url = ''
  185. if (this.data.returnUrl && this.data.returnUrl.length > 0) {
  186. url = this.data.returnUrl
  187. }
  188. if (url.length > 0) {
  189. Navi.redirectTo({
  190. url,
  191. })
  192. return
  193. }
  194. const pages = getCurrentPages() //获取小程序页面栈
  195. // const beforePage = pages[pages.length - 2] //获取上个页面的实例对象
  196. // beforePage.research(keyword) //触发上个页面自定义的shuaxin方法
  197. if (pages.length - 2 > -1) {
  198. const beforePage = pages[pages.length - 2]
  199. if (beforePage.init) {
  200. beforePage.init()
  201. }
  202. wx.navigateBack({
  203. delta: 1,
  204. })
  205. return
  206. }
  207. Navi.switchTab({
  208. url: '/pages/mine/personal',
  209. })
  210. },
  211. })