common.js 3.3 KB

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