doum
9 天以前 59b1f0e9967902aa10f5e017d5a0bdfd1b60c9ea
small-program/App.vue
@@ -1,51 +1,135 @@
<script>
   import { mapState } from 'vuex'
   var QQMapWX = require('@/utils/qqmap-wx-jssdk.js')
   var amapFile = require('@/utils/amap-wx.130.js')
   export default {
      computed: {
         ...mapState(['userInfo','openid'])
         ...mapState(['userInfo','token'])
      },
      onLaunch: function() {
      onLaunch: async function() {
         var that = this;
         if (!this.openid || this.openid =='') {
         if (!this.token) {
            uni.login({
               provider: 'weixin',
               success: async function (loginRes) {
                  let { code } = loginRes;
                  let res = await that.$u.api.wxLogin({ code })
                  if (res && res.code === 200) {
                     that.$store.commit('setToken', res.data.token)
                     that.$store.commit('setOpenid', res.data.openid)
                     that.$store.commit('setUserInfo', res.data.member)
                     console.log(res, '==================登录成功');
                     // 判断有无openid
                     if (res.data.openid) {
                        await that.$store.commit('setOpenid', res.data.openid)
                     }
                     // 判断有无token
                     if (res.data.token) {
                        await that.$store.commit('setToken', res.data.token)
                     }
                     // 判断有无用户信息
                     if (res.data.member) {
                        await that.$store.commit('setUserInfo', res.data.member)
                        await that.$store.commit('setOpenid', res.data.member.openId)
                     }
                     // 设置定位
                     await that.checkLocationAuth()
                  }
               }
            });
         } else {
            var that = this
            let res = this.$u.api.getMemberInfo({}).then(res =>{
               if (res && res.code === 200) {
                  that.$store.commit('setUserInfo', res.data)
               } else {
                  that.$store.commit('setToken','')
                  that.$store.commit('setUserInfo',{})
               }
            })
            // 缓存用户信息
            let res = await that.$u.api.getMemberInfo({})
            if (res.code === 200) {
               await that.$store.commit('setUserInfo', res.data)
               await that.$store.commit('setOpenid', res.data.openid)
            }
            // 设置定位
            await that.checkLocationAuth()
         }
         uni.showShareMenu({
           withShareTicket: true,
           menus: ['shareAppMessage', 'shareTimeline']
         })
      },
      onShow: function() {
         console.log('App Show')
         this.getLocaltionBiz()
         // this.getLocaltionBiz()
      },
      onHide: function() {
         console.log('App Hide')
      },
      methods:{
         // 定位
         positioning() {
            var that = this;
            uni.getLocation({
               type: 'gcj02',
               highAccuracyExpireTime: 3000,
               isHighAccuracy: true,
               success: function (addr) {
                  const locParam = { latitude: addr.latitude, longitude: addr.longitude };
                  var myAmapFun = new amapFile.AMapWX({ key: that.$gaodeMapKey});
                  myAmapFun.getRegeo({
                     location: addr.longitude + ',' + addr.latitude,
                     success: async function(data) {
                        let info = data[0];
                        console.log(info, '==================获取地址');
                        locParam.province = info.province;
                        locParam.city = info.regeocodeData.addressComponent.city;
                        var ta = info.desc || '地址获取失败';
                        locParam.address = ta
                        const resCity = await that.$u.api.getCityByName({ cityName: locParam.city })
                        if (resCity.code === 200 && resCity.data) {
                           locParam.cityId = resCity.data.id
                           that.$store.commit('setPosition', locParam)
                        } else {
                           that.$store.commit('setPosition', locParam)
                           that.$store.commit('clearCityId')
                        }
                        that.$isResolve()
                     },
                     fail: function(err) {
                        console.error('获取位置失败===========', err);
                        that.$store.commit('setPosition', locParam)
                        that.$isResolve()
                     }
                  });
               }
            });
         },
         goToAppSetting() {
            var that = this;
             uni.openSetting({
                 success: (res) => {
                  if (res.authSetting['scope.userLocation']) {
                     that.positioning()
                  }
                  that.$isResolve()
                 }
             });
         },
         checkLocationAuth() {
             uni.getSetting({
                 success: (res) => {
                     if (!res.authSetting['scope.userLocation']) {
                         uni.authorize({
                             scope: 'scope.userLocation',
                             success: () => {
                                 this.positioning()
                             },
                             fail: () => {
                                 uni.showModal({
                                     title: '提示',
                                     content: '需要获取您的位置信息,请在设置中开启位置权限',
                                     confirmText: '去设置',
                                     success: (res) => {
                                         if (res.confirm) {
                                             this.goToAppSetting();
                                         } else {
                                    this.$isResolve()
                                 }
                                     }
                                 });
                             }
                         });
                     } else {
                         this.positioning()
                     }
                 }
             });
         },
         getLocaltionBiz(){
            var that =this
            uni.getLocation({
@@ -53,35 +137,28 @@
               success: (addr) => {
                  const locParam = { latitude: addr.latitude, longitude: addr.longitude };
                  uni.setStorageSync('address', locParam);
                  var address =null
                  const qqmapsdk = new QQMapWX({
                      key: '4PXBZ-JLBC7-LCLXG-P2SZX-C4HYE-LDBLQ' //腾讯地图申请的key(后续需要公司提供,个人开发者额度有限)
                   });
                    // 腾讯地图逆地理编码
                  qqmapsdk.reverseGeocoder({
                     locParam,
                     success: function(res) {
                        console.log(res, '==================获取地址');
                        let info = res.result;
                        locParam.province = info.address_component.province;
                        locParam.city = info.address_component.city;
                        locParam.area = info.address_component.district;
                        locParam.street = info.address_component.street;
                        var ta = info.address  || '地址获取失败' ;
                        if(info.formatted_addresses  && info.formatted_addresses.recommend){
                           ta =info.formatted_addresses.recommend
                        }
                        locParam.address =ta
                        that.$store.commit('setPosition',locParam)
                  var address = null
                  var myAmapFun = new amapFile.AMapWX({ key: 'f2e6c0bfcac14c27b4f0d6d0d5b9d7d' });
                  myAmapFun.getRegeo({
                     location: addr.longitude + ',' + addr.latitude,
                     success: function(data) {
                        console.log(data, '==================获取地址');
                        let info = data[0];
                        locParam.province = info.province;
                        locParam.city = info.city;
                        locParam.area = info.district;
                        locParam.street = info.street;
                        var ta = info.name || '地址获取失败';
                        locParam.address = ta
                        that.$store.commit('setPosition', locParam)
                        that.$isResolve()
                     },
                     fail: (err) => {
                     fail: function(err) {
                        console.error('获取位置失败===========', err);
                        that.$store.commit('setPosition',locParam)
                        that.$store.commit('setPosition', locParam)
                        that.$isResolve()
                     }
                  });
                  // that.$store.commit('setPosition',locParam)
                  that.$isResolve()
               },
               fail: (err) => {
@@ -101,4 +178,124 @@
       width: 500px !important;
       font-size: 14px !important;
   }
   /*每个页面公共css */
   .tc {
      width: calc(100vw - 148rpx);
      .tc-btn {
         width: 100%;
         height: 102rpx;
         display: flex;
         align-items: center;
         justify-content: space-between;
         border-top: 1rpx solid #EEEEEE;
         .tc-btn-item {
            flex: 1;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 400;
            font-size: 32rpx;
            color: #666666;
            border-right: 1rpx solid #EEEEEE;
            &:last-child {
               border: none !important;
            }
         }
      }
      .tc-contemt {
         width: 100%;
         padding: 40rpx 0;
         box-sizing: border-box;
         display: flex;
         align-items: center;
         justify-content: center;
         flex-direction: column;
         .tc-contemt-title {
            width: 100%;
            text-align: center;
            font-weight: 600;
            font-size: 32rpx;
            color: #111111;
         }
         .tc-contemt-nr {
            width: 472rpx;
            text-align: center;
            font-weight: 400;
            font-size: 28rpx;
            color: #333333;
            margin-top: 40rpx;
         }
      }
   }
   .nomore{
      margin-top: 30rpx;
      width: 100%;
      text-align: center;
      font-size: 24rpx;
      color: #666666;
   }
   .phone {
      width: 100%;
      padding-top: 40rpx;
      box-sizing: border-box;
      .phone-botton {
         width: 100%;
         height: 88rpx;
         line-height: 88rpx;
         text-align: center;
         font-weight: 500;
         font-size: 32rpx;
         color: #FFFFFF;
         background: #00BC12;
         border-radius: 44rpx;
         margin-top: 60rpx;
      }
      .phone-head {
         width: 100%;
         text-align: center;
         justify-content: space-between;
         margin-bottom: 40rpx;
         text {
            font-weight: 800;
            font-size: 32rpx;
            color: #222222;
         }
      }
      .phone-item {
         width: 100%;
         text-align: center;
         display: flex;
         justify-content: center;
         flex-direction: column;
         // border-bottom: 1rpx solid #eeeeee;
         line-height: 78rpx;
         button{
            border: 1rpx white solid;
            display: inline;
            width: 300rpx;
            font-weight: 500;
            font-size: 32rpx;
            line-height: 32rpx;
            color: #222222;
         }
         image{
            width: 28rpx;
            height: 28rpx;
            margin-right: 30rpx;
         }
         .line{
            width: 100%;
            height: 1rpx;
            background-color: #f2f2f2
         }
         text {
            font-weight: 500;
            font-size: 28rpx;
            line-height: 28rpx;
            color: #222222;
         }
      }
   }
</style>