common.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import * as db from './db.js' //引入common
  2. import store from '../store/index.js'
  3. //把obj对象里的值覆盖到newobj里面
  4. function deepCopy(newobj, obj) {
  5. if (typeof obj != 'object') {
  6. return obj
  7. }
  8. for (var attr in obj) {
  9. var a = {}
  10. if (newobj[attr]) {
  11. a = newobj[attr]
  12. }
  13. newobj[attr] = deepCopy(a, obj[attr])
  14. }
  15. return newobj
  16. }
  17. //操作成功后,的提示信息
  18. function successToShow(msg, callback = function() {}) {
  19. setTimeout(function() {
  20. uni.showToast({
  21. title: msg,
  22. icon: 'success',
  23. duration: 1000,
  24. success() {
  25. setTimeout(function() {
  26. callback()
  27. }, 500)
  28. }
  29. })
  30. }, 100)
  31. }
  32. //操作失败的提示信息
  33. function errorToShow(msg = '操作失败', callback = function() {}) {
  34. setTimeout(function() {
  35. uni.showToast({
  36. title: msg,
  37. icon: 'none',
  38. duration: 1500,
  39. success() {
  40. setTimeout(function() {
  41. callback()
  42. }, 1500)
  43. }
  44. })
  45. }, 100)
  46. }
  47. //加载显示
  48. function loadToShow(msg = '加载中') {
  49. uni.showToast({
  50. title: msg,
  51. icon: 'loading'
  52. })
  53. }
  54. //加载隐藏
  55. function loadToHide() {
  56. uni.hideToast()
  57. }
  58. // 提示框
  59. function modelShow(
  60. title = '提示',
  61. content = '确认执行此操作吗?',
  62. callback = () => {},
  63. showCancel = true,
  64. cancelText = '取消',
  65. confirmText = '确定'
  66. ) {
  67. uni.showModal({
  68. title: title,
  69. content: content,
  70. showCancel: showCancel,
  71. cancelText: cancelText,
  72. confirmText: confirmText,
  73. cancelText: cancelText,
  74. success: function(res) {
  75. if (res.confirm) {
  76. // 用户点击确定操作
  77. setTimeout(() => {
  78. callback()
  79. }, 500)
  80. } else if (res.cancel) {
  81. // 用户取消操作
  82. }
  83. }
  84. })
  85. }
  86. //时间戳转时间格式
  87. function timeToDate(date, flag = false) {
  88. var date = new Date(date * 1000) //如果date为13位不需要乘1000
  89. var Y = date.getFullYear() + '-'
  90. var M =
  91. (date.getMonth() + 1 < 10 ?
  92. '0' + (date.getMonth() + 1) :
  93. date.getMonth() + 1) + '-'
  94. var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
  95. var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  96. var m =
  97. (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  98. var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  99. if (flag) {
  100. return Y + M + D
  101. } else {
  102. return Y + M + D + h + m + s
  103. }
  104. }
  105. function timeToDateObj(micro_second) {
  106. var time = {}
  107. // 总秒数
  108. var second = Math.floor(micro_second)
  109. // 天数
  110. time.day = Math.floor(second / 3600 / 24)
  111. // 小时
  112. time.hour = Math.floor((second / 3600) % 24)
  113. // 分钟
  114. time.minute = Math.floor((second / 60) % 60)
  115. // 秒
  116. time.second = Math.floor(second % 60)
  117. return time
  118. }
  119. //货币格式化
  120. function formatMoney(number, places, symbol, thousand, decimal) {
  121. number = number || 0
  122. places = !isNaN((places = Math.abs(places))) ? places : 2
  123. symbol = symbol !== undefined ? symbol : '¥'
  124. thousand = thousand || ','
  125. decimal = decimal || '.'
  126. var negative = number < 0 ? '-' : '',
  127. i = parseInt((number = Math.abs(+number || 0).toFixed(places)), 10) + '',
  128. j = (j = i.length) > 3 ? j % 3 : 0
  129. return (
  130. symbol +
  131. negative +
  132. (j ? i.substr(0, j) + thousand : '') +
  133. i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) +
  134. (places ?
  135. decimal +
  136. Math.abs(number - i)
  137. .toFixed(places)
  138. .slice(2) :
  139. '')
  140. )
  141. }
  142. function throttle(fn, context, delay) {
  143. clearTimeout(fn.timeoutId)
  144. fn.timeoutId = setTimeout(function() {
  145. fn.call(context)
  146. }, delay)
  147. }
  148. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  149. function dateformat(micro_second) {
  150. var time = {}
  151. // 总秒数
  152. var second = Math.floor(micro_second / 1000) // 天数
  153. time.day = PrefixInteger(Math.floor(second / 3600 / 24), 2) // 小时
  154. time.hour = PrefixInteger(Math.floor((second / 3600) % 24), 2) // 分钟
  155. time.minute = PrefixInteger(Math.floor((second / 60) % 60), 2) // 秒
  156. time.second = PrefixInteger(Math.floor(second % 60), 2)
  157. return time
  158. }
  159. //不足位数前面补0
  160. function PrefixInteger(num, length) {
  161. return (Array(length).join('0') + num).slice(-length)
  162. }
  163. //验证是否是手机号
  164. function isPhoneNumber(str) {
  165. var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  166. if (!myreg.test(str)) {
  167. return false
  168. } else {
  169. return true
  170. }
  171. }
  172. /**
  173. *
  174. * 对象参数转为url参数
  175. *
  176. */
  177. function builderUrlParams(url, data) {
  178. if (typeof url == 'undefined' || url == null || url == '') {
  179. return ''
  180. }
  181. if (typeof data == 'undefined' || data == null || typeof data != 'object') {
  182. return ''
  183. }
  184. url += url.indexOf('?') != -1 ? '' : '?'
  185. for (var k in data) {
  186. url += (url.indexOf('=') != -1 ? '&' : '') + k + '=' + encodeURI(data[k])
  187. }
  188. return url
  189. }
  190. /**
  191. * 使用循环的方式判断一个元素是否存在于一个数组中
  192. * @param {Object} arr 数组
  193. * @param {Object} value 元素值
  194. */
  195. function isInArray(arr, value) {
  196. for (var i = 0; i < arr.length; i++) {
  197. if (value === arr[i]) {
  198. return true
  199. }
  200. }
  201. return false
  202. }
  203. /**
  204. * 统一跳转
  205. */
  206. function navigateTo(url) {
  207. uni.navigateTo({
  208. url: url,
  209. animationType: 'pop-in',
  210. animationDuration: 300
  211. })
  212. }
  213. /**
  214. * 关闭当前页面并跳转
  215. */
  216. function redirectTo(url) {
  217. uni.redirectTo({
  218. url: url,
  219. animationType: 'pop-in',
  220. animationDuration: 300
  221. })
  222. }
  223. export {
  224. deepCopy,
  225. timeToDate,
  226. formatMoney,
  227. successToShow,
  228. throttle,
  229. errorToShow,
  230. isPhoneNumber,
  231. isInArray,
  232. loadToShow,
  233. loadToHide,
  234. navigateTo,
  235. redirectTo,
  236. modelShow,
  237. builderUrlParams,
  238. dateformat,
  239. timeToDateObj,
  240. }