不知不觉后端搬砖六年了,回想过去,什么也没留下,突然觉得是时候写点什么了。

为什么我要选择小程序呢,主要是觉得上手简单,易于传播,同时可以投放微信广告为自己赚零花钱,何乐而不为。我的第一个小程序主要是想总结下这几年的编程积累,同时分享出来。分为基础内容和实战篇,目前仅上线了基础内容。目前我已开发了一个小程序和一款基于LayaBox的小游戏。先把小程序的讲解完,然后会分享LayaBox开发的小游戏,目前小游戏在申请软著,申请后会全部分享出来给大家分享,请期待。

大家可以体验一下,请扫码:

闲话少聊,我们开始正事。先把我的小程序运行效果图发一下。PS:(前端太烂,勿喷,欢迎高手不吝赐教)

  

 

  关于小程序文档结构的内容大家可以扫上边的二位码在小程序里查看或者去查看官方文档。

UI界面基于WEUI,目前处于学习阶段,欢迎大家加群交流,我会在评论区留言。

页面内容:

  1. <view class="container">
  2. <view class="userinfo">
  3. <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
  4. <block wx:else>
  5. <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
  6. <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  7. </block>
  8. </view>
  9. <view class="usermotto">
  10. <text class="user-motto">
  11. 我们坚持分享基于LayaBox开发的微信小游戏,并在这里讲解开发步骤,同时分享一些公共模块给大家!!!
  12. </text>
  13. </view>
  14. <view class="usermotto">
  15. <text class="user-motto">Copyright © 2019</text>
  16. </view>
  17. <view>
  18. <text class="user-motto">{{motto}}</text>
  19. </view>
  20. </view>

 

这个首页的创建大家可以直接创一个快速启动项目,不需要过多修改。

脚本文件

  1. const app = getApp()
  2. Page({
  3. data: {
  4. motto: \' 莹百游 All Rights Reserved.\',
  5. userInfo: {},
  6. hasUserInfo: false,
  7. canIUse: wx.canIUse(\'button.open-type.getUserInfo\')
  8. },
  9. //事件处理函数
  10. bindViewTap: function() {
  11. wx.navigateTo({
  12. url: \'../logs/logs\'
  13. })
  14. },
  15. onLoad: function () {
  16. if (app.globalData.userInfo) {
  17. this.setData({
  18. userInfo: app.globalData.userInfo,
  19. hasUserInfo: true
  20. });
  21. wx.redirectTo({
  22. url: \'../home/home\'
  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. wx.redirectTo({
  33. url: \'../home/home\'
  34. });
  35. }
  36. } else {
  37. // 在没有 open-type=getUserInfo 版本的兼容处理
  38. wx.getUserInfo({
  39. success: res => {
  40. app.globalData.userInfo = res.userInfo
  41. this.setData({
  42. userInfo: res.userInfo,
  43. hasUserInfo: true
  44. })
  45. wx.redirectTo({
  46. url: \'../home/home\'
  47. });
  48. }
  49. })
  50. }
  51. },
  52. getUserInfo: function(e) {
  53. app.globalData.userInfo = e.detail.userInfo
  54. this.setData({
  55. userInfo: e.detail.userInfo,
  56. hasUserInfo: true
  57. })
  58. wx.redirectTo({
  59. url: \'../home/home\'
  60. });
  61. }
  62. })

 

这段脚本我们几乎不要修改什么内容,就可以实现用户授权获取用户头像和昵称,我们只需要在里面实现跳转页面就好了。

本项目目前三个界面,我会分3篇讲解,希望大家关注。全部代码我会在第三篇分享出来给大家,希望大家加V群一起学习。

 

版权声明:本文为wyang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wyang/p/10259572.html