commonuni.ts 6.1 KB

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