The installation

npm install wepy-utils

According to the need to introduce

import { UTILS, HTTP, TIPS } from 'wepy-utils'

Utils

UTILS.now()

Gets the current timestamp

let now = UTILS.now()
console.log(now)
UTILS.random()

Returns an arbitrary interval random number

let random = UTILS.random(1, 5)
console.log(random)
UTILS.param()

Parses the object into a URL string

let obj = {id: 1, name: 'Ming'} / / parser object let strResult = UTILS. The param (obj, true) / / second argument true | false indicates whether or not to use unDecodeURI coding, Default false console.log(strResult) //? id=1&name=ming
UTILS.unparam()

Parses the URL string into an object

let url = '? Id = 1 & name = Ming '/ / the object of the need to parse the let objResult = UTILS. Unparam (url, true) / / second argument true | false indicates whether or not to use unDecodeURI decoding, Default false console.log(objResult) // {id: 1, name: 'Ming '}

HTTP Request

GET
POST
PATCH
PUT
DELETE

HTTP.get()

The first way to use it is to take no arguments to the URL. The second way to use it is to add a parameter after the request URL, such as:
? id=1&name=ming

  • HTTP.get(url).then((data) => {}).catch((error) => {})
  • HTTP.get({url: url, params: [JSON Object] }, mask: [Boolean]).then((data) => {}).catch((error) => {})
let url = 'urlpath'
HTTP.get({
  url: url,
  params: {id: 1, name: 'ming'},
  mask: true
}).then((data) => {
  console.log(data)
}).catch((error) => {
  console.log(error)
})
HTTP.post()

You can customize headers if desired
AuthorizationEtc. default:
'Content-Type': 'application/json'

HTTP.post({
  url: url, params: {id: 1, name: 'ming' },
  mask: true,
  loading: false,
  headers: {'X-Requested-With': 'XMLHttpRequest'}
}).then((data) => {
  console.log(data)
}).catch((error) => {
  console.log(error)
})
HTTP.patch() HTTP.put() HTTP.delete()Request mode andHTTP.post()Write a similar
// HTTP PATCH
HTTP.patch({url: url, params: [JSON Object], headers: [JSON Object], mask: [Boolean] }).then((data) => {}).catch((error) => {})
// HTTP PUT
HTTP.put({url: url, params: [JSON Object], headers: [JSON Object], mask: [Boolean] }).then((data) => {}).catch((error) => {})
// HTTP DELETE
HTTP.delete({url: url, params: [JSON Object], headers: [JSON Object], mask: [Boolean] }).then((data) => {}).catch((error) => {})

maskWhether to display a transparent mask to prevent touch penetration. Default:
false.
loadingWhether to display during a network request add
wx.showLoadingLoad animation, display by default.

⚠️ OnPullDownRefresh listener drop-down action when used with wx.showLoading causes a top bounce BUG. So you can set Loading to false when using OnPullDownRefresh

Tips

interface

TIPS.toast()

Display message prompt box (all parameters of wx.showToast can be customized, except success, fail, complete)

Tips. toast({title: 'prompt title '}) // After setting duration > 0, hide it to support callback (duration default 1500) tips. toast({title:' prompt title '}) Then (() => {console.log(' Hide back callback ')}).
TIPS.confirm()

Resolve is optional. The other optional parameters are the same as wx.showModal, except that showCancel is changed to cancel

Confirm ({title: 'prompt ', content:' prompt ', payload: [1, 2, 3]}). Then ((arr) = > {the console. The log (' click the confirm, arr [2]) / / 3}). The catch (() = > {the console. The log (' click on the cancel ')})
TIPS.setTitle()

Dynamic sets the title of the current page

TIPS.setTitle('Hello WePy')
TIPS.loading()

Loading prompt box is displayed, and the prompt content can be customized. Transparent mask is displayed by default to prevent touch penetration

Tips. loading(' Load title ')
TIPS.loaded()

Hide the Loading prompt box

TIPS.loaded()

file

TIPS.downloadSaveFile()

Download a single file

let url = 'url'
TIPS.downloadSaveFile({
  url: url,
  success: (res) => {
    console.log(res)
  },
  fail: (err) => {
    console.log(err)
  }
})
TIPS.downloadSaveFiles()

Download multiple files

let urls = ['url1','url2','url3'] TIPS.downloadSaveFiles({ urls: urls, progress: true, success: (res) => {// Progress if set: False data will be returned after full download) console.log(' Download progress :${res.step}% ') // All loaded if (res.step === 100) {console.log(res) res.forEach((value, key) => { console.log(`Key:${key} = Value:${value.savedFilePath}`) }) } }, fail: (err) => { console.log(err) } })

navigation

TIPS.navigateTo()

Keep the current page and jump to a page in the application

let url = 'test' let params = {id:1 ,name: 'ming'} TIPS.navigateTo(url, params) // test? id=1&name=ming
TIPS.redirectTo()

Close the current page and jump to a page in the application

let url = 'test' let params = {id:1 ,name: 'ming'} TIPS.redirectTo(url, params) // test? id=1&name=ming
TIPS.switchTab()

Jump to the tabBar page and close all other non-tabbar pages

let url = 'test' let params = {id:1 ,name: 'ming'} TIPS.switchTab(url, params) // test? id=1&name=ming
TIPS.reLaunch()

Close all the pages and go to a page in your app

let url = 'test' let params = {id:1 ,name: 'ming'} TIPS.reLaunch(url, params) // test? id=1&name=ming

TIPS.navigateTo()
TIPS.redirectTo()
TIPS.switchTab()
TIPS.reLaunch()In the
paramsThe argument is an optional object