Parcourir la source

fix: "TODO: 微信登录"

ritchie il y a 2 ans
Parent
commit
2503c4d659
6 fichiers modifiés avec 158 ajouts et 110 suppressions
  1. 1 1
      src/service/bindWechat.js
  2. 70 34
      src/service/device/index.js
  3. 6 2
      src/service/login.js
  4. 51 50
      src/views/bindWechat.vue
  5. 28 21
      src/views/login.vue
  6. 2 2
      src/views/user.vue

+ 1 - 1
src/service/bindWechat.js

@@ -2,5 +2,5 @@ import axios from '../utils/axios';
 
 // 绑定微信
 export function bindWechat(params) {
-    return axios.get(`/SZWL-SERVER/tWechat/bindWechat?adminId=${params.adminId}`, params);
+    return axios.post(`/SZWL-SERVER/tWechat/bindWechat`, params);
 }

+ 70 - 34
src/service/device/index.js

@@ -1,140 +1,176 @@
 import axios from '../../utils/axios';
-import { stringToUrl } from '@/common/js/utils';
+import {stringToUrl} from '@/common/js/utils';
 
 
 // 获取设备列表
 export function getDeviceList(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/findList?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/findList?${stringToUrl(params)}`, params);
 }
+
 // 获取设备详情
 export function getDeviceDetal(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/findById?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/findById?${stringToUrl(params)}`, params);
 }
+
 // 修改设备详情
 export function updateDevice(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/update?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/update?${stringToUrl(params)}`, params);
 }
+
 // 消除报警
 export function eliminate(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/eliminate?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/eliminate?${stringToUrl(params)}`, params);
 }
+
 // 重启炉头/开启关闭炉头
 export function setFurnace(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/onOff?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/onOff?${stringToUrl(params)}`, params);
 }
+
 // 睡眠
 export function sleepEquipment(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/sleep?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/sleep?${stringToUrl(params)}`, params);
 }
+
 // 远程开门
 /* export function openDoor(params) {
   return axios.post(`/SZWL-SERVER/tEquipment/openDoor?${stringToUrl(params)}`, params);
 } */
 export function Api_openDoor(params) {
-  return axios.get(`/SZWL-SERVER/tEquipment/openDoor`, {params})
+    return axios.get(`/SZWL-SERVER/tEquipment/openDoor`, {params})
 }
+
 // 系统脱机
 export function deviceTuoji(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/tuoji?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/tuoji?${stringToUrl(params)}`, params);
 }
+
 // 音量调解
 export function updateVolume(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/updateVolume?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tEquipment/updateVolume?${stringToUrl(params)}`, params);
 }
+
 // 获取商品下拉列表
 export function selectProducts(params) {
-  return axios.get(`/SZWL-SERVER/tProduct/selectProducts?${stringToUrl(params)}`);
+    return axios.get(`/SZWL-SERVER/tProduct/selectProducts?${stringToUrl(params)}`);
 }
+
 // 远程做糖
 export function doSugar(params) {
-  return axios.get(`/SZWL-SERVER/tSugarDo/doSugar?${stringToUrl(params)}`);
+    return axios.get(`/SZWL-SERVER/tSugarDo/doSugar?${stringToUrl(params)}`);
 }
+
 // 查询做糖状态
 export function selectSugarStatus(params) {
-  return axios.get(`/SZWL-SERVER/tSugarDo/selectSugarStatus?${stringToUrl(params)}`);
+    return axios.get(`/SZWL-SERVER/tSugarDo/selectSugarStatus?${stringToUrl(params)}`);
 }
+
 // 今日做糖列表
 export function selectSugarList(params) {
-  return axios.get(`/SZWL-SERVER/tSugarDo/selectSugarList?${stringToUrl(params)}`);
+    return axios.get(`/SZWL-SERVER/tSugarDo/selectSugarList?${stringToUrl(params)}`);
 }
