commonuni.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import CryptoJS from 'crypto-js'
  2. import http from '@/utils/request';
  3. const UrlPath = {
  4. getBaseUrl() {
  5. return "http://localhost:48080/admin-api"
  6. }
  7. }
  8. //加密处理
  9. const Encrypt = {
  10. AESINFO: null,
  11. GetAESINFO() {//获取编码信息
  12. if (this.AESINFO == null) {
  13. this.AESINFO = JSON.parse(this.Base64Decode("eyJrZXkiOiJjaWljY2lpY2NpaWNjaWljIiwiaXYiOiJjaWljMjAyMmNpaWMyMDIyIn0="));
  14. }
  15. return this.AESINFO;
  16. },//如果空返回
  17. IsNull(WhenInfo: any, ThenInfo: any) {
  18. if (WhenInfo === null || WhenInfo === undefined) {
  19. return (ThenInfo === null || ThenInfo === undefined) ? "" : ThenInfo;
  20. }
  21. return WhenInfo;
  22. },//如果空返回
  23. IsNullOrEmpty(Info: any) {
  24. return this.IsNull(Info, "") === "";
  25. }, //Base64编码
  26. Base64Encode(str: string) {
  27. return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(str));
  28. },//Base64解码
  29. Base64Decode(str: string) {
  30. return CryptoJS.enc.Base64.parse(str).toString(CryptoJS.enc.Utf8);
  31. },//AES编码
  32. AESEncode(data: string, key: string, iv: string) {
  33. if (typeof data === "object") {
  34. // 如果传入的data是json对象,先转义为json字符串
  35. try {
  36. data = JSON.stringify(data)
  37. } catch (error) {
  38. console.log("error:", error)
  39. }
  40. }
  41. if (this.IsNullOrEmpty(key)) {
  42. key = this.GetAESINFO().key;
  43. }
  44. if (this.IsNullOrEmpty(iv)) {
  45. iv = this.GetAESINFO().iv;
  46. }
  47. const dataHex = CryptoJS.enc.Utf8.parse(data) // 需要加密的数据
  48. const keyHex = CryptoJS.enc.Utf8.parse(key) // 秘钥
  49. const ivHex = CryptoJS.enc.Utf8.parse(iv) // 偏移量
  50. const encrypted = CryptoJS.AES.encrypt(dataHex, keyHex, {
  51. iv: ivHex,
  52. mode: CryptoJS.mode.CBC, // 加密模式
  53. padding: CryptoJS.pad.Pkcs7
  54. })
  55. return encrypted.ciphertext.toString() // 返回加密后的值
  56. },
  57. AESDecode(data: string, key: string, iv: string) {//AES解码
  58. if (this.IsNullOrEmpty(key)) {
  59. key = this.GetAESINFO().key;
  60. }
  61. if (this.IsNullOrEmpty(iv)) {
  62. iv = this.GetAESINFO().iv;
  63. }
  64. let encryptedHexStr = CryptoJS.enc.Hex.parse(data);
  65. let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  66. const keyHex = CryptoJS.enc.Utf8.parse(key) // 秘钥
  67. const ivHex = CryptoJS.enc.Utf8.parse(iv) // 偏移量
  68. let decrypt = CryptoJS.AES.decrypt(srcs, keyHex, {
  69. iv: ivHex,
  70. mode: CryptoJS.mode.CBC,
  71. padding: CryptoJS.pad.Pkcs7
  72. });
  73. return decrypt.toString(CryptoJS.enc.Utf8).toString();
  74. }
  75. }
  76. //消息功能
  77. const Message = {
  78. success(content: string) {
  79. uni.showToast({
  80. title: content,
  81. icon: 'success',
  82. duration: 2000
  83. })
  84. },
  85. error(content: string) {
  86. uni.showToast({
  87. title: content,
  88. icon: 'error',
  89. duration: 2000
  90. })
  91. },
  92. info(content: string) {
  93. uni.showToast({
  94. title: content,
  95. icon: 'none',
  96. duration: 2000
  97. })
  98. }
  99. }
  100. //文件上传
  101. const FileInfo = {
  102. Upload(CallBackEvent: Function, FileList: Array<any>, ExtendParameter: any) {
  103. this.defaults = {
  104. Async: true,
  105. ShowWaiting: true,
  106. Later: false,
  107. Encrypt: false,
  108. Timeout: 120000,
  109. Tip: null,
  110. Folder: "Temp",
  111. MedID: "",
  112. IsFileServer: true,
  113. Url: UrlPath.getBaseUrl() + "ExecuteSystemUpLoadFile"
  114. };
  115. const formData = { files: [], formData: null }
  116. let allowfiles: Array<string> = []
  117. let temp_extend: Array<string> = []
  118. if (FileList == null || FileList.length == 0) {
  119. Message.error("没有需要上传的文件!");
  120. return;
  121. }
  122. for (let i = 0; i < FileList.length; i++) {
  123. temp_extend = FileList[i].name.split('.')
  124. allowfiles.push(temp_extend[temp_extend.length - 1])
  125. formData.files.push({
  126. name: "file:" + FileList[i].url,
  127. uri: FileList[i].url
  128. });
  129. }
  130. var Parameters = Object.assign(this.defaults, ExtendParameter);
  131. if (Parameters.IsFileServer === true) {
  132. var cur = null;//state.getters.userinfo;
  133. if (cur == null) {
  134. let uinfo = Utility.GetStorage("userinfo");
  135. if (uinfo == null || uinfo == "") {
  136. Message.error("未获取到登录信息!");
  137. return;
  138. }
  139. cur = JSON.parse(uinfo);
  140. }
  141. if (!Encrypt.IsNullOrEmpty(cur.LoadBalancingFileUrl)) {
  142. Parameters.Url = cur.LoadBalancingFileUrl.replace("[LOCALPATH]", location.protocol + "//" + location.host) + 'Api/File/UploadFilesAsync';
  143. }
  144. Parameters.MedID = cur.MedicalInstitutionID;
  145. Parameters.HttpPath = cur.ResourcePath;
  146. }
  147. formData.formData = { SaveFileInfo: "{\"data\":\"" + Encrypt.AESEncode(JSON.stringify({ IsSystem: false, Folder: Parameters.Folder, AllowFiles: allowfiles.join(","), MedID: Parameters.MedID, Token: Utility.GetStorage("Token"), HttpPath: Parameters.HttpPath }), "", "") + "\"}" }
  148. http.upload(Parameters.Url, formData).then(res => {
  149. CallBackEvent(res);
  150. });
  151. }
  152. }
  153. //通用方法
  154. const Utility = {
  155. //校验密码强度
  156. ValidPassWord(value: string) {
  157. var level = 0;
  158. if (value.length < 6) {
  159. return level;
  160. }
  161. if (/\d/.test(value)) {
  162. level++;
  163. }
  164. if (/[a-z]/.test(value)) {
  165. level++;
  166. }
  167. if (/[A-Z]/.test(value)) {
  168. level++;
  169. }
  170. if (/[\W_!@#$%^&*`~()-+=]/.test(value)) {
  171. level++;
  172. }
  173. return level;
  174. },
  175. //校验手机号
  176. ValidMobild(value: string) {
  177. return /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/.test(value);
  178. },
  179. SetStorage(key: string, value: string) {
  180. uni.setStorageSync(key, value)
  181. },
  182. GetStorage(key: string) {
  183. return uni.getStorageSync(key)
  184. },
  185. SetStorageJson(key: string, value: object) {
  186. uni.setStorageSync(key, JSON.stringify(value))
  187. },
  188. GetStorageJson(key: string) {
  189. let value = uni.getStorageSync(key)
  190. if (value != null && value != "") {
  191. return JSON.parse(value)
  192. }
  193. return null
  194. },
  195. GetPlatform() {//ios android devtools
  196. return uni.getSystemInfoSync().platform
  197. },
  198. GetRuntime() {
  199. const sysinfo = uni.getSystemInfoSync()
  200. switch (sysinfo.uniPlatform) {
  201. case "app":
  202. return "app";
  203. case "web":
  204. case "h5":
  205. return "web"
  206. default:
  207. return "mp"
  208. }
  209. },
  210. IsWeChat() {//是否微信小程序
  211. return this.GetRuntime() === "mp";
  212. },
  213. TrimEnd(text: string, content: string) {
  214. if (text == null || text == "") {
  215. return "";
  216. }
  217. var str = text.substring(text.length - content.length, text.length);
  218. if (content === str) {
  219. return text.substring(0, text.length - content.length);
  220. }
  221. return text;
  222. }
  223. }
  224. export { Encrypt, Message, FileInfo, Utility, UrlPath }