// pages/login/login.js
const cp_app= getApp()
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
console.log("====login code====");
console.log(cp_app.globalData.code);
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
},
getuser_ceshi2() {
console.log("data====")
console.log(this.data.tel)
console.log(this.data.name)
console.log(tel)
console.log(name)
return;
},
getuser(res) {
console.log("点击了登录--按钮")
console.log(res)
console.log("cp_app=====")
console.log(cp_app)
if (res.detail.errMsg === "getUserInfo:ok") {
var userData = JSON.parse(res.detail.rawData),
_this = this,
tel = this.data.tel,
name = this.data.name,
openid = '',
nikeName = userData.nikeName,
avatar = userData.avatarUrl,
setuserData = {};
console.log("登录--成功")
console.log("userData===")
console.log(userData)
console.log("data====")
console.log(this.data)
console.log(this.data.tel)
console.log(this.data.name)
console.log(tel)
console.log(name)
// 判断 手机号是否输入
if (!tel) {
wx.hideLoading();
wx.showToast({
title: '请输入手机号',
icon: 'none'
})
return
}
// 判断密码是否输入
if (!name) {
wx.hideLoading();
wx.showToast({
title: '请输入姓名',
icon: 'none'
})
return
}
//登录核心代码
var userInfo = res.detail.userInfo;
var openIdurl = cp_app.globalData.https + "index.php/small/login/index";
console.log("====userInfo====");
console.log(userInfo);
console.log("====openIdurl====");
console.log(openIdurl);
console.log("====code====");
console.log(cp_app.globalData.code);
var code = cp_app.globalData.code;
// 获取openid
cp_app.W_post(openIdurl, { code: code }, res => {
console.log("====获取到服务器返回数据====");
console.log(res);
if (res.data.openid) {
var openid = res.data.openid;
var logurl = cp_app.globalData.https + "index.php/small/login/logincheck";
var logdata = { openid: openid, name: userInfo.nickName, pic: userInfo.avatarUrl };
cp_app.W_post(logurl, logdata, res => {
console.log("====二次请求。获取到服务器返回数据====");
console.log(res);
if (res.data.code === 1) {
// 获取uid
wx.setStorage({ key: "name", data: userInfo.nickName });
wx.setStorage({ key: "pic", data: userInfo.avatarUrl });
wx.setStorage({ key: "uid", data: res.data.uid });
if (callback) { callback() }
} else if (res.data.code === -1) {
// 已有uid
wx.setStorage({ key: "name", data: userInfo.nickName });
wx.setStorage({ key: "pic", data: userInfo.avatarUrl });
wx.setStorage({ key: "uid", data: res.data.uid });
if (callback) { callback() }
}
})
}
})
//结束-登录核心代码
return;
} else {
// 不同意
wx.hideLoading();
wx.showToast({
title: '授权失败',
icon: 'none'
})
}
},
// input Pass
inputName(e) {
this.setData({ name: e.detail.value })
},
// input Tel
inputTel(e) {
this.setData({ tel: e.detail.value })
}
})
html
<view class="head row a-c f-b">
<image style="width: 25rpx; height: 25rpx;" src="/img/icon_18.png"></image>
</view>
<view class="logo row f-c a-c">
<image style="width: 180rpx; height: 180rpx;" src="/img/logo.png"></image>
</view>
<view class="regist-input row a-c">
<image src="/img/icon_19.png" style="width: 28rpx; height: 40rpx;"></image>
<input bindinput="inputTel" class="f-26" type="text" placeholder="请输入您的手机号"/>
</view>
<view class="regist-input row a-c">
<image src="/img/icon_21.png" style="width: 28rpx; height: 40rpx;"></image>
<input bindinput="inputName" class="f-26" type="text" placeholder="姓名"/>
<!-- <image class="view-password" src="/img/icon_22.png" style="width: 29rpx; height: 17rpx;"></image> -->
</view>
<button class="regist-btn f-30" bindgetuserinfo="getuser" open-type="getUserInfo">登录</button>
<!-- <view class="order-option public-gray row a-c f-b">
<view class="f-26">立即注册</view>
<view class="f-26">忘记密码?</view>
</view> -->