+
 // 查询定时开关列表
 export function getAlaramClockList(params) {
-  return axios.get(`/SZWL-SERVER/tAlarmClock/list?${stringToUrl(params)}`);
+    return axios.get(`/SZWL-SERVER/tAlarmClock/list?${stringToUrl(params)}`);
 }
+
 // 新增闹钟
 export function addAlaramClock(params) {
-  return axios.post(`/SZWL-SERVER/tAlarmClock/add?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tAlarmClock/add?${stringToUrl(params)}`, params);
 }
+
 // 删除闹钟
 export function deleteAlaramClock(params) {
-  return axios.post(`/SZWL-SERVER/tAlarmClock/delete?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tAlarmClock/delete?${stringToUrl(params)}`, params);
 }
+
 // 闹钟回显
 export function getAlaramClock(params) {
-  return axios.post(`/SZWL-SERVER/tAlarmClock/selectOne?${stringToUrl(params)}`, params);
+    return axios.post(`/SZWL-SERVER/tAlarmClock/selectOne?${stringToUrl(params)}`, params);
 }
+
 // 修改闹钟
 export function alaramClockUpdate(params) {
-  return axios.post(`/SZWL-SERVER/tAlarmClock/updata`, params);
+    return axios.post(`/SZWL-SERVER/tAlarmClock/updata`, params);
 }
+
 // 获取旗下的机器
 export function getListEquipment(params) {
-  return axios.get(`/SZWL-SERVER/tEquipment/listEquipment?${stringToUrl(params)}`)
+    return axios.get(`/SZWL-SERVER/tEquipment/listEquipment?${stringToUrl(params)}`)
 }
+
 // 设备充值
 export function rechargeAjax(params) {
-  return axios.get(`/SZWL-SERVER/tOrder/equipmentPay?${stringToUrl(params)}`)
+    return axios.get(`/SZWL-SERVER/tOrder/equipmentPay?${stringToUrl(params)}`)
 }
+
 // 获取参数设置
 export function getParameters(params) {
-  return axios.post(`/SZWL-SERVER/tParameters/getParameters`, params)
+    return axios.post(`/SZWL-SERVER/tParameters/getParameters`, params)
 }
+
 // 修改参数设置
 export function updateParameters(params) {
-  return axios.post(`/SZWL-SERVER/tParameters/updateParameters`, params)
+    return axios.post(`/SZWL-SERVER/tParameters/updateParameters`, params)
 }
 
 // 分销人回显
 export function getProportion(params) {
-  return axios.post(`/SZWL-SERVER/tProportion/getOne?${stringToUrl(params)}`)
+    return axios.post(`/SZWL-SERVER/tProportion/getOne?${stringToUrl(params)}`)
 }
+
 // 提交审批或修改
 export function saveProportion(params) {
-  return axios.post(`/SZWL-SERVER/tProportion/save`, params)
+    return axios.post(`/SZWL-SERVER/tProportion/save`, params)
 }
+
 // 单个/批量修改商品价格
 export function Api_getUpdaProdPrice(params) {
-  return axios.get(`/SZWL-SERVER/tProduct/updateProduct`, {params})
+    return axios.get(`/SZWL-SERVER/tProduct/updateProduct`, {params})
 }
+
 // 一键补料
 export function Api_getReplenishment(params) {
-  return axios.get(`/SZWL-SERVER/tEquipment/buliao`, {params})
+    return axios.get(`/SZWL-SERVER/tEquipment/buliao`, {params})
 }
+
 // 远程
 export function Api_getTApkInfo_updateApk(params) {
-  return axios.get(`/SZWL-SERVER/tApkInfo/updateApk`, {params})
+    return axios.get(`/SZWL-SERVER/tApkInfo/updateApk`, {params})
 }
+
 // 修改优惠码开关状态
 export function Api_getDiscCodeStatus(params) {
-  return axios.get(`/SZWL-SERVER/tEquipment/updateCouponStatus`, {params})
+    return axios.get(`/SZWL-SERVER/tEquipment/updateCouponStatus`, {params})
 }
