首頁  >  文章  >  微信小程式  >  微信小程式商城開發之用微信授權並實現個人中心的頁面代碼

微信小程式商城開發之用微信授權並實現個人中心的頁面代碼

不言
不言原創
2018-08-16 17:03:3710144瀏覽

這篇文章帶給大家的內容是關於微信小程式商城開發之用微信授權並實現個人中心的頁面代碼,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

看效果


開發計畫

#1、實作微信授權並取得使用者資訊
2、個人中心頁面佈局

一、實作微信授權並取得使用者資訊

mine.js

onLoad: function () {    
    if (app.globalData.userInfo) {      
        this.setData({        
        userInfo: app.globalData.userInfo,        
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {        this.setData({          userInfo: res.userInfo,          hasUserInfo: true
        })
      }
    } else {      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({        success: res => {
          app.globalData.userInfo = res.userInfo          this.setData({            userInfo: res.userInfo,            hasUserInfo: true
          })
        }
      })
    }
  },  
getUserInfo: function (e) {    
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo    
    this.setData({      
        userInfo: e.detail.userInfo,      
        hasUserInfo: true
    })
  }

二、實作微信授權並取得使用者資訊

mine. wxml

<view class="userinfo">
  <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo" class="userinfo-btn"> 点击微信授权 </button>
  <block wx:else>
    <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  </block></view><view class="separate"></view><view class="order" catchtap="toOrder">
  <text class="myorder-text">我的订单</text>
  <text class="myorderlook-text">查看全部订单</text></view><view class="line"></view><view class="navs">
  <block wx:for-items="{{orderItems}}" wx:key="name">
    <view class="nav-item" catchtap="toOrder" data-type="{{item.name}}" data-typeid="{{item.typeId}}">
      <image src="{{item.imageurl}}" class="nav-image" />
      <text>{{item.name}}</text>
    </view>
  </block></view><view class="separate"></view><view class="person-list">

  <view class="list-item">
    <image class="item-image" src="../../images/person/personal_card.png"></image>
    <text class="item-text">优惠券</text>
  </view>
  <view class="person-line"></view>
  <view class="list-item">
    <image class="item-image" src="../../images/person/personal_favorite.png"></image>
    <text class="item-text">我的收藏</text>
  </view>
  <view class="person-line"></view>
  <view class="list-item">
    <image class="item-image" src="../../images/person/personal_site.png"></image>
    <text class="item-text">收货地址</text>
  </view>
  <view class="person-line"></view>
  <view class="list-item">
    <image class="item-image" src="../../images/person/personal_sale_record.png"></image>
    <text class="item-text">售后记录</text>
  </view>
  <view class="person-line"></view>
  <view class="list-item">
    <image class="item-image" src="../../images/person/personal_evaluated.png"></image>
    <text class="item-text">我的评价</text>
  </view>
  <view class="person-line"></view>
  <view class="list-item">
    <image class="item-image" src="../../images/person/personal_customer.png"></image>
    <text class="item-text">在线客服</text>
  </view></view><view class="separate"></view>

mine.wxss

.userinfo {  
    display: flex;  
    flex-direction: column;  
    align-items: center;  
    background: #f0145a;   
    width: 100%; 
    height: 300rpx;
}
.userinfo-btn{  
    margin-top: 50rpx;  
    background: none !important;  
    color: #fff !important;  
    font-size: 40rpx;
}
.account-bg {  
    width: 100%;  
    height: 150rpx;
}
.userinfo-avatar {  
    width: 108rpx;  
    height: 108rpx;  
    margin: 40rpx;  
    border-radius: 50%;
}
.userinfo-nickname {  
    color: #fff;
}
/* 订单 */
.order {  
    display: flex;  
    flex-direction: row;  
    align-items: center;  
    width: 100%;  
    height: 90rpx;
}
.myorder-text {  
    font-size: 34rpx;  
    color: gray;  
    margin: 20rpx;  
    width: 40%;
}
.myorderlook-text {  
    font-size: 32rpx;  
    color: gray;  
    position: relative;  
    right: 20rpx;  
    width: 60%;  
    text-align: right;
}
.next-image {  
    width: 20rpx;  
    height: 25rpx;  
    position: relative;  
    right: 10rpx;
}
.navs {  
    display: flex;
}
.nav-item {  
    width: 25%;  
    display: flex;  
    align-items: center;  
    flex-direction: column;  
    padding: 20rpx;
}
.nav-item .nav-image {  
    width: 55rpx;  
    height: 55rpx;  
    margin: 5rpx;
}
.nav-item text {  
    margin-top: 20rpx;  
    font-size: 28rpx;  
    color: gray;
}
/* 列表 */
.person-list {  
    display: flex;  
    flex-direction: column;  
    align-items: left;
}
.list-item {      
    display: flex;  
    flex-direction: row;  
    align-items: center;  
    height: 80rpx;
}
.item-image {  
    width: 40rpx;  
    height: 40rpx;  
    margin: 20rpx;
}
.item-text {  
    color: gray;  
    font-size: 30rpx;  
    margin-left: 20rpx;
}
.person-line {  
    width: 80%;  
    height: 2rpx;  
    background: lightgray;  
    margin-left: 90rpx;
}

mine.js

var app = getApp()
Page({  
    data: {    
        userInfo: {},    
        hasUserInfo: false,        
        canIUse: wx.canIUse(&#39;button.open-type.getUserInfo&#39;),    
        orderItems: [
      {        
        typeId: 0,        
        name: &#39;待付款&#39;,        
        url: &#39;bill&#39;,        
        imageurl: &#39;../../images/person/personal_pay.png&#39;,
      },
      {        
        typeId: 1,        
        name: &#39;待收货&#39;,        
        url: &#39;bill&#39;,        
        imageurl: &#39;../../images/person/personal_receipt.png&#39;,
      },
      {        
        typeId: 2,        
        name: &#39;待评价&#39;,        
        url: &#39;bill&#39;,        
        imageurl: &#39;../../images/person/personal_comment.png&#39;
      },
      {        
        typeId: 3,        
        name: &#39;退换/售后&#39;,        
        url: &#39;bill&#39;,        
        imageurl: &#39;../../images/person/personal_service.png&#39;
      }
    ],
  },  
    //事件处理函数
  toOrder: function () {
    wx.navigateTo({      url: &#39;../order/order&#39;
    })
  }
}

mine.json

{  "navigationBarTitleText": "个人中心"}

相關推薦:

#微信小程式商城開發之實現商品加入購物車的功能(程式碼)

#微信小程式商城開發之動態API實作特賣商品的串流佈局程式碼

######################################## ######微信小程式商城開發之動態API實作商品的詳情頁的程式碼(下)#######

以上是微信小程式商城開發之用微信授權並實現個人中心的頁面代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn