util.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import CONSTANT from '@/constant'
  2. import { mobileReg } from '@/utils/tools/validate'
  3. import { getMiniAppUrl, isWxBrowser } from '@/utils/common'
  4. // 是否为空
  5. const isEmpty = (val) => {
  6. if (val instanceof Array) {
  7. if (val.length === 0) return true
  8. } else if (val instanceof Object) {
  9. if (!Object.keys(val).length) return true
  10. } else {
  11. if (val === 'null' || val === null || val === 'undefined' || val === undefined || val === '') return true
  12. return false
  13. }
  14. return false
  15. }
  16. // 截取图片上传字符串
  17. const imgSubstr = (val) => {
  18. if (isEmpty(val)) return []
  19. if (isArray(val)) {
  20. return val.map(e => {
  21. let index = e.indexOf('/uploads/')
  22. e = e.substring(index)
  23. return e
  24. })
  25. } else {
  26. let index = val.indexOf('/uploads/')
  27. return val.substring(index)
  28. }
  29. }
  30. // 是否为数组
  31. const isArray = (arr) => {
  32. return (typeof arr == 'object') && arr.constructor == Array
  33. }
  34. // 是否为字符串
  35. const isString = (str) => {
  36. return (typeof str == 'string') && str.constructor == String
  37. }
  38. // 是否为对象
  39. const isObject = (obj) => {
  40. return (typeof obj == 'object') && obj.constructor == Object
  41. }
  42. // 是否为数字
  43. const isNumber = (num) => {
  44. return (typeof num == 'number') && num.constructor == Number
  45. }
  46. // 是否为日期类型
  47. const isDate = (date) => {
  48. return (typeof date == 'object') && date.constructor == Date
  49. }
  50. // 是否为函数
  51. const isFunction = (obj) => {
  52. return (typeof obj == 'object') && obj.constructor == Function
  53. }
  54. const checkMobile = (num) => {
  55. return mobileReg.test(num)
  56. }
  57. const checkName = (str) => {
  58. return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str)
  59. }
  60. /** *
  61. * 检查登录
  62. * @parmas query 拼接得参数对象
  63. * @parmas isFrist 为true时开头为&
  64. */
  65. export async function getCheckLogin() {
  66. let status = false
  67. let token = uni.getStorageSync('token')
  68. if (token) {
  69. status = true
  70. } else {
  71. // #ifdef H5
  72. if (isWxBrowser()) {
  73. let option = JSON.parse(uni.getStorageSync('currentQuery')) || {}
  74. console.log('option', option)
  75. // console.log('option', getMiniAppUrl(option, true))
  76. // return false
  77. location.href = `${CONSTANT.hostUrl}/auth/prepare?url=${encodeURIComponent(getMiniAppUrl(option, true))}`
  78. } else {
  79. // uni.showToast({
  80. // title: '请用微信浏览器打开!',
  81. // icon: 'none',
  82. // mask: true
  83. // })
  84. }
  85. // #endif
  86. // #ifndef H5
  87. // #endif
  88. }
  89. return status
  90. }
  91. export const floatFormat = (f, n) => {
  92. let m = Math.pow(10, n)
  93. return parseInt(f * m, 10) / m
  94. }
  95. /** *
  96. * 对象参数转为url参数
  97. * @parmas query 拼接得参数对象
  98. * @parmas isFrist 为true时开头为&
  99. */
  100. export const parse = (query, isFrist = false) => {
  101. let str = Object.keys(query)
  102. .filter(key => !isEmpty(query[key]))
  103. .reduce((result, key) => {
  104. const value = query[key]
  105. // in查询特殊处理
  106. if (Array.isArray(value) && !isEmpty(value)) {
  107. return `${result}&${value.reduce((val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`, '')}`
  108. }
  109. // between查询做特殊处理
  110. if (typeof value === 'object' && !isEmpty(value)) {
  111. const [start, end] = value
  112. return `${result}&${key}[]=${start}&${key}[]=${end}`
  113. }
  114. return `${result}&${key}=${value}`
  115. }, '')
  116. return isFrist ? str : str.replace(/^&/, '?')
  117. }
  118. /** *
  119. * 全局时间转换
  120. * @parmas num 时间戳默认为11位时间戳
  121. * @parmas fmt 默认转换的时间格式
  122. * @parmas all 为true时time为13位时间戳
  123. */
  124. export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm', all) => {
  125. num = all ? num : num * 1000
  126. let date = new Date()
  127. date.setTime(num)
  128. let o = {
  129. 'M+': date.getMonth() + 1,
  130. 'D+': date.getDate(),
  131. 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
  132. 'H+': date.getHours(),
  133. 'm+': date.getMinutes(),
  134. 's+': date.getSeconds(),
  135. 'q+': Math.floor((date.getMonth() + 3) / 3),
  136. 'S': date.getMilliseconds()
  137. }
  138. let week = {
  139. '0': '\u65e5',
  140. '1': '\u4e00',
  141. '2': '\u4e8c',
  142. '3': '\u4e09',
  143. '4': '\u56db',
  144. '5': '\u4e94',
  145. '6': '\u516d'
  146. }
  147. if (/(Y+)/.test(fmt)) {
  148. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  149. }
  150. if (/(E+)/.test(fmt)) {
  151. fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
  152. }
  153. for (let k in o) {
  154. if (new RegExp('(' + k + ')').test(fmt)) {
  155. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  156. }
  157. }
  158. return fmt
  159. }
  160. /** *
  161. * 全局公共按钮跳转
  162. * @parmas item.funtion 为可执行函数
  163. * @parmas item.url 为跳转的路径
  164. * @parmas item.query 为跳转携带的参数
  165. * @parmas item.type 为跳转调用的函数 1为navigateTo, 2为redirectTo, 3为reLaunch, 4为switchTab
  166. */
  167. export const pageTo = (item) => {
  168. if (isNumber(item)) {
  169. uni.navigateBack({
  170. delta: item
  171. })
  172. }
  173. if (isString(item)) {
  174. uni.navigateTo({
  175. url: item
  176. })
  177. return false
  178. }
  179. if (item.funtion) {
  180. item.funtion()
  181. }
  182. if (item.url) {
  183. let query = item.query ? parse(item.query) : ''
  184. let allUrl = `${item.url}${query}`
  185. if (item.type == 2) {
  186. uni.redirectTo({
  187. url: allUrl
  188. })
  189. } else if (item.type == 3) {
  190. uni.reLaunch({
  191. url: allUrl
  192. })
  193. } else if (item.type == 4) {
  194. console.log('item', item)
  195. console.log('switch shop', item.query)
  196. uni.setStorageSync('switchTabQuery', item.query)
  197. if (item.query.account) {
  198. uni.setStorageSync('account', item.query.account)
  199. }
  200. uni.switchTab({
  201. url: item.url
  202. })
  203. } else {
  204. uni.navigateTo({
  205. url: allUrl
  206. })
  207. }
  208. }
  209. }
  210. /** *
  211. * 是否登录
  212. */
  213. export const isLogin = () => {
  214. let token = uni.getStorageSync('token')
  215. if (token) {
  216. return true
  217. }
  218. return false
  219. }
  220. export default {
  221. isEmpty,
  222. imgSubstr,
  223. isArray,
  224. parse,
  225. checkMobile,
  226. checkName,
  227. $formatDate,
  228. floatFormat,
  229. pageTo,
  230. isLogin,
  231. getCheckLogin
  232. }