authorize_three.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { protocolEnum, protocolNavigateTo } from '/utils/common/protocol'
  2. import { tokenCheck, pushThree } from '/utils/api/auth_api'
  3. Page({
  4. data: {
  5. protocolEnum: protocolEnum,
  6. protocolAgree: false,
  7. status: {
  8. // 当前的申请状态: 0表示初始状态,1表示授权受限,2表示授权成功,3表示授权失败
  9. statusCode: 0,
  10. statusPrompt: ''
  11. },
  12. // 申请授权的商户信息
  13. formOption: {
  14. // 授权商户名称
  15. name: '',
  16. // 授权成功的商户服务器回调地址
  17. notify: null,
  18. // 认证分配的唯一token
  19. token: '',
  20. // 任务id
  21. taskId: ''
  22. }
  23. },
  24. protocol(e) {
  25. protocolNavigateTo(e.currentTarget.dataset.type)
  26. },
  27. protocolChange(e) {
  28. this.setData({
  29. protocolAgree: e.detail.value
  30. })
  31. },
  32. authClick() {
  33. if(!this.data.protocolAgree) {
  34. return
  35. }
  36. const authPopup = this.$selectComponent('#authPopup')
  37. authPopup.open(this.data.formOption.name)
  38. },
  39. // 授权获取三要素结果
  40. async authResult(e) {
  41. if(e.detail && e.detail.code === 200) {
  42. my.showLoading({
  43. content: "加载中..."
  44. })
  45. const params = {
  46. userId: e.detail.data.userId,
  47. certName: e.detail.data.username,
  48. certNo: e.detail.data.idNumber,
  49. certType: 'IDENTITY_CAR',
  50. phone: e.detail.data.phone,
  51. avatar: e.detail.data.avatar || null,
  52. personPictures: e.detail.data.personPictures || null,
  53. photoBase64: e.detail.data.photoBase64 || null,
  54. pushUrl: this.data.formOption.notify || null,
  55. taskId: this.data.formOption.taskId,
  56. token: this.data.formOption.token
  57. }
  58. // my.alert({
  59. // title: '提示authResult',
  60. // content: JSON.stringify(params)
  61. // })
  62. const result = await pushThree(params)
  63. if(result.data) {
  64. this.setData({
  65. status: {
  66. statusCode: 2,
  67. statusPrompt: '身份授权认证成功!'
  68. }
  69. })
  70. } else {
  71. this.setData({
  72. status: {
  73. statusCode: 1,
  74. statusPrompt: result.msg
  75. }
  76. })
  77. }
  78. my.hideLoading()
  79. } else {
  80. this.setData({
  81. status: {
  82. statusCode: 3,
  83. statusPrompt: '身份授权认证失败!'
  84. }
  85. })
  86. }
  87. },
  88. backClick() {
  89. my.navigateBackMiniProgram({
  90. extraData: {
  91. authStatus: (this.data.status && this.data.status.statusCode === 2) ? true : false
  92. }
  93. })
  94. },
  95. // 更新status状态
  96. updateStatus(status = null, obj = null) {
  97. if(status && Object.keys(status).length > 0) {
  98. // my.alert({
  99. // title: '提示updateStatus',
  100. // content: JSON.stringify(obj)
  101. // })
  102. this.setData({
  103. status,
  104. formOption: obj
  105. })
  106. } else {
  107. this.setData({
  108. status: {
  109. statusCode: 1,
  110. statusPrompt: '授权错误'
  111. }
  112. })
  113. }
  114. },
  115. async checkToken(options) {
  116. const result = await tokenCheck(options.token)
  117. // my.alert({
  118. // title: 'tokenCheck',
  119. // content: JSON.stringify(result)
  120. // })
  121. if(result.data) {
  122. this.updateStatus({statusCode: 0}, options)
  123. } else {
  124. this.updateStatus({statusCode: 1, statusPrompt: result.msg}, options)
  125. }
  126. },
  127. onLoad() {
  128. let launchOptions = my.getLaunchOptionsSync()
  129. if(!launchOptions.query) {
  130. launchOptions = my.getEnterOptionsSync()
  131. }
  132. // my.alert({
  133. // title: 'launchOptions',
  134. // content: JSON.stringify(launchOptions.query)
  135. // })
  136. if(launchOptions.query && Object.keys(launchOptions.query).length > 0 && launchOptions.query.token) {
  137. this.checkToken(launchOptions.query)
  138. } else {
  139. this.updateStatus()
  140. }
  141. },
  142. onShow: function () {
  143. my.hideBackHome()
  144. }
  145. });