微信小程序学习回顾-3.2-【零基础】
2020-03-07 本文已影响0人
初九简书
前端和服务端接口打通
- 工具准备(微信开发者工具)
- 实操:注册登陆
注册代码编写
else {
// console.log('/index.php/Home/User/sign')
wx.request({
url: getApp().globalData.server + '/index.php/Home/User/sign',
data: {
username: that.data.username, //用户名
phone: that.data.phonenumber,
password: that.data.password,
password_again: that.data.passwordack,
face_url: getApp().globalData.userInfo.avatarUrl,
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded' // thinkphp
},
success(res) {
console.log(res.data)
if (res.data.error_code == 1){
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
success(res) { }
})
}
else if (res.data.error_code == 2){
wx.showModal({
title: '提示',
content: '两次输入的密码不一致',
showCancel: false,
success(res) { }
})
}
else if (res.data.error_code == 3){
wx.showModal({
title: '提示',
content: '该手机号已被注册',
showCancel: false,
success(res) { }
})
}
else if (res.data.error_code != 0){
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
success(res) { }
})
}
else if (res.data.error_code == 0) {
// console.log('test')
getApp().globalData.user = res.data.data
console.log(getApp().globalData.user)
//在这里调用缓存API,目的是实现授权注册后再次打开小程序可以免登录直接进入广场页
wx.setStorage({
data: res.data.data.username,
key: 'minename',
})
wx.setStorage({
data: res.data.data,
key: 'user_sign',
})
wx.setStorage({
data: res.data.data.user_id,
key: "sign_id",
})
wx.showModal({
title: '提示',
content: '注册成功',
showCancel: false,
success(res) { },
complete: function (res) {
wx.reLaunch({
url: '../square/square'
})
}
})
},
fail: function(res) {
wx.showModal({
title: '提示',
content: '网络不在状态',
showCancel: false,
})
}
})
}
登录页代码编写
else {
wx.request({
url: getApp().globalData.server + '/index.php/Home/User/login',
data: {
phone: that.data.phone,
password: that.data.password,
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success(res) {
console.log(res.data)
if (res.data.error_code == 1)
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
})
else if (res.data.error_code == 2)
wx.showModal({
title: '提示',
content: '手机号未注册',
showCancel: false,
})
else if (res.data.error_code == 3)
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
})
else if (res.data.error_code != 0)
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
})
else if (res.data.error_code == 0) {
getApp().globalData.user = res.data.data
console.log(getApp().globalData.user)
//调用缓存API,实现授权注册或已登录后再次打开小程序免登录
wx.setStorage({
data: res.data.data,
key: 'user_sign',
})
wx.setStorage({
data: res.data.data.user_id,
key: "sign_id",
})
wx.showModal({
title: '提示',
content: '登录成功',
showCancel: false,
complete: function() {
wx.reLaunch({
url: '../square/square'
})
}
})
}
},
fail: function(res) {
wx.showModal({
title: '提示',
content: '网络不在状态',
showCancel: false,
})
}
})
}