(小白)尝试用PHP梳理 借阅系统源码002 20200929

3 篇文章 0 订阅
1 篇文章 0 订阅

(小白)尝试用PHP梳理 借阅系统源码002 20200929

相关工具

phpstudy:8.1.0.5
navicat:11.2.7
HBuilder: 7.6.5.201612301621

历程

  1. post和get的用法
    个人理解,get是用于小内容的传值,post是大内容的,且不会显示在地址中
    https://www.cnblogs.com/zhengweizhao/p/6532952.html

  2. 小程序request
    https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html

getbookbyisbn(isbn){
    const that = this
    let apidata = {
      isbn: isbn,
      token: wx.getStorageSync('token')
    }
    WXAPI.bookisbn(apidata).then(res => {
      if (res.status == 1) {
        if (res.list) {
          that.setData({
            list: res.list,
            publish: res.list.publish
          })
          that.onEditorReady();
        }
      } else {
        wx.showModal({
          title: '提示',
          content: res.info
        })
      }

    })
  },

可知,这里的apidata:

{
  isbn: isbn,
  token:'7bb...'
}
bookisbn: (apidata) => {
  return request('/Home/Book/isbn', false, 'get', apidata)
},

按小程序的标准wx.request是不同的,后来查找发现,这里的request并没有wx.所以是自己写的一个方法

const request = (url, needSubDomain, method, data) => {
  let _url = API_BASE_URL + (needSubDomain ? '/' + CONFIG.subDomain : '') + url
  wx.showLoading({
    title: '加载中',
  })
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        if (request.data.status === 2) {
          let reapidata={
            url,
            needSubDomain,
            method,
            data
          }
          relogin(reapidata, resolve);
          return;
        }
        console.log(request.data)
        resolve(request.data)
      },
      fail(error) {
        reject(error.data)
      },
      complete(aaa) {
        wx.hideLoading()
      }
    })
  })
}
  1. 这里用到Promise

度娘给的答案:
https://www.jianshu.com/p/b16e7c9e1f9f
Promise是异步处理的方法

这里有一个:relogin

const relogin = (reapidata, resolve) => {
  wx.login({
    success: res => {
      wx.request({
        url: API_BASE_URL + (reapidata.needSubDomain ? '/' + CONFIG.subDomain : '') + '/Home/Base/login',
        method: 'get',
        data: {
          code: res.code
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function(res2) {
          if (res2.status == 0) {
            wx.showModal({
              title: '提示',
              content: res2.info,
              showCancel: false
            })
          } else {
            //登录成功后
            let token = res2.data.token;
            wx.setStorageSync('token', res2.data.token);
            reapidata['data']['token'] = token
            let _url = API_BASE_URL + (reapidata.needSubDomain ? '/' + CONFIG.subDomain : '') + reapidata.url
            wx.request({
              url: _url,
              method: reapidata.method,
              data: reapidata.data,
              header: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              success(request) {
                console.log(request.data)
                resolve(request.data)
              },
            })

            

          }
        }
      })
    }
  })
}

这里相当于如果没有登陆的话,获取登陆后再执行查询

还有一个resolve
原本以为是自己写的,查完资料发现,这是Promise的静态方法
https://blog.csdn.net/weixin_41888813/article/details/82882375

  1. 这里的Promise是一个构造函数
    https://blog.csdn.net/weixin_41888813/article/details/82882375

resolve是Promise的一个方法

  1. header
header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },

度娘给的答案:
https://blog.csdn.net/jackjia2015/article/details/94381171

常见的表单数据提交数据的编码类型content-type

application/x-www-form-urlencoded
application/json
multipart/form-data
text/xml

每个都有不同的适用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值