All Projects → thundernet8 → common-mpvue

thundernet8 / common-mpvue

Licence: other
使用mpvue开发小程序通用能力封装

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to common-mpvue

Alaweb
一套 Vue 代码,多端可用(H5、小程序、苹果App、安卓App、头条等)。系统含150+页面,200+组件(5端通用),30+元件(每个终端独立完成)
Stars: ✭ 837 (+2600%)
Mutual labels:  wxapp, mpvue
Ithome Lite
🥛 IT之家第三方小程序版客户端(使用 mpvue 开发,兼容 web)
Stars: ✭ 531 (+1612.9%)
Mutual labels:  wxapp, mpvue
citySelector
🛳基于mpvue的微信小程序 城市/区县定位选择模块 汉字/拼音搜索 可直接使用 已更新
Stars: ✭ 52 (+67.74%)
Mutual labels:  wxapp, mpvue
Meedu Wxapp
📜meEdu微信小程序。(wxapp for meedu)
Stars: ✭ 199 (+541.94%)
Mutual labels:  wxapp, mpvue
Wx Book
仿追书神器的小说阅读器小程序
Stars: ✭ 122 (+293.55%)
Mutual labels:  wxapp, mpvue
Mpvue Weui
用 vue 写小程序,基于 mpvue 框架重写 weui。
Stars: ✭ 1,463 (+4619.35%)
Mutual labels:  wxapp, mpvue
Vue Mpvue Chatrobot
㊙A chat robot for web & Wechat producted by vue+mpvue+nodejs.
Stars: ✭ 269 (+767.74%)
Mutual labels:  wxapp, mpvue
Mp canvas drawer
🚀 微信小程序上canvas绘制图片助手,一个json就制作分享朋友圈图片
Stars: ✭ 1,611 (+5096.77%)
Mutual labels:  wxapp, mpvue
We Cropper
微信小程序图片裁剪工具
Stars: ✭ 1,972 (+6261.29%)
Mutual labels:  wxapp, mpvue
mpvue canvas drawer
[mpvue版本]微信小程序上canvas绘制图片助手,一个json就制作分享朋友圈图片
Stars: ✭ 43 (+38.71%)
Mutual labels:  wxapp, mpvue
wxapp-storage
简单的微信小程序Storage相关的封装, 特点是安全的数据源, 有效的存储时间
Stars: ✭ 13 (-58.06%)
Mutual labels:  wxapp
uParse
📰适用于 uni-app/mpvue 的富文本解析自定义组件
Stars: ✭ 45 (+45.16%)
Mutual labels:  mpvue
mpvue-apps
使用 Vue.js 开发小程序的前端框架
Stars: ✭ 21 (-32.26%)
Mutual labels:  mpvue
taroCloud
记日常、GitHub trending资讯小程序 taro-hooks + rematch+云开发
Stars: ✭ 25 (-19.35%)
Mutual labels:  wxapp
mp-framework-benchmark
mp-framework-benchmark
Stars: ✭ 49 (+58.06%)
Mutual labels:  mpvue
wxsport
微信运动运营小程序,模仿腾讯公益,捐步
Stars: ✭ 22 (-29.03%)
Mutual labels:  wxapp
mpvue-cnode
mpvue实现的微信小程序版的cnode论坛
Stars: ✭ 84 (+170.97%)
Mutual labels:  mpvue
taro-weapp
🎮一款提供餐桌,酒桌上小游戏的小程序。
Stars: ✭ 28 (-9.68%)
Mutual labels:  wxapp
mpvue-one
使用mpvue构建的ONE·一个小程序,仅供学习使用
Stars: ✭ 19 (-38.71%)
Mutual labels:  mpvue
mobx-wxapp
在小程序中使用mobx
Stars: ✭ 54 (+74.19%)
Mutual labels:  wxapp

MPVue 开发小程序的通用能力封装

Promisify 的 wx API

import { wxp } from 'common-mpvue';

wxp.request({ ...config }).then(resp => {
    // do something
});

可持久化至小程序 storage 的 VueStore

import { PersistStore } from 'common-mpvue';

const store = new PersistStore(storageKey, {
    ...storeOptions
});

Emitter

