作者 刘晓艳

框架

//app.js
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
globalData: {
userInfo: null
}
})
\ No newline at end of file
... ...
{
"pages": [
"pages/my/my",
"pages/index/index"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black"
}
}
\ No newline at end of file
... ...
/**app.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}
... ...
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
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
})
}
})
... ...
{
"usingComponents": {}
}
\ No newline at end of file
... ...
<!--index.wxml-->
<view class="container">
<view class="userinfo">
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
</view>
</view>
... ...
/**index.wxss**/
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
}
.userinfo-avatar {
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}
.userinfo-nickname {
color: #aaa;
}
.usermotto {
margin-top: 200px;
}
\ No newline at end of file
... ...
// pages/my/my.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
\ No newline at end of file
... ...
{
"usingComponents": {}
}
\ No newline at end of file
... ...
<!--pages/my/my.wxml-->
<view class='banner_box'>
<view class='head_img'>
<image src='{{user_info.avatar}}'></image>
</view>
<view class='head_info_box'>
<view class='user_name'>{{user_info.name}}
<image wx:if='{{user_info.approve==1}}' src='/images/verify.png'></image>
</view>
<view>{{user_info.mobile}}</view>
<view>{{user_info.work}}</view>
<view>{{user_info.work_post}}</view>
</view>
<view class='edit_box' bindtap='editInfo' wx:if='{{user_type!=2}}'>
<view class='iconfont icon-xiugai'></view>
<view>修改个人资料</view>
</view>
<view class='edit_box edit2' bindtap='charge' wx:if='{{user_type==4}}'>
<view>身份切换</view>
</view>
</view>
<!-- 顾问个人中心 -->
<view class='content_box' wx:if='{{user_type==3||temp_UserType==3}}'>
<view class='item_list' bindtap='TB'>
<view class='list_icon'>
<text class='iconfont icon-kakou'></text>
</view>
<view class='list_item_box'>
<view>T币交易记录</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='vipCharge'>
<view class='list_icon'>
<text class='iconfont icon-gray-crown'></text>
</view>
<view class='list_item_box'>
<view>会员充值</view>
<view class='list_info'>
<text>{{counselor.end_time}}{{counselor.end_time!=''?'到期':''}}</text>
<text class='iconfont icon-jinru'></text>
</view>
</view>
</view>
<view class='item_list' bindtap='reportList'>
<view class='list_icon'>
<text class='iconfont icon-baogao'></text>
</view>
<view class='list_item_box'>
<view>报告浏览记录</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='articlList'>
<view class='list_icon'>
<text class='iconfont icon-baogao1'></text>
</view>
<view class='list_item_box'>
<view>文章浏览记录</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='suggest'>
<view class='list_icon'>
<text class='iconfont icon-jianyi'></text>
</view>
<view class='list_item_box'>
<view>建议预设</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='project'>
<view class='list_icon'>
<text class='iconfont icon-fangan'></text>
</view>
<view class='list_item_box'>
<view>方案预设</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='share'>
<view class='list_icon'>
<text class='iconfont icon-fenxiang1'></text>
</view>
<view class='list_item_box'>
<view>分享赚T币</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='twoCode' data-imgurl='{{counselor.wechat_code}}'>
<view class='list_icon'>
<text class='iconfont icon-xiazai'></text>
</view>
<view class='list_item_box'>
<view>下载专属二维码</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='inviteList'>
<view class='list_icon'>
<text class='iconfont icon-yaoqing'></text>
</view>
<view class='list_item_box'>
<view>邀请名单</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
<view class='item_list' bindtap='remind'>
<view class='list_icon'>
<text class='iconfont icon-YDUI-naozhong'></text>
</view>
<view class='list_item_box'>
<view>7天到期提醒</view>
<view class='iconfont icon-jinru'></view>
</view>
</view>
</view>
\ No newline at end of file
... ...
/* pages/my/my.wxss */
\ No newline at end of file
... ...
{
"description": "项目配置文件",
"packOptions": {
"ignore": []
},
"setting": {
"urlCheck": true,
"es6": true,
"postcss": true,
"minified": true,
"newFeature": true,
"autoAudits": false
},
"compileType": "miniprogram",
"libVersion": "2.6.1",
"appid": "wx9053b434e50ee2dd",
"projectname": "%E6%9D%8E%E4%B8%83%E5%BA%84",
"debugOptions": {
"hidedInDevtools": []
},
"isGameTourist": false,
"condition": {
"search": {
"current": -1,
"list": []
},
"conversation": {
"current": -1,
"list": []
},
"game": {
"currentL": -1,
"list": []
},
"miniprogram": {
"current": -1,
"list": []
}
}
}
\ No newline at end of file
... ...
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime
}
... ...