common.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * 显示消息提示框
  3. * @param content 提示的标题
  4. */
  5. import config from '@/config'
  6. const baseUrl = config.baseUrl
  7. import {
  8. getAccessToken
  9. } from '@/utils/auth'
  10. import loadImage from 'blueimp-load-image';
  11. export function toast(content) {
  12. uni.showToast({
  13. icon: 'none',
  14. title: content
  15. })
  16. }
  17. /**
  18. * 显示模态弹窗
  19. * @param content 提示的标题
  20. */
  21. export function showConfirm(content) {
  22. return new Promise((resolve, reject) => {
  23. uni.showModal({
  24. title: '提示',
  25. content: content,
  26. cancelText: '取消',
  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 checkType(dictTypeData, dictTypeValue) {
  61. let name = []
  62. const data = uni.getStorageSync('getlistSimpleDict_key')
  63. data.map(v => {
  64. if (dictTypeData == v.dictType && dictTypeValue == v.value) {
  65. name = v.label
  66. }
  67. })
  68. return name
  69. }
  70. //时间戳转换为年月日
  71. export function shijianc(time) {
  72. let date = new Date(time)
  73. let Y = date.getFullYear() + '-'
  74. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  75. let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
  76. let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  77. let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  78. let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  79. return Y + M + D + h + m + s
  80. }
  81. //删除
  82. export function delSure(delInterface, val, context) {
  83. uni.showModal({
  84. title: '确认删除',
  85. content: `是否确认删除?`,
  86. success: function(res) {
  87. if (res.confirm) {
  88. delInterface({
  89. id: val
  90. }).then(delres => {
  91. context.$modal.msg('删除成功')
  92. })
  93. } else if (res.cancel) {
  94. context.$modal.msg('取消删除')
  95. }
  96. context.loading = true;
  97. context.isLoaded = false
  98. context.queryParams.pageNo = 1;
  99. context.list = []
  100. context.getData()
  101. context.$forceUpdate()
  102. }
  103. });
  104. }
  105. //9个表的输入框规则
  106. export function NumericRule() {
  107. return [{
  108. pattern: /^[0-9]+$/,
  109. message: '请输入数字',
  110. trigger: ['change', 'blur'],
  111. }];
  112. }
  113. //拍照npm install blueimp-load-image
  114. export function takePhotos(vm) {
  115. let pictures = [];
  116. uni.chooseImage({
  117. sourceType: ['camera'], //实现拍照
  118. count: 3,
  119. sizeType: ['original', 'compressed'],
  120. // sourceType: ['album','camera'], //打开系统相册
  121. success(res) {
  122. if (res.tempFilePaths.length === 0) {
  123. vm.loading = false;
  124. return;
  125. }else{
  126. vm.loading=true
  127. }
  128. if (Array.isArray(res.tempFilePaths)) {
  129. res.tempFilePaths.forEach(item => {
  130. loadImage(
  131. item,
  132. function(canvas) {
  133. canvas.toBlob(function(blob) {
  134. // 压缩后的 Blob 对象
  135. const compressedFile = blob;
  136. // 将压缩后的文件 `compressedFile` 转换为临时URL
  137. const fileUrl = URL.createObjectURL(compressedFile);
  138. // 接下来将临时URL作为文件路径上传到服务器
  139. uni.uploadFile({
  140. url: baseUrl + '/admin-api/infra/file/upload',
  141. filePath: fileUrl, // 使用临时URL作为文件路径
  142. name: 'file',
  143. header: {
  144. "Authorization": 'Bearer ' +
  145. getAccessToken(),
  146. },
  147. success: (res) => {
  148. vm.loading = false
  149. let imgUrl = JSON.parse(res.data);
  150. vm.formData.streetRepPicsDOList.push({
  151. url: imgUrl.data,
  152. feildname: '',
  153. repid: ''
  154. });
  155. vm.$forceUpdate();
  156. },
  157. fail: (err) => {
  158. vm.loading = false
  159. this.$modal.msg(err)
  160. },
  161. complete: () => {
  162. vm.loading =
  163. false; // 无论成功还是失败,都将loading状态设置为false
  164. vm.$forceUpdate();
  165. }
  166. });
  167. // 上传完毕后,释放临时URL
  168. URL.revokeObjectURL(fileUrl);
  169. }, 'image/jpeg', 0.6); // 设置压缩后的图片格式为 JPEG,压缩质量为 0.6
  170. }, {
  171. canvas: true,
  172. maxWidth: 800
  173. } // 设置最大宽度为 800px
  174. );
  175. });
  176. }
  177. },
  178. fail: (err) => {
  179. vm.loading = false;
  180. },
  181. })
  182. }
  183. export function choiceStreet(vm) {
  184. const deptValue = uni.getStorageSync('getDepartment_key'); //所有部门
  185. const deptId = uni.getStorageSync('getUserInfo_key').streetInfo ? uni.getStorageSync('getUserInfo_key').streetInfo
  186. .id : ''; //登录用户
  187. var newArrar = {}; //新对象
  188. if (deptId) {
  189. newArrar = deptValue.find((item) => {
  190. if (item.id === deptId) {
  191. vm.formData.streetId = item.id;
  192. vm.formData.streetName = item.qumc;
  193. } else {
  194. vm.actionsStreet.push({
  195. id: item.id,
  196. name: item.qumc
  197. });
  198. vm.actionsMaplist = vm.splitArray(vm.actionsStreet, 9);
  199. }
  200. })
  201. // show1 ='0' 点击将不再显示弹窗
  202. vm.show1 = '0'
  203. }
  204. }
  205. // 下载附件
  206. export function downSee(appendix) {
  207. const fileExt = appendix.substring(appendix.lastIndexOf('.') + 1);
  208. //加载框动画
  209. uni.showLoading({
  210. title: '正在下载……'
  211. });
  212. // #ifdef H5
  213. if (fileExt === 'png' || fileExt === 'jpg') {
  214. // 预览图片
  215. uni.previewImage({
  216. urls: [appendix],
  217. // longPressActions: {
  218. // itemList: ['发送给朋友', '保存图片', '收藏'],
  219. success: function(data) {
  220. uni.hideLoading();
  221. },
  222. fail: function(err) {
  223. }
  224. // }
  225. })
  226. } else {
  227. const link = document.createElement('a');
  228. link.href = appendix;
  229. link.download = appendix;
  230. document.body.appendChild(link);
  231. link.click();
  232. link.remove();
  233. uni.hideLoading();
  234. uni.showToast({
  235. icon: 'success',
  236. mask: true,
  237. title: '文件已下载',
  238. duration: 2000
  239. });
  240. }
  241. // #endif
  242. // #ifdef MP-WEIXIN
  243. uni.downloadFile({
  244. url: appendix, //下载地址接口返回
  245. success: data => {
  246. // console.log('打印data', data);
  247. if (data.statusCode === 200) {
  248. //隐藏加载框
  249. // uni.hideLoading();
  250. //文件保存到本地
  251. uni.getFileSystemManager().saveFile({
  252. tempFilePath: data.tempFilePath, //临时路径
  253. success: function(res) {
  254. // console.log('打印res', res);
  255. uni.showToast({
  256. icon: 'success',
  257. mask: true,
  258. title: '文件已保存:' + res.savedFilePath, //保存路径
  259. title: '加载成功',
  260. duration: 2000
  261. });
  262. //自动打开文档查看
  263. setTimeout(() => {
  264. var filePath = res.savedFilePath;
  265. uni.openDocument({
  266. //新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
  267. filePath: filePath,
  268. fileType: fileExt,
  269. showMenu: false,
  270. success: function(res) {
  271. },
  272. fail: function() {
  273. uni.showToast({
  274. title: '暂不支持打开此类型',
  275. duration: 2000,
  276. icon: 'none'
  277. });
  278. }
  279. });
  280. }, 1000);
  281. }
  282. });
  283. }
  284. },
  285. fail: err => {
  286. uni.showToast({
  287. icon: 'none',
  288. mask: true,
  289. title: '文件下载失败'
  290. });
  291. }
  292. }) // #endif
  293. }
  294. export function uploadFile(tempFilePaths, i, that) {
  295. return new Promise((resolve, reject) => {
  296. const loading = uni.showLoading({
  297. title: '文件上传中...',
  298. mask: true
  299. });
  300. uni.uploadFile({
  301. url: baseUrl + '/admin-api/infra/file/upload', // 后端用于处理图片并返回图片地址及文件的接口
  302. filePath: tempFilePaths[i],
  303. name: 'file',
  304. header: {
  305. Authorization: 'Bearer ' + getAccessToken() // 请求token
  306. },
  307. success: res => {
  308. let data = JSON.parse(res.data);
  309. // that.formData.attId.push(data.data)
  310. that.formData.attId=['']
  311. that.formData.attId[0] = data.data
  312. uni.showToast({
  313. title: '文件上传成功!'
  314. });
  315. that.loading = false
  316. },
  317. fail: err => {
  318. uni.showToast({
  319. icon: 'error',
  320. title: '文件上传失败!'
  321. });
  322. that.loading = false
  323. },
  324. complete: () => {
  325. uni.hideLoading(loading);
  326. }
  327. });
  328. });
  329. }
  330. //重要提醒上传
  331. export function uploadFileComp(tempFilePaths, i, that,lengths) {
  332. return new Promise((resolve, reject) => {
  333. const loading = uni.showLoading({
  334. title: '文件上传中...',
  335. mask: true
  336. });
  337. uni.uploadFile({
  338. url: baseUrl + '/admin-api/infra/file/upload', // 后端用于处理图片并返回图片地址及文件的接口
  339. filePath: tempFilePaths[i],
  340. name: 'file',
  341. header: {
  342. Authorization: 'Bearer ' + getAccessToken() // 请求token
  343. },
  344. success: res => {
  345. let data = JSON.parse(res.data);
  346. that.formData.attId.push({id:tempFilePaths[i],path:data.data})
  347. uni.showToast({
  348. title: '文件上传成功!'
  349. });
  350. that.loading = false
  351. },
  352. fail: err => {
  353. uni.showToast({
  354. icon: 'error',
  355. title: '文件上传失败!'
  356. });
  357. that.loading = false
  358. },
  359. complete: () => {
  360. uni.hideLoading(loading);
  361. }
  362. });
  363. });
  364. }
  365. // 图片放大
  366. export function viewImgBig(i, imgList) {
  367. uni.previewImage({
  368. urls: imgList,
  369. current: i
  370. });
  371. }