我的第一个微信小程序

1. 前言

牛客网第三期编程作业是体验下微信小程序的编写, 由于本菜鸟对前端这块理解并不深入,整起来比较费劲, 突然想到一个idea, 顺手简单实现一下, 基本效果如下:
这里写图片描述
有点恶搞的成分, 哈哈~~

2. 参考资料

很快论坛
官网教程
微天气 - 开发一个完整的微信小程序(上)
微天气 - 开发一个完整的微信小程序(中)

3. 注意点

这里写图片描述
这个问题在于, 没有在utils.js 中将需要的函数进行导出

4. 基本实现

4.1 项目框架

这里写图片描述

4.2 index

index.js

//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: '来让我们看看百度的源码吧',
    userInfo: {}
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../source_get/source_get'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  }
})

index.wxml

<!--index.wxml-->
<view class="container">
  <view  bindtap="bindViewTap" class="userinfo">
    <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  </view>
  <view class="usermotto">
    <text class="user-motto">{{motto}}</text>
  </view>
</view>

index.wxss

/**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;
}

4.3 source_get

source_get.js

var utils_t = require('../../utils/util.js')

Page({
    data: {
        data: {}
    },
    onLoad: function (){
        var that = this;

        utils_t.source_get(function(data){
            that.setData({
                data: "就不给你看, 你咬我呀~~~"
            })
        });
    }
})

source_get.wxml

<!--source_get.wxml-->
<view class="container"> 
    <view class = "top">
        <view>{{data}}</view>
</view>

source_get.wxss

.top{
    width: 500rpx;
    height: 5rpx;
    margin: 3rpx;
}

4.4 utils.js

function source_get(callback){
  var url = "https://www.baidu.com";

  wx.request({
    url: url,
    data: {},
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function(res){
      // success   
      callback(res.data);   
    },
    fail: function() {
      // fail
      callback("failed to get the source");
    },
    complete: function() {
      // complete
    }
  });
}

module.exports = {
  source_get: source_get
}

4.5 app

app.js

//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData:{
    userInfo:null
  }
})

app.json

{
  "pages":[
    "pages/index/index",
    "pages/source_get/source_get"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle":"black"
  }
}

app.wxss

/**app.wxss**/
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
} 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值