123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /**
- * @Time 2019-12-12
- * @Author Zhengjingyi
- * @Action 全局公共方法
- */
- import requestUrl from '@/services/config.env.js'
- export function getComStorage(key) { // 获取本地Storage
- return new Promise(function(resolve, reject) {
- uni.getStorage({
- key: key,
- success: function(res) {
- resolve(res.data)
- },
- fail: function(res) {
- reject(false)
- }
- })
- })
- }
- export function setStorage(key, data) { // 存储本地Storage
- return new Promise(function(resolve, reject) {
- uni.setStorage({
- key: key,
- data: data,
- success: function(res) {}
- })
- })
- }
- export function getStorage() { // 获取本地userInfo
- return new Promise(function(resolve, reject) {
- uni.getStorage({
- key: 'userInfo',
- success: function(res) {
- resolve(res.data)
- },
- fail: function(res) {
- reject(false)
- }
- })
- })
- }
- export function getStorageAddressKey() { // 获取本地地址信息
- return new Promise(function(resolve, reject) {
- uni.getStorage({
- key: 'address_key',
- success: function(res) {
- resolve(res.data)
- }
- })
- })
- }
- export function navigateTo(url) {
- //路由跳转:页面之间路由跳转
- uni.navigateTo({
- url: url
- })
- }
- export function redirectTo(url) {
- //路由跳转:关闭当前页跳转到新页面
- uni.redirectTo({
- url: url
- })
- }
- export function switchTabTo(url) {
- //路由跳转:底部 tab页
- uni.switchTab({
- url: url
- })
- }
- export function getWindowHeight() {
- // 获取窗口高度
- const { windowHeight, pixelRatio } = wx.getSystemInfoSync()
- return windowHeight
- }
- export function adaptRichTextImg(res) {
- /**
- *@富文本实现图片自适应
- *@style再添加自适应样式
- */
- const html = res.replace(/<img[^>]*>/gi, function(match, capture) {
- let match1 = match.replace(/<img*/gi,
- '<img style="width:100% !important;height:auto !important;float:left !important;"'),
- results = match1.replace(/style=/gi, 'style="width:100%;height:auto;float:left;"')
- return results
- })
- return html
- }
- export function formatDate() {
- //获取当前时间
- let date = new Date()
- let y = date.getFullYear()
- let MM = date.getMonth() + 1
- MM = MM < 10 ? ('0' + MM) : MM
- let d = date.getDate()
- d = d < 10 ? ('0' + d) : d
- let h = date.getHours()
- h = h < 10 ? ('0' + h) : h
- let m = date.getMinutes()
- m = m < 10 ? ('0' + m) : m
- let s = date.getSeconds()
- s = s < 10 ? ('0' + s) : s
- return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
- }
- export function isNumber(value) { //验证是否为数字
- var patrn = /^(-)?\d+(\.\d+)?$/
- if (patrn.exec(value) == null || value == '') {
- return false
- } else {
- return true
- }
- }
- export function FormatMoney(num) {
- // 金额千分位
- return num.toString().replace(/\d+/, function(n) { // 先提取整数部分
- return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) { // 对整数部分添加分隔符
- return $1 + ','
- })
- })
- }
- const install = Vue => {
- console.log('初始化挂载($api)工具方法 utilsTools.js')
- Vue.prototype.$api = {
- FormatMoney,
- formatDate,
- navigateTo,
- redirectTo,
- switchTabTo,
- isNumber,
- setStorage,
- getStorage,
- getComStorage,
- getWindowHeight,
- adaptRichTextImg,
- getStorageAddressKey,
- }
- }
- export default install
|