WeChat - API

Obtaining user information

fetchinfo: function(){
    var that = this
    wx.getUserInfo({
      withCredentials: true,
      lang: '',
      success: function(res) {
        that.setData({
          name:res.userInfo.nickName,
          path:res.userInfo.avatarUrl
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

Acquiring location

getLocalPath:function(){
    var that = this
    wx.chooseLocation({
      success: function(res) {
        that.setData({
          localPath:res.address
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  

Upload pictures, stored in the buffer

uploadImage:function(){
    var that = this
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          imageList:that.data.imageList.concat(res.tempFilePaths)
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

Jump pages

click_acution:function(e){
    var nid = e.currentTarget.dataset.nid;
    wx.navigateTo({
      url: '/pages/redirect/redirect?id=' + nid,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
## 跳转到指定页面后自动触发指定页面的js文件中的的onLoad函数,option接收传过来的参数

```
Page({
/**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
  }
})
```

Pop Interface: wx.showToast

wx.showToast({
  title: '',
  icon: '',
  image: '',
  duration: 0,
  mask: true,
  success: function(res) {},
  fail: function(res) {},
  complete: function(res) {},
})

wxml page

<view>手机号:</view>
<input value="{{phone}}" placeholder="请输入手机号" bindinput="getPhone"></input>

<view>验证码:<view bindtap="messageCode">点击获取验证码 </view></view>
<input value="{{code}}" placeholder="请输入验证码" bindinput="getCode"></input>

<button bindtap="login">登录</button>

Mobile phone format verification:

  * 发送短信验证码
  messageCode: function () {
    // 手机长度限制
    if (this.data.phone.length != 11) {
      // 弹窗
      wx.showToast({
        title: '手机号长度错误',
        icon: "none", // loading/success/none
      })
      return;
    }


    // 正则匹配手机格式
    var reg = /^(1[3|4|5|6|7|8|9])\d{9}$/;
    if (!reg.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: "none", // loading/success/none
      })
      return;
    }

    wx.request({
      url: 'http://127.0.0.1:8000/api/message/',
      data: { phone: this.data.phone },
      method: 'GET',
      success: function (res) {
        console.log(res);
      }
    })
  },

API sends a request: wx.request

wx.request({
  url: '',
  data: '',
  header: {},
  method: 'GET',
  dataType: 'json',
  responseType: 'text',
  success: function(res) {},
  fail: function(res) {},
  complete: function(res) {},
})

Example: The phone number verification code landing

    * 用户登录

  login: function () {
    console.log(this.data.phone, this.data.code);
    // 将手机号和验证码发送到后端,后端进行登录。
    wx.request({
      url: 'http://127.0.0.1:8000/api/login/',
      data: { phone: this.data.phone, code: this.data.code },
      method: 'POST',
      success: function (res) {
        console.log(res);
      }
    })
  },

Guess you like

Origin www.cnblogs.com/daviddd/p/12168158.html