• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

小程序获取用户默认地址的代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

ps:今天好不开心,有个bug有复现了,不需要安慰不需要安慰

代码在下面

wxml:

<view class="address-wraper">
        <view bindtap="chooseAddress" wx:if="{{user_address_flag}}">
            <view class="address-mesg flex-container">
              <view class="mesg-title">收货人</view>
              <view class="mesg-content-p flex-one flex-container flex-between" style="padding-right:60rpx;">
                <view class="addressee">{{receiver}}</view>
                <view>{{mobile}}</view>
              </view>
            </view>
            <view class="address-mesg flex-container" style="margin-top:9rpx;">
              <view class="mesg-title">收货地址</view>
              <view class="flex-one mesg-content-p" style="padding-right:60rpx;">{{address}}</view>
              <image class="address-arrow" src="https://try.fishqc.com/img/go.png"/>
            </view>
        </view>
        <!-- 选择地址 -->
        <view wx:if="{{!user_address_flag}}" bindtap="chooseAddress" class="flex-container horizontal-between vertical-center" style="height:80rpx;margin-top:20rpx;">
            <view class="flex-one choose-address">请点击选择收货地址</view>
            <image style="width:17rpx;height:30rpx;" src="https://try.fishqc.com/img/go.png"/>
        </view>
    </view>
/* 地址 */

.address-wraper {
    padding: 0 30rpx 35rpx;
    background: #ffffff;
    margin-bottom: 24rpx;
    background: #ffffff url(https://buy.fishqc.com/img/address-line.png) no-repeat left bottom;
    background-size: contain;
    overflow: hidden;
}

.address-mesg {
    margin-top: 20rpx;
    position: relative;
}

.mesg-title {
    width: 128rpx;
    padding-right: 15rpx;
    font-family: 'PingFangSC-Light';
    font-size: 28rpx;
    color: #848a95;
    letter-spacing: 0;
    line-height: 40rpx;
}

.mesg-content-p {
    font-family: 'PingFangSC-Light';
    font-size: 24rpx;
    color: #2f3736;
    letter-spacing: 0;
    line-height: 36rpx;
}

.flex-between {
    justify-content: space-between;
    -webkit-justify-content: space-between;
}

.addressee {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 270rpx;
}

.address-arrow {
    width: 17rpx;
    height: 30rpx;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
}

.choose-address {
    min-height: 40rpx;
    background: url(https://try.fishqc.com/img/address.jpg) no-repeat;
    background-size: 32rpx 40rpx;
    padding-left: 50rpx;
    font-family: 'PingFangSC-Light';
    font-size: 32rpx;
    color: #2f3736;
    letter-spacing: 0;
    line-height: 40rpx;
}

.flex-one {
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
}

.vertical-center {
    align-items: center;
    -webkit-align-items: center;
}

.horizontal-between {
    justify-content: space-between;
    -webkit-justify-content: space-between;
}

.flex-container {
  display:flex;
}

  js部分

 judgeAuth:function(authStr) {
        return new Promise((resolve, reject) => {
            wx.getSetting({
                success(res) {
                    if (!(authStr in res.authSetting) || res.authSetting[authStr]) {
                        // 同意授权且调用对应api后取消操作
                        resolve(true)
                    } else {
                        // 未同意授权时,打开微信授权设定列表重新尝试获得授权
                        resolve(false)
                    }
                },
                fail(err) {
                    reject(err)
                }
            })
        })
    },
    chooseAddress: function chooseAddress() {
        if (this.pay_coundDown_flag) {
            return;
        }
        var that = this;
        // 先判断是否已经授权
        this.judgeAuth('scope.address').then(function (isSuccess) {
            if (isSuccess) {
                wx.chooseAddress({
                    success: function success(res) {
                        // 賦值
                        that.setData({ 
                            address: '' + res.provinceName + res.cityName + res.countyName + res.detailInfo,
                            receiver: res.userName,
                            mobile: res.telNumber,    
                            user_address_flag:true
                         })
                        console.log(that.data.address,101010)
                        //缓存收货信息
                        wx.setStorage({
                            key: 'receiver',
                            data: res.userName
                        });
                        wx.setStorage({
                            key: 'mobile',
                            data: res.telNumber
                        });
                        wx.setStorage({
                            key: 'address',
                            data: that.data.address
                        });
                    },
                    fail: function fail() {
                        // that.$parent.toasttips('选择地址失败')
                    }
                });
            } else {
                // 未授权则提醒用户打开授权列表
                wx.showModal({
                    title: '小鱼提示',
                    content: '请允许我们读取您的通信地址',
                    showCancel: false,
                    success: function success(res) {
                        if (res.confirm) {
                            wx.openSetting({
                                success: function success(res) {
                                    if (res.authSetting['scope.address']) {
                                        // 使用 call 调用将 this 指向指回 EvaluateCard 类上的原因是,这里直接调用 that.methods 会导致 this 指向 methods
                                        // 使其函数重新执行时,上面的this指向全部变为了methods
                                        that.methods.chooseAddress.call(that);
                                    } else {
                                        that.$parent.toasttips('选择地址需同意授权');
                                    }
                                    if (res.confirm) {
                                        console.log('用户点击确定')
                                    } else if (res.cancel) {
                                        console.log('用户点击取消')
                                    }
                                },
                                fail: function fail() {
                                    that.$parent.toasttips('调用授权列表管理失败');
                                }
                            });
                        }
                    }
                });
            }
        });
    },

  

       receiver:'',
        mobile:'',
        address:'',
        upAvater:false,
        avatar:''

  

  onLoad: function() {
        if (wx.getStorageSync('receiver') && wx.getStorageSync('mobile') && wx.getStorageSync('address')) {
            this.setData({
                user_address_flag:true,
                receiver:wx.getStorageSync('receiver'),
                mobile: wx.getStorageSync('mobile'),
                address: wx.getStorageSync('address')
            })
        }
        var that = this
        that.getuserInfo()
      
    },

  告辞!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
uniapp小程序--自定义分享标题发布时间:2022-07-18
下一篇:
微信小程序_(组件)canvas画布发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap