api.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. let s_url = '' // 请求地址
  2. let s_data = '' // 请求数据
  3. let s_time = new Date() // 请求时间
  4. const interval = 1000
  5. const dev = 'dev'
  6. const prod = 'prod'
  7. const devCloud = 'dev_cloud'
  8. // const environment = dev
  9. // const environment = devCloud
  10. const environment = prod
  11. // 全局请求路径,也就是后端的请求基准路径
  12. export let BASE_URL
  13. export let BASE_SOCKET_URL = 'localhost'
  14. export let BASE_URL_ML
  15. export let BASE_URL_HSKJ
  16. export let BASE_URL_MASTER
  17. export let BASE_URL_BUSINESS
  18. switch (environment) {
  19. case 'dev':
  20. BASE_URL = 'https://localhost:3005'
  21. BASE_SOCKET_URL = 'localhost'
  22. BASE_URL_ML = 'https://localhost:3005'
  23. BASE_URL_HSKJ = 'https://localhost:3012'
  24. BASE_URL_MASTER = 'https://localhost:3010'
  25. BASE_URL_BUSINESS = 'https://localhost:3011'
  26. break;
  27. case 'dev_cloud':
  28. BASE_URL = 'https://ts.willalp.com:3005'
  29. BASE_SOCKET_URL = 'ts.willalp.com'
  30. BASE_URL_ML = 'https://ts.willalp.com:3005'
  31. BASE_URL_HSKJ = 'https://ts.willalp.com:3012'
  32. BASE_URL_MASTER = 'https://ts.willalp.com:3010'
  33. BASE_URL_BUSINESS = 'https://ts.willalp.com:3011'
  34. break;
  35. case 'prod':
  36. BASE_URL = "https://hzl.willalp.com:3005"
  37. BASE_SOCKET_URL = 'hzl.willalp.com'
  38. BASE_URL_ML = 'https://hzl.willalp.com:3005'
  39. BASE_URL_HSKJ = 'https://hzl.willalp.com:3012'
  40. BASE_URL_MASTER = 'https://hzl.willalp.com:3010'
  41. BASE_URL_BUSINESS = 'https://hzl.willalp.com:3011'
  42. break;
  43. }
  44. // 根据机构类型获取 baseUrl
  45. export const getServerURl = (urlType) => {
  46. if (urlType === 1) {
  47. return BASE_URL_MASTER
  48. } else if (urlType === 2) {
  49. return BASE_URL_BUSINESS
  50. } else if (urlType === 3) {
  51. return BASE_URL_ML
  52. } else if (urlType === 4) {
  53. return BASE_URL_HSKJ
  54. } else {
  55. return BASE_URL_ML
  56. }
  57. return BASE_URL;
  58. }
  59. export const getUrlType = () => {
  60. return uni.getStorageSync('tabType')
  61. }
  62. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  63. let ajaxTimes = 0;
  64. // 封装请求方法,并向外暴露该方法
  65. export const httpRequest = (options) => {
  66. // 解构请求头参数
  67. let header = {
  68. ...options.header
  69. }
  70. if (uni.getStorageSync('isTabLogin') === 0) {
  71. return
  72. }
  73. header["UserName"] = uni.getStorageSync('setUserName');
  74. // 当前请求不是登录和获取所有机构时的请求,在header中加上后端返回的token
  75. if (!options.isNotToken) {
  76. header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  77. // 显示加载中 效果
  78. // uni.showLoading({
  79. // title: "加载中",
  80. // mask: true
  81. // });
  82. }
  83. if (options.urlType === 1) {
  84. BASE_URL = BASE_URL_MASTER
  85. } else if (options.urlType === 2) {
  86. BASE_URL = BASE_URL_BUSINESS
  87. } else if (options.urlType === 3) {
  88. BASE_URL = BASE_URL_ML
  89. } else if (options.urlType === 4) {
  90. BASE_URL = BASE_URL_HSKJ
  91. } else {
  92. BASE_URL = BASE_URL_ML
  93. }
  94. header['Content-Type'] = 'application/json';
  95. header['Allow-Control-Allow-Origin'] = '*';
  96. ajaxTimes++;
  97. // 间隔时间(ms),小于此时间视为重复提交
  98. if (s_data === options.data && options.time - s_time < interval && s_url === options.url) {
  99. uni.showToast({
  100. duration: 2500,
  101. title: '数据正在处理,请勿重复提交',
  102. icon: 'error',
  103. });
  104. return
  105. } else {
  106. s_url = options.url; // 请求地址
  107. s_data = options.data; // 请求数据
  108. s_time = new Date(); // 请求时间
  109. }
  110. return new Promise((resolve, reject) => {
  111. uni.request({
  112. url: BASE_URL + options.url,
  113. method: options.method || 'POST',
  114. data: options.data || {},
  115. header,
  116. success: (res) => {
  117. const data = res.data;
  118. let pages = getCurrentPages()
  119. // console.log(pages[pages.length - 1].route)
  120. if (data.code === 401) {
  121. uni.showToast({
  122. duration: 2500,
  123. title: '登录信息过期',
  124. icon: 'error',
  125. });
  126. uni.clearStorageSync()
  127. // console.log("接口:" + BASE_URL + options.url + " - 401异常")
  128. goto('../../pageA/login/login')
  129. } else if (data.code === 403) {
  130. uni.showToast({
  131. duration: 2500,
  132. title: '登录信息过期',
  133. icon: 'error',
  134. });
  135. uni.clearStorageSync()
  136. // console.log("接口:" + BASE_URL + options.url + " - 403异常")
  137. goto('../../pageA/login/login')
  138. } else if (data.code != 200) {
  139. // if (!options.isNotErrorMsg) {
  140. // uni.showToast({
  141. // duration: 1500,
  142. // title: data.msg ? data.msg : '请求失败',
  143. // icon: 'none'`
  144. // });
  145. // }
  146. }
  147. resolve(res)
  148. },
  149. fail: (err) => {
  150. resolve(err)
  151. },
  152. // 完成之后关闭加载效果
  153. complete: () => {
  154. ajaxTimes--;
  155. if (ajaxTimes === 0) {
  156. // 关闭正在等待的图标
  157. // uni.hideLoading();
  158. }
  159. }
  160. })
  161. })
  162. }
  163. // 获取所有已配置字典
  164. export const getAllDicts = () => {
  165. let dictTypes = ['app_vacate_type', 'out_storage_type', 'app_msg_type', 'job_title', 'dimission_type',
  166. 'access_stroage_type', 'sys_user_sex', '', 'degree_dict', 'interview_type', 'hire_result',
  167. "hzl_seal_use_type", "seal_type", "hzl_seal_use_address", 'hzl_file_type', 'welfare_apply_addr',
  168. 'device_repairs_urgency_level', 'device_scrap_status', 'goods_storage_type'
  169. ]
  170. for (var i = 0; i < dictTypes.length; i++) {
  171. getDicts(dictTypes[i]).then(res => {
  172. uni.setStorage({
  173. key: res.dictType,
  174. data: res.data
  175. })
  176. })
  177. }
  178. }
  179. // 根据key查询config表数据信息
  180. export const getAllConfigData = (key) => {
  181. let configDatas = ['org_dept_parent_id', 'file_approval_flow_run_key', 'biz_leave_flow_run_key',
  182. 'reserve_consuming_flow_run_key', 'personnel_hire_approval_flow_run_key', 'reserve_return_flow_run_key',
  183. 'reserve_register_flow_run_key', 'personnel_dimission_approval_flow_run_key',
  184. 'interview_record_approval_flow_run_key', 'instant_notice_approval_flow_run_key',
  185. 'shift_official_approval_flow_run_key', 'device_repairs_approval_flow_run_key',
  186. 'use_seal_approval_flow_run_key', 'purchase_approval_flow_run_key', 'device_scrap_approval_flow_run_key'
  187. ]
  188. for (var i = 0; i < configDatas.length; i++) {
  189. getConfigData(configDatas[i]).then(res => {
  190. uni.setStorage({
  191. data: res.msg,
  192. key: res.key
  193. })
  194. })
  195. }
  196. }
  197. // 通用翻译功能
  198. export const dictTranslation = (value, dictType, type) => {
  199. let data = uni.getStorageSync(dictType)
  200. for (var i = 0; i < data.length; i++) {
  201. if (type === 'dept') {
  202. if (parseInt(value) === parseInt(data[i].deptId)) {
  203. return data[i].deptName;
  204. }
  205. } else if (type === 'dict') {
  206. if (parseInt(value) === parseInt(data[i].dictValue)) {
  207. return data[i].dictLabel;
  208. }
  209. } else if (type === 'post') {
  210. if (parseInt(value) === parseInt(data[i].postId)) {
  211. return data[i].postName;
  212. }
  213. }
  214. }
  215. }
  216. // 根据字典类型查询字典数据信息
  217. export const getDicts = (dictType) => {
  218. let header = {
  219. ...dictType
  220. }
  221. header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  222. header["UserName"] = uni.getStorageSync('setUserName');
  223. // 当前请求不是登录和获取所有机构时的请求,在header中加上后端返回的token
  224. // header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  225. header['Content-Type'] = 'application/json';
  226. header['Allow-Control-Allow-Origin'] = '*';
  227. return new Promise((resolve, reject) => {
  228. uni.request({
  229. url: getServerURl(getUrlType()) +
  230. '/system/dict/data/type/' + dictType,
  231. method: 'get',
  232. header,
  233. success: (res) => {
  234. res.data.dictType = dictType
  235. resolve(res.data);
  236. }
  237. })
  238. })
  239. }
  240. // 根据key查询config表数据信息
  241. export const getConfigData = (key) => {
  242. let header = {
  243. ...key
  244. }
  245. header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  246. header["UserName"] = uni.getStorageSync('setUserName');
  247. // 当前请求不是登录和获取所有机构时的请求,在header中加上后端返回的token
  248. // header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  249. header['Content-Type'] = 'application/json';
  250. header['Allow-Control-Allow-Origin'] = '*';
  251. return new Promise((resolve, reject) => {
  252. uni.request({
  253. url: getServerURl(getUrlType()) +
  254. '/system/config/configKey/' + key,
  255. method: 'get',
  256. header,
  257. success: (res) => {
  258. res.data.key = key
  259. resolve(res.data);
  260. }
  261. })
  262. })
  263. }
  264. export const showModal = (content) => {
  265. uni.showModal({
  266. title: "提示",
  267. content: content,
  268. showCancel: false,
  269. confirmColor: 'rgb(55,186,189)'
  270. })
  271. }
  272. export const goto = (url) => {
  273. uni.navigateTo({
  274. url: url,
  275. animationType: 'pop-in',
  276. animationDuration: 200
  277. });
  278. }
  279. export const goTab = (url) => {
  280. uni.switchTab({
  281. url: url
  282. });
  283. }
  284. export const goBack = () => {
  285. uni.navigateBack({
  286. delta: 1,
  287. animationType: 'pop-out',
  288. animationDuration: 200
  289. });
  290. }
  291. export const formatter = (type, value) => {
  292. if (type === 'year') {
  293. return `${value}年`
  294. }
  295. if (type === 'month') {
  296. return `${value}月`
  297. }
  298. if (type === 'day') {
  299. return `${value}日`
  300. }
  301. return value
  302. }
  303. export const dateResult = (time, mode) => {
  304. time = parseInt(time)
  305. const timeFormat = uni.$u.timeFormat
  306. switch (mode) {
  307. case 'datetime':
  308. return timeFormat(time, 'yyyy-mm-dd hh:MM:ss')
  309. case 'date':
  310. return timeFormat(time, 'yyyy-mm-dd')
  311. case 'year-month':
  312. return timeFormat(time, 'yyyy-mm')
  313. case 'time':
  314. return time
  315. default:
  316. return ''
  317. }
  318. }