common.js 8.8 KB

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