微信小程序wx.request接口

微信小程序wx.request接口

wx.request是小程序客户端与服务器端交互的接口
HTTPS 请求
一个微信小程序,只能同时(同时不能大于5个)有5个网络请求

wx.request(OBJECT)

发起网络请求

url
data
header  
method
dataType
wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
    'content-type': 'application/json' // 默认值
  },
  success: function(res) {
    console.log(res.data)
  }
})

四种网络请求:

(wx.request)
(wx.uploadFile)
(wx.downloadFile)
(wx.connectSocket)

var request = {
  url: '',
  data: {},
  method: '',
  success: function (res) {
  },
  fail: function () {
  },
  complete: function () {
  }
}

wx.openSetting 来跳转到设置授权界面

/* index.js */
// 若有用户信息存在则继续
Page({
  onLoad () {
    wx.getStorage({
      key: 'userinfo',
      success: (res) => {
        this.setUserinfo(res)
      },
      fail: (res) => {
        api.login().then((res) => {
          this.setUserinfo(res)
        }).catch(e => {
          if (e.errMsg && e.errMsg === 'getUserInfo:fail auth deny') {
            this.setData({
              isauth: false
            })
          }
        })
      }
    })
  },
  toSetting() {
    wx.openSetting({
      success: (res) => {
        this.setData({
          isauth: res.authSetting['scope.userInfo']
        })
        if (res.authSetting['scope.userInfo']) {
          api.login().then((res) => {
            this.setUserinfo(res)
          })
        }
      }
    })
  }
})
// setUserinfo 就是对用户信息做一下处理 不具体展开了

/* index.wxml */
<view class="unauth" wx:if="{{!isauth}}">
   <image class="unauth-img" src="../../images/auth.png"></image> 
  <text class="unauth-text">检查到您没打开授权</text>
  <button class="color-button unauth-button" bindtap="toSetting">去设置</button>
</view>
<view class="container" wx:else>
...
</view>
function queryRequest(data){    
    wx.request({
        url:"https://example.com/api/",
        data:data,
        header:{
           // "Content-Type":"application/json"
        },
        success:function(res){
            console.log(res.data)
        },
        fail:function(err){
            console.log(err)
        }

    })

}

服务器设置:

在这里插入图片描述

上传文件

// Content-type为multipart/form-data
function uploadFile(file,data) {
    wx.uploadFile({
        url: 'http://example.com/upload',
        filePath: file,
        name: 'file',
        formData:data,
        success:function(res){
            console.log(res.data)
        },
        fail:function(err){
            console.log(err)
        }
    })
}

下载文件

function downloadFile(url,typ,success){
    wx.downloadFile({
        url:url,
        type:type,
        success:function(res){
            if(success){
                success(res.tempFilePath)
            }
        },
        fail:function(err){
            console.log(err)
        }
    })
}
function svaeFile(tempFile,success){
    wx.saveFile({
        tempFilePath:tempFile,
        success:function(res){
            var svaedFile=res.savedFilePath
            if(success){
                success(svaeFile)
            }
        }
    })
}

转载于:https://www.cnblogs.com/dashucoding/p/9922911.html

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中,使用`wx.request`进行网络请求时,涉及到数据传输安全,特别是在处理敏感信息时,可能需要采用国密(国家密码算法)进行加密和解密。微信提供了相应的API来支持这些操作,具体步骤如下: 1. **引入加密库**: 首先,在你的小程序项目中,需要安装微信提供的`miniprogram-crypto`库,用于国密相关操作。在`pages/index.js`或相关页面的入口文件中,添加: ```javascript const crypto = require('miniprogram-crypto'); ``` 2. **加密和解密**: 使用`crypto.encrypt`方法对数据进行加密,`crypto.decrypt`方法进行解密。例如,假设你想加密一个字符串: ```javascript const originalData = 'your sensitive data'; const encryptedData = crypto.encrypt(originalData, 'your-encryption-key'); ``` 解密时,用相同的密钥: ```javascript const decryptedData = crypto.decrypt(encryptedData, 'your-encryption-key'); ``` 3. **在`wx.request`中应用加密**: 当发送数据到服务器时,可以在发送前加密,接收后解密。示例代码: ```javascript wx.request({ url: 'your-server-url', data: { encryptedData }, // 注意:这里加密Data而不是明文 method: 'POST', header: { 'Content-Type': 'application/json' // 假设你的服务器需要JSON格式的数据 }, success(res) { const serverResponse = res.data; const decryptedResponse = crypto.decrypt(serverResponse, 'your-encryption-key'); // 解析并处理解密后的数据 } }); ``` 同样,服务器也需要支持接收加密数据,并返回解密后的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值