npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@evolify/wxtools

v0.1.1

Published

* ### 安装:

Downloads

1

Readme

微信开发中常用的工具,目前主要是网络请求方法的增强,以及对几乎所有的异步操作的Promisify.

  • 安装:

yarn add @evolify/wxtools

或者

npm i @evolify/wxtools
  • 小程序网络请求

 把wx.request封装成了Promise风格的。使用的时候使用get、post等方法即可。支持添加拦截器,可以单独设置header、token、baseUrl等。下面以post方法为例。

  • 引入js文件:

    import {request as req} from '@evolify/wxtools'
  • 配置Request,一般在小程序App.js>onLaunch方法中配置

      configReq(){
        //配置baseUrl和拦截器,baseUrl例如 /api
        req.baseUrl(config.serverUrl)
          .interceptor(res=>{
            switch(res.data.code){
              case 401: 
                wx.showToast({
                  icon: 'loading',
                  title: '重新登录',
                })
                this.login()
                return false;
              case 0:
                return true;
              default:
                wx.showToast({
                  title: '操作失败',
                })
                return false;
            }
          })
      },
  • 登录成功后设置token

    req.token(token)
  • 发起网络请求

    req.post('/goods',data,header)
    	.then(res=>res.data.data)
    	.then(data=>{
        	wx.showToast({
                title:'创建成功'
            })
    	})
  • 异步操作Promise的使用

  wx api里面很多操作都是异步的,基本上参数形式都差不多.这里做了Promise封装,方法名称、参数和官方api一致,只是参数里面不用传success、fail回调。以showToast为例,简单对比一下:
  常规用法:

wx.showToast({
  title: '成功',
  icon: 'success',
  duration: 2000,
  success: ()=>{
    console.log('调用成功')
  },
  fail: err=>{
    console.error('调用失败',err)
  }
})

  Promise用法:

import {Wx} from '@evolify/wxtools'

Wx.showToast({
  title: '成功',
  icon: 'success',
  duration: 2000,
}).then(()=>{
  console.log('调用成功')
}).catch(err=>{
  console.error('调用失败',err)
})

  其他的api用法都一样,方法、参数、返回值可以参考官方文档。如果有错误或者有缺失的,欢迎提issue.

  • Promisify

    import {promisify} from '@evolify/wxtools'
      
    function func({
      param1,
      param2,
      success: ()=>{},
      fail: ()=>{}
    }){
      // do something
      success()
    }
      
    const promisifyFunc = params => promisify(func, params)
      
    // then you can use
    promisifyFunc(data)
    	.then(res=>{
      		// on success
    	})
    	.catch(err=>{
      	// on failed
    })
    // instand of 
    func({
      ...data,
      success: res=>{
          
      },
      fail: res => {
          
      }
    })
  • 定时任务队列

     有些场景,我们需要定义一序列的延时任务,比如就界面动画而言,一些元素依次进场,并且有一定间隔,这时候如果我们直接用setTimeout, 就会遇到毁掉地狱的问题,代码很难看,且难以维护。这里封装了一个工具,可解决此类问题,具体介绍请参考文章:js定时任务队列,用法如下:

    import {Schedule} from '@evolify/wxtools'
      
    new Schedule().task(task1)
      .delay(1000).task(()=>this.setData({title:'Shedule'}))
      .delay(500).task(task3)