Преглед изворни кода

微信官方隐私协议集成

zuy пре 6 месеци
родитељ
комит
96e7f537dc
3 измењених фајлова са 93 додато и 31 уклоњено
  1. 1 0
      src/manifest.json
  2. 82 25
      src/pages-user/login/index.vue
  3. 10 6
      src/pages/user/index.vue

+ 1 - 0
src/manifest.json

@@ -7,6 +7,7 @@
     "transformPx" : false,
     "mp-weixin" : {
         "appid" : "wxe466e6b630cc47ae",
+      "__usePrivacyCheck__": true,
         "optimization": { "subPackages": true },
         "setting": {
             "urlCheck": false,

+ 82 - 25
src/pages-user/login/index.vue

@@ -8,22 +8,40 @@
 
     <!-- 登录按钮 -->
     <div class="login-box">
-<!--      <div class="wechat-login-btn" @click="handleWechatLogin">
-        <img src="@/assets/wechat-icon.png" alt="微信" class="wechat-icon">
-        微信一键登录
-      </div>-->
-      <uv-button :disabled="!isAgreePrivacy||isAgreePrivacy.length===0"
-                 class="phone-login-btn" type="primary"        color="#C6171E"
-                 shape="circle"
-                 @getphonenumber="handleGetPhone"  open-type="getPhoneNumber">
-        手机号一键登录
+      <!--      <div class="wechat-login-btn" @click="handleWechatLogin">
+              <img src="@/assets/wechat-icon.png" alt="微信" class="wechat-icon">
+              微信一键登录
+            </div>-->
+<!--      <uv-button
+          class="phone-login-btn" type="primary" color="#C6171E"
+          shape="circle"
+          @click="handleShowModal"
+      >
+        一键登录
+      </uv-button>-->
+      <uv-button
+          class="phone-login-btn" type="primary" color="#C6171E"
+          shape="circle"
+          @getphonenumber="handleGetPhone" open-type="getPhoneNumber|agreePrivacyAuthorization">
+    一键登录
       </uv-button>
     </div>
 
+<!--
+    <uv-button
+        ref="phone_ref"
+        style="visibility: hidden;"
+        class="phone-login-btn" type="primary" color="#C6171E"
+        shape="circle"
+        @getphonenumber="handleGetPhone" open-type="getPhoneNumber">
+      一键登录
+    </uv-button>
+-->
+
     <!-- 隐私协议 -->
-    <div class="privacy-box">
+    <div class="privacy-box" style="display: none;">
       <view style="display: inline-flex" class="agreement">
-<!--        <uv-checkbox   v-model="isAgreePrivacy"    @change="handlePrivacyChange"></uv-checkbox>-->
+        <!--        <uv-checkbox   v-model="isAgreePrivacy"    @change="handlePrivacyChange"></uv-checkbox>-->
         <uv-checkbox-group
             v-model="isAgreePrivacy"
             shape="circle">
@@ -44,18 +62,18 @@
 </template>
 
 <script setup lang="ts">
-import { ref } from 'vue'
+import {ref} from 'vue'
 import {login} from "@/utils/auth";
 import {onLoad} from "@dcloudio/uni-app";
 
 const isAgreePrivacy = ref([])
-const redirectUrl =ref ("")
-const shortId =ref (null)
-const stationId =ref (null)
-
+const redirectUrl = ref("")
+const shortId = ref(null)
+const stationId = ref(null)
+const phone_ref = ref(null)
 
 
-onLoad((options:any)=>{
+onLoad((options: any) => {
   console.log(options)
   redirectUrl.value = options?.redirectUrl
   shortId.value = options?.shortId
@@ -71,6 +89,41 @@ const handleWechatLogin = () => {
   console.log('微信登录')
 }
 
+const handleShowModal = () => {
+ //  #ifdef MP-WEIXIN
+  // 1. 检查是否需要弹隐私协议(微信自动弹)
+  wx.getPrivacySetting({
+    success: res => {
+      if (res.needAuthorization) {
+        // 2. 拉起官方隐私协议弹窗
+        wx.requirePrivacyAuthorize({
+          success: () =>handleGetPhone1, // 同意后立即走授权
+          fail: () => uni.showToast({ title: '需同意协议', icon: 'none' })
+        })
+      } else {
+        // 已经同意过,直接拿手机号
+        handleGetPhone1()
+      }
+    }
+  })
+  // uni.showModal({
+  //   title: '提示',
+  //   content: '请先阅读用户协议和隐私政策?',
+  //   success: (res) => {
+  //     if (res.confirm) {
+  //       console.log("确认")
+  //       isAgreePrivacy.value = ["aa"];
+  //     } else {
+  //       isAgreePrivacy.value = [];
+  //     }
+  //   }
+  // })
+  // #endif
+}
+
+const handleGetPhone1 =async  () => {
+  phone_ref.value?.click();
+}
 
 // 处理隐私协议变更
 const handlePrivacyChange = () => {
@@ -80,24 +133,28 @@ const handlePrivacyChange = () => {
 // 显示用户协议
 const showUserAgreement = () => {
   uni.navigateTo({
-    url:'/pages-user/agreement/index'
+    url: '/pages-user/agreement/index'
   })
 }
 
 // 显示隐私政策
 const showPrivacyPolicy = () => {
   uni.navigateTo({
-    url:'/pages-user/policy/index'
+    url: '/pages-user/policy/index'
   })
 }
 
-const handleGetPhone = (e:any) => {
+const handleClickLoginBtn = () => {
+  console.log("handleClickLoginBtn")
+}
+
+const handleGetPhone = (e: any) => {
   console.log(e)
-  if(shortId.value){
-    e.shortId=shortId.value;
+  if (shortId.value) {
+    e.shortId = shortId.value;
   }
   login(e).then((token: string) => {
-    console.log(">>>>>>>>>",token)
+    console.log(">>>>>>>>>", token)
     uni.navigateBack()
   })
 }
@@ -138,7 +195,7 @@ const handleGetPhone = (e:any) => {
 
 .wechat-login-btn,
 .phone-login-btn {
-  color:#C6171E !important;
+  color: #C6171E !important;
   width: 100%;
   height: 44px;
   border-radius: 2px;
@@ -197,6 +254,6 @@ const handleGetPhone = (e:any) => {
 }
 
 .link {
-  color:$uni-color-primary
+  color: $uni-color-primary
 }
 </style>

+ 10 - 6
src/pages/user/index.vue

@@ -181,6 +181,8 @@ const logoutUser = () => {
             icon: "success",
             title: "已退出",
           });
+          isLogin.value = false;
+          getApp<any>().globalData.user = {};
           getApp<any>().globalData.manualLogout = true;
           clearToken();
           setTimeout(() => {
@@ -224,15 +226,17 @@ const removeListener = () => {
 
 onShow(() => {
   console.log("onShow")
-  let gd =  getApp<any>().globalData;
-  if(gd.refresh){
-    refreshUserProfile()
-  }else{
+  let gd = getApp<any>().globalData;
+  if (gd.refresh) {
+    if (gd.user?.id) {
+      refreshUserProfile()
+    }
+  } else {
     //半小时前登录的刷新
-    if(gd.user){
+    if (gd.user?.id) {
       if (new Date().getTime() - new Date(gd.user.lastLoginTime).getTime() > 300 * 1000) {
         refreshUserProfile()
-      }else{
+      } else {
         isLogin.value = true;
         user.value = gd.user;
       }