123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- // 日期格式化1
- export function parseTime(time, pattern) {
- if (arguments.length === 0) {
- return null
- }
- const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
- let date
- if (typeof time === 'object') {
- date = time
- } else {
- if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
- time = parseInt(time)
- }
- if ((typeof time === 'number') && (time.toString().length === 10)) {
- time = time * 1000
- }
- date = new Date(time)
- }
- const formatObj = {
- y: date.getFullYear(),
- m: date.getMonth() + 1,
- d: date.getDate(),
- h: date.getHours(),
- i: date.getMinutes(),
- s: date.getSeconds(),
- a: date.getDay()
- }
- const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
- let value = formatObj[key]
- // Note: getDay() returns 0 on Sunday
- if (key === 'a') {
- return ['日', '一', '二', '三', '四', '五', '六'][value]
- }
- if (result.length > 0 && value < 10) {
- value = '0' + value
- }
- return value || 0
- })
- return time_str
- }
- /**
- * 防止小程序多次点击跳转
- * @param {*} obj
- * @returns
- */
- export function throttle(fn, gapTime) {
- if (gapTime == null || gapTime == undefined) {
- gapTime = 1500
- }
- let _lastTime = null
- uni.scanCode({
- success: function(res) {
- console.log('条码类型:' + res.scanType);
- console.log('条码内容:' + res.result);
- }
- });
- // 返回新的函数
- return function() {
- let _nowTime = +new Date()
- if (_nowTime - _lastTime > gapTime || !_lastTime) {
- fn.apply(this, arguments) //将this和参数传给原函数
- _lastTime = _nowTime
- }
- }
- }
- export function authorizedLocation() {
- return new Promise((resolve, reject) => {
- uni.getSetting({
- success: (res) => {
- // res.authSetting['scope.userLocation'] === undefined 表示 初始化进入,从未授权
- // res.authSetting['scope.userLocation'] === true 表示 已授权
- // res.authSetting['scope.userLocation'] === false 表示 授权拒绝
- if (res.authSetting['scope.userLocation'] === undefined) {
- // console.log("弹出位置授权框")
- getLocation().then((res) => {
- // 授权位置成功
- uni.setStorageSync("LocationPlace", res)
- resolve(res)
- })
- .catch((err) => {
- // 授权位置失败
- reject(err)
- uni.showToast({
- title: '授权位置失败',
- icon: 'none',
- duration: 3000
- })
- })
- } else if (res.authSetting['scope.userLocation'] === true) {
- // console.log("已经授权")
- getLocation().then((res) => {
- uni.setStorageSync("LocationPlace", res)
- // 授权位置成功
- resolve(res)
- })
- .catch((err) => {
- // 授权位置失败
- reject(err)
- uni.showToast({
- title: '授权位置失败',
- icon: 'none',
- duration: 3000
- })
- })
- } else if (res.authSetting['scope.userLocation'] === false) {
- // console.log("弹出允许授权设置框")
- uni.authorize({
- scope: 'scope.userLocation',
- success() {
- getLocation().then((res) => {
- // 授权位置成功
- uni.setStorageSync("LocationPlace", res)
- resolve(res)
- })
- .catch((err) => {
- // 授权位置失败
- reject(err)
- uni.showToast({
- title: '授权位置失败',
- icon: 'none',
- duration: 3000
- })
- })
- },
- fail() {
- uni.showModal({
- title: '您未开启地理位置授权',
- content: '是否前往授权?',
- success: res => {
- if (res.confirm) {
- uni.openSetting()
- } else {
- console.log("取消授权")
- resolve('取消授权')
- }
- },
- })
- }
- })
- }
- }
- })
- })
- }
- // 获取用户当前位置
- export function getLocation() {
- return new Promise((resolve, reject) => {
- uni.getLocation({
- type: 'wgs84',
- success: (res) => {
- resolve(res)
- // console.log(res)
- // console.log('当前位置的经度:' + res.longitude);
- // console.log('当前位置的纬度:' + res.latitude);
- },
- fail: (err) => {
- reject(err)
- console.log(err)
- }
- })
- })
- }
|