作者 刘晓艳

框架

  1 +//app.js
  2 +App({
  3 + onLaunch: function () {
  4 + // 展示本地存储能力
  5 + var logs = wx.getStorageSync('logs') || []
  6 + logs.unshift(Date.now())
  7 + wx.setStorageSync('logs', logs)
  8 +
  9 + // 登录
  10 + wx.login({
  11 + success: res => {
  12 + // 发送 res.code 到后台换取 openId, sessionKey, unionId
  13 + }
  14 + })
  15 + // 获取用户信息
  16 + wx.getSetting({
  17 + success: res => {
  18 + if (res.authSetting['scope.userInfo']) {
  19 + // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  20 + wx.getUserInfo({
  21 + success: res => {
  22 + // 可以将 res 发送给后台解码出 unionId
  23 + this.globalData.userInfo = res.userInfo
  24 +
  25 + // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  26 + // 所以此处加入 callback 以防止这种情况
  27 + if (this.userInfoReadyCallback) {
  28 + this.userInfoReadyCallback(res)
  29 + }
  30 + }
  31 + })
  32 + }
  33 + }
  34 + })
  35 + },
  36 + globalData: {
  37 + userInfo: null
  38 + }
  39 +})
  1 +{
  2 + "pages": [
  3 + "pages/my/my",
  4 + "pages/index/index"
  5 + ],
  6 + "window": {
  7 + "backgroundTextStyle": "light",
  8 + "navigationBarBackgroundColor": "#fff",
  9 + "navigationBarTitleText": "WeChat",
  10 + "navigationBarTextStyle": "black"
  11 + }
  12 +}
  1 +/**app.wxss**/
  2 +.container {
  3 + height: 100%;
  4 + display: flex;
  5 + flex-direction: column;
  6 + align-items: center;
  7 + justify-content: space-between;
  8 + padding: 200rpx 0;
  9 + box-sizing: border-box;
  10 +}
  1 +//index.js
  2 +//获取应用实例
  3 +const app = getApp()
  4 +
  5 +Page({
  6 + data: {
  7 + motto: 'Hello World',
  8 + userInfo: {},
  9 + hasUserInfo: false,
  10 + canIUse: wx.canIUse('button.open-type.getUserInfo')
  11 + },
  12 + //事件处理函数
  13 + bindViewTap: function() {
  14 + wx.navigateTo({
  15 + url: '../logs/logs'
  16 + })
  17 + },
  18 + onLoad: function () {
  19 + if (app.globalData.userInfo) {
  20 + this.setData({
  21 + userInfo: app.globalData.userInfo,
  22 + hasUserInfo: true
  23 + })
  24 + } else if (this.data.canIUse){
  25 + // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  26 + // 所以此处加入 callback 以防止这种情况
  27 + app.userInfoReadyCallback = res => {
  28 + this.setData({
  29 + userInfo: res.userInfo,
  30 + hasUserInfo: true
  31 + })
  32 + }
  33 + } else {
  34 + // 在没有 open-type=getUserInfo 版本的兼容处理
  35 + wx.getUserInfo({
  36 + success: res => {
  37 + app.globalData.userInfo = res.userInfo
  38 + this.setData({
  39 + userInfo: res.userInfo,
  40 + hasUserInfo: true
  41 + })
  42 + }
  43 + })
  44 + }
  45 + },
  46 + getUserInfo: function(e) {
  47 + console.log(e)
  48 + app.globalData.userInfo = e.detail.userInfo
  49 + this.setData({
  50 + userInfo: e.detail.userInfo,
  51 + hasUserInfo: true
  52 + })
  53 + }
  54 +})
  1 +{
  2 + "usingComponents": {}
  3 +}
  1 +<!--index.wxml-->
  2 +<view class="container">
  3 + <view class="userinfo">
  4 + <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
  5 + <block wx:else>
  6 + <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
  7 + <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  8 + </block>
  9 + </view>
  10 + <view class="usermotto">
  11 + <text class="user-motto">{{motto}}</text>
  12 + </view>
  13 +</view>
  1 +/**index.wxss**/
  2 +.userinfo {
  3 + display: flex;
  4 + flex-direction: column;
  5 + align-items: center;
  6 +}
  7 +
  8 +.userinfo-avatar {
  9 + width: 128rpx;
  10 + height: 128rpx;
  11 + margin: 20rpx;
  12 + border-radius: 50%;
  13 +}
  14 +
  15 +.userinfo-nickname {
  16 + color: #aaa;
  17 +}
  18 +
  19 +.usermotto {
  20 + margin-top: 200px;
  21 +}
  1 +// pages/my/my.js
  2 +Page({
  3 +
  4 + /**
  5 + * 页面的初始数据
  6 + */
  7 + data: {
  8 +
  9 + },
  10 +
  11 + /**
  12 + * 生命周期函数--监听页面加载
  13 + */
  14 + onLoad: function (options) {
  15 +
  16 + },
  17 +
  18 + /**
  19 + * 生命周期函数--监听页面初次渲染完成
  20 + */
  21 + onReady: function () {
  22 +
  23 + },
  24 +
  25 + /**
  26 + * 生命周期函数--监听页面显示
  27 + */
  28 + onShow: function () {
  29 +
  30 + },
  31 +
  32 + /**
  33 + * 生命周期函数--监听页面隐藏
  34 + */
  35 + onHide: function () {
  36 +
  37 + },
  38 +
  39 + /**
  40 + * 生命周期函数--监听页面卸载
  41 + */
  42 + onUnload: function () {
  43 +
  44 + },
  45 +
  46 + /**
  47 + * 页面相关事件处理函数--监听用户下拉动作
  48 + */
  49 + onPullDownRefresh: function () {
  50 +
  51 + },
  52 +
  53 + /**
  54 + * 页面上拉触底事件的处理函数
  55 + */
  56 + onReachBottom: function () {
  57 +
  58 + },
  59 +
  60 + /**
  61 + * 用户点击右上角分享
  62 + */
  63 + onShareAppMessage: function () {
  64 +
  65 + }
  66 +})
  1 +{
  2 + "usingComponents": {}
  3 +}
  1 +<!--pages/my/my.wxml-->
  2 +<view class='banner_box'>
  3 + <view class='head_img'>
  4 + <image src='{{user_info.avatar}}'></image>
  5 + </view>
  6 + <view class='head_info_box'>
  7 + <view class='user_name'>{{user_info.name}}
  8 + <image wx:if='{{user_info.approve==1}}' src='/images/verify.png'></image>
  9 + </view>
  10 + <view>{{user_info.mobile}}</view>
  11 + <view>{{user_info.work}}</view>
  12 + <view>{{user_info.work_post}}</view>
  13 + </view>
  14 + <view class='edit_box' bindtap='editInfo' wx:if='{{user_type!=2}}'>
  15 + <view class='iconfont icon-xiugai'></view>
  16 + <view>修改个人资料</view>
  17 + </view>
  18 + <view class='edit_box edit2' bindtap='charge' wx:if='{{user_type==4}}'>
  19 + <view>身份切换</view>
  20 + </view>
  21 +</view>
  22 +<!-- 顾问个人中心 -->
  23 +<view class='content_box' wx:if='{{user_type==3||temp_UserType==3}}'>
  24 + <view class='item_list' bindtap='TB'>
  25 + <view class='list_icon'>
  26 + <text class='iconfont icon-kakou'></text>
  27 + </view>
  28 + <view class='list_item_box'>
  29 + <view>T币交易记录</view>
  30 + <view class='iconfont icon-jinru'></view>
  31 + </view>
  32 + </view>
  33 + <view class='item_list' bindtap='vipCharge'>
  34 + <view class='list_icon'>
  35 + <text class='iconfont icon-gray-crown'></text>
  36 + </view>
  37 + <view class='list_item_box'>
  38 + <view>会员充值</view>
  39 + <view class='list_info'>
  40 + <text>{{counselor.end_time}}{{counselor.end_time!=''?'到期':''}}</text>
  41 + <text class='iconfont icon-jinru'></text>
  42 + </view>
  43 + </view>
  44 + </view>
  45 + <view class='item_list' bindtap='reportList'>
  46 + <view class='list_icon'>
  47 + <text class='iconfont icon-baogao'></text>
  48 + </view>
  49 + <view class='list_item_box'>
  50 + <view>报告浏览记录</view>
  51 + <view class='iconfont icon-jinru'></view>
  52 + </view>
  53 + </view>
  54 + <view class='item_list' bindtap='articlList'>
  55 + <view class='list_icon'>
  56 + <text class='iconfont icon-baogao1'></text>
  57 + </view>
  58 + <view class='list_item_box'>
  59 + <view>文章浏览记录</view>
  60 + <view class='iconfont icon-jinru'></view>
  61 + </view>
  62 + </view>
  63 + <view class='item_list' bindtap='suggest'>
  64 + <view class='list_icon'>
  65 + <text class='iconfont icon-jianyi'></text>
  66 + </view>
  67 + <view class='list_item_box'>
  68 + <view>建议预设</view>
  69 + <view class='iconfont icon-jinru'></view>
  70 + </view>
  71 + </view>
  72 + <view class='item_list' bindtap='project'>
  73 + <view class='list_icon'>
  74 + <text class='iconfont icon-fangan'></text>
  75 + </view>
  76 + <view class='list_item_box'>
  77 + <view>方案预设</view>
  78 + <view class='iconfont icon-jinru'></view>
  79 + </view>
  80 + </view>
  81 + <view class='item_list' bindtap='share'>
  82 + <view class='list_icon'>
  83 + <text class='iconfont icon-fenxiang1'></text>
  84 + </view>
  85 + <view class='list_item_box'>
  86 + <view>分享赚T币</view>
  87 + <view class='iconfont icon-jinru'></view>
  88 + </view>
  89 + </view>
  90 + <view class='item_list' bindtap='twoCode' data-imgurl='{{counselor.wechat_code}}'>
  91 + <view class='list_icon'>
  92 + <text class='iconfont icon-xiazai'></text>
  93 + </view>
  94 + <view class='list_item_box'>
  95 + <view>下载专属二维码</view>
  96 + <view class='iconfont icon-jinru'></view>
  97 + </view>
  98 + </view>
  99 + <view class='item_list' bindtap='inviteList'>
  100 + <view class='list_icon'>
  101 + <text class='iconfont icon-yaoqing'></text>
  102 + </view>
  103 + <view class='list_item_box'>
  104 + <view>邀请名单</view>
  105 + <view class='iconfont icon-jinru'></view>
  106 + </view>
  107 + </view>
  108 + <view class='item_list' bindtap='remind'>
  109 + <view class='list_icon'>
  110 + <text class='iconfont icon-YDUI-naozhong'></text>
  111 + </view>
  112 + <view class='list_item_box'>
  113 + <view>7天到期提醒</view>
  114 + <view class='iconfont icon-jinru'></view>
  115 + </view>
  116 + </view>
  117 +</view>
  1 +/* pages/my/my.wxss */
  1 +{
  2 + "description": "项目配置文件",
  3 + "packOptions": {
  4 + "ignore": []
  5 + },
  6 + "setting": {
  7 + "urlCheck": true,
  8 + "es6": true,
  9 + "postcss": true,
  10 + "minified": true,
  11 + "newFeature": true,
  12 + "autoAudits": false
  13 + },
  14 + "compileType": "miniprogram",
  15 + "libVersion": "2.6.1",
  16 + "appid": "wx9053b434e50ee2dd",
  17 + "projectname": "%E6%9D%8E%E4%B8%83%E5%BA%84",
  18 + "debugOptions": {
  19 + "hidedInDevtools": []
  20 + },
  21 + "isGameTourist": false,
  22 + "condition": {
  23 + "search": {
  24 + "current": -1,
  25 + "list": []
  26 + },
  27 + "conversation": {
  28 + "current": -1,
  29 + "list": []
  30 + },
  31 + "game": {
  32 + "currentL": -1,
  33 + "list": []
  34 + },
  35 + "miniprogram": {
  36 + "current": -1,
  37 + "list": []
  38 + }
  39 + }
  40 +}
  1 +const formatTime = date => {
  2 + const year = date.getFullYear()
  3 + const month = date.getMonth() + 1
  4 + const day = date.getDate()
  5 + const hour = date.getHours()
  6 + const minute = date.getMinutes()
  7 + const second = date.getSeconds()
  8 +
  9 + return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  10 +}
  11 +
  12 +const formatNumber = n => {
  13 + n = n.toString()
  14 + return n[1] ? n : '0' + n
  15 +}
  16 +
  17 +module.exports = {
  18 + formatTime: formatTime
  19 +}