tool.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import store from "@/store/index.js";
  2. import { aliyunpolicy } from "@/utils/login";
  3. import config from "@/common/config";
  4. export default {
  5. isGoLogin(isBack = true) {
  6. if (!uni.getStorageSync("user_account")) {
  7. uni.navigateTo({
  8. url: "/pages4/login/login?isBack=" + isBack,
  9. });
  10. return true;
  11. } else {
  12. return false;
  13. }
  14. },
  15. isLogin() {
  16. if (uni.getStorageSync("user_account")) {
  17. return true;
  18. } else {
  19. return false;
  20. }
  21. },
  22. isLogout() {
  23. // uni.removeStorageSync('user_account')
  24. // uni.removeStorageSync('token')
  25. },
  26. //提示
  27. showToast(title, icon = "none", time = 2000) {
  28. return setTimeout(() => {
  29. uni.showToast({
  30. title: title,
  31. icon: icon,
  32. duration: time,
  33. });
  34. }, 500);
  35. },
  36. //图片路径填补
  37. splitImgHost(url, scale = false, width = 250) {
  38. if (!url) {
  39. return "";
  40. } else if (
  41. url.indexOf("http") != -1 ||
  42. url.indexOf("https") != -1 ||
  43. url.indexOf("wxfile") != -1
  44. ) {
  45. } else {
  46. url = config.BASE_IMG_URL + url;
  47. }
  48. if (scale) {
  49. url = url + "?x-oss-process=image/resize,w_" + width;
  50. }
  51. return url;
  52. },
  53. setUuid(id) {
  54. uni.setStorageSync("uuid", id);
  55. },
  56. getUuid() {
  57. return uni.getStorageSync("uuid");
  58. },
  59. exit() {
  60. uni.removeStorageSync("user_account");
  61. uni.removeStorageSync("token");
  62. uni.removeStorageSync("h5_code");
  63. store.state.userInfo = null;
  64. uni.reLaunch({
  65. url: "/pages/index/index",
  66. });
  67. },
  68. //压缩图片
  69. imageInfos(url) {
  70. var self = this;
  71. return new Promise((resolve, reject) => {
  72. uni.getImageInfo({
  73. src: url,
  74. success: async (res) => {
  75. let canvasWidth = res.width; //图片原始长宽
  76. let canvasHeight = res.height;
  77. if (canvasWidth > 2000 || canvasHeight > 2000) {
  78. uni.compressImage({
  79. src: url,
  80. quality: 75,
  81. width: "35%",
  82. height: "35%",
  83. success: async (rest) => {
  84. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  85. console.log(waitUpload, "waitUpload");
  86. resolve(waitUpload);
  87. },
  88. });
  89. } else if (canvasWidth > 1000 || canvasHeight > 1000) {
  90. uni.compressImage({
  91. src: url,
  92. quality: 75,
  93. width: "50%",
  94. height: "50%",
  95. success: async (rest) => {
  96. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  97. console.log(waitUpload, "waitUpload");
  98. resolve(waitUpload);
  99. },
  100. });
  101. } else {
  102. const waitUpload = await self.uploadFile(url, 0);
  103. console.log(waitUpload, "waitUpload");
  104. resolve(waitUpload);
  105. // console.log('无需压缩', url);
  106. // resolve(url);
  107. }
  108. },
  109. });
  110. });
  111. },
  112. //上传图片
  113. uploadFile(options, int) {
  114. return new Promise((resolve, reject) => {
  115. var self = this;
  116. // if (options.indexOf("//tmp") === -1 && options.indexOf("//temp") === -1) {
  117. // resolve(options);
  118. // return;
  119. // }
  120. var data = {
  121. imageStatus: int,
  122. };
  123. aliyunpolicy(data).then((res) => {
  124. var ossToken = res.data.data.resultContent;
  125. uni.uploadFile({
  126. url: ossToken.host,
  127. name: "file",
  128. filePath: options,
  129. fileType: "image",
  130. header: {
  131. AuthorizationToken: "WX " + uni.getStorageSync("token"),
  132. },
  133. formData: {
  134. key: ossToken.dir,
  135. OSSAccessKeyId: ossToken.accessid,
  136. policy: ossToken.policy,
  137. Signature: ossToken.signature,
  138. // callback: ossToken.callback,
  139. success_action_status: 200,
  140. },
  141. success: (result) => {
  142. // if (result.statusCode === 200) {
  143. resolve(ossToken.dir);
  144. // } else {
  145. // uni.showToast({
  146. // title: '上传失败',
  147. // icon: 'none'
  148. // });
  149. // return;
  150. // }
  151. },
  152. fail: (error) => {
  153. uni.showToast({
  154. title: "上传接口报错",
  155. icon: "none",
  156. });
  157. return;
  158. },
  159. });
  160. });
  161. });
  162. },
  163. };
  164. export function checkDomain(str) {
  165. let domain =
  166. /^([\w-]+\.)+((com)|(net)|(org)|(gov\.cn)|(info)|(cc)|(com\.cn)|(net\.cn)|(org\.cn)|(name)|(biz)|(tv)|(cn)|(mobi)|(name)|(sh)|(ac)| (io)|(tw)|(com\.tw)|(hk)|(com\.hk)|(ws)|(travel)|(us)|(tm)|(la)|(me\.uk)|(org\.uk)|(ltd\.uk)|(plc\.uk)|(in)|(eu)|(it)|(jp))$/;
  167. return domain.test(str);
  168. }