commonuni.ts 6.2 KB

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