小程序常用方法集合

1.登录

调用微信wx.login接口,会返回唯一的code,然后根据code去请求后端登录接口,换openid(唯一的)和其他的相应的字段信息(是否绑定,id 等等),获取成功后,最好把这些都存到全局变量里,其他页面请求时直接使用

注意:其他页面使用的时候,不能在data里直接设置global里的内容,要在onload中重新setData  

错误使用: data{id:globalData.id}

正确使用: 先在data 中设置id,然后在onload 中setData{id:globalData.id}

2.注册通用逻辑

 a.获取验证码

//收取验证码
getCodeTap: function () {
//判断手机格式
let mobile = this.data.mobile_input;
let reg = /^1[3|4|5|7|8|9][0-9]{9}$/;
let flag = reg.test(mobile);
let that = this;
if (!flag) {
wx.showToast({
title: '手机号码有误',
icon: 'loading',
duration: 1000,
mask: true
})
} else {
// 将获取验证码按钮隐藏60s,60s后再次显示
that.setData({
is_show: true, //false
code_mobile: this.data.mobile_input
})
settime(that);
// 发送验证码
wx.request({
url: domain + '/api/code.php',
data: {
mobile: this.data.mobile_input
},
method: 'POST',
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
console.log('获取验证码成功',res.data)
if (res.data.status) {
that.setData({
code: res.data.data
})
console.log(that.data.code)
}

},
fail:function(res){
console.log('获取验证码失败',res.data)

}
 
})
}
},
 
b.设置60s定时器
var settime = function (that) {
if (countdown == 0) {
that.setData({
is_show: true
})
countdown = 60;
return;
} else {
that.setData({
is_show: false,
last_time: countdown
})

countdown--;
}
setTimeout(function () {
settime(that)
}
, 1000)
}
 
c.注册提交
如果需要在提交的时候同时获取授权信息,就要把提交方法绑定在button的bindGetUserInfo中,先判断各种验证逻辑,然后判断是否授权,最后提交
bindGetUserInfo: function (e) {
console.log(e.detail.userInfo)
 
let nickName = app.globalData.base_info.nick_name;
let avatarUrl = app.globalData.base_info.avatarUrl;

let that = this;
if (this.data.real_name == undefined || this.data.real_name.length == 0) {
wx.showToast({
title: '请填写姓名',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!this.data.age) {
wx.showToast({
title: '请输入年龄',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!this.data.mobile || this.data.mobile !== this.data.code_mobile) {
wx.showToast({
title: '手机号有误',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!this.data.code_input || this.data.code_input != this.data.code) {
wx.showToast({
title: '验证码有误',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!that.data.work_units) {
wx.showToast({
title: '请输入所在医院',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!that.data.work_year) {
wx.showToast({
title: '请输入工作年限',
icon: 'loading',
duration: 1000,
mask: true
})

} else if (!that.data.imageList || that.data.imageList.length != 2) {
wx.showToast({
title: '请上传从医资格证',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!e.detail.userInfo) {
nickName = app.globalData.base_info.nick_name;
avatarUrl = app.globalData.base_info.avatarUrl;
 
that.setData({
nick_name: nickName,
avatarUrl: avatarUrl
})

wx.request({
url: domain + '/api/bind.php?a=bind',
data: {
real_name: that.data.real_name,
age: that.data.age,
mobile: that.data.mobile_input,
sex: that.data.sex,
province: that.data.region[0],
city: that.data.region[1],
area: that.data.region[2],
work_units: that.data.work_units,
work_year: that.data.work_year,
certificate_img: that.data.imageList,
openid:this.data.openid,
experts_id: this.data.experts_id,
head_img: that.data.avatarUrl
},
method: 'POST',
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log('注册成功返回的数据', res.data);
if (res.data.status == true) {
wx.showToast({
title: '注册成功',
icon: 'loading',
duration: 5000,
mask: true
});

console.log('授权头像信息', that.data.nick_name, that.data.avatarUrl)

//存全局变量
app.globalData.studentInfo.student_id = res.data.data.student_id;
app.globalData.base_info.nick_name = that.data.nick_name;
app.globalData.base_info.avatarUrl = that.data.avatarUrl;
app.globalData.is_bind = res.data.data.is_bind;
console.log(app.globalData)
wx.redirectTo({
url: 'userinfo/userinfo',
});



} else if (res.data.status == false) {
wx.showToast({
title: '手机号码已存在,请更换手机号码重新提交!',
icon: 'loading',
duration: 2000,
mask: true

});
}
},
fail: function (res) {
console.log('注册失败' + res.data)

}
})

} else {

nickName = e.detail.userInfo.nickName;
avatarUrl = e.detail.userInfo.avatarUrl;

that.setData({
nick_name: nickName,
avatarUrl: avatarUrl
})

console.log('授权头像信息', that.data.nick_name, that.data.avatarUrl)

wx.request({
url: domain + '/api/bind.php?a=bind',
data: {
real_name: that.data.real_name,
age: that.data.age,
mobile: that.data.mobile_input,
sex: that.data.sex,
province: that.data.region[0],
city: that.data.region[1],
area: that.data.region[2],
work_units: that.data.work_units,
work_year: that.data.work_year,
certificate_img: that.data.imageList,
openid: this.data.openid,
experts_id: this.data.experts_id,
head_img: that.data.avatarUrl

},
method: 'POST',
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log('注册成功返回的数据', res.data);
if (res.data.status == true) {
wx.showToast({
title: '注册成功',
icon: 'loading',
duration: 5000,
mask: true
});

 

//存全局变量
app.globalData.studentInfo.student_id = res.data.data.student_id;
app.globalData.base_info.nick_name = that.data.nick_name;
app.globalData.base_info.avatarUrl = that.data.avatarUrl;
app.globalData.is_bind = res.data.data.is_bind;
console.log(app.globalData)

wx.redirectTo({
url: 'userinfo',
});


} else if (res.data.status == false) {
wx.showToast({
title: '手机号码已存在,请更换手机号码重新提交!',
icon: 'loading',
duration: 2000,
mask: true

});
}
},
fail: function (res) {
console.log('注册失败' + res.data)

}
})
}

}
 
3.多组图片上传
 
 
 
4.多组单选pick逻辑
 
 
5.多组input 值逻辑
 
6.单选多选按钮逻辑
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/cytheria/p/9197360.html