common.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. console.log(res, 'res');
  88. if (res.confirm) {
  89. delInterface({
  90. id: val
  91. }).then(delres => {
  92. context.$modal.msg('删除成功')
  93. })
  94. } else if (res.cancel) {
  95. context.$modal.msg('取消删除')
  96. }
  97. context.loading = true;
  98. context.isLoaded = false
  99. context.queryParams.pageNo = 1;
  100. context.list = []
  101. context.getData()
  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. vm.loading = true
  123. if (Array.isArray(res.tempFilePaths)) {
  124. res.tempFilePaths.forEach(item => {
  125. loadImage(
  126. item,
  127. function(canvas) {
  128. canvas.toBlob(function(blob) {
  129. // 压缩后的 Blob 对象
  130. const compressedFile = blob;
  131. // 将压缩后的文件 `compressedFile` 转换为临时URL
  132. const fileUrl = URL.createObjectURL(compressedFile);
  133. // 接下来将临时URL作为文件路径上传到服务器
  134. uni.uploadFile({
  135. url: baseUrl + '/admin-api/infra/file/upload',
  136. filePath: fileUrl, // 使用临时URL作为文件路径
  137. name: 'file',
  138. header: {
  139. "Authorization": 'Bearer ' +
  140. getAccessToken(),
  141. },
  142. success: (res) => {
  143. vm.loading = false
  144. let imgUrl = JSON.parse(res.data);
  145. vm.formData.streetRepPicsDOList.push({
  146. url: imgUrl.data,
  147. feildname: '',
  148. repid:''
  149. });
  150. vm.$forceUpdate();
  151. },
  152. fail: (err) => {
  153. vm.loading = false
  154. this.$modal.msg(err)
  155. },
  156. complete: () => {
  157. vm.loading =
  158. false; // 无论成功还是失败,都将loading状态设置为false
  159. vm.$forceUpdate();
  160. }
  161. });
  162. // 上传完毕后,释放临时URL
  163. URL.revokeObjectURL(fileUrl);
  164. }, 'image/jpeg', 0.6); // 设置压缩后的图片格式为 JPEG,压缩质量为 0.6
  165. }, {
  166. canvas: true,
  167. maxWidth: 800
  168. } // 设置最大宽度为 800px
  169. );
  170. });
  171. }
  172. },
  173. })
  174. }
  175. export function choiceStreet(vm) {
  176. const deptValue = uni.getStorageSync('getDepartment_key'); //所有部门
  177. const deptId = uni.getStorageSync('getUserInfo_key').streetInfo.id; //登录用户
  178. var newArrar = {}; //新对象
  179. if (deptId) {
  180. console.log(deptValue, 'deptValue部门', deptId);
  181. newArrar = deptValue.find((item) => {
  182. if (item.id === deptId) {
  183. console.log(vm.formData.streetName, ' this.formData.streetName');
  184. vm.formData.streetId = item.id;
  185. vm.formData.streetName = item.qumc;
  186. } else {
  187. vm.actionsStreet.push({
  188. id: item.id,
  189. name: item.qumc
  190. });
  191. vm.actionsMaplist = vm.splitArray(vm.actionsStreet, 9);
  192. }
  193. })
  194. // show1 ='0' 点击将不再显示弹窗
  195. vm.show1 = '0'
  196. }
  197. }
  198. // 下载附件
  199. export function downSee(appendix) {
  200. const fileExt = appendix.substring(appendix.lastIndexOf('.') + 1);
  201. //加载框动画
  202. uni.showLoading({
  203. title: '正在下载……'
  204. });
  205. // #ifdef H5
  206. if (fileExt === 'png' || fileExt === 'jpg') {
  207. // 预览图片
  208. uni.previewImage({
  209. urls: [appendix],
  210. // longPressActions: {
  211. // itemList: ['发送给朋友', '保存图片', '收藏'],
  212. success: function(data) {
  213. console.log(data, 'dataaaaaaaaaaaa');
  214. uni.hideLoading();
  215. },
  216. fail: function(err) {
  217. console.log(err.errMsg);
  218. }
  219. // }
  220. })
  221. } else {
  222. const link = document.createElement('a');
  223. link.href = appendix;
  224. link.download = appendix;
  225. document.body.appendChild(link);
  226. link.click();
  227. link.remove();
  228. uni.hideLoading();
  229. uni.showToast({
  230. icon: 'success',
  231. mask: true,
  232. title: '文件已下载',
  233. duration: 2000
  234. });
  235. }
  236. // #endif
  237. // #ifdef MP-WEIXIN
  238. uni.downloadFile({
  239. url: appendix, //下载地址接口返回
  240. success: data => {
  241. // console.log('打印data', data);
  242. if (data.statusCode === 200) {
  243. //隐藏加载框
  244. // uni.hideLoading();
  245. //文件保存到本地
  246. uni.getFileSystemManager().saveFile({
  247. tempFilePath: data.tempFilePath, //临时路径
  248. success: function(res) {
  249. // console.log('打印res', res);
  250. uni.showToast({
  251. icon: 'success',
  252. mask: true,
  253. title: '文件已保存:' + res.savedFilePath, //保存路径
  254. title: '加载成功',
  255. duration: 2000
  256. });
  257. //自动打开文档查看
  258. setTimeout(() => {
  259. var filePath = res.savedFilePath;
  260. console.log(res, 'filePathfilePath');
  261. uni.openDocument({
  262. //新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
  263. filePath: filePath,
  264. fileType: fileExt,
  265. showMenu: false,
  266. success: function(res) {
  267. console.log('打开文档成功');
  268. },
  269. fail: function() {
  270. uni.showToast({
  271. title: '暂不支持打开此类型',
  272. duration: 2000,
  273. icon: 'none'
  274. });
  275. }
  276. });
  277. }, 1000);
  278. }
  279. });
  280. }
  281. },
  282. fail: err => {
  283. console.log(err);
  284. uni.showToast({
  285. icon: 'none',
  286. mask: true,
  287. title: '文件下载失败'
  288. });
  289. }
  290. }) // #endif
  291. }
  292. export function uploadFile(tempFilePaths, i, that) {
  293. return new Promise((resolve, reject) => {
  294. const loading = uni.showLoading({
  295. title: '文件上传中...',
  296. mask: true
  297. });
  298. uni.uploadFile({
  299. url: baseUrl + '/admin-api/infra/file/upload', // 后端用于处理图片并返回图片地址及文件的接口
  300. filePath: tempFilePaths[i],
  301. name: 'file',
  302. header: {
  303. Authorization: 'Bearer ' + getAccessToken() // 请求token
  304. },
  305. success: res => {
  306. let data = JSON.parse(res.data);
  307. // that.formData.attId.push(data.data)
  308. that.formData.attId[0] = data.data
  309. uni.showToast({
  310. title: '文件上传成功!'
  311. });
  312. that.loading = false
  313. },
  314. fail: err => {
  315. uni.showToast({
  316. icon: 'error',
  317. title: '文件上传失败!'
  318. });
  319. that.loading = false
  320. },
  321. complete: () => {
  322. uni.hideLoading(loading);
  323. }
  324. });
  325. });
  326. }