get-univerify-manager.uvue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <view class="uni-btn-v uni-common-mt">
  6. <button type="default" @click="verify(false)">一键登录(半屏)</button>
  7. </view>
  8. <view class="uni-btn-v uni-common-mt">
  9. <button type="default" @click="verify(true)">一键登录(全屏)</button>
  10. </view>
  11. <view class="uni-btn-v uni-common-mt">
  12. <button type="default" @click="customLoginIn()">一键登录(自定义页面)</button>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. title: '一键登录',
  22. uniVerifyManager: null as UniVerifyManager | null,
  23. phone: '',
  24. slogan: '',
  25. privacyName: '',
  26. privacyUrl: ''
  27. }
  28. },
  29. onLoad() {
  30. this.uniVerifyManager = uni.getUniVerifyManager();
  31. // 预登录
  32. this.preLogin(() => { });
  33. },
  34. methods: {
  35. customLoginIn() {
  36. if('production' === process.env.NODE_ENV && '__UNI__HelloUniAppX'===uni.getAppBaseInfo().appId){
  37. uni.showModal({
  38. title: '提示',
  39. content: '一键登录为收费功能,当前环境暂不支持。请在HBuilderX中新建Hello uni-app x项目真机运行体验!',
  40. showCancel: false
  41. })
  42. return
  43. }
  44. const isPreLoginValid = this.uniVerifyManager?.isPreLoginValid() ?? false;
  45. if (isPreLoginValid) {
  46. this.pushCustomPage();
  47. } else {
  48. this.preLogin(() => {
  49. this.pushCustomPage();
  50. })
  51. }
  52. },
  53. pushCustomPage() {
  54. const url = '/pages/API/get-univerify-manager/univerify-custom-page?phone=' + this.phone + '&slogan=' + this.slogan + '&name=' + this.privacyName + '&link=' + this.privacyUrl;
  55. uni.openDialogPage({
  56. url: url,
  57. animationType: 'slide-in-bottom',
  58. success(res) {
  59. console.log("成功打开自定义登录页面");
  60. },
  61. fail(err) {
  62. console.log(err);
  63. }
  64. })
  65. },
  66. verify(fullScreen : boolean) {
  67. if('production' === process.env.NODE_ENV && '__UNI__HelloUniAppX'===uni.getAppBaseInfo().appId){
  68. uni.showModal({
  69. title: '提示',
  70. content: '一键登录为收费功能,当前环境暂不支持。请在HBuilderX中新建Hello uni-app x项目真机运行体验!',
  71. showCancel: false
  72. })
  73. return
  74. }
  75. // 校验预登录是否有效
  76. const isPreLoginValid = this.uniVerifyManager?.isPreLoginValid() ?? false;
  77. if (isPreLoginValid) {
  78. // 预登录有效,执行登录
  79. this.login(fullScreen);
  80. } else {
  81. // 预登录无效,执行预登录
  82. this.preLogin(() => {
  83. this.login(fullScreen);
  84. })
  85. }
  86. },
  87. preLogin(callback: (() => void)) {
  88. this.uniVerifyManager?.preLogin({
  89. success: (res) => {
  90. this.phone = res.number;
  91. this.slogan = res.slogan;
  92. this.privacyName = res.privacyName;
  93. this.privacyUrl = res.privacyUrl;
  94. console.log("pre login success");
  95. callback();
  96. },
  97. fail: (err) => {
  98. console.error("pre login fail => " + JSON.stringify(err));
  99. const hasCauseMessage = (err.cause?.cause?.message ?? '').length > 0
  100. uni.showModal({
  101. title: '预登录失败',
  102. content: hasCauseMessage ? JSON.parseObject(err.cause?.cause?.message ?? '')?.getString("errorDesc") : err.errMsg,
  103. showCancel: false
  104. });
  105. }
  106. });
  107. },
  108. login(fullScreen : boolean) {
  109. this.uniVerifyManager?.login({
  110. uniVerifyStyle:{
  111. fullScreen: fullScreen,
  112. loginBtnText: "一键登录",
  113. logoPath: "/static/logo.png"
  114. },
  115. success: (res) => {
  116. console.log("login success => " + JSON.stringify(res));
  117. this.takePhoneNumber(res.accessToken,res.openId);
  118. },
  119. fail: (err) => {
  120. console.error("login fail => " + err);
  121. const hasCauseMessage = (err.cause?.cause?.message ?? '').length > 0
  122. uni.showModal({
  123. title: '登录失败',
  124. content: hasCauseMessage ? JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") : err.errMsg,
  125. showCancel: false
  126. });
  127. }
  128. });
  129. },
  130. takePhoneNumber(accessToken : string, openId : string) {
  131. // 云函数取号
  132. uniCloud.callFunction({
  133. name: 'univerify',
  134. data: {
  135. access_token: accessToken, // 客户端一键登录接口返回的access_token
  136. openid: openId // 客户端一键登录接口返回的openid
  137. }
  138. }).then(res => {
  139. // 关闭登录页
  140. this.uniVerifyManager?.close();
  141. setTimeout(() => {
  142. uni.showModal({
  143. title: '取号成功',
  144. content: res.result.getJSON("res")?.getString("phoneNumber"),
  145. showCancel: false
  146. });
  147. }, 100);
  148. }).catch(err => {
  149. console.error(JSON.stringify(err));
  150. // 关闭登录页
  151. this.uniVerifyManager?.close();
  152. setTimeout(() => {
  153. uni.showModal({
  154. title: '取号失败',
  155. content: (err as Error).message,
  156. showCancel: false
  157. });
  158. }, 100);
  159. });
  160. }
  161. }
  162. }
  163. </script>
  164. <style>
  165. </style>/style>