+
 // 修改睡眠描述
 export function changeSleepDesc(params) {
-  return axios.get(`/SZWL-SERVER/tEquipmentDesc/updateSleepDesc`, {params})
+    return axios.get(`/SZWL-SERVER/tEquipmentDesc/updateSleepDesc`, {params})
 }
+
 // 删除设备
 export function delOneDevice(params) {
-  return axios.post(`/SZWL-SERVER/tEquipmentDeleted/delOneDevice`, {params})
+    return axios.post(`/SZWL-SERVER/tEquipmentDeleted/delOneDevice`, {params})
 }
+
 // 屏蔽/显示商品
 export function updateProductsShow(params) {
-  return axios.post(`/SZWL-SERVER/tProduct/updateProductsShow`, params)
+    return axios.post(`/SZWL-SERVER/tProduct/updateProductsShow`, params)
 }
+
 // 远程修改机器密码
 export function updateDevicePassword(params) {
-  return axios.post(`/SZWL-SERVER/tEquipment/updatePassword`, params)
+    return axios.post(`/SZWL-SERVER/tEquipment/updatePassword`, params)
 }
+
 // 定制Logo
 export function customLogo(params) {
-  return axios.post(`/SZWL-SERVER/tLogo/customLogo`, params)
+    return axios.post(`/SZWL-SERVER/tLogo/customLogo`, params)
+}
+
+// 上传日志
+export function uploadLog(params) {
+    return axios.post('/SZWL-SERVER/Log/uploadLog', params);
 }

+ 6 - 2
src/service/login.js

