httputil.js 8.7 KB

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