2007 年 1 月 9 日,乔布斯在旧金山莫斯科尼会展中心发布了首款 iPhone,而在十年后的 1 月 9 日,微信小程序正式上线。张小龙以这样的形式,向乔布斯致敬。

小程序在哪里?

小程序功能模块在“发现”频道最下方的位置。
如果没有,请先将微信升级到最新版本,然后在通讯录搜索‘小程序示例’,点击之后返回“发现频道”即可。
Tip:小程序搜索目前不支持模糊查询

小程序会带来什么

无处不在,随时访问

移动互联网的下一站是“唾手可得”
——张小龙

切入正题:怎么开发?

1. 获取微信小程序的 AppID
登录 https://mp.weixin.qq.com,就可以在网站的“设置”-“开发者设置”中,查看到微信小程序的 AppID 了,注意不可直接使用服务号或订阅号的 AppID 。

2. 下载开发者工具
下载地址:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=201715

新建项目

只是尝试一把,可以选择游客模式,无需填写AppId

小程序 IDE-编辑

小程序 IDE-调试

界面类似于chrome浏览器调试界面

项目结构

App.js 是全局脚本文件
App.json 是全局配置文件
App.wxss 是全局样式
Pages 中为页面

创建页面

在小程序IDE中,配置文件保存的同时,pages中会自动创建页面文件夹,文件夹中包含以下四种文件:
order.js order.json order.wxml order.wxss

逻辑层(App Service)

1.小程序开发框架的逻辑层是由JavaScript编写。
2.逻辑层将数据进行处理后发送给视图层,同时接受视图层的事件反馈(数据单向绑定,vue是双向绑定,小程序没有vm层)。
每个页面有独立的作用域,并提供模块化能力。
3.由于框架并非运行在浏览器中,所以 JavaScript 在 web 中一些能力都无法使用,如 document,window 等。
4.开发者写的所有代码最终将会打包成一份 JavaScript,并在小程序启动的时候运行,直到小程序销毁。类似 ServiceWorker,所以逻辑层也称之为 App Service。

App( )

App( ) 函数接受的 object 参数

getApp( )

全局的getApp( )函数,可以获取到小程序实例。

Page()

Page()函数用来注册一个页面。接受一个 object 参数,其指定页面的初始数据、生命周期函数、事件处理函数等。

setData()

setData()函数用于将数据从逻辑层发送到视图层,同时改变对应的this.data

坑:直接修改 this.data 无效,无法改变页面的状态,还会造成数据不一致

模块化

1
2
3
4
5
var common = require('common.js')
Page({
  helloMINA: function() {
    common.sayHello('MINA')
  },})

WXSS(WeiXin Style Sheets)

就是微信小程序的css,新增了尺寸单位rpx:

rpx(responsive pixel): 可以根据屏幕宽度进行自适应。规定屏幕宽为750rpx。如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则750rpx = 375px = 750物理像素,1rpx = 0.5px = 1物理像素

组件

小程序中没有a标签,页面跳转需要用navigator组件

Navigator 页面链接 <navigator url="url">跳转</navigator>

其他组件

媒体组件,地图,画布,在此不一一赘述,请大家查看小程序API

https://mp.weixin.qq.com/debug/wxadoc/dev/api/?t=201715

最后附上快递查询功能代码,调用快递100物流查询公共接口

index.wxml

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--index.wxml-->
<view class="container">
  <view bindtap="bindViewTap" class="userinfo">
    <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">欢迎您,{{userInfo.nickName}}</text>
  </view>
  <view class="section">
    <input placeholder-style="color:white" class="order-input" placeholder="请输入订单号" bindchange="bindChange" id="orderId" />
    <input placeholder-style="color:white" class="order-input" placeholder="请输入快递公司" bindchange="bindChange" id="company" />
  </view>
  <text>{{errTip}}</text>
  <button class="query-btn" type="default" size="{{primarySize}}" loading="" plain="true" disabled="{{disabled}}" bindtap="query"> 查询 </button>
</view>

index.js

小程序只支持https的请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var app = getApp()
// var inputCon = {}
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    inputCon: {},
    errTip: ''
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  bindChange: function (e) {
    var id;
    var value;
    id = e.target.id;
    value = e.detail.value + '';
    this.data.inputCon[id] = value;
  },
  // query
  query: function () {
    // 测试用例
    // 417941822755 zhongtong
    // 884044987614211278 yuantong
    var that = this;
    var type = that.data.inputCon.company;
    var postid = that.data.inputCon.orderId;
    var data = {
      'type':type,
      'postid':postid
    }
    app.globalData.queryInfo.push(data);
    console.log(app)
 
    wx.request({
      url: 'https://www.kuaidi100.com/query',
      data: data,
      header: {
        'content-type''application/json'
      },
      success: function (res) {
        var errTip = res.data.message;
        var orderInfo = res.data.data;
        if(orderInfo.length == 0) {
 
          console.log(errTip)
          that.setData({
            errTip:errTip
          })
          return
        }
        that.setData({
            errTip:''
          })
        app.globalData.orderInfo = orderInfo;
        wx.navigateTo({
          url: '../order/order'
        })
      }
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  }
})

