小程序学习问题总结

1.微信小程序placeholder设置自定义颜色


       
 


.phcolor{
    color: #18acff;
}

2.小程序js中的onLoad函数: (页面加载,自动执行)

wx.request:小程序的请求函数(类比于ajax)

文档:https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html

setData:设置Page中的data的值

wx.request发起的是 https 请求,而不是 http 请求。一个小程序 同时 只能有 5个 网络请求。开发过程中,可以通过开发者工具调试http的情况

微信小程序对HTTPS的要求

每个微信小程序必须事先设置一个通讯域名,并通过HTTPS请求进行网络通信,不满足条件的域名和协议无法请求。也就是说,请求request地址必须是合法域名(必须是https请求),需要有SSL证书认证过。

2.小程序js中的onLoad函数:

注意点:
(1)onLoad中的形参是一个对象
(2)onLoad是一个生命周期函数,表示页面加载,不需要手动调,小程序自动调取
(3)拿到后台返回的数据,渲染的时候,要先setData,在页面再渲染

 onLoad:function(a){
    if(typeof a=='object'){
      var url = 'https://www.apiopen.top/weatherApi';
    }else{
      var url = 'https://www.apiopen.top/weatherApi?city='+a;
    }
    var that=this;
    wx.request({
      url: url,
      success:function(res){
        console.log(res)
        var list = res.data.data.forecast;
        var yesterday = res.data.data.yesterday;
        yesterday.extended_high = yesterday.high.split(" ")[1];
        for(var k in list){
          list[k].extended_temp = list[k].low.split(" ")[1]+'-'+list[k].high.split(" ")[1];
          list[k].extended_date = list[k].date.slice(0, list[k].date.search(/日/)+1)  
        }
          that.setData({
            weatherList: list,
            cityName: res.data.data.city,
            yesterdays: yesterday
          })
      }
    })
  },




//wxml



  
   {{item.extended_date}}
   {{item.extended_temp}}
   {{item.type}}
  





{{yesterdays.extended_high}}
{{yesterdays.type}}




3.微信小程序中,获取当前地理位置(城市位置)

1. 申请开发者密钥(key):申请密钥

2. 下载微信小程序JavaScriptSDK,微信小程序JavaScriptSDK v1.0

3. 安全域名设置,需要在微信公众平台添加域名地址  https://apis.map.qq.com

4. 小程序示例

//index.js
//获取应用实例
const app = getApp();
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {
    province: '',
    city: '',
    latitude: '',
    longitude: ''
  },
  onLoad: function () {
    qqmapsdk = new QQMapWX({
      key: 'XXXX-XXXX-XXXX-XXXX' //这里自己的key秘钥进行填充
    });
  },
  onShow: function () {
    let vm = this;
    vm.getUserLocation();
  },
  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置,请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权,调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        }
        else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(JSON.stringify(res));
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude
        })
 
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  }





   
    所在城市
    
  

 

4.小程序的template的使用:

步骤一:先定义模板(通过name属性来区别)

1、新建一个template文件夹用来管理项目中所有的模板; 
2、新建一个courseList.wxml文件来定义模板; 
3、使用name属性,作为模板的名字。然后在