interface.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * 通用uni-app网络请求
  3. * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
  4. */
  5. /*
  6. // 开放的接口
  7. import http from './interface'
  8. http.config.baseUrl = "http://localhost:8080/api/"
  9. http.request(url:'user/list',method:'GET').then((res)=>{
  10. console.log(JSON.stringify(res))
  11. })
  12. http.get('user/list').then((res)=>{
  13. console.log(JSON.stringify(res))
  14. })
  15. http.get('user/list', {status: 1}).then((res)=>{
  16. console.log(JSON.stringify(res))
  17. })
  18. http.post('user', {id:1, status: 1}).then((res)=>{
  19. console.log(JSON.stringify(res))
  20. })
  21. http.put('user/1', {status: 2}).then((res)=>{
  22. console.log(JSON.stringify(res))
  23. })
  24. http.delete('user/1').then((res)=>{
  25. console.log(JSON.stringify(res))
  26. })
  27. */
  28. export default {
  29. config: {
  30. baseUrl: "http://nmjt.nxjiewei.com:8011/api",
  31. header: {
  32. "Content-Type":"multipart/form-data",
  33. 'Content-Type':'application/json;charset=UTF-8',
  34. 'Content-Type':'application/x-www-form-urlencoded',
  35. 'Authorization' : uni.getStorageSync('token_type') +' '+uni.getStorageSync('Authorization') || {}
  36. },
  37. data: {
  38. mine: "640181B0011010027175",
  39. mine_code: "640181B0011010027175",
  40. org_num: "1019",
  41. },
  42. method: "GET",
  43. dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
  44. responseType: "text",
  45. success() {},
  46. fail() {},
  47. complete() {
  48. // uni.hideLoading()
  49. }
  50. },
  51. interceptor: {
  52. request: null,
  53. response: null
  54. },
  55. request(options) {
  56. if (!options) {
  57. options = {}
  58. }
  59. options.baseUrl = options.baseUrl || this.config.baseUrl
  60. options.dataType = options.dataType || this.config.dataType
  61. options.url = options.baseUrl + options.url
  62. // 合并对象
  63. options.data = {...options.data,...this.config.data}
  64. options.method = options.method || this.config.method
  65. //TODO 加密数据
  66. //TODO 数据签名
  67. /*
  68. _token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
  69. _sign = {'sign': sign(JSON.stringify(options.data))}
  70. options.header = Object.assign({}, options.header, _token,_sign)
  71. */
  72. return new Promise((resolve, reject) => {
  73. let _config = null
  74. // uni.showLoading({
  75. // icon:"none",
  76. // title:"加载中...",
  77. // mask:true
  78. // })
  79. options.complete = (response) => {
  80. let statusCode = response.statusCode
  81. // console.log("【" + _config.requestId + "】 状态码:" + JSON.stringify(statusCode))
  82. response.config = _config
  83. if (process.env.NODE_ENV === 'development') {
  84. if (statusCode === 200) {
  85. // uni.hideLoading()
  86. // console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
  87. if(response.data.code == 401){
  88. uni.showToast({
  89. icon:"none",
  90. title:"登录失效、请重新登录"
  91. })
  92. setTimeout(function(){
  93. uni.redirectTo({
  94. url:"/pages/login/login"
  95. })
  96. },2000)
  97. }
  98. }else if(statusCode === 500){
  99. uni.hideLoading()
  100. uni.showToast({
  101. icon:"none",
  102. title:"500"
  103. })
  104. // setTimeout(function(){
  105. // uni.navigateTo({
  106. // url:"/pages/login/login"
  107. // })
  108. // },1500)
  109. }
  110. }
  111. if (this.interceptor.response) {
  112. let newResponse = this.interceptor.response(response)
  113. if (newResponse) {
  114. response = newResponse
  115. }
  116. }
  117. // 统一的响应日志记录
  118. // _reslog(response)
  119. if (statusCode === 200) { //成功
  120. resolve(response);
  121. } else {
  122. reject(response)
  123. }
  124. }
  125. _config = Object.assign({}, this.config, options)
  126. _config.requestId = new Date().getTime()
  127. if (this.interceptor.request) {
  128. this.interceptor.request(_config)
  129. }
  130. // 统一的请求日志记录
  131. // _reqlog(_config)
  132. if (process.env.NODE_ENV === 'development') {
  133. // console.log("【" + _config.requestId + "】 地址:" + _config.url)
  134. if (_config.data) {
  135. // console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
  136. }
  137. }
  138. uni.request(_config);
  139. });
  140. },
  141. get(url, data, options) {
  142. if (!options) {
  143. options = {}
  144. }
  145. options.url = url
  146. options.data = data
  147. options.method = 'GET'
  148. return this.request(options)
  149. },
  150. post(url, data, options) {
  151. if (!options) {
  152. options = {}
  153. }
  154. options.url = url
  155. options.data = data
  156. options.method = 'POST'
  157. return this.request(options)
  158. },
  159. put(url, data, options) {
  160. if (!options) {
  161. options = {}
  162. }
  163. options.url = url
  164. options.data = data
  165. options.method = 'PUT'
  166. return this.request(options)
  167. },
  168. delete(url, data, options) {
  169. if (!options) {
  170. options = {}
  171. }
  172. options.url = url
  173. options.data = data
  174. options.method = 'DELETE'
  175. return this.request(options)
  176. }
  177. }
  178. /**
  179. * 请求接口日志记录
  180. */
  181. function _reqlog(req) {
  182. if (process.env.NODE_ENV === 'development') {
  183. console.log("【" + req.requestId + "】 地址:" + req.url)
  184. if (req.data) {
  185. console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
  186. }
  187. }
  188. //TODO 调接口异步写入日志数据库
  189. }
  190. /**
  191. * 响应接口日志记录
  192. */
  193. function _reslog(res) {
  194. let _statusCode = res.statusCode;
  195. if (process.env.NODE_ENV === 'development') {
  196. console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
  197. if (res.config.data) {
  198. console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
  199. }
  200. console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
  201. }
  202. //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  203. switch(_statusCode){
  204. case 200:
  205. break;
  206. case 401:
  207. break;
  208. case 404:
  209. break;
  210. default:
  211. break;
  212. }
  213. }