Kaynağa Gözat

fix 登录请求优化

wzh 4 ay önce
ebeveyn
işleme
232df62c68
3 değiştirilmiş dosya ile 101 ekleme ve 28 silme
  1. 20 2
      api/hexiao.js
  2. 76 25
      pages/hexiao/login.vue
  3. 5 1
      utils/requestAdmin.js

+ 20 - 2
api/hexiao.js

@@ -15,8 +15,26 @@ export function getVerificationCode(tel,userType,tenantId){
     })
 }
 
-
-
+/**
+ * 一键授权登录
+ * @param code
+ * @param userType
+ * @param jsCode
+ * @param tenantId
+ * @returns {*}
+ */
+export function oneKeyLogin(code,userType,jsCode,tenantId){
+    return request({
+        url: `/miniapp/oneKeyLogin`,
+        method: 'post',
+        params: {
+            userType:userType,
+            code:code,
+            jsCode:jsCode,
+            tenantId:tenantId
+        }
+    })
+}
 //登录
 export function login(tel,code,userType,jsCode,tenantId){
     return request({

+ 76 - 25
pages/hexiao/login.vue

@@ -4,7 +4,7 @@
       智能营销系统,欢迎您!
     </view>
 
-    <view class="main-card">
+    <view class="main-card" style="margin-top:20%">
 <!--      <image class="logo" :src="imgurl+'/cjx/hexiao/lobin_lobin.png'" mode="aspectFit"></image>-->
 
       <view class="user-type-selector">
@@ -26,42 +26,53 @@
         </view>
       </view>
 
-      <view class="form-group">
-        <view class="input-wrapper">
-          <uni-icons type="person" size="24" color="#cccccc"></uni-icons>
-          <input class="input-field" type="number" v-model="phone" placeholder="请输入手机号" placeholder-class="placeholder" />
-        </view>
-
-        <view class="input-wrapper">
-          <uni-icons type="locked" size="24" color="#cccccc"></uni-icons>
-          <input class="input-field" type="number" v-model="code" placeholder="请输入验证码" placeholder-class="placeholder" />
-          <text
-              class="code-btn"
-              :class="{ 'disabled': isCountingDown }"
-              @click="getVerificationCode"
-          >
-            {{ codeBtnText }}
-          </text>
-        </view>
+     <view v-if="showTel">
+       <view class="form-group">
+         <view class="input-wrapper">
+           <uni-icons type="person" size="24" color="#cccccc"></uni-icons>
+           <input class="input-field" type="number" v-model="phone" placeholder="请输入手机号" placeholder-class="placeholder" />
+         </view>
+
+         <view class="input-wrapper">
+           <uni-icons type="locked" size="24" color="#cccccc"></uni-icons>
+           <input class="input-field" type="number" v-model="code" placeholder="请输入验证码" placeholder-class="placeholder" />
+           <text
+               class="code-btn"
+               :class="{ 'disabled': isCountingDown }"
+               @click="getVerificationCode"
+           >
+             {{ codeBtnText }}
+           </text>
+         </view>
+       </view>
+
+       <button class="login-btn" @click="handleLogin">登 录</button>
+     </view>
+    <view v-if="!showTel">
+      <view>
+        请点击下方按钮进行授权登录
       </view>
-
-      <button class="login-btn" @click="handleLogin">登 录</button>
-
-<!--      <view class="version-info">-->
-<!--        版本号:V1.0.0-->
-<!--      </view>-->
+      <button class="login-btn" open-type="getPhoneNumber" @getphonenumber="handlePhone">
+        授权登录
+      </button>
+    </view>
+    <view style="font-size: 12px;margin-top:12%;" @click="showTel = !showTel">
+      <view v-if="!showTel">使用手机号验证码登录</view>
+      <view v-if="showTel">一键登录</view>
+    </view>
     </view>
   </view>
 </template>
 
 <script>
 // 1. 导入你的API方法
-import { getVerificationCode, login } from '@/api/hexiao.js';
+import { getVerificationCode, login ,oneKeyLogin} from '@/api/hexiao.js';
 import {getAdminInfo, getAdminToken, setAdminInfo, setAdminToken} from "@/utils/auth";
 
 export default {
   data() {
     return {
+      showTel: false,
       userType: 'distributor',
       phone: '',
       code: '',
@@ -91,6 +102,46 @@ export default {
     }
   },
   methods: {
+    handlePhone(e) {
+      if (e.detail.code) {
+        // this.loginDate.iv = e.detail.iv;
+        // this.loginDate.encryptedData = e.detail.encryptedData;
+        uni.showLoading({ title: '登录中...' });
+        let userType = 0;
+        if(this.userType === 'distributor'){
+          userType = 1;
+        }
+        if(this.userType === 'salesman'){
+          userType = 2;
+        }
+        let self = this;
+        uni.login({
+          provider: 'weixin', //使用微信登录
+          success: function (loginRes) {
+            let jsCode = loginRes.code;
+            oneKeyLogin( e.detail.code,userType,jsCode,self.tenantId).then(res => {
+              // 登录成功 (res.code === 0)
+              uni.hideLoading();
+
+              if (res.code === 0) {
+                setAdminToken(res.data.sessionId)
+                self.setUserInfo(res.data);
+                self.doLoginSuccess();
+              } else {
+                console.log('登录失败!' + res.msg);
+                uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
+              }
+            }).catch(err => {
+              // 登录失败 (res.code !== 0 或网络错误)
+              uni.hideLoading();
+              console.error('登录失败:', err);
+              uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
+            });
+          }
+        });
+
+      }
+    },
     doLoginSuccess(){
 
       let info = getAdminInfo()

+ 5 - 1
utils/requestAdmin.js

@@ -24,6 +24,9 @@ const request = config => {
   }
 
   return new Promise(resolve => {
+    uni.showLoading({
+      mask: true
+    });
     uni.request({
       method: config.method || 'get',
       timeout: config.timeout || timeout,
@@ -32,6 +35,7 @@ const request = config => {
       header: config.header,
       dataType: 'json'
     }).then(response => {
+      uni.hideLoading();
       const error = response.statusCode !== 200
       const res = response
 	  console.log(response)
@@ -62,7 +66,7 @@ const request = config => {
         if (getAdminToken()) {
           clearAdminToken();
           store.dispatch('LogOut').then(() => {
-            uni.reLaunch({ url: '/pages/cjx/hexiao/login' })
+            uni.reLaunch({ url: '/pages/hexiao/login' })
           })
         }
         result.success = false