tool.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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: "/pages/login/login?isBack=" + 1,
  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. store.state.userInfo = null;
  63. uni.reLaunch({
  64. url: "/pages/index/index",
  65. });
  66. },
  67. //压缩图片
  68. imageInfos(url) {
  69. var self = this;
  70. return new Promise((resolve, reject) => {
  71. uni.getImageInfo({
  72. src: url,
  73. success: async (res) => {
  74. let canvasWidth = res.width; //图片原始长宽
  75. let canvasHeight = res.height;
  76. if (canvasWidth > 2000 || canvasHeight > 2000) {
  77. uni.compressImage({
  78. src: url,
  79. quality: 75,
  80. width: "35%",
  81. height: "35%",
  82. success: async (rest) => {
  83. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  84. console.log(waitUpload, "waitUpload");
  85. resolve(waitUpload);
  86. },
  87. });
  88. } else if (canvasWidth > 1000 || canvasHeight > 1000) {
  89. uni.compressImage({
  90. src: url,
  91. quality: 75,
  92. width: "50%",
  93. height: "50%",
  94. success: async (rest) => {
  95. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  96. console.log(waitUpload, "waitUpload");
  97. resolve(waitUpload);
  98. },
  99. });
  100. } else {
  101. const waitUpload = await self.uploadFile(url, 0);
  102. console.log(waitUpload, "waitUpload");
  103. resolve(waitUpload);
  104. // console.log('无需压缩', url);
  105. // resolve(url);
  106. }
  107. },
  108. });
  109. });
  110. },
  111. //上传图片
  112. uploadFile(options, int) {
  113. return new Promise((resolve, reject) => {
  114. var self = this;
  115. // if (options.indexOf("//tmp") === -1 && options.indexOf("//temp") === -1) {
  116. // resolve(options);
  117. // return;
  118. // }
  119. var data = {
  120. imageStatus: int,
  121. };
  122. aliyunpolicy(data).then((res) => {
  123. var ossToken = res.data.data.resultContent;
  124. uni.uploadFile({
  125. url: ossToken.host,
  126. name: "file",
  127. filePath: options,
  128. fileType: "image",
  129. header: {
  130. AuthorizationToken: "WX " + uni.getStorageSync("token"),
  131. },
  132. formData: {
  133. key: ossToken.dir,
  134. OSSAccessKeyId: ossToken.accessid,
  135. policy: ossToken.policy,
  136. Signature: ossToken.signature,
  137. // callback: ossToken.callback,
  138. success_action_status: 200,
  139. },
  140. success: (result) => {
  141. // if (result.statusCode === 200) {
  142. resolve(ossToken.dir);
  143. // } else {
  144. // uni.showToast({
  145. // title: '上传失败',
  146. // icon: 'none'
  147. // });
  148. // return;
  149. // }
  150. },
  151. fail: (error) => {
  152. uni.showToast({
  153. title: "上传接口报错",
  154. icon: "none",
  155. });
  156. return;
  157. },
  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. }
  169. export function getQueryString(name) {
  170. const url = location.search; //获取url中"?"符后的字串
  171. let theRequest = new Object();
  172. if (url.indexOf("?") != -1) {
  173. let str = url.substr(1);
  174. let strs = str.split("&");
  175. for (let i = 0; i < strs.length; i++) {
  176. theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  177. }
  178. }
  179. if (name) {
  180. return theRequest[name];
  181. }
  182. return theRequest;
  183. }
  184. export function isWechat() {
  185. var ua = window.navigator.userAgent.toLowerCase();
  186. return ua.match(/micromessenger/i) == "micromessenger"
  187. }