common.js 8.3 KB

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