学习新东西时,大体都遵循一样的道理,由总入深。
以下整理一下学习小程序的过程。虽然现在做的东西还有许多问题,比如说数据超过一定数量时循环效率低,或者是多次跳转页面后会变卡等问题。这些问题只解决了部分,所以肯定会有许多错误。仅仅记录下来,自娱自乐。
官网API糊上:https://mp.weixin.qq.com/debug/wxadoc/dev/api/
1、目录结构
工具卸载了,好久没登所以尬写一下吧:
登录页面:
<view class="container">
<view class="userinfo">
<image class="userinfo-avatar" src="/pages/index/images/logo01.png" ></image>
</view>
<form bindsubmit="formSubmit">
<view class="section">
<image class="section01" src="/pages/index/images/iphone.png" >
</image><input name="phone" bindblur="getPhone" placeholder="请输入您的手机号" auto-focus/>
</view>
<view class="section section02">
<image class="section03" src="/pages/index/images/suo.png" ></image>
<input name="pwd" bindblur="getPwd" type="password" placeholder="请输入您的密码" />
</view>
<button type="" formType="submit" class="anniu" bindtap=""> 登录 </button>
</form>
</view>
//index.js
//导入js
var network = require("../../utils/network.js")
//获取应用实例
var app = getApp()
Page({
data: {
wxInfo: {},
userInfo: {},
custmerInfo: {},
imgsrc:'/pages/index/images/03.jpg',
phone:'',
pwd:'',
tip:'',
result:''
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
//获取输入的手机号与密码保存至data,未使用form提交
getPhone:function(e) {
this.setData({
phone:e.detail.value
})
},
getPwd:function(e) {
this.setData({
pwd:e.detail.value
})
},
//提交
formSubmit: function(e) {
var URL=getApp().globalData.ZZTURL;
var page =this;
var phone=e.detail.value.phone;
var pwd =e.detail.value.pwd;
console.log(phone + "===" + pwd + "===" + URL);
//非空判断
if(phone==null||phone==''||phone==undefined){
wx.showToast({
title: '手机号不能为空',
icon: 'loading',
duration: 2000
});
return;
}
if(pwd==null||pwd==''||pwd==undefined){
wx.showToast({
title: '密码不能为空',
icon: 'loading',
duration: 2000
});
return;
}
//正则判断
//声明验证正则,超过长度验证失效
var regPhone = new RegExp('13[0-9]{9}|14[0-9]{9}|15[0-9]{9}|17[0-9]{9}|18[0-9]{9}', 'g');
var regMail = new RegExp('^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$', 'g');
var phReg = regPhone.exec(phone);
var mgReg = regMail.exec(phone);
console.log("phReg"+phReg)
console.log("mgReg" + mgReg)
if ((phReg == null || phReg == '' || phReg == ',' || phReg == undefined) && (mgReg == null || mgReg == '' || mgReg == ',' || mgReg == undefined)){
wx.showToast({
title: '请输入正确的手机号',
icon: 'loading',
duration: 2000
});
return;
}else{
//调用接口查询用户名密码是否正确,如果正确,返回用户详细信息(密码set为XXX)
wx.request({
method:'GET',
url: URL+'/registService/validateLoginApp/'+this.data.phone+','+this.data.pwd, ///registService/
header: {
'content-type': 'application/json'
},
success: function(res) {
//result状态 0:出错 1:密码正确 2:密码错误 3账户不存在
var result =res.data.result;
var userInf=res.data.userInf;
if(result==1){
//密码正确,判断是否冻结
if(userInf.is_freeze){
wx.setStorageSync('isLogin', false);
//提示已经被冻结,请联系客服
wx.showToast({
title: '账户已被冻结',
icon: 'loading',
duration: 2000
});
return;
}
//赋值userInfo并且跳转
page.setData({//这个可以不赋值,其它需求备用
userInfo: userInf
})
//同步
wx.setStorageSync('isLogin', true);
wx.setStorageSync('userInf', userInf);
//页面跳转
wx.redirectTo({
url: '/pages/shouye/shouye'
})
}else if(result==2){
wx.setStorageSync('isLogin', false);
wx.showToast({
title: '密码错误',
icon: 'loading',
duration: 2000
});
return;
}else if(result==3){
wx.setStorageSync('isLogin', false);
wx.showToast({
title: '用户不存在',
icon: 'loading',
duration: 2000
});
return;
}
},
fail:function(){
wx.showToast({
title: '网络异常',
icon: 'loading',
duration: 2000
});
return;
}
})
}
},
onLoad: function () {
console.log('onLoad')
var isLogin=wx.getStorageSync('isLogin');
console.log("isLogin"+isLogin)
if(isLogin==true){
//页面跳转
wx.navigateTo({
url: '/pages/shouye/shouye'
})
}
var that = this
//调用应用实例的方法获取全局数据
app.getUserInfo(function(userInfo){
//更新数据
that.setData({
wxInfo:userInfo
})
})
}
})