httputil.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // let BASE_Server = "http://127.0.0.1:8329"
  2. //let BASE_Server = "https://192.168.1.114:18080"
  3. let BASE_Server = "https://sookajs.top:18080"
  4. let app = getApp()
  5. //-----------------------------登录,获取token-----------------------------------
  6. function init_userInfo() {
  7. if (app.globalToken == null) {
  8. wx.getUserProfile({
  9. desc: 'desc',
  10. success: (res) => {
  11. getSysUserInfo(res.userInfo)
  12. },
  13. fail: res => {
  14. }
  15. })
  16. }
  17. }
  18. function getSysUserInfo(info) {
  19. let that = this
  20. wx.login({
  21. success(res) {
  22. var code = res.code
  23. var data = {
  24. wxCode: code,
  25. wxNickName: info.nickName,
  26. wxAvatarUrl: info.avatarUrl
  27. }
  28. // http.send_post_login("/minapp/AppLoginController/appLogin",data)
  29. send_post("/auth/applogin", data, loginSuccess)
  30. },
  31. fail(res) {
  32. }
  33. })
  34. }
  35. function loginSuccess(res) {
  36. if (res.data.code != 200) {
  37. wx.showToast({
  38. title: res.data.msg != null ? res.data.msg : '',
  39. icon: "none"
  40. })
  41. } else {
  42. app.globalToken = res.data.access_token
  43. //登录成功,获取首页数据
  44. this.initIndexPage()
  45. }
  46. }
  47. //------------------------------------------------------------------
  48. /**
  49. * 携带token json数据 POST请求
  50. * @param {*} url
  51. * @param {*} data
  52. * @param {*} successfun
  53. */
  54. function send_post(url, data, successfun) {
  55. if (app.globalToken == null) {
  56. wx.showToast({
  57. title: '尚未登录,登录后即可使用',
  58. icon: 'none'
  59. })
  60. return
  61. }
  62. wx.request({
  63. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  64. method: "POST",
  65. header: {
  66. 'content-type': 'application/json',
  67. 'Authorization': 'Bearer ' + app.globalToken
  68. },
  69. data: data,
  70. success(res) {
  71. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  72. // var json = JSON.parse(d)
  73. authToken(res.data)
  74. successfun(res.data)
  75. },
  76. fail(res){
  77. wx.showToast({
  78. title: "网络请求失败",
  79. })
  80. }
  81. })
  82. }
  83. /**
  84. * 携带token表单数据POST请求
  85. * @param {*} url
  86. * @param {*} data
  87. * @param {*} successfun
  88. */
  89. function post(url, data, successfun) {
  90. if (app.globalToken == null) {
  91. wx.showToast({
  92. title: '尚未登录,登录后即可使用',
  93. icon: 'none'
  94. })
  95. return
  96. }
  97. wx.request({
  98. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  99. method: "POST",
  100. header: {
  101. 'content-type': 'application/x-www-form-urlencoded',
  102. 'Authorization': 'Bearer ' + app.globalToken
  103. },
  104. data: data,
  105. success(res) {
  106. successfun(res.data)
  107. },
  108. fail(res){
  109. wx.showToast({
  110. title: "网络请求失败",
  111. })
  112. }
  113. })
  114. }
  115. /**
  116. * 无token json数据使用的POST请求
  117. * @param {*} url
  118. * @param {*} data
  119. * @param {*} successfun
  120. */
  121. function post_token(url, data, successfun) {
  122. wx.request({
  123. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  124. method: "POST",
  125. header: {
  126. 'content-type': 'application/json',
  127. 'Authorization': 'Bearer ' + app.globalToken
  128. },
  129. data: data,
  130. success(res) {
  131. authToken(res.data)
  132. successfun(res.data)
  133. },
  134. fail(res){
  135. wx.showToast({
  136. title: "网络请求失败",
  137. icon:'none',
  138. })
  139. }
  140. })
  141. }
  142. /**
  143. * 备用的GET请求
  144. * @param {*} url
  145. * @param {*} data
  146. * @param {*} successfun
  147. */
  148. function get(url, data, successfun) {
  149. if (app.globalToken == null) {
  150. wx.showToast({
  151. title: '尚未登录,登录后即可使用',
  152. icon: 'none'
  153. })
  154. return
  155. }
  156. wx.request({
  157. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  158. method: "GET",
  159. header: {
  160. 'content-type': 'application/x-www-form-urlencoded',
  161. 'Authorization': 'Bearer ' + app.globalToken
  162. },
  163. params: data,
  164. success(res) {
  165. successfun(res.data)
  166. },
  167. fail(res){
  168. wx.showToast({
  169. title: "网络请求失败",
  170. })
  171. }
  172. })
  173. }
  174. /**
  175. * 携带token使用的GET请求
  176. * @param {*} url
  177. * @param {*} data
  178. * @param {*} successfun
  179. */
  180. function send_get(url, data, successfun) {
  181. if (app.globalToken == null) {
  182. wx.showToast({
  183. title: '尚未登录,登录后即可使用',
  184. icon: 'none'
  185. })
  186. return
  187. }
  188. wx.request({
  189. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  190. method: "GET",
  191. header: {
  192. 'content-type': 'application/json',
  193. 'Authorization': 'Bearer ' + app.globalToken
  194. },
  195. data: data,
  196. success(res) {
  197. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  198. // var json = JSON.parse(d)
  199. authToken(res.data)
  200. successfun(res.data)
  201. },
  202. fail(res){
  203. wx.showToast({
  204. title: "网络请求失败",
  205. icon:'none',
  206. })
  207. }
  208. })
  209. }
  210. /**
  211. * 无token 可使用的 GET 请求
  212. * @param {*} url
  213. * @param {*} data
  214. * @param {*} successfun
  215. */
  216. function get_token(url, data, successfun) {
  217. wx.request({
  218. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  219. method: "GET",
  220. header: {
  221. 'content-type': 'application/json',
  222. 'Authorization': 'Bearer ' + app.globalToken
  223. },
  224. data: data,
  225. success(res) {
  226. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  227. // var json = JSON.parse(d)
  228. authToken(res.data)
  229. successfun(res.data)
  230. },
  231. fail(res){
  232. wx.showToast({
  233. title: "网络请求失败",
  234. icon:'none',
  235. })
  236. }
  237. })
  238. }
  239. function send_postdecode(url, data, successfun) {
  240. wx.request({
  241. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  242. method: "POST",
  243. header: {
  244. 'content-type': 'application/x-www-form-urlencoded'
  245. },
  246. data: data,
  247. success(res) {
  248. var json = JSON.parse(res.data)
  249. successfun(json)
  250. }
  251. })
  252. }
  253. function send_photo(data, successfun) {
  254. var that = this
  255. for (let i = 0; i < data.length; i++) {
  256. wx.uploadFile({
  257. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  258. filePath: data[i] + "",
  259. name: 'file',
  260. success: function (res) {
  261. var json = JSON.parse(res.data)
  262. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  263. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  264. successfun(sss)
  265. }
  266. })
  267. }
  268. }
  269. function send(data, successfun) {
  270. var that = this
  271. wx.uploadFile({
  272. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  273. filePath: data[0] + "",
  274. name: 'file',
  275. success: function (res) {
  276. var json = JSON.parse(res.data)
  277. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  278. // wx.setStorageSync("carnumber", sss.data.carNumber)
  279. successfun(sss)
  280. },
  281. fail: function (res) {}
  282. })
  283. }
  284. module.exports = {
  285. BASE_Server: BASE_Server,
  286. send_photo: send_photo,
  287. send: send,
  288. post: post,
  289. send_post: send_post,
  290. post_token: post_token,
  291. // wxpost: wxpost,
  292. get: get,
  293. send_get:send_get,
  294. get_token:get_token,
  295. send_postdecode: send_postdecode,
  296. // send_post_login,
  297. showLoading,
  298. hideLoading,
  299. }
  300. function showLoading() {
  301. wx.showLoading({
  302. title: '加载中...',
  303. mask: true,
  304. })
  305. }
  306. function hideLoading() {
  307. wx.hideLoading()
  308. }
  309. function authToken(res){
  310. if(res.code==401){//登录token过期状态码
  311. wx.clearStorage({
  312. success:(res)=>{
  313. wx.reLaunch({
  314. url: '../denglu/denglu',
  315. })
  316. }
  317. })
  318. }
  319. }
  320. // function wxpost(url, data, successfun) {
  321. // wx.request({
  322. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  323. // method: "POST",
  324. // header: {
  325. // 'content-type': 'application/json',
  326. // 'Authorization': 'Bearer ' + app.globalToken
  327. // },
  328. // data: data,
  329. // success(res) {
  330. // successfun(res.data)
  331. // }
  332. // })
  333. // }