微信小程序学习实录7(H5嵌入小程序、获取微信收货地址、数组对象url传值、js获取url参数)

简介: 微信小程序学习实录7(H5嵌入小程序、获取微信收货地址、数组对象url传值、js获取url参数)

获取微信收货地址准备动作:在小程序管理后台,「开发」-「开发管理」-「接口设置」中自助开通该接口权限。

一、web-view加载H5

<web-view src="https://test.com/data/address.html"></web-view>


2.引入外部JS库

    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>获取微信收获地址</title>
    <!--微信小程序API-->
    <script src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <!--layUI核心库-->
    <script type="text/javascript" src="static/layui/layui.js" charset="utf-8"></script>
    <link href="static/layui/css/layui.css" type="text/css" rel="stylesheet">


3.构建HTML表单

<button type="button" class="layui-btn layui-btn-normal layui-btn-fluid" onclick="getAddInfo();">获取微信地址</button>
<div class="layui-fluid" style="margin-top: 20px;">
    <form class="layui-form layui-form-pane" lay-filter="component-form-group">
        <div class="layui-card">
            <div class="layui-card-header" style="text-align: center;font-weight: bold;">地址信息</div>
            <div class="layui-card-body">
                <div class="layui-form-item">
                    <label for="provinceName" class="layui-form-label">省份<span class="x-red">*</span></label>
                    <div class="layui-input-block"><input type="text" id="provinceName" name="provinceName" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="cityName" class="layui-form-label">地市<span class="x-red">*</span></label>
                    <div class="layui-input-block"><input type="text" id="cityName" name="cityName" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="countyName" class="layui-form-label">区县 <span class="x-red">*</span></label>
                    <div class="layui-input-block"><input type="text" id="countyName" name="countyName" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="detailInfo" class="layui-form-label">地址<span class="x-red">*</span></label>
                    <div class="layui-input-block"><input type="text" id="detailInfo" name="detailInfo" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="userName" class="layui-form-label">姓名 <span class="x-red">*</span></label>
                    <div class="layui-input-block"><input type="text" id="userName" name="userName" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="telNumber" class="layui-form-label">电话<span class="x-red">*</span></label>
                    <div class="layui-input-block"><input type="text" id="telNumber" name="telNumber" class="layui-input"></div>
                </div>
            </div>
        </div>
    </form>
</div>


4.判断运行宿主环境

  • wx.miniProgram.getEnv,API接口,在H5中使用,必须引入jweixin-1.6.0.js
    <!-- 判断运行宿主环境-->
    var wxApp = false;
    wx.miniProgram.getEnv(function (res) {
        if (res.miniprogram) {
            wxApp = true;
        } else {
            wxApp = false;
        }
    });


5.调用小程序地址

  • https://test.com,必须完成域名备案和业务域名验证;
  • wx.miniProgram.redirectTo,跳转到微信小程序,并传递相关参数;
    //调用小程序地址
    function getAddInfo() {
        if (wxApp) {
            wx.miniProgram.redirectTo({
                url: '/pages/wxaddr/wxaddr?posurl=https://test.com/data/address.html',//跳转回小程序页面,传参
                success: function () {
                    console.log('成功跳回小程序')
                },
                fail: function () {
                    console.log('跳转回小程序页面失败');
                },
            });
        } else {
            console.log('未在小程序宿主环境');
        }
    }


6.js获取url参数

    //获取参数;
    function getQueryVar(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return (false);
    }

二、小程序核心代码

1.wxaddr.wxml

  • userName,是一组从微信收货地址获取到的对象,在url传递过程中,转为字符串。
<web-view src="https://test.com/data/address.html?userName={{address}}"></web-view>


2.wxaddr.js

  • address赋值
    data: {
        address: '',
    },


  • 加载调用chooseAddress API接口
onLoad: function (options) {
        console.log(options)
        var that = this;
        if (options.posurl) {
            that.chooseAddress(options);
        } else {
            wx.navigateBack();
        }
    },


3.选择微信地址

  • address: JSON.stringify(res),赋值格式转换;
//选择微信地址
    chooseAddress: function (a) {
        console.log(a)
        var that = this;
        if (wx.chooseAddress) {
            wx.chooseAddress({
                success: function (res) {
                    // console.log(res);
                    that.setData({
                        address: JSON.stringify(res),
                        //具体收货地址显示
                        flag: false,
                    })
                },
                fail: function (err) {
                    console.log(JSON.stringify(err));
                    console.info("收货地址授权失败");
                    wx.showModal({
                        title: "网络超时",
                        content: "刷新重试",
                        showCancel: !1
                    });
                    that.setData({
                        bt: ''
                    });
                }
            })
        } else {
            console.log('当前微信版本不支持调用或网络超时,请手动输入地址或重试');
            that.setData({
                bt: ''
            });
        }
    },


@漏刻有时

相关文章
|
4天前
|
存储 JavaScript 索引
js开发:请解释什么是ES6的Map和Set,以及它们与普通对象和数组的区别。
ES6引入了Map和Set数据结构。Map的键可以是任意类型且有序,与对象的字符串或符号键不同;Set存储唯一值,无重复。两者皆可迭代,支持for...of循环。Map有get、set、has、delete等方法,Set有add、delete、has方法。示例展示了Map和Set的基本操作。
17 3
|
6天前
|
JavaScript 前端开发 开发者
JavaScript中的错误处理:try-catch语句与错误对象
【4月更文挑战第22天】JavaScript中的错误处理通过try-catch语句和错误对象实现。try块包含可能抛出异常的代码,catch块捕获并处理错误,finally块则无论是否出错都会执行。错误对象提供关于错误的详细信息,如类型、消息和堆栈。常见的错误类型包括RangeError、ReferenceError等。最佳实践包括及时捕获错误、提供有用信息、不忽略错误、利用堆栈信息和避免在finally块中抛错。
|
8天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的微信课堂助手小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的微信课堂助手小程序的详细设计和实现
40 3
|
8天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的英语学习交流平台的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的英语学习交流平台的详细设计和实现
25 2
|
8天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的微信阅读网站小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的微信阅读网站小程序的详细设计和实现
37 2
|
8天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的移动学习平台的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的移动学习平台的详细设计和实现
33 1
|
8天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的健身管理系统及会员微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的健身管理系统及会员微信小程序的详细设计和实现
30 0
|
2月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
27 0
|
2月前
|
消息中间件 Web App开发 JavaScript
Node.js【简介、安装、运行 Node.js 脚本、事件循环、ES6 作业队列、Buffer(缓冲区)、Stream(流)】(一)-全面详解(学习总结---从入门到深化)
Node.js【简介、安装、运行 Node.js 脚本、事件循环、ES6 作业队列、Buffer(缓冲区)、Stream(流)】(一)-全面详解(学习总结---从入门到深化)
77 0
|
5天前
|
JavaScript 前端开发 测试技术
学习JavaScript
【4月更文挑战第23天】学习JavaScript
11 1