@@ -9,7 +9,11 @@ export function login(params) {
 export function getSys(params) {
   return axios.get(`/SZWL-SERVER/tLogo/getLogo?${stringToUrl(params)}`);
 }
+// 微信授权 -> 获取 openid
+export function getOpenid() {
+  return axios.get('/SZWL-SERVER/wxLogin/menuOauth').then(response => response.data);
+}
 // 微信登录
-export function wxLogin(params) {
-  return axios.get('/SZWL-SERVER/wxLogin/menuOauth', params);
+export function wxLogin(openid) {
+  return axios.get('/SZWL-SERVER/wxLogin/wxLogin', { params: { openid } }).then(response => response.data);
 }

+ 51 - 50
src/views/bindWechat.vue

@@ -5,37 +5,22 @@
         :name="$t('wechat.headerName')"
         :noback="false"
     ></s-header>
-
-
-<!--    <div class="loginLogoBox l-re">-->
-<!--      &lt;!&ndash;      用户头像 用户昵称 &ndash;&gt;-->
-<!--      <van-image class="userAvatar" :src="avatarUrl"></van-image>-->
-<!--      <view class="title">-->
-<!--        {{ 用户昵称 }}-->
-<!--      </view>-->
-<!--    </div>-->
-
-
     <div class="loginFormBox">
       <van-form @submit="onSubmit">
-        <van-field
-            v-model="userName"
-            name="userName"
-            label=""
-            :placeholder="$t('login.userNameInput')"
-            :rules="[{ required: true, message: $t('login.userNameInput') }]"
-        />
-        <van-field
-            v-model="userPwd"
-            type="password"
-            name="userPwd"
-            label=""
-            :placeholder="$t('login.passWordInput')"
-            :rules="[{ required: true, message: $t('login.passWordInput') }]"
-        />
-
-
+        <!-- 微信头像 -->
+        <div style="display: flex;justify-content: center;align-items: center">
+          <van-image
+              width="100px"
+              height="100px"
+              :src="avatarUrl"
+              fit="cover"
+              radius="50%"
+              style="margin: auto"
+          />
+        </div>
 
+        <br>
+        <br>
         <div class="buttonBox">
           <!-- 取消绑定微信,返回登录页 -->
           <van-button
@@ -51,9 +36,10 @@
               round
               type="primary"
               native-type="submit"
-          >{{ $t('bindWechat.bindCommit') }}</van-button>
-
+          >{{ $t('bindWechat.bindCommit') }}
+          </van-button>
         </div>
+
       </van-form>
     </div>
   </div>
@@ -61,20 +47,19 @@
 
 <script>
 import {onMounted, ref} from "vue";
-import { Toast } from "vant";
-import { bindWechat } from "@/service/bindWechat";
+import {Toast} from "vant";
+import {bindWechat} from "@/service/bindWechat";
 import {setLocal, getLocal, navigatorLanguage, getLoginUser} from "@/common/js/utils";
 import sHeader from "@/components/SimpleHeader";
 import logiLogoImgUrl from "@/assets/login/logo.png";
-import {  useRouter } from "vue-router";
-import { useI18n } from "vue-i18n";
-
+import {useRouter} from "vue-router";
+import {useI18n} from "vue-i18n";
 
 export default {
   setup() {
     let languageName = ref(getLocal("curLang"));
-    const { t } = useI18n();
-
+    // eslint-disable-next-line no-unused-vars
+    const {t} = useI18n();
 
     const checked = ref(false); // 是否记住密码状态
     const userName = ref("");
@@ -82,7 +67,7 @@ export default {
     const router = useRouter();
     const sys = ref(null);
     // 页面初始化
-    onMounted(() => {
+    onMounted(async () => {
       // localStorage.clear();
       // 如果没有语言缓存
       if (!getLocal("curLang")) {
@@ -90,7 +75,6 @@ export default {
         setLocal("curLang", navigatorLanguage());
         languageName.value = getLocal("curLang");
       }
-
     });
 
     const user = getLoginUser();
@@ -101,20 +85,33 @@ export default {
     }
     // 绑定微信
     const onSubmit = async () => {
-      const { data } = await bindWechat({
-        adminId: user.id,
-      });
-
-      if (data.code === "00000") {
-        Toast(t('bindWechat.bindSuccess'));
-        await window.open(data.data, "_self")
-        // await router.push({path: '/user'});
-      } else {
-        Toast.fail(data.message);
+      try {
+        const response = await bindWechat({
+          adminId: user.id,
+        })
+        if (response.data.code === 200) {
+          window.location.href = response.data.data;
+        } else {
+          Toast.fail("Failed to retrieve authorization link.");
+        }
+      } catch (error) {
+        console.error(error);
+        Toast.fail("绑定失败")
       }
 
     };
 
+    // 监听页面hash变化
+    window.addEventListener("hashchange", async () => {
+      if (window.location.hash === "#wechatBindSuccess") {
+        // 绑定成功
+        Toast.success("绑定成功");
+        await router.push("/user");
+      } else if (window.location.hash === "#wechatBindFailure") {
+        // 绑定失败
+        Toast.fail("绑定失败");
+      }
+    });
 
     return {
       logiLogoImgUrl,
@@ -126,7 +123,7 @@ export default {
       cancelClick,
     };
   },
-  components: { sHeader },
+  components: {sHeader},
 };
 </script>
 
@@ -148,6 +145,7 @@ export default {
       height: 87px;
       margin-top: 41px;
     }
+
     .languageCon {
       right: 10px;
       top: 10px;
@@ -270,6 +268,7 @@ export default {
     justify-content: center;
     align-items: center;
   }
+
   .wechat-btn {
     width: 60px;
     height: 60px;
@@ -278,10 +277,12 @@ export default {
     border: none;
     border-radius: 50%; /* 将按钮样式修改为圆形 */
   }
+
   .wechat-icon {
     width: 40px;
     height: 40px;
   }
+
   .buttonBox {
     width: 281px;
     margin: 20px auto 0 auto;

+ 28 - 21
src/views/login.vue

@@ -52,8 +52,9 @@
         <br>
         <br>
         <!-- 微信登录 -->
-        <div v-if="isInWeChat" class="loginWithWechatClass">
-          <van-button class="wechat-btn" type="primary" size="large" @click="wxLoginHandler" >
+        <!--        <div v-if="isInWeChat" class="loginWithWechatClass">-->
+        <div class="loginWithWechatClass">
+          <van-button class="wechat-btn" type="primary" size="large" @click="wxLoginHandler">
             <van-icon name="wechat" color="#1bd66c" size="40" class="wechat-icon"/>
           </van-button>
         </div>
@@ -80,7 +81,7 @@
 import md5 from "js-md5";
 import {onMounted, ref, computed, reactive} from "vue";
 import {Toast, Dialog, Button} from "vant";
-import {login, getSys, wxLogin} from "@/service/login";
+import {login, getSys, wxLogin, getOpenid} from "@/service/login";
 import {setLocal, getLocal, navigatorLanguage} from "@/common/js/utils";
 import sHeader from "@/components/SimpleHeader";
 import logiLogoImgUrl from "@/assets/login/logo.png";
@@ -92,6 +93,7 @@ import {Locale} from "vant";
 import enUS from "vant/es/locale/lang/en-US";
 // 引入简体中文语言包
 import zhCN from "vant/es/locale/lang/zh-CN";
+
 export default {
   setup() {
     let languageName = ref(getLocal("curLang"));
@@ -168,36 +170,38 @@ export default {
     };
     // 跳转注册页面
     const registerClick = async () => {
-      router.push("/register");
+      await router.push("/register");
     };
     // 跳转忘记密码页面
     const forgetPassword = async () => {
-      router.push("/forgetpassword");
+      await router.push("/forgetpassword");
     };
 
-
     const state = reactive({
       isLoading: false
     })
-
+    // 微信登录
     const wxLoginHandler = async () => {
-      state.isLoading = true
+      state.isLoading = true;
       try {
-        // 用户默认授权,获取 openid
-        const {data} = await wxLogin();
-        if (data.code === "00000") {
-          Toast.success(t('login.loginSucess'));
-          setTimeout(() => {
-            router.push("/home");
-          }, 200);
-        } else {
-          // 用户未绑定微信
-          handleError('您还未绑定微信,请先前往个人中心进行绑定。')
+        // 用户静默授权,获取 openid
+        const { data: { data: { openid } } } = await getOpenid();
+        if (openid) {
+          // 调用后端接口进行微信登录
+          const { data: { code, message } } = await wxLogin(openid);
+          if (code === "00000") {
+            // 登录成功,跳转到 home 页面
+            await router.push('/home');
+            return;
+          }
+          throw new Error(message || '登录失败');
         }
+        // 未获取到 openid
+        throw new Error('微信授权失败');
       } catch (error) {
-        handleError(error.message || '登录失败,请重试')
+        handleError(error.message || '登录失败,请重试');
       } finally {
-        state.isLoading = false
+        state.isLoading = false;
       }
     }
 
@@ -226,7 +230,9 @@ export default {
       languageClk,
       isInWeChat,
       state,
-      wxLoginHandler
+      wxLoginHandler,
+      getOpenid,
+
     };
   },
   components: {
@@ -378,6 +384,7 @@ export default {
     justify-content: center;
     align-items: center;
   }
+
   .wechat-btn {
     width: 60px;
     height: 60px;

+ 2 - 2
src/views/user.vue

@@ -183,8 +183,8 @@
           </div>
 
           <!-- 绑定微信 -->
-          <div v-if="isInWeChat" class="taskListRow flex-col" @click="pushPageList('/bindWechat')">
-<!--            <div class="taskListRow flex-col" @click="pushPageList('/bindWechat')">-->
+<!--          <div v-if="isInWeChat" class="taskListRow flex-col" @click="pushPageList('/bindWechat')">-->
+            <div class="taskListRow flex-col" @click="pushPageList('/bindWechat')">
             <div class="taskIcon bindWechatIcon"></div>
             <div class="taskRight">
               <div class="taskTitle">