订单物流页面

order.wxml

1
2
3
4
5
6
7
<!--pages/order/order.wxml-->
<view class="order-list">
  <block wx:for="{{orders}}">
    <view class="order-time"> {{item.ftime}}: </view>
    <view class "order-txt"> {{item.context}} </view>
  </block>
</view>

order.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// pages/order/order.js
var app = getApp();
Page({
  data:{
    orders:[]
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var orderInfo = app.globalData.orderInfo;
    this.setData({
      orders: orderInfo
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})

微信小程序(一)快递查询的更多相关文章

  1. 微信小程序-全国快递查询

    微信小程序-全国快递查询 摘要:WeChat.小程序.JS 开发过程 源码下载 1. GitHub 2. 百度云 链接:https://pan.baidu.com/s/1XVbtT2JsZslg4Y0 ...

  2. 微信小程序之媒体查询@media

    微信小程序支持媒体查询,代码如下: @media screen and (min-width: 700px) { .container { padding: 30rpx; } } @media scr ...

  3. 微信小程序调用快递物流查询API的实现方法

    一. 创建index.wxml.index.wxss.index.js 附上代码: <view class='container'> <input class='info' plac ...

  4. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

  5. 微信小程序 教程及示例

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有,转载请联系作者获得授权.微信小程序正式公测, ...

  6. 微信小程序学习指南

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  7. 微信小程序资料集合

    一:官方地址集合: 1:官方工具:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=1476434678461 2: ...

  8. 近期热门微信小程序demo源码下载汇总

    近期微信小程序demo源码下载汇总,乃小程序学习分析必备素材!点击标题即可下载: 即速应用首发!原创!电商商场Demo 优质微信小程序推荐 -秀人美女图 图片下载.滑动翻页 微信小程序 - 新词 GE ...

  9. 微信小程序+OLAMI(欧拉蜜)自然语言API接口制作智能查询工具--快递、聊天、日历等

    微信小程序最近比较热门,再加上自然语义理解也越来越被人关注,于是我想赶赶潮流,做一个小程序试试.想来想去快递查询应该是一种比较普遍的需求. 如果你也在通过自然语言接口做点什么,希望我的这篇博客能帮到你 ...

随机推荐

  1. nginx_ssl安装

    Nginx的安装依赖于以下三个包,意思就是在安装Nginx之前首先必须安装一下的三个包,安装顺序为我写的顺序: c.1 SSL功能需要openssl库,下载地址:http://www.openssl. ...

  2. Caused by: java.lang.ClassNotFoundException: Could not load requested class :XXX.XXX.XXX 异常处理

    在ssh整合中:出现的异常 Exception sending context initialized event to listener instance of class org.springfr ...

  3. MVC的HTTP请求处理过程(IIS应用程序池、CLR线程池)

    主要内容 本文讲解的是:服务器接受Http Request请求之后,是如何进入.Net CLR,从而进一步操作的. 我们大家都知道,IIS必须先接受请求,然后才能有机会进入CLR,但对请求(reque ...

  4. Socket 的网络编程

    socket 网络编程的服务端: 1) 创建socket 套接字. 2) 和socket绑定主机地址和端口 3) socket 主动监听端口,看又没有来连接. 4) 当执行到 accept() 时, ...

  5. scala-创建泛型数组(T: Manifest)

    def arrayT[T: Manifest](ary: T*): Array[T] = {//接受多个参数 val arys = new Array[T](ary.length) //初始化一个数组 ...

  6. C++11 std::ref使用场景

    C++本身有引用(&),为什么C++11又引入了std::ref(或者std::cref)? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: # ...

  7. Spring之IOC注入

    注入 spring依赖注入 set方法: <property name="属性名" values ="值">--ref="对象名" ...

  8. Fernflower 反编译.class文件

    最近有些奇怪Intellij IDEA通过什么查看的源码,通过打开源码意外的发现如下注释 原来是通过Fernflower这个反编译工具w(゚Д゚)w. 使用Fernflower反编译出的代码相当友好, ...

  9. 在Windows上搭建Git Server

    Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用. 那么私有项目,如何用Git进行代码版本控制呢?我们可以自 ...

  10. Oracle 数据库逻辑结构.md

    一.存储关系Oracle 数据库逻辑上是由一个或多个表空间组成的,表空间物理上是由一个或多个数据文件组成的:而在逻辑上表空间又是由一个或多个段组成的.在Oracle 数据库中,通过为每种不同的数据对象 ...