common.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {
  2. getDicts
  3. } from '@/api/dict.js'
  4. /**
  5. * 显示消息提示框
  6. * @param content 提示的标题
  7. */
  8. export function toast(content) {
  9. uni.showToast({
  10. icon: 'none',
  11. title: content,
  12. mask: true,
  13. })
  14. }
  15. /**
  16. * 显示模态弹窗
  17. * @param content 提示的标题
  18. */
  19. export function showConfirm(content, showCancel) {
  20. if (!showCancel) showCancel = false
  21. return new Promise((resolve, reject) => {
  22. uni.showModal({
  23. title: '提示',
  24. content: content,
  25. cancelText: '取消',
  26. showCancel: showCancel,
  27. confirmText: '确定',
  28. success: function(res) {
  29. resolve(res)
  30. }
  31. })
  32. })
  33. }
  34. /**
  35. * 参数处理
  36. * @param params 参数
  37. */
  38. export function tansParams(params) {
  39. let result = ''
  40. for (const propName of Object.keys(params)) {
  41. const value = params[propName]
  42. var part = encodeURIComponent(propName) + "="
  43. if (value !== null && value !== "" && typeof(value) !== "undefined") {
  44. if (typeof value === 'object') {
  45. for (const key of Object.keys(value)) {
  46. if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
  47. let params = propName + '[' + key + ']'
  48. var subPart = encodeURIComponent(params) + "="
  49. result += subPart + encodeURIComponent(value[key]) + "&"
  50. }
  51. }
  52. } else {
  53. result += part + encodeURIComponent(value) + "&"
  54. }
  55. }
  56. }
  57. return result
  58. }
  59. // 返回上一页
  60. export function back() {
  61. uni.navigateBack({
  62. delta: 1
  63. })
  64. }
  65. // 页面跳转
  66. export function navigateTo(url) {
  67. uni.navigateTo({
  68. url: url
  69. })
  70. }
  71. // 回显数据字典
  72. export function transDictLabel(datas, value) {
  73. if (value === undefined) {
  74. return "";
  75. }
  76. var actions = [];
  77. Object.keys(datas).some((key) => {
  78. if (datas[key].value == ('' + value)) {
  79. actions.push(datas[key].text);
  80. return true;
  81. }
  82. })
  83. if (actions.length === 0) {
  84. actions.push(value);
  85. }
  86. return actions.join('');
  87. }
  88. /**
  89. * 批量获取字典数据
  90. * dictTypeArr: 字典名称数组
  91. * result: 接数据容器
  92. */
  93. export function getDictList(dictTypeArr, result) {
  94. dictTypeArr.forEach(dictType => {
  95. getDicts(dictType).then(res => {
  96. if (res.code !== 200) return
  97. result[dictType] = changeDictAttrName(res.data)
  98. })
  99. })
  100. }
  101. // 更改字典数据的属性名
  102. export function changeDictAttrName(array) {
  103. let result = JSON.stringify(array).replaceAll('dictLabel', 'text').replaceAll('dictValue', 'value')
  104. return JSON.parse(result)
  105. }
  106. // 匹配字典值
  107. export function getDictInfo(array, value) {
  108. return array.filter(e => e.text.includes(value))
  109. }
  110. /**
  111. * @description 本地图片转base64方法(兼容APP、H5、小程序)
  112. * @param {number} path 图片本地路径
  113. * @returns Promise对象
  114. */
  115. const toBase64 = (path) => {
  116. return new Promise((resolve, reject) => {
  117. // #ifdef APP-PLUS
  118. plus.io.resolveLocalFileSystemURL(path, (entry) => {
  119. entry.file((file) => {
  120. let fileReader = new plus.io.FileReader()
  121. fileReader.readAsDataURL(file)
  122. fileReader.onloadend = (evt) => {
  123. let base64 = evt.target.result.split(",")[1]
  124. resolve(base64)
  125. }
  126. })
  127. })
  128. // #endif
  129. // #ifdef H5
  130. uni.request({
  131. url: path,
  132. responseType: 'arraybuffer',
  133. success: (res) => {
  134. resolve(uni.arrayBufferToBase64(res.data))
  135. }
  136. })
  137. // #endif
  138. // #ifdef MP-WEIXIN
  139. uni.getFileSystemManager().readFile({
  140. filePath: path,
  141. encoding: 'base64',
  142. success: (res) => {
  143. resolve(res.data)
  144. }
  145. })
  146. // #endif
  147. })
  148. }
  149. export {
  150. toBase64
  151. }