不多说,上代码,备忘
Page({
data: {
items: [
{ name: '普通会员', value: 0 },
{ name: '高级会员', value: 1 },
{ name: 'VIP会员', value: 666 }
], //单选群组
userType: '', //用户类型
iCard: '', //身份证号码(用户名)
passWord: '', //密码
msg: '', //返回消息
status: '', //返回状态码
key: 'token', //令牌key 存入Storage之用
value: '', //令牌字符
},
onLoad() {
},
radioChange(e) {
//console.log('你选择的是:', e.detail.value);
},
//登陆提交
onFormSubmit: function (se) {
var app = getApp();
var seData = se.detail.value;
//console.log(se.detail.value);
var userType = this.data.userType = seData.user_type;
var iCard = this.data.iCard = seData.icard;
var passWord = this.data.passWord = seData.password;
if (typeof (userType) === 'undefined') {
dd.alert({
content: '类型不能为空', buttonText: '确定'
});
return false;
}
if (iCard == null || iCard == '') {
dd.alert({
content: '帐号不能为空', buttonText: '确定'
});
return false;
}
if (passWord == null || passWord == '') {
dd.alert({
content: '密码不能为空', buttonText: '确定'
});
return false;
}
dd.httpRequest(
{
url: app.getLoginUrl(),
method: 'POST',
data: {
user_type: userType,
icard: iCard,
password: passWord
},
success: (e) => {
//console.log(e.data);
this.setData({
msg: e.data.msg,
status: e.data.status
});
if (e.data.status === 1001) {
dd.setStorage({
key: this.data.key,
data: e.data.jwt,
success: function () {
dd.alert({ content: e.data.msg, buttonText: '确定' });
}
});
} else {
dd.alert({ content: e.data.msg, buttonText: '确定' });
}
},
fail: (fe) => {
dd.alert({ content: fe.data.msg });
}
});
},
//测试验证token后获取数据
onClickData: function () {
dd.showLoading({
content: '加载中...',
});
var app = getApp();
dd.httpRequest({
url: app.geturl(),
method: 'POST',
headers: {
'Content-Type': 'application/json', // 使用这个能正常获取数据
'token': dd.getStorageSync({ key: 'token' }).data,
},
data: JSON.stringify({
username: '我是一只小小鸟',
}),
dataType: 'json',
success: function (e) {
console.log(e);
dd.alert({
content: e.data.msg, //弹出来的是返回结果
buttonText: '好的'
});
},
fail: function (x) {
console.log('出错了');
},
complete: function () {
dd.hideLoading();
}
});
}
});