commonuni.ts 6.2 KB

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