diff --git a/framework/1-utils.js b/framework/1-utils.js index 48fd4a2..6457a5f 100644 --- a/framework/1-utils.js +++ b/framework/1-utils.js @@ -10,26 +10,26 @@ let transformRequest = obj => { let query = '' let name, value, fullSubName, subName, subValue, innerObj, i - for(name in obj) { + for (name in obj) { value = obj[name] - if(value instanceof Array) { - for(i = 0; i < value.length; ++i) { + if (value instanceof Array) { + for (i = 0; i < value.length; ++i) { subValue = value[i] fullSubName = name + '[' + i + ']' innerObj = {} innerObj[fullSubName] = subValue query += transformRequest(innerObj) + '&' } - } else if(value instanceof Object) { - for(subName in value) { + } else if (value instanceof Object) { + for (subName in value) { subValue = value[subName] fullSubName = name + '[' + subName + ']' innerObj = {} innerObj[fullSubName] = subValue query += transformRequest(innerObj) + '&' } - } else if(value !== undefined && value !== null) { + } else if (value !== undefined && value !== null) { query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&' } @@ -44,7 +44,7 @@ let timestamp = function() { let isNavigating = false let isNavigate = () => { - if(isNavigating) { + if (isNavigating) { return true } else { isNavigating = true @@ -62,11 +62,11 @@ let guid = (function() { let guid = new Date().getTime().toString(32), i - for(i = 0; i < 5; i++) { + for (i = 0; i < 5; i++) { guid += Math.floor(Math.random() * 65535).toString(32) } - return(prefix || '') + guid + (counter++).toString(32) + return (prefix || '') + guid + (counter++).toString(32) } }()) @@ -75,7 +75,7 @@ let sortTransform = (obj) => { objKeys = objKeys.sort() var ret = {} - for(var i = 0; i < objKeys.length; i++) { + for (var i = 0; i < objKeys.length; i++) { let objVal = obj[objKeys[i]] ret[objKeys[i]] = objVal } @@ -95,8 +95,8 @@ function isEmptyObject(v) { return Object.keys(v).length == 0 } -function sleep (time) { - return new Promise((resolve) => setTimeout(resolve, time)) +function sleep(time) { + return new Promise((resolve) => setTimeout(resolve, time)) } const throttle = function(func, wait = 200, options) { @@ -110,7 +110,7 @@ const throttle = function(func, wait = 200, options) { var context, args, result var timeout = null var previous = 0 - if(!options) options = { + if (!options) options = { leading: true, trailing: false } @@ -118,26 +118,26 @@ const throttle = function(func, wait = 200, options) { previous = options.leading === false ? 0 : new Date().getTime() timeout = null result = func.apply(context, args) - if(!timeout) context = args = null + if (!timeout) context = args = null } return function() { var now = new Date().getTime() - if(!previous && options.leading === false) previous = now + if (!previous && options.leading === false) previous = now // 计算剩余时间 var remaining = wait - (now - previous) context = this args = arguments // 当到达wait指定的时间间隔,则调用func函数 - if(remaining <= 0 || remaining > wait) { + if (remaining <= 0 || remaining > wait) { // 由于setTimeout存在最小时间精度问题,因此会存在到达wait的时间间隔,但之前设置的setTimeout操作还没被执行,因此为保险起见,这里先清理setTimeout操作 - if(timeout) { + if (timeout) { clearTimeout(timeout) timeout = null } previous = now result = func.apply(context, args) - if(!timeout) context = args = null - } else if(!timeout && options.trailing !== false) { + if (!timeout) context = args = null + } else if (!timeout && options.trailing !== false) { // options.trailing=true时,延时执行func函数 timeout = setTimeout(later, remaining) } @@ -153,13 +153,13 @@ const debounce = function(func, wait, immediate) { // 当wait指定的时间间隔期间多次调用_.debounce返回的函数,则会不断更新timestamp的值,导致last < wait && last >= 0一直为true,从而不断启动新的计时器延时执行func var last = new Date().getTime() - timestamp - if(last < wait && last >= 0) { + if (last < wait && last >= 0) { timeout = setTimeout(later, wait - last) } else { timeout = null - if(!immediate) { + if (!immediate) { result = func.apply(context, args) - if(!timeout) context = args = null + if (!timeout) context = args = null } } } @@ -171,8 +171,8 @@ const debounce = function(func, wait, immediate) { // 第一次调用该方法时,且immediate为true,则调用func函数 var callNow = immediate && !timeout // 在wait指定的时间间隔内首次调用该方法,则启动计时器定时调用func函数 - if(!timeout) timeout = setTimeout(later, wait) - if(callNow) { + if (!timeout) timeout = setTimeout(later, wait) + if (callNow) { result = func.apply(context, args) context = args = null } @@ -181,12 +181,14 @@ const debounce = function(func, wait, immediate) { } } -Promise.prototype.finally = function (callback) { - let P = this.constructor - return this.then( - value => P.resolve(callback()).then(() => value), - reason => P.resolve(callback()).then(() => { throw reason }) - ) +Promise.prototype.finally = function(callback) { + let P = this.constructor + return this.then( + value => P.resolve(callback()).then(() => value), + reason => P.resolve(callback()).then(() => { + throw reason + }) + ) } const info_distance = function(e) { //获取元素位置 return new Promise((resolve, reject) => { @@ -204,6 +206,37 @@ const getCurrentRoute = function() { return '/' + getCurrentPage().route } +const pluschooseImage = function() { + // #ifdef APP + if (plus.os.name == 'Android' && plus.navigator.checkPermission('android.permission.CAMERA') === + 'undetermined') { + //未授权 + uni.showModal({ + title: '权限说明', + content: '便于您使用该功能上传您的照片/图片等,请您确认授权相机与相册,否则无法使用该功能', + confirmText: "去设置", + success: (res) => { + if (res.confirm) { + uni.openAppAuthorizeSetting({ + success(res) { + console.log(res); + } + }); + } + if (res.cancel) { + console.log('用户点击取消'); + } + } + }); + } else { + return true + } + // #endif + // #ifdef MP-WEIXIN + return true + // #endif +} + uni.utils = { md5, transformRequest, @@ -219,5 +252,6 @@ uni.utils = { throttle, debounce, getCurrentPage, - getCurrentRoute -} + getCurrentRoute, + pluschooseImage +} \ No newline at end of file diff --git a/pages/index/components/diamond.vue b/pages/index/components/diamond.vue index d6120e3..fb991be 100644 --- a/pages/index/components/diamond.vue +++ b/pages/index/components/diamond.vue @@ -1,6 +1,7 @@