const BASE_URL = 'https://xcx.duolalive.com/apixcx/';
const BASE_METHOD = 'POST';
const DATA_TYPE = 'json';
const TOAST_DURATION = 1500;
let header = {};
//抽象出处理错误信息的公共方法
const handleErrorMsg = (errMsg) => {
wx.showModal({
title: '提示',
content: errMsg,
showCancel: false,
});
}
function request(url, params = {}, showMsg = false) {
// 绑定手机需要携带cookie
if (params.regist) {
header['cookie'] = wx.getStorageSync("SESSION_ID")
}
return new Promise((resolve, reject) => {
wx.showLoading({
title: '加载中'
});
wx.request({
data: params,
url: BASE_URL + url,
method: params.method ? params.method : BASE_METHOD,
dataType: DATA_TYPE,
header: {
...header,
"content-type": params.method ? "application/json" : "application/x-www-form-urlencoded",
},
success: ({
data,
statusCode,
errMsg,
header
}) => {
let errStr = '';
wx.hideLoading();
let _data = JSON.parse(JSON.stringify(data))
const { code, msg } = _data
// 发送验证码需要携带session_id
if (params.send_code) {
wx.removeStorageSync('SESSION_ID')
wx.nextTick(() => {
wx.setStorageSync("SESSION_ID", header["Set-Cookie"]);
})
}
//增加代码的健壮性
if (statusCode === 200) {
if (Number(code) !== 0) { // 接口出错
errStr = msg
} else {
if (!showMsg) {
return resolve(_data);
}
// 展示操作提示
wx.showToast({
duration: TOAST_DURATION,
title: '操作成功',
icon: 'success',
complete: () => resolve(_data),
});
return;
}
} else {
errStr = errMsg // 小程序方出错
}
if (statusCode !== 200 || code !== 0) {
if (code !== -100) {
handleErrorMsg(errStr)
}
reject(data)
}
},
fail: ({
statusCode,
errMsg
}) => {
wx.hideLoading();
handleErrorMsg(`${statusCode}: ${errMsg}`)
reject({
statusCode,
errMsg
});
},
});
})
}
export default request
接口://https://xcx.duolalive.com/apixcx/wx_login.php
测试数据://phone:17722486332;send_code: 1
``>
// 60秒后重新获取验证码
this.login(true);
inter = setInterval(function () {
this.setData({
smsFlag: true,
sendColor: '#cccccc',
sendTime: this.data.snsMsgWait + 's后重发',
snsMsgWait: this.data.snsMsgWait - 1
});
if (this.data.snsMsgWait < 0) {
clearInterval(inter)
this.setData({
sendTime: '获取验证码',
snsMsgWait: 60,
smsFlag: false
});
}
}.bind(this), 1000); //注意定时器中的this
},
//tomcat默认端口号8080;mysql:3306;http:80;https:443
//默认主机名为localhost->127.0.0.1
//默认网址应用存放位置:webapps
nexus-aliyun
*,!jeecg,!jeecg-snapshots
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public
G:\apache-maven-3.6.3\maven-repo
package com.huang.cookies;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
HttpSession session = req.getSession();
String name = (String) session.getAttribute("name");
// null;大神
System.out.println(name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
getSession
com.huang.cookies.SessionDemo2
getSession
/getSession