11 Star 42 Fork 8

evolify / wxtools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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

使用方法:把你需要的工具文件拷贝到你的项目中,然后import进去。

  • 小程序网络请求

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

    • 引入js文件:

      import req from '../../utils/Request.js'
    • 配置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 'Wx.js'

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

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

  • 定时任务队列

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

    import Schedule from 'Shedule.js'
    
    new Schedule().task(task1)
      .delay(1000).task(()=>this.setData({title:'Shedule'}))
      .delay(500).task(task3)
MIT License Copyright (c) 2017 evolify Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

微信开发中常用的一些封装。比如小程序中wx.request的Promise封装。 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/evolify/wxtools.git
git@gitee.com:evolify/wxtools.git
evolify
wxtools
wxtools
master

搜索帮助