o2Request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. let app = getApp();
  2. function request(method, url, param, isShowLoading) {
  3. //返回一个Promise对象
  4. return new Promise(function(resolve, reject) {
  5. if (isShowLoading) {
  6. wx.showLoading({
  7. title: '加载中...',
  8. mask: true
  9. });
  10. }
  11. wx.request({
  12. url: url,
  13. method: method,
  14. data: param,
  15. header: {
  16. 'content-type': 'application/json',
  17. 'cookie': wx.getStorageSync('cookie')
  18. },
  19. success: function(res) {
  20. // 保存登录返回的cookie
  21. if (res.header['Set-Cookie']) {
  22. wx.setStorageSync('cookie', res.header['Set-Cookie']);
  23. // wx.setStorage({
  24. // key: 'cookie',
  25. // data: res.header['Set-Cookie'],
  26. // })
  27. }
  28. // console.log('接口:' + url, ' 参数:', param, '\n返回值:', res.data)
  29. if (isShowLoading) {
  30. wx.hideLoading();
  31. }
  32. if (res.data.type == 'success') { // 接口正常返回
  33. resolve(res.data.data);
  34. } else { // 出现异常
  35. wx.showToast({
  36. title: res.data.message,
  37. icon: 'none',
  38. duration: 2000
  39. });
  40. reject(res.data);
  41. }
  42. },
  43. fail: function(res) {
  44. wx.hideLoading();
  45. console.log('请求异常 接口:' + url, ' 参数:', param, '\n返回值:', res)
  46. wx.showToast({
  47. title: '请求异常',
  48. icon: 'none',
  49. duration: 2000
  50. });
  51. reject(res);
  52. }
  53. });
  54. });
  55. }
  56. //设置应用地址到存储中
  57. function setDistribute(distribute) {
  58. ['webServer', 'assembles', 'tokenName'].forEach( t => {
  59. wx.removeStorageSync(t);
  60. if (t === 'tokenName') {
  61. wx.setStorageSync(t, distribute[t] || 'x-token');
  62. } else {
  63. wx.setStorageSync(t, distribute[t] || {});
  64. }
  65. });
  66. }
  67. // 获取模块的baseUrl
  68. function getO2AssembleUrl(contextName) {
  69. var assemble = wx.getStorageSync('assembles')[contextName] || {host: app.globalData.o2oa.centerHost, port: app.globalData.o2oa.centerPort, context: "/"+contextName};
  70. return app.globalData.o2oa.httpProtocol + "://" + assemble["host"] + ":" + assemble["port"]+ assemble["context"];
  71. // var assemble = wx.getStorageSync('assembles')[contextName] || {host: app.globalData.o2oa.centerHost, port: app.globalData.o2oa.centerPort, context: "/"+contextName};
  72. // return app.globalData.o2oa.httpProtocol + "://" + assemble["host"] + ":" + assemble["context"];
  73. }
  74. //web服务器根地址 如 https://sample.o2oa.net:443
  75. function getO2WebBaseUrl() {
  76. var webServer = wx.getStorageSync('webServer');
  77. return app.globalData.o2oa.httpProtocol + "://" + webServer["host"] + ":" + webServer["port"];
  78. // var webServer = wx.getStorageSync('webServer');
  79. // return app.globalData.o2oa.httpProtocol + "://" + webServer["host"];
  80. }
  81. // 中心服务器获取
  82. function o2oaCenterUrl() {
  83. return app.globalData.o2oa.httpProtocol + "://" + app.globalData.o2oa.centerHost + ":" + app.globalData.o2oa.centerPort + app.globalData.o2oa.centerContext + "/jaxrs/distribute/webserver/assemble/source/" + app.globalData.o2oa.centerHost;
  84. // return app.globalData.o2oa.httpProtocol + "://" + app.globalData.o2oa.centerHost + ":" + app.globalData.o2oa.centerContext + "/jaxrs/distribute/webserver/assemble/source/" + app.globalData.o2oa.centerHost
  85. }
  86. // 获取演示服务器账号列表
  87. function wwwGetSampleServerAccountsUrl() {
  88. return "https://www.o2oa.net/oa/x_program_center/jaxrs/invoke/demo_app_get_login_accounts/execute";
  89. }
  90. //认证模块
  91. function o2oaOrganizationAuthenticationBaseUrl() {
  92. return getO2AssembleUrl('x_organization_assemble_authentication');
  93. }
  94. //cms模块
  95. function o2oaCmsServiceBaseUrl() {
  96. return getO2AssembleUrl('x_cms_assemble_control');
  97. }
  98. //bbs论坛模块
  99. function o2oaBBSServiceBaseUrl() {
  100. return getO2AssembleUrl('x_bbs_assemble_control');
  101. }
  102. //热点图片模块
  103. function o2oaHotPicServiceBaseUrl() {
  104. return getO2AssembleUrl('x_hotpic_assemble_control');
  105. }
  106. //文件管理模块
  107. function o2oaFileServiceBaseUrl() {
  108. return getO2AssembleUrl('x_file_assemble_control');
  109. }
  110. //流程模块
  111. function o2oaProcessServiceBaseUrl() {
  112. return getO2AssembleUrl('x_processplatform_assemble_surface');
  113. }
  114. //人员管理
  115. function o2oaPersonalServiceBaseUrl() {
  116. return getO2AssembleUrl('x_organization_assemble_personal');
  117. }
  118. // get请求
  119. function get(path, param = {}, isShowLoading = true) {
  120. return request("GET", path, param, isShowLoading);
  121. }
  122. // post请求
  123. function post(path, param = {}, isShowLoading = true) {
  124. return request("POST", path, param, isShowLoading);
  125. }
  126. // PUT 请求
  127. function put(path, param = {}, isShowLoading = true) {
  128. return request("PUT", path, param, isShowLoading);
  129. }
  130. // DELETE 请求
  131. function deleteReq(path, param = {}, isShowLoading = true) {
  132. return request("DELETE", path, param, isShowLoading);
  133. }
  134. module.exports = {
  135. get: get,
  136. post: post,
  137. put: put,
  138. delete: deleteReq,
  139. o2oaOrganizationAuthenticationBaseUrl: o2oaOrganizationAuthenticationBaseUrl,
  140. o2oaCenterUrl: o2oaCenterUrl,
  141. setDistribute: setDistribute,
  142. getO2WebBaseUrl: getO2WebBaseUrl,
  143. o2oaCmsServiceBaseUrl: o2oaCmsServiceBaseUrl,
  144. o2oaHotPicServiceBaseUrl: o2oaHotPicServiceBaseUrl,
  145. o2oaFileServiceBaseUrl: o2oaFileServiceBaseUrl,
  146. o2oaProcessServiceBaseUrl: o2oaProcessServiceBaseUrl,
  147. o2oaBBSServiceBaseUrl: o2oaBBSServiceBaseUrl,
  148. o2oaPersonalServiceBaseUrl: o2oaPersonalServiceBaseUrl,
  149. wwwGetSampleServerAccountsUrl: wwwGetSampleServerAccountsUrl
  150. }