方便不同页面间通信,为保证同一个 emitter 实例,请通过getApp().emitter获取这个绑定在 app 下的 emitter 实例

const app = getApp();

// 添加监听
app.emitter.on('event1', function() {});

// 添加一次性监听
app.emitter.once('event1', function() {});

// 移除监听
app.emitter.off('event1', listener);

// 移除全部监听
app.emitter.offAll('event1');

Navigator

解决了页面栈过大时无法跳转的问题,自动使用redirectTo替代navigateTo

const app = new App();

app.nav.navigateTo('/pages/index/index', { param1: '1' });

app.nav.navigateToH5('https://example.com');
// 注意配置小程序webview页面 /pages/webview/webview

Utilities

提供少量的实用方法

const app = getApp();

// 给url添加query参数
app.utils.addUrlQuery('/pages/index/index', { param1: '1' });
// => /pages/index/index?param1=1

// 解析url的query为对象
app.utils.parseUrlQuery('/pages/index/index?param1=1');
// => {param1: 1}

Request

提供了封装的 Request 对象,适度的请求队列管理,建议使用wx.httpRequestwxp.httpRequest这个绑定在全局变量下的固定实例

wx.httpRequest.httpGet('https://example.com', { param1: '1' });

// json形式post
wx.httpRequest.httpJsonPost('https://example.com', { data1: '1' }, { ...requestOptions });

// form表单形式post
wx.httpRequest.httpFormPost('https://example.com', { data1: '1' }, { ...requestOptions });

// httpPost等同httpFormPost
wx.httpRequest.httpPost('https://example.com', { data1: '1' }, { ...requestOptions });

为了减少重复填写配置,可以重新实例化一个有固定配置的请求实例

// 配置请求携带token并以cookie形式传递
const httpRequest = wx.httpRequest
    .auth()
    .cookieToken()
    .form();

httpRequest.GET('https://example.com');

httpRequest.POST('https://example.com', { data1: '1' });

目前支持如下配置链

  • auth - 携带 token 用于鉴权,auth(true)
  • tokenKey - 携带 token 的 key,tokenKey('key')
  • headerToken - 通过 header 携带 token
  • cookieToken - 通过 cookie 携带 token
  • qsToken - 通过 url query 携带 token
  • form - 通过 form 表单形式 post
  • custom - 携带自定义 header 头,包含设备信息等

按照约定优于配置的原则,推荐数据接口有统一约定的响应

推荐格式

{
    code: 200, // 标记本次响应状态,200为成功,当API不论错误还是异常均是以200的http status响应时,通过此code标记实际的响应status是必要的
    loginCode: 200, // loginCode可选,当登录态失效时返回非200的loginCode,方便库清理本地登录状态以引导重新登录
    message: "", // 错误时推荐返回非空message,便于promise catch
    data: {
        posts: [],
        total: 100
    }
}

说明

为了将这些能力引入到 app,wx,wxp 等对象上,需要按如下操作:

入口 main.js

import { wrap } from 'common-mpvue';
import App from './index.vue';

wrap(App, appConfig, appProps);

wrap的签名如下

/**
 * 封装和扩展小程序app实例以及wx
 * @param App 入口的Vue组件
 * @param config app配置
 * @param props 给app实例扩展更多的方法或属性
 */
wrap(App, config: AppConfig, props?: BaseKV)

interface AppConfig {
    // 小程序名称
    name: string;
    // 小程序版本
    version: string;
    // 小程序工程package.json中的name
    pkgName: string;
    // 开发环境
    env?: 'production' | 'development';
    // api域名
    domain?: string;
    // 使用app.logger可上报信息的API
    reportDomain?: string;
    // 用于展示H5的页面,例如/pages/webview/webview
    webviewSchema?: string;
}

页面 main.js

import { WrapPage } from 'common-mpvue';
import Page from './index.vue';

new WrapPage(Page);

// 或者为页面添加vuex store
new WrapPage(Page, {
    state() {
        name: '';
    },
    mutations: {
        updateName(state, name) {
            state.name = name;
        }
    }
});

在页面的 vue 单文件组件内,可以通过this.$app访问小程序 app 实例,可以通过this.$store访问共享的业务 vuex store,可以通过this.$state访问该页面的状态模块

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].