quanyawei
2023-10-20 d8b41fff43a2cee6a8f714ffa807623b15803786
App.vue
@@ -1,97 +1,71 @@
<script>
import Vue from 'vue'
import {
   httpPost,
   httpGet
} from '@/utils/http.js'
export default {
   onLaunch: function() {
      // 自动登录检测
      this.getUserInfo()
   },
   methods: {
      /**
   import Vue from 'vue'
   import {
      getUserInfor,
      getDic
   } from '@/utils/login.js' // 配置文件
   import {
      httpPost,
      httpGet
   } from '@/utils/http.js'
   export default {
      onLaunch: function() {
         // 自动登录检测
         this.getUserInfo()
      },
      methods: {
         /**
          * 自动登录
          * 判断本地是否有账号信息,如果有,就自动重新登录
          *
          */
      getUserInfo() {
         uni.login({
            provider: 'weixin',
            success(res) {
               if (res.code) {
                  console.log('code', res.code)
                  // 调用接口。将登录code传给后端进行判断
                  httpGet('/AppUser/wx/login', {
                     'code': res.code
                  }).then((result)=> {
                     if (result.data.code === 0) {
                        // 存在用户
                     } else {
                        // 不存在用户
                        uni.setStorageSync('openId', result.data.openId)
                        uni.showModal({
                           title: '未登录',
                           content: '您未登录,需要登录后才能继续',
                           showCancel: false,
                           confirmText: '确定',
                           success: (res)=> {
                              if (res.confirm) {
                                 uni.reLaunch({
                                    url: '/pages/login/login',
                                 })
                              }
                           },
                        })
                     }
                  })
               } else {
                  console.log(res.errMsg)
               }
            }
         })
      },
      autoLogin() {
         console.log('Vue.prototype.$storage', Vue.prototype.$storage.get('tonken'))
         let tonken = this.$storage.get('utoken')
         console.log('tonken', tonken)
         // 判断本地是否有账号信息,如果有,就自动重新登录
         if (!tonken) {
            const params = {
               account: accountInFor.account,
               password: accountInFor.password,
            }
            uni.showLoading({
               title: '自动登录中...',
            })
            setTimeout(()=> {
               uni.reLaunch({
                  url: '/pages/actionChange/agencyPage/index',
               })
            }, 3000)
         } else {
            // 如果本地没有账号信息,就提示用户必须登录
            uni.showModal({
               title: '未登录',
               content: '您未登录,需要登录后才能继续',
               showCancel: false,
               confirmText: '确定',
               success: (res)=> {
                  if (res.confirm) {
                     uni.reLaunch({
                        url: '/pages/login/login',
         getUserInfo() {
            uni.login({
               provider: 'weixin',
               success(res) {
                  if (res.code) {
                     // 调用接口。将登录code传给后端进行判断
                     httpGet('/AppUser/wx/login', {
                        'code': res.code
                     }).then(result => {
                        if (result.data.code === 0) {
                           // 存在用户
                           uni.clearStorageSync()
                           uni.setStorageSync('token', result.data.token)
                           // setTimeout(() => {
                           //    uni.reLaunch({
                           //       url: '/pages/actionChange/agencyPage/index',
                           //    })
                           // }, 3000)
                           getUserInfor(result.data.token)
                           getDic()
                        } else {
                           // 不存在用户
                           uni.setStorageSync('openId', result.data.openId)
                           uni.showModal({
                              title: '未登录',
                              content: '您未登录,需要登录后才能继续',
                              showCancel: false,
                              confirmText: '确定',
                              success: res => {
                                 if (res.confirm) {
                                    uni.reLaunch({
                                       url: '/pages/login/login',
                                    })
                                 }
                              },
                           })
                        }
                     })
                  } else {
                     console.log(res.errMsg)
                  }
               },
               }
            })
         }
         },
      },
   },
}
   }
</script>
<style lang="scss">
   /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
   @import "@/uni_modules/uview-ui/index.scss";