小程序

存储数据
wx.setStorageSync("number_phonenum", res.data.data.phoneNumber)

获取存储的数据
wx.getStorageSync("number_phonenum"),
<open-data type="userAvatarUrl"></open-data>    //获取用户头像直接显示在小程序中
<open-data type="userNickName" lang="zh_CN"></open-data>    //获取用户昵称直接显示在小程序中
//更改checkbox选中后的样式
checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  background: #FF525C;
  color: #FFF;
}

//更改checkbox选中前的样式
checkbox .wx-checkbox-input {
  border-radius: 50%;
}
小程序页面跳转
navigateTo, redirectTo 只能打开非 tabBar 页面。 switchTab 只能打开 tabBar 页面。 reLaunch 可以打开任意页面。 解决方法: 把index.js中的wx.redirectTo换成wx.reLaunch
微信小程序页面几秒后自动跳转
setTimeout(function () {
  wx.reLaunch({
     url: '../appointments/appointments'
   })
}, 2000)
小程序弹窗提示
wx: wx.showToast({
  title: '预约成功',
  icon: 'success',
  duration: 2000,
  image: '../../img/dui.png',
})

参数讲解:

title          --------     标题,要显示的提示信息

icon         --------     图标,只支持"success"、"loading"

image     --------    自定义图标的本地路径,image的优先级高于icon

duration  --------    提示的延迟时间,单位毫秒,默认:1500

mask       --------   是否显示透明蒙层,防止触摸穿透,默认:false

小程序判断输入框是否为空
if (name == '') {
  wx: wx.showToast({
    title: '请输入姓名'
  })
  return false
  }
  else if (phone == '') {
    wx: wx.showToast({
      title: '请输入手机号'
    })
    return false
}
判断元素是否显示
<view wx:if="{{number_phonenum != ''}}">{{number_phonenum}}</view>
微信小程序获取手机号流程
小程序需企业认证,才可以获取用户的手机号,个人开发者是不能获取的
官方文档给出需先登录才可获取手机号 传送门 思路为:login登录获取code
-->code传给后台-->后台根据code获取sessionKey 前台通过触发获取手机号事件得到加密数据-->传给后台,后台通过之前获取的 sessionKey来解密,并将数据返回给前台 code传给后台时后台需去访问官网给的接口去检验 传送门 getPhoneNumber: function (e) { let _this = this; if (e.detail.errMsg == "getPhoneNumber:ok") { //校验是否过期 wx.checkSession({ success: function () { // session_key 未过期,并且在本生命周期一直有效 _this.gettel(e);//获取手机号 }, fail: function () { // session_key 已经失效,需要重新执行登录流程 wx.login({ success: function (res) { wx.request({ url: 'xxxxxx', method: 'post', data: { code: res.code }, header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function (r) { _this.gettel(e);//获取手机号 }, fail: function (err) { console.log("登录失败"); console.log(err); } }) } }) } }) } else { wx.showModal({ title: '提示', content: '需获取信息才可查看历史预约', }) } }, gettel: function (e) { let _this = this; let iv = e.detail.iv; let encryptedData = e.detail.encryptedData; wx.request({ url: 'xxxxxxxxxxxxxxxx', method: 'post', data: { iv: iv, encryptedData: encryptedData }, header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function (res) { console.log(res);//手机号在这里面哦 <!--let item = JSON.parse(res.data.data);--> }, fail: function (err) { console.log(err); } }) } wx.login首次登录我写在需要获取手机号时的page中onload事件中 因为login返回的code有时效,所以在需要的时候在调用 !该内容来自博客园:https://www.cnblogs.com/ybchen292/p/11567585.html
登录 + 获取用户信息
onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'xxxx',
            data: {
              code: res.code
            },
            success (res) {
              console.log(res);
              wx.removeStorageSync('sessionid');
              //储存res.header['Set-Cookie']
              wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
底部菜单栏与头部样式
"window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "预约",
    "navigationBarTextStyle": "black"
  },
  "tabBar": {
    "selectedColor": "#1E90FF",
    "fontSize": "40px",
    "list": [{
      "pagePath": "pages/home/home",
      "text": "预约"
    },
    {
      "pagePath": "pages/appointments/appointments",
      "text": "个人中心"
    }]
  },
  "sitemapLocation": "sitemap.json"
//判断状态
<text class='tv_title' wx:if="{{status == 1}}">订单完成</text> <text class='tv_title' wx:elif="{{status == 2}}">该预约已取消</text> <text class='tv_title' wx:else>预约成功</text>
//获取验证码
obtain: function(){ var that = this; that.setData({ gettime: true }); let getsix = setInterval(function(){ let times = that.data.counts - 1; that.setData({ counts: times }); if (that.data.counts < 1){ clearInterval(that.data.counts); that.setData({ gettime: false, counts: 60 }) } },1000); wx.request({ url: dispose.HOST + '接口', method: 'GET', header: { //将sessionid放在cookie中以请求头的方式带回给服务端 'cookie': wx.getStorageSync("sessionid") }, data:{ telephone: this.data.number_phonenum }, success: function(res){ console.log(res); } }) }, confirm_btn: function(){ console.log("确定") if(this.data.captcha_val == ""){ wx: wx.showToast({ title: '验证码不能为空', icon: 'success', duration: 2000, image: '../../img/cuo.png', }) }else{ this.sub() } },
原文地址:https://www.cnblogs.com/yyy251/p/14440428.html