util.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // 日期格式化1
  2. export function parseTime(time, pattern) {
  3. if (arguments.length === 0) {
  4. return null
  5. }
  6. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  7. let date
  8. if (typeof time === 'object') {
  9. date = time
  10. } else {
  11. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  12. time = parseInt(time)
  13. }
  14. if ((typeof time === 'number') && (time.toString().length === 10)) {
  15. time = time * 1000
  16. }
  17. date = new Date(time)
  18. }
  19. const formatObj = {
  20. y: date.getFullYear(),
  21. m: date.getMonth() + 1,
  22. d: date.getDate(),
  23. h: date.getHours(),
  24. i: date.getMinutes(),
  25. s: date.getSeconds(),
  26. a: date.getDay()
  27. }
  28. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  29. let value = formatObj[key]
  30. // Note: getDay() returns 0 on Sunday
  31. if (key === 'a') {
  32. return ['日', '一', '二', '三', '四', '五', '六'][value]
  33. }
  34. if (result.length > 0 && value < 10) {
  35. value = '0' + value
  36. }
  37. return value || 0
  38. })
  39. return time_str
  40. }
  41. /**
  42. * 防止小程序多次点击跳转
  43. * @param {*} obj
  44. * @returns
  45. */
  46. export function throttle(fn, gapTime) {
  47. if (gapTime == null || gapTime == undefined) {
  48. gapTime = 1500
  49. }
  50. let _lastTime = null
  51. uni.scanCode({
  52. success: function(res) {
  53. console.log('条码类型:' + res.scanType);
  54. console.log('条码内容:' + res.result);
  55. }
  56. });
  57. // 返回新的函数
  58. return function() {
  59. let _nowTime = +new Date()
  60. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  61. fn.apply(this, arguments) //将this和参数传给原函数
  62. _lastTime = _nowTime
  63. }
  64. }
  65. }
  66. export function authorizedLocation() {
  67. return new Promise((resolve, reject) => {
  68. uni.getSetting({
  69. success: (res) => {
  70. // res.authSetting['scope.userLocation'] === undefined 表示 初始化进入,从未授权
  71. // res.authSetting['scope.userLocation'] === true 表示 已授权
  72. // res.authSetting['scope.userLocation'] === false 表示 授权拒绝
  73. if (res.authSetting['scope.userLocation'] === undefined) {
  74. // console.log("弹出位置授权框")
  75. getLocation().then((res) => {
  76. // 授权位置成功
  77. uni.setStorageSync("LocationPlace", res)
  78. resolve(res)
  79. })
  80. .catch((err) => {
  81. // 授权位置失败
  82. reject(err)
  83. uni.showToast({
  84. title: '授权位置失败',
  85. icon: 'none',
  86. duration: 3000
  87. })
  88. })
  89. } else if (res.authSetting['scope.userLocation'] === true) {
  90. // console.log("已经授权")
  91. getLocation().then((res) => {
  92. uni.setStorageSync("LocationPlace", res)
  93. // 授权位置成功
  94. resolve(res)
  95. })
  96. .catch((err) => {
  97. // 授权位置失败
  98. reject(err)
  99. uni.showToast({
  100. title: '授权位置失败',
  101. icon: 'none',
  102. duration: 3000
  103. })
  104. })
  105. } else if (res.authSetting['scope.userLocation'] === false) {
  106. // console.log("弹出允许授权设置框")
  107. uni.authorize({
  108. scope: 'scope.userLocation',
  109. success() {
  110. getLocation().then((res) => {
  111. // 授权位置成功
  112. uni.setStorageSync("LocationPlace", res)
  113. resolve(res)
  114. })
  115. .catch((err) => {
  116. // 授权位置失败
  117. reject(err)
  118. uni.showToast({
  119. title: '授权位置失败',
  120. icon: 'none',
  121. duration: 3000
  122. })
  123. })
  124. },
  125. fail() {
  126. uni.showModal({
  127. title: '您未开启地理位置授权',
  128. content: '是否前往授权?',
  129. success: res => {
  130. if (res.confirm) {
  131. uni.openSetting()
  132. } else {
  133. console.log("取消授权")
  134. resolve('取消授权')
  135. }
  136. },
  137. })
  138. }
  139. })
  140. }
  141. }
  142. })
  143. })
  144. }
  145. // 获取用户当前位置
  146. export function getLocation() {
  147. return new Promise((resolve, reject) => {
  148. uni.getLocation({
  149. type: 'wgs84',
  150. success: (res) => {
  151. resolve(res)
  152. // console.log(res)
  153. // console.log('当前位置的经度:' + res.longitude);
  154. // console.log('当前位置的纬度:' + res.latitude);
  155. },
  156. fail: (err) => {
  157. reject(err)
  158. console.log(err)
  159. }
  160. })
  161. })
  162. }