第一次写小程序,因为偏心 Vue,因此选则了 mpvue 这个框架。那就写个青铜版微博来练练手吧。javascript
图1:微博首页
图2:发微博
图3:个人
一、小程序框架:mpvuecss
二、http 请求库:fly.jsvue
三、状态管理:vuexjava
四、数据来源:微博开放平台 apigit
一、微博列表github
二、发微博vuex
三、我的信息json
四、个人粉丝小程序
五、我关注的人api
六、个人收藏(收藏和取消收藏)
七、查看个人微博(因为api限制,只能查看本身的)
八、分享到微信聊天
api ----------------------- 全部的api划分模块
|-- user.js // 用户相关的 api
config ---------------------- 一些基本的配置
|-- const.js // 基本常量
|-- fly.js // fly 的配置
|-- http.js // 基本的请求封装 好比 get,post 请求
components -------------------- 最小单位的基础组件
|-- Btn.vue // button 组件
|-- Input.vue // input 组件
views -------------------- 业务组件
|-- PostCell.vue // 信息流单条微博组件
|-- UserCell.vue // 单个用户信息组件 pages ---------------------- 全部的页面
|-- timeline ------------- 时间线页面(一个页面一个文件夹)
|-- index.vue // 页面组件
|-- main.js // 页面的入口文件
|-- main.json // 页面配置文件
store --------------------- 状态管理
| -- module -- 模块
| |- user.js // 用户模块
| |- post.js // 微博模块
| -- getters.js // 全局getters
| -- actions.js // 全局 actions
| -- mutations.js // 全局 mutations
| -- state.js // 全局 state
| -- mutation-types.js // 全部的 mutation types
| -- index.js // 整合成一个 store,导出
utils --------------------- 存放全部的工具函数
|-- index.js // 工具函数
images --------------------- 全部的图片资源(小程序中不支持 svg)
|-- home.png
app.json -------------------- 整个小程序的全局配置
App.vue -------------------- 给全局入口一个挂载点
main.js -------------------- 全局入口文件
复制代码
跟 Vue 项目中使用只有一个区别
vue :
new Vue({
el: '#app',
store,
....
})复制代码
Vue.prototype.$store = store
挂在到全局
<template>
<div class="container">
我是一个 .vue 组件
</div>
</template>
<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
export default {
data () {
return {}
},
computed: {
...mapGetters({
userInfo: 'userInfo'
}),
...mapState({
someState: 'someState'
})
},
components: { },
onShow () {
// 小程序的钩子函数
},
methods: {
...mapMutations({
someMutation: 'someMutation'
}),
...mapActions({
someAction: 'someAction'
})
}
}
</script>
<style lang="scss" scoped>
</style>
复制代码
// vue 的钩子函数
created () {
},
// 小程序的生命周期函数
onShow() {
}
。。。复制代码
看到这里你会发现,跟写 vue 项目没啥太大的区别。彻底是 vue 的写法,只须要去关注小程序提供哪些能力便可。
相比于微信 oauth 受权,微博显得更加开放,微信是不给开发权限的,必须提供备案的域名。
说明:发微博的接口微博是没有提供的,我借用了第三方分享到微博这个接口,看上去跟发微博没啥区别,但内容中必须带上一个安全域名,这个域名是在微博开放平台设置的。