(小白)尝试用PHP梳理 借阅系统源码002 20200929
相关工具
phpstudy:8.1.0.5
navicat:11.2.7
HBuilder: 7.6.5.201612301621
历程
-
post和get的用法
个人理解,get是用于小内容的传值,post是大内容的,且不会显示在地址中
https://www.cnblogs.com/zhengweizhao/p/6532952.html -
小程序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()
}
})
})
}
- 这里用到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
- 这里的Promise是一个构造函数
https://blog.csdn.net/weixin_41888813/article/details/82882375
resolve是Promise的一个方法
- 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
每个都有不同的适用