当前位置:   article > 正文

uniapp微信小程序授权登录实现

uniapp微信小程序授权登录实现

我们在做小程序的时候用的最多的方式登录 就是原生的授权登录的功能 

这个方法 也不是很难  首先我们要获取我们在小程序中的code值

我们小封装一个获取code 的方法 在页面中可以直接调用 封装在 js 中

  1. export const wxlogin = () => {
  2. return new Promise((resolve, reject) => {
  3. uni.login({
  4. provider: 'weixin',
  5. success: function(loginRes) {
  6. resolve(loginRes.code);
  7. },
  8. fail(err) {
  9. reject(err)
  10. }
  11. })
  12. })
  13. }

把登录的要封装在一个单独的文件中,这样修改的时候,也方便修改

  1. <template>
  2. <view class="province_box" v-if="isPopupVisible">
  3. <view class="container">
  4. <view class="popupMobile_box">
  5. <view class="please-mobile">请授权手机号并登录</view>
  6. <view class="login_mobile">登录就代表已阅读并同意 <text style="color:#0091FF;">《用户隐私协议》</text></view>
  7. <view class="popup-action">
  8. <view class="action-button" @click="onCloseAuthMobile">
  9. 取消
  10. </view>
  11. <view>
  12. <button class="action-confirm" @click="onConfirmAuthMobile" open-type="getPhoneNumber"
  13. @getphonenumber="getPhoneNumber" hover-class="none">
  14. 授权手机号
  15. </button>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import {
  24. wxlogin
  25. } from '@/utils/wxlogin.js'
  26. export default {
  27. data() {
  28. return {
  29. wxPhoneParams: {
  30. authCode: "",
  31. iv: "",
  32. encryptedData: ""
  33. },
  34. isPopupVisible: false, // 初始状态为隐藏
  35. }
  36. },
  37. mounted() {
  38. this.towxlogin()
  39. },
  40. methods: {
  41. onOpenAuthMobile() {
  42. this.isPopupVisible = true; // 打开弹窗
  43. },
  44. async onShowAuthMobile() {
  45. await this.towxlogin();
  46. this.onOpenAuthMobile()
  47. },
  48. // 获取code 值
  49. async towxlogin() {
  50. let code = await wxlogin();
  51. this.wxPhoneParams.authCode = code;
  52. },
  53. //取消授权
  54. onCloseAuthMobile() {
  55. this.isPopupVisible = false; // 关闭弹窗
  56. },
  57. onConfirmAuthMobile() {
  58. this.isPopupVisible = false; // 关闭弹窗
  59. },
  60. //微信登录
  61. getPhoneNumber(e) {
  62. if (e.detail.iv && e.detail.encryptedData) {
  63. this.wxPhoneParams.iv = e.detail.iv
  64. this.wxPhoneParams.encryptedData = e.detail.encryptedData
  65. this.wxMobileLogin(this.wxPhoneParams)
  66. }
  67. },
  68. wxMobileLogin(wxParams) {
  69. console.log('登陆中....')
  70. // 这个里面就可以调用登录的时候传入的接口
  71. }
  72. }
  73. }
  74. </script>
  75. <style scoped lang="less">
  76. .province_box {
  77. position: fixed;
  78. top: 0;
  79. left: 0;
  80. width: 100%;
  81. height: 100%;
  82. background-color: rgba(0, 0, 0, 0.5);
  83. z-index: 999;
  84. }
  85. .container {
  86. background-color: #fff;
  87. border-radius: 8px 8px 0 0;
  88. width: 100%;
  89. max-width: 400px;
  90. position: absolute;
  91. bottom: 0;
  92. left: 50%;
  93. transform: translateX(-50%);
  94. box-sizing: border-box;
  95. padding-bottom: constant(safe-area-inset-bottom);
  96. padding-bottom: env(safe-area-inset-bottom);
  97. }
  98. .popupMobile_box {
  99. background-color: #fff;
  100. height: 380rpx;
  101. border-radius: 30rpx 30rpx 0 0;
  102. }
  103. .please-mobile {
  104. text-align: center;
  105. padding: 50rpx 0 30rpx;
  106. font-size: 34rpx;
  107. color: #000;
  108. }
  109. .login_mobile {
  110. text-align: center;
  111. font-size: 32rpx;
  112. color: #333;
  113. }
  114. .popup-action {
  115. display: flex;
  116. align-items: center;
  117. justify-content: space-between;
  118. margin: 70rpx 100rpx 0;
  119. .action-button {
  120. width: 240rpx;
  121. height: 80rpx;
  122. font-size: 32rpx;
  123. font-weight: 600;
  124. color: #9f9f9f;
  125. line-height: 80rpx;
  126. border: 1rpx solid #DFDFDF;
  127. text-align: center;
  128. border-radius: 10rpx;
  129. }
  130. .action-confirm {
  131. background: #0091FF;
  132. color: #fff;
  133. width: 240rpx;
  134. height: 80rpx;
  135. font-size: 32rpx;
  136. line-height: 80rpx;
  137. text-align: center;
  138. border-radius: 10rpx;
  139. }
  140. button {
  141. border-radius: 0rpx;
  142. }
  143. button:after {
  144. border: none;
  145. }
  146. }
  147. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/828749
推荐阅读
相关标签
  

闽ICP备14008679号