用戶頁面wxml <!--pages/home/home.wxml--> <view class="container"> <view class="top-view"> <view class="user"> <view class="row"> <image class="avatar" wx: ...
用戶頁面wxml
<!--pages/home/home.wxml-->
<view class="container">
<view class="top-view">
<view class="user">
<view class="row">
<image class="avatar" wx:if="{{userInfo}}" src="{{userInfo.avatarUrl}}"></image>
<image class="avatar" wx:else="{{userInfo}}" src="/static/2.png"></image>
<!-- 點擊跳轉到登錄註冊頁面 -->
<view class="name" wx:if="{{userInfo}}">
<view bindtap="onClickLogout">
{{userInfo.nickName}}
</view>
</view>
<view class="name" wx:else="{{userInfo}}">
<navigator url="/pages/auth/auth">登錄</navigator>
|
<navigator url="/pages/auth/auth">註冊 </navigator>
</view>
</view>
<view class="site">查看個人主頁</view>
</view>
<view class="numbers">
<view class="row">
<text>0</text>
<text>關註</text>
</view>
<view class="row">
<text>0</text>
<text>粉絲</text>
</view>
<view class="row">
<text>0</text>
<text>贊與收藏</text>
</view>
<view class="row">
<text>0</text>
<text>好友動態</text>
</view>
</view>
</view>
<view class="middle-view">
<view class="item">
<image src="/static/images/icon/transaction_order1_icon_show.png"></image>
<text>待支付</text>
</view>
<view class="item">
<image src="/static/images/icon/transaction_order2_icon_show.png"></image>
<text>待支付</text>
</view>
<view class="item">
<image src="/static/images/icon/transaction_order3_icon_show.png"></image>
<text>待支付</text>
</view>
<view class="item">
<image src="/static/images/icon/transaction_order4_icon_show.png"></image>
<text>待支付</text>
</view>
<view class="item">
<image src="/static/images/icon/transaction_order5_icon_show.png"></image>
<text>待支付</text>
</view>
</view>
<view class="function-view">
<view class="row">
<view class="left">我的錢包</view>
<view class="right">
<text>¥20</text>
<image class="go-icon" src='/static/images/icon/to_icon_show_small.png'></image>
</view>
</view>
<view class="row">
<view class="left">我的優惠券</view>
<view class="right">
<text>暫無課用</text>
<image class="go-icon" src='/static/images/icon/to_icon_show_small.png'></image>
</view>
</view>
<view class="row">
<view class="left">領劵中心</view>
<view class="right">
<text>你的福利都在這裡</text>
<image class="go-icon" src='/static/images/icon/to_icon_show_small.png'></image>
</view>
</view>
</view>
<view class="contact-view">
<button open-type="contact">
<image src="/static/images/icon/wechat_contact_icon_show.png"></image>
</button>
<button bindtap="onClickCall">
<image src="/static/images/icon/phone_contact_icon_show.png"></image>
</button>
</view>
</view>
用戶頁面js
// pages/home/home.js
var app = getApp();
Page({
/**
* 頁面的初始數據
*/
data: {
userInfo: null,
},
/**
* 生命周期函數--監聽頁面載入(第一次打開時會執行)
*/
onLoad: function (options) {
},
/**
* 生命周期函數--監聽頁面初次渲染完成(第一次打開時會執行)
*/
onReady: function () {
},
/**
* 生命周期函數--監聽頁面顯示
*/
onShow: function () {
//本地storage中獲取值
// var phone = wx.getStorageSync('phone')
// this.setData({
// phone:phone
// })
//從全局變數中獲取值
this.setData({
// phone:app.globalData.phone
userInfo: app.globalData.userInfo
})
},
/**
* 用戶註銷
*/
onClickLogout:function(){
app.delUserInfo();
this.setData({
userInfo: null
})
},
/**
* 生命周期函數--監聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數--監聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關事件處理函數--監聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom: function () {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {
}
})
用戶頁面css
/* pages/home/home.wxss */
.top-view{
background-color: #01ccb6;
color: white;
padding: 40rpx;
}
.top-view .user{
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.top-view .user .row{
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
.top-view .user .avatar{
width: 100rpx;
height: 100rpx;
border-radius: 50%;
}
.top-view .user .name{
display: flex;
flex-direction: row;
justify-content: space-around;
width: 200rpx;
}
.top-view .site{
background-color: rgba(0, 0, 0, 0.16);
padding: 20rpx;
border-top-left-radius: 32rpx;
border-bottom-left-radius: 32rpx;
}
.top-view .numbers{
display: flex;
flex-direction: row;
justify-content: space-between;
font-size: 28rpx;
padding: 40rpx;
padding-bottom: 0rpx;
}
.top-view .numbers .row{
display: flex;
flex-direction: column;
align-items: center;
}
.middle-view{
padding: 40rpx;
display: flex;
flex-direction: row;
justify-content: space-between;
font-size: 25rpx;
border-bottom: 18rpx solid #f5f5f5;
}
.middle-view .item{
display: flex;
flex-direction: column;
align-items: center;
}
.middle-view .item image{
width: 50rpx;
height: 50rpx;
margin-bottom: 20rpx;
}
.function-view{
padding: 40rpx;
font-size: 28rpx;
}
.function-view .row{
padding: 30rpx 0;
border-bottom: 1px solid #efefef;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.function-view .row .right{
color: #8c8c8c;
}
.function-view .row .go-icon{
margin: 0 20rpx;
width: 16rpx;
height: 16rpx;
}
.contact-view{
padding: 40rpx;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.contact-view button {
background-color: transparent;
border: 0;
padding: 0;
}
.contact-view button::after{
border: 0;
}
.contact-view image{
width: 204rpx;
height: 66rpx;
}
登錄註冊頁wxml
<!--pages/auth/auth.wxml-->
<view class="logo">
<image src='/static/2.png'></image>
<text>交流交易社區</text>
</view>
<view class="form">
<view class="row-group">
<text>手機</text>
<!-- value屬性是後端對於前端的綁定,bindinput的前端變化時後端數據也會相應變化 -->
<input placeholder="請填寫手機號碼" placeholder-class='txt' maxlength='11' value="{{phone}}" bindinput="bindPhoneInput" />
</view>
<view class="row-group">
<text>驗證碼</text>
<input placeholder="請填寫驗證碼" placeholder-class='txt' maxlength='4' value="{{code}}" bindinput="bindCodeInput" />
<view class="code" bindtap="onClickCheckCode">獲取驗證碼</view>
</view>
<view>
<button class="submit" open-type="getUserInfo" bindgetuserinfo="onClickSubmit">登錄 | 註冊</button>
</view>
</view>
登錄註冊頁js
// pages/auth/auth.js
var app = getApp()
Page({
/**
* 頁面的初始數據
*/
data: {
phone: "15822539779",
code: "",
},
bindPhoneInput: function (e) {
this.setData({
phone: e.detail.value
});
},
bindCodeInput: function (e) {
this.setData({
code: e.detail.value
});
},
/**
* 點擊獲取簡訊驗證碼
*/
onClickCheckCode: function (e) {
// 判斷手機號格式是否正確
if (this.data.phone.length == 0) {
wx.showToast({
title: '請填寫手機號碼',
icon: 'none'
})
return
}
var reg = /^(1[3|4|5|6|7|8|9])\d{9}$/;
if (!reg.test(this.data.phone)) {
wx.showToast({
title: '手機格式錯誤',
icon: 'none'
})
return
}
// 發送簡訊驗證碼,登錄成功之後獲取jwt和微信用戶信息,保存到globalData和本地存儲中。
wx.request({
url: "http://127.0.0.1:8000/api/message/",
data: {
phone: this.data.phone
},
method: 'GET',
dataType: 'json',
success: function (res) {
// 根據後端發送的status判斷是否發送成功
if (res.data.status) {
// 提示驗證碼倒計時或者提示發送成功
wx.showToast({
title: res.data.message,
icon: 'none'
});
} else {
// 簡訊發送失敗
wx.showToast({
title: res.data.message,
icon: 'none'
});
}
}
})
},
onClickSubmit(e) {
console.log(e)
wx.request({
url: "http://127.0.0.1:8000/api/login/",
data: {
phone: this.data.phone,
code: this.data.code
},
method: 'POST',
dataType: 'json',
success: function (res) {
// 根據status狀態判斷是否成功
if (res.data.status) {
// 1.可以將手機號放到公共的位置,比如app.js里,方便別的頁面進行獲取
// app.globalData.phone = res.data.data.phone
// 2.上述方法是將值存在變數里的,一重啟就沒有了,我們也可以將值保存在cookie中
// wx.setStorageSync('phone', res.data.data.phone)
// 3.將上述兩種存儲優化一下,在全局的app中定義一個initUserInfo方法,將用戶初始化的信息(電話和token)傳入該函數,然後上面1和2兩部就可以放到該函數內執行了
// 4.優化,我們還可以將wx的用戶信息也初始化到initUserInfo函數內
app.initUserInfo(res.data.data, e.detail.userInfo)
// 登錄成功之後,自動跳轉到上一級頁面
//getCurrentPages方法是記錄程式的頁面,包括上一頁信息,下一頁信息等,pages是一個列表,最後一個是元素是當前頁面,前一個元素是上一頁
// var pages = getCurrentPages()
// console.log(pages)
// 得到上一頁
// prepage = pages[pages.length-2]
// 使用該方法就可以之前跳轉到登錄之前的上一頁
wx.navigateBack({})
} else {
wx.showToast({
title: "登錄失敗",
icon: 'none'
});
}
}
})
},
/**
* 生命周期函數--監聽頁面載入
*/
onLoad(options) {
},
/**
* 生命周期函數--監聽頁面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函數--監聽頁面顯示
*/
onShow() {
},
/**
* 生命周期函數--監聽頁面隱藏
*/
onHide() {
},
/**
* 生命周期函數--監聽頁面卸載
*/
onUnload() {
},
/**
* 頁面相關事件處理函數--監聽用戶下拉動作
*/
onPullDownRefresh() {
},
/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom() {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage() {
}
})
登錄註冊頁css
/* pages/auth/auth.wxss */
.logo{
display: flex;
flex-direction: column;
align-items: center;
}
.logo image {
margin-top: 140rpx;
width: 216rpx;
height: 100rpx;
}
.logo text {
margin-top: 26rpx;
margin-bottom: 50rpx;
font-size: 24rpx;
line-height: 24rpx;
font-weight: 400;
color: #8c8c8c;
text-align: center;
}
.form{
padding: 40rpx;
}
.form .row-group{
padding: 20rpx 0;
border-bottom: 1rpx solid #ddd;
position: relative;
}
.form .row-group text{
font-size: 28rpx;
padding:10rpx 0;
}
.form .row-group input{
padding: 10rpx 0;
}
.form .row-group .txt{
color: #ccc;
}
.form .row-group .code{
position: absolute;
right: 0;
bottom: 26rpx;
z-index: 2;
width: 206rpx;
height: 60rpx;
border: 2rpx solid #00c8b6;
border-radius: 12rpx;
font-size: 26rpx;
font-weight: 400;
color: #00c8b6;
display: flex;
align-items: center;
justify-content: center;
}
.form .submit{
margin-top: 80rpx;
color: #fff;
border: 2rpx solid #00c8b6;
background-color: #00c8b6;
font-size: 32rpx;
font-weight: bold;
}