common.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * 显示消息提示框
  3. * @param content 提示的标题
  4. */
  5. export function toast(content) {
  6. uni.showToast({
  7. icon: 'none',
  8. title: content
  9. })
  10. }
  11. /**
  12. * 传入毫秒,转成i按月日
  13. * @param milliseconds
  14. * @returns {string}
  15. */
  16. export function formatDate(milliseconds) {
  17. let date = new Date(milliseconds);
  18. let year = date.getUTCFullYear();
  19. let month = date.getUTCMonth() + 1;
  20. let day = date.getUTCDate();
  21. let hours = date.getUTCHours();
  22. let minutes = date.getUTCMinutes();
  23. let seconds = date.getUTCSeconds();
  24. // 为了使结果更加易读,可以对月、日、小时、分钟和秒进行补零操作
  25. month = month < 10 ? '0' + month : month;
  26. day = day < 10 ? '0' + day : day;
  27. hours = hours < 10 ? '0' + hours : hours;
  28. minutes = minutes < 10 ? '0' + minutes : minutes;
  29. seconds = seconds < 10 ? '0' + seconds : seconds;
  30. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  31. }
  32. /**
  33. * 显示模态弹窗
  34. * @param content 提示的标题
  35. */
  36. export function showConfirm(content) {
  37. return new Promise((resolve, reject) => {
  38. uni.showModal({
  39. title: '提示',
  40. content: content,
  41. cancelText: '取消',
  42. confirmText: '确定',
  43. success: function(res) {
  44. resolve(res)
  45. }
  46. })
  47. })
  48. }
  49. /**
  50. * 参数处理
  51. * @param params 参数
  52. */
  53. export function tansParams(params) {
  54. let result = ''
  55. for (const propName of Object.keys(params)) {
  56. const value = params[propName]
  57. var part = encodeURIComponent(propName) + "="
  58. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  59. if (typeof value === 'object') {
  60. for (const key of Object.keys(value)) {
  61. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  62. let params = propName + '[' + key + ']'
  63. var subPart = encodeURIComponent(params) + "="
  64. result += subPart + encodeURIComponent(value[key]) + "&"
  65. }
  66. }
  67. } else {
  68. result += part + encodeURIComponent(value) + "&"
  69. }
  70. }
  71. }
  72. return result
  73. }