common.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. 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. console.log(deptValue, 'deptValue部门', deptId);
  190. newArrar = deptValue.find((item) => {
  191. if (item.id === deptId) {
  192. console.log(vm.formData.streetName, ' this.formData.streetName');
  193. vm.formData.streetId = item.id;
  194. vm.formData.streetName = item.qumc;
  195. } else {
  196. vm.actionsStreet.push({
  197. id: item.id,
  198. name: item.qumc
  199. });
  200. vm.actionsMaplist = vm.splitArray(vm.actionsStreet, 9);
  201. }
  202. })
  203. // show1 ='0' 点击将不再显示弹窗
  204. vm.show1 = '0'
  205. }
  206. }
  207. // 下载附件
  208. export function downSee(appendix) {
  209. const fileExt = appendix.substring(appendix.lastIndexOf('.') + 1);
  210. //加载框动画
  211. uni.showLoading({
  212. title: '正在下载……'
  213. });
  214. // #ifdef H5
  215. if (fileExt === 'png' || fileExt === 'jpg') {
  216. // 预览图片
  217. uni.previewImage({
  218. urls: [appendix],
  219. // longPressActions: {
  220. // itemList: ['发送给朋友', '保存图片', '收藏'],
  221. success: function(data) {
  222. console.log(data, 'dataaaaaaaaaaaa');
  223. uni.hideLoading();
  224. },
  225. fail: function(err) {
  226. console.log(err.errMsg);
  227. }
  228. // }
  229. })
  230. } else {
  231. const link = document.createElement('a');
  232. link.href = appendix;
  233. link.download = appendix;
  234. document.body.appendChild(link);
  235. link.click();
  236. link.remove();
  237. uni.hideLoading();
  238. uni.showToast({
  239. icon: 'success',
  240. mask: true,
  241. title: '文件已下载',
  242. duration: 2000
  243. });
  244. }
  245. // #endif
  246. // #ifdef MP-WEIXIN
  247. uni.downloadFile({
  248. url: appendix, //下载地址接口返回
  249. success: data => {
  250. // console.log('打印data', data);
  251. if (data.statusCode === 200) {
  252. //隐藏加载框
  253. // uni.hideLoading();
  254. //文件保存到本地
  255. uni.getFileSystemManager().saveFile({
  256. tempFilePath: data.tempFilePath, //临时路径
  257. success: function(res) {
  258. // console.log('打印res', res);
  259. uni.showToast({
  260. icon: 'success',
  261. mask: true,
  262. title: '文件已保存:' + res.savedFilePath, //保存路径
  263. title: '加载成功',
  264. duration: 2000
  265. });
  266. //自动打开文档查看
  267. setTimeout(() => {
  268. var filePath = res.savedFilePath;
  269. console.log(res, 'filePathfilePath');
  270. uni.openDocument({
  271. //新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
  272. filePath: filePath,
  273. fileType: fileExt,
  274. showMenu: false,
  275. success: function(res) {
  276. console.log('打开文档成功');
  277. },
  278. fail: function() {
  279. uni.showToast({
  280. title: '暂不支持打开此类型',
  281. duration: 2000,
  282. icon: 'none'
  283. });
  284. }
  285. });
  286. }, 1000);
  287. }
  288. });
  289. }
  290. },
  291. fail: err => {
  292. console.log(err);
  293. uni.showToast({
  294. icon: 'none',
  295. mask: true,
  296. title: '文件下载失败'
  297. });
  298. }
  299. }) // #endif
  300. }
  301. export function uploadFile(tempFilePaths, i, that) {
  302. return new Promise((resolve, reject) => {
  303. const loading = uni.showLoading({
  304. title: '文件上传中...',
  305. mask: true
  306. });
  307. uni.uploadFile({
  308. url: baseUrl + '/admin-api/infra/file/upload', // 后端用于处理图片并返回图片地址及文件的接口
  309. filePath: tempFilePaths[i],
  310. name: 'file',
  311. header: {
  312. Authorization: 'Bearer ' + getAccessToken() // 请求token
  313. },
  314. success: res => {
  315. let data = JSON.parse(res.data);
  316. // that.formData.attId.push(data.data)
  317. that.formData.attId=['']
  318. that.formData.attId[0] = data.data
  319. uni.showToast({
  320. title: '文件上传成功!'
  321. });
  322. that.loading = false
  323. },
  324. fail: err => {
  325. uni.showToast({
  326. icon: 'error',
  327. title: '文件上传失败!'
  328. });
  329. that.loading = false
  330. },
  331. complete: () => {
  332. uni.hideLoading(loading);
  333. }
  334. });
  335. });
  336. }