api.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. 'condole_renshenshanghai_type'
  170. ]
  171. for (var i = 0; i < dictTypes.length; i++) {
  172. getDicts(dictTypes[i]).then(res => {
  173. uni.setStorage({
  174. key: res.dictType,
  175. data: res.data
  176. })
  177. })
  178. }
  179. }
  180. // 根据key查询config表数据信息
  181. export const getAllConfigData = (key) => {
  182. let configDatas = ['org_dept_parent_id', 'file_approval_flow_run_key', 'biz_leave_flow_run_key',
  183. 'reserve_consuming_flow_run_key', 'personnel_hire_approval_flow_run_key', 'reserve_return_flow_run_key',
  184. 'reserve_register_flow_run_key', 'personnel_dimission_approval_flow_run_key',
  185. 'interview_record_approval_flow_run_key', 'instant_notice_approval_flow_run_key',
  186. 'shift_official_approval_flow_run_key', 'device_repairs_approval_flow_run_key',
  187. 'use_seal_approval_flow_run_key', 'purchase_approval_flow_run_key', 'device_scrap_approval_flow_run_key'
  188. ]
  189. for (var i = 0; i < configDatas.length; i++) {
  190. getConfigData(configDatas[i]).then(res => {
  191. uni.setStorage({
  192. data: res.msg,
  193. key: res.key
  194. })
  195. })
  196. }
  197. }
  198. // 通用翻译功能
  199. export const dictTranslation = (value, dictType, type) => {
  200. let data = uni.getStorageSync(dictType)
  201. for (var i = 0; i < data.length; i++) {
  202. if (type === 'dept') {
  203. if (parseInt(value) === parseInt(data[i].deptId)) {
  204. return data[i].deptName;
  205. }
  206. } else if (type === 'dict') {
  207. if (parseInt(value) === parseInt(data[i].dictValue)) {
  208. return data[i].dictLabel;
  209. }
  210. } else if (type === 'post') {
  211. if (parseInt(value) === parseInt(data[i].postId)) {
  212. return data[i].postName;
  213. }
  214. }
  215. }
  216. }
  217. // 根据字典类型查询字典数据信息
  218. export const getDicts = (dictType) => {
  219. let header = {
  220. ...dictType
  221. }
  222. header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  223. header["UserName"] = uni.getStorageSync('setUserName');
  224. // 当前请求不是登录和获取所有机构时的请求,在header中加上后端返回的token
  225. // header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  226. header['Content-Type'] = 'application/json';
  227. header['Allow-Control-Allow-Origin'] = '*';
  228. return new Promise((resolve, reject) => {
  229. uni.request({
  230. url: getServerURl(getUrlType()) +
  231. '/system/dict/data/type/' + dictType,
  232. method: 'get',
  233. header,
  234. success: (res) => {
  235. res.data.dictType = dictType
  236. resolve(res.data);
  237. }
  238. })
  239. })
  240. }
  241. // 根据key查询config表数据信息
  242. export const getConfigData = (key) => {
  243. let header = {
  244. ...key
  245. }
  246. header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  247. header["UserName"] = uni.getStorageSync('setUserName');
  248. // 当前请求不是登录和获取所有机构时的请求,在header中加上后端返回的token
  249. // header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  250. header['Content-Type'] = 'application/json';
  251. header['Allow-Control-Allow-Origin'] = '*';
  252. return new Promise((resolve, reject) => {
  253. uni.request({
  254. url: getServerURl(getUrlType()) +
  255. '/system/config/configKey/' + key,
  256. method: 'get',
  257. header,
  258. success: (res) => {
  259. res.data.key = key
  260. resolve(res.data);
  261. }
  262. })
  263. })
  264. }
  265. export const showModal = (content) => {
  266. uni.showModal({
  267. title: "提示",
  268. content: content,
  269. showCancel: false,
  270. confirmColor: 'rgb(55,186,189)'
  271. })
  272. }
  273. export const goto = (url) => {
  274. uni.navigateTo({
  275. url: url,
  276. animationType: 'pop-in',
  277. animationDuration: 200
  278. });
  279. }
  280. export const goTab = (url) => {
  281. uni.switchTab({
  282. url: url
  283. });
  284. }
  285. export const goBack = () => {
  286. uni.navigateBack({
  287. delta: 1,
  288. animationType: 'pop-out',
  289. animationDuration: 200
  290. });
  291. }
  292. export const formatter = (type, value) => {
  293. if (type === 'year') {
  294. return `${value}年`
  295. }
  296. if (type === 'month') {
  297. return `${value}月`
  298. }
  299. if (type === 'day') {
  300. return `${value}日`
  301. }
  302. return value
  303. }
  304. export const dateResult = (time, mode) => {
  305. time = parseInt(time)
  306. const timeFormat = uni.$u.timeFormat
  307. switch (mode) {
  308. case 'datetime':
  309. return timeFormat(time, 'yyyy-mm-dd hh:MM:ss')
  310. case 'date':
  311. return timeFormat(time, 'yyyy-mm-dd')
  312. case 'year-month':
  313. return timeFormat(time, 'yyyy-mm')
  314. case 'time':
  315. return time
  316. default:
  317. return ''
  318. }
  319. }