查看: 633|回复: 0

新建微信小程序官方默认初始代码

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2024-5-26 13:00:56 | 显示全部楼层 |阅读模式
app.js
  1. // app.js
  2. App({
  3.   onLaunch() {
  4.     // 展示本地存储能力
  5.     const logs = wx.getStorageSync('logs') || []
  6.     logs.unshift(Date.now())
  7.     wx.setStorageSync('logs', logs)

  8.     // 登录
  9.     wx.login({
  10.       success: res => {
  11.         // 发送 res.code 到后台换取 openId, sessionKey, unionId
  12.       }
  13.     })
  14.   },
  15.   globalData: {
  16.     userInfo: null
  17.   }
  18. })
复制代码

index.wxml
  1. <!--index.wxml-->
  2. <view class="container">
  3.   <view class="userinfo">
  4.     <block wx:if="{{canIUseOpenData}}">
  5.       <view class="userinfo-avatar" bindtap="bindViewTap">
  6.         <open-data type="userAvatarUrl"></open-data>
  7.       </view>
  8.       <open-data type="userNickName"></open-data>
  9.     </block>
  10.     <block wx:elif="{{!hasUserInfo}}">
  11.       <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
  12.       <button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
  13.       <view wx:else> 请使用1.4.4及以上版本基础库 </view>
  14.     </block>
  15.     <block wx:else>
  16.       <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
  17.       <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  18.     </block>
  19.   </view>
  20.   <view class="usermotto">
  21.     <text class="user-motto">{{motto}}</text>
  22.   </view>
  23. </view>
复制代码
index.js
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()

  4. Page({
  5.   data: {
  6.     motto: 'Hello World',
  7.     userInfo: {},
  8.     hasUserInfo: false,
  9.     canIUse: wx.canIUse('button.open-type.getUserInfo'),
  10.     canIUseGetUserProfile: false,
  11.     canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  12.   },
  13.   // 事件处理函数
  14.   bindViewTap() {
  15.     wx.navigateTo({
  16.       url: '../logs/logs'
  17.     })
  18.   },
  19.   onLoad() {
  20.     if (wx.getUserProfile) {
  21.       this.setData({
  22.         canIUseGetUserProfile: true
  23.       })
  24.     }
  25.   },
  26.   getUserProfile(e) {
  27.     // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  28.     wx.getUserProfile({
  29.       desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  30.       success: (res) => {
  31.         console.log(res)
  32.         this.setData({
  33.           userInfo: res.userInfo,
  34.           hasUserInfo: true
  35.         })
  36.       }
  37.     })
  38.   },
  39.   getUserInfo(e) {
  40.     // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
  41.     console.log(e)
  42.     this.setData({
  43.       userInfo: e.detail.userInfo,
  44.       hasUserInfo: true
  45.     })
  46.   }
  47. })
复制代码
index.wxss
  1. /**index.wxss**/
  2. .userinfo {
  3.   display: flex;
  4.   flex-direction: column;
  5.   align-items: center;
  6.   color: #aaa;
  7. }

  8. .userinfo-avatar {
  9.   overflow: hidden;
  10.   width: 128rpx;
  11.   height: 128rpx;
  12.   margin: 20rpx;
  13.   border-radius: 50%;
  14. }

  15. .usermotto {
  16.   margin-top: 200px;
  17. }
复制代码


道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部