common.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { getDicts } from '@/api/dict.js'
  2. /**
  3. * 显示消息提示框
  4. * @param content 提示的标题
  5. */
  6. export function toast(content) {
  7. uni.showToast({
  8. icon: 'none',
  9. title: content
  10. })
  11. }
  12. /**
  13. * 显示模态弹窗
  14. * @param content 提示的标题
  15. */
  16. export function showConfirm(content) {
  17. return new Promise((resolve, reject) => {
  18. uni.showModal({
  19. title: '提示',
  20. content: content,
  21. cancelText: '取消',
  22. confirmText: '确定',
  23. success: function(res) {
  24. resolve(res)
  25. }
  26. })
  27. })
  28. }
  29. /**
  30. * 参数处理
  31. * @param params 参数
  32. */
  33. export function tansParams(params) {
  34. let result = ''
  35. for (const propName of Object.keys(params)) {
  36. const value = params[propName]
  37. var part = encodeURIComponent(propName) + "="
  38. if (value !== null && value !== "" && typeof(value) !== "undefined") {
  39. if (typeof value === 'object') {
  40. for (const key of Object.keys(value)) {
  41. if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
  42. let params = propName + '[' + key + ']'
  43. var subPart = encodeURIComponent(params) + "="
  44. result += subPart + encodeURIComponent(value[key]) + "&"
  45. }
  46. }
  47. } else {
  48. result += part + encodeURIComponent(value) + "&"
  49. }
  50. }
  51. }
  52. return result
  53. }
  54. // 返回上一页
  55. export function back() {
  56. uni.navigateBack({
  57. delta: 1
  58. })
  59. }
  60. // 页面跳转
  61. export function navigateTo(url) {
  62. uni.navigateTo({
  63. url: url
  64. })
  65. }
  66. // 回显数据字典
  67. export function transDictLabel(datas, value) {
  68. if (value === undefined) {
  69. return "";
  70. }
  71. var actions = [];
  72. Object.keys(datas).some((key) => {
  73. if (datas[key].value == ('' + value)) {
  74. actions.push(datas[key].text);
  75. return true;
  76. }
  77. })
  78. if (actions.length === 0) {
  79. actions.push(value);
  80. }
  81. return actions.join('');
  82. }
  83. /**
  84. * 批量获取字典数据
  85. * dictTypeArr: 字典名称数组
  86. * result: 接数据容器
  87. */
  88. export function getDictList(dictTypeArr, result) {
  89. dictTypeArr.forEach(dictType => {
  90. getDicts(dictType).then(res => {
  91. if (res.code !== 200) return
  92. result[dictType] = changeDictAttrName(res.data)
  93. })
  94. })
  95. }
  96. // 更改字典数据的属性名
  97. export function changeDictAttrName(array) {
  98. let result = JSON.stringify(array).replaceAll('dictLabel', 'text').replaceAll('dictValue', 'value')
  99. return JSON.parse(result)
  100. }