qingyiay
2023-04-14 0becab45282781f281ad5aa04202b83039ab275e
pages/login/login.vue
@@ -2,7 +2,7 @@
   <view class="login">
      <view class="logo">无人值守</view>
      <!--用户名密码登录-->
      <user-password />
      <user-password :btnText="0" />
      <!-- 微信openid登录 -->
      <view class="loginBtn">
         <u-button class="button" @click.stop="beforeLoginEvent" :disabled="btnLoading" :loading="btnLoading" type="primary" text="微信一键登录"></u-button>
@@ -24,20 +24,27 @@
            </view>
         </u-popup>
      </view>
      <!-- 询问是否绑定微信 -->
      <view class="bindWxModel">
         <u-modal :show="bindWxModelShow" :title="bindWxModelTitle" showCancelButton :content="bindWxContent" @confirm="bindWxConfirm" @cancel="bindWxCancel"></u-modal>
      </view>
   </view>
</template>
<script>
import { apiLogin, apiLogin2, apiLoginPassword } from '@/api/publicInterface.js';
import { apiLoginWx, apiLoginPassword } from '@/api/publicInterface.js';
import { mapMutations } from 'vuex';
import userPassword from './userPassword.vue';
import { setToken, setRefreshToken, setUsernameKey, setCustomerId, redirectHome } from '@/utils/status.js';
export default {
   data() {
      return {
         wxcode: '', // 司机1
         btnLoading: false,
         loginPopupShow: false
         loginPopupShow: false,
         // 绑定微信模态框
         bindWxModelShow: false,
         bindWxModelTitle: '提示',
         bindWxContent: '如果已拥有系统账号,可以直接跳转绑定微信页面'
      };
   },
   components: {
@@ -54,8 +61,8 @@
         uni.login({
            success: res => {
               if (res.code) {
                  this.wxcode = res.code;
                  this.apiLogin();
                  uni.setStorageSync('code', res.code);
                  this.apiLogin(res.code);
               } else {
                  uni.showToast({
                     title: '微信登录失败!',
@@ -66,44 +73,59 @@
         });
      },
      // 登录接口
      apiLogin() {
         // apiLogin2({ code: this.wxcode })
         //    .then(res => {
         //       console.log('请求登录', res);
         //       if (res.msg == '需绑定') {
         //          uni.navigateTo({
         //             url: `/pages/register/register?code=${res.data}`
         //          });
         //       }
         //       if (res.msg == '登录成功') {
         //          uni.setStorageSync('userInfo', res.data);
         //          this.setUserTabbar(res.data.type);
         //          uni.switchTab({
         //             url: '/pages/tabbar-page/index-tabbar/index-tabbar'
         //          });
         //       }
         //    })
         //    .catch(err => {
         //       console.log('错误', err);
         //    });
      apiLogin(code) {
         /**
          * @openid
          * 获取微信用户openid,微信一键登录暂无相关接口,用于测试呼叫客服功能
          */
         wx.request({
            url: `https://api.weixin.qq.com/sns/jscode2session?appid=wx30f254cbb0b90b3c&secret=938a068410796cda21e5e03b9435cdbf&js_code=${this.wxcode}&grant_type=authorization_code`,
            success(res) {
               let id = res.data.openid;
               uni.setStorageSync('openid', id);
               uni.showToast({
                  title: '成功获取测试openid',
                  icon: 'none'
               });
            }
         uni.showLoading({
            title: '加载中...'
         });
         this.loginPopupShow = false
         // wx.request({
         //    url: `https://api.weixin.qq.com/sns/jscode2session?appid=wx4a62fc1684c429a9&secret=ee90afe376cdcec2fdd947c0a4db961a&js_code=${code}&grant_type=authorization_code`,
         //    success(res) {
         //       uni.setStorageSync('openid', res.data.openid);
         //       uni.showToast({
         //          title: '成功获取测试openid',
         //          icon: 'none'
         //       });
         //    }
         // });
         this.$nextTick(() => {
            apiLoginWx({ code: code }).then(res => {
               if (res.code != 1) {
                  this.$u.toast('登录成功');
                  // 登陆成功,存储相关信息
                  setToken(res.access_token);
                  setRefreshToken(res.refresh_token);
                  setUsernameKey(res.username);
                  //查询用户详细信息并储存
                  this.$reqGet('getUserEntity').then(res => {
                     this.setUserTabbar(res.data.type);
                     uni.setStorageSync('roleType', res.data.type);
                     setCustomerId(res.data.customerid);
                     uni.hideLoading();
                     //跳转页面
                     this.$nextTick(() => {
                        redirectHome();
                     });
                  });
               } else {
                  this.$u.toast(res.msg);
                  this.bindWxModelShow = true;
               }
            });
         });
         this.loginPopupShow = false;
      },
      bindWxConfirm() {
         uni.navigateTo({
            url: `/pages/login/wxLogin?code=${uni.getStorageSync('code')}`
         });
         this.bindWxModelShow = false;
      },
      bindWxCancel() {
         this.bindWxModelShow = false;
      },
      // 弹出层时间
      closePopup() {