httputil.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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:28080"
  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. // console.log("TAG","返回数据:"+res.errno+'_'+res.errMsg)
  78. wx.showToast({
  79. title: "网络请求失败",
  80. })
  81. }
  82. })
  83. }
  84. /**
  85. * 携带token表单数据POST请求
  86. * @param {*} url
  87. * @param {*} data
  88. * @param {*} successfun
  89. */
  90. function post(url, data, successfun) {
  91. if (app.globalToken == null) {
  92. wx.showToast({
  93. title: '尚未登录,登录后即可使用',
  94. icon: 'none'
  95. })
  96. return
  97. }
  98. wx.request({
  99. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  100. method: "POST",
  101. header: {
  102. 'content-type': 'application/x-www-form-urlencoded',
  103. 'Authorization': 'Bearer ' + app.globalToken
  104. },
  105. data: data,
  106. success(res) {
  107. successfun(res.data)
  108. },
  109. fail(res){
  110. wx.showToast({
  111. title: "网络请求失败",
  112. })
  113. }
  114. })
  115. }
  116. /**
  117. * 无token json数据使用的POST请求
  118. * @param {*} url
  119. * @param {*} data
  120. * @param {*} successfun
  121. */
  122. function post_token(url, data, successfun) {
  123. wx.request({
  124. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  125. method: "POST",
  126. header: {
  127. 'content-type': 'application/json',
  128. 'Authorization': 'Bearer ' + app.globalToken
  129. },
  130. data: data,
  131. success(res) {
  132. authToken(res.data)
  133. successfun(res.data)
  134. },
  135. fail(res){
  136. wx.showToast({
  137. title: "网络请求失败",
  138. icon:'none',
  139. })
  140. }
  141. })
  142. }
  143. /**
  144. * 备用的GET请求
  145. * @param {*} url
  146. * @param {*} data
  147. * @param {*} successfun
  148. */
  149. function get(url, data, successfun) {
  150. if (app.globalToken == null) {
  151. wx.showToast({
  152. title: '尚未登录,登录后即可使用',
  153. icon: 'none'
  154. })
  155. return
  156. }
  157. wx.request({
  158. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  159. method: "GET",
  160. header: {
  161. 'content-type': 'application/x-www-form-urlencoded',
  162. 'Authorization': 'Bearer ' + app.globalToken
  163. },
  164. params: data,
  165. success(res) {
  166. successfun(res.data)
  167. },
  168. fail(res){
  169. wx.showToast({
  170. title: "网络请求失败",
  171. })
  172. }
  173. })
  174. }
  175. /**
  176. * 携带token使用的GET请求
  177. * @param {*} url
  178. * @param {*} data
  179. * @param {*} successfun
  180. */
  181. function send_get(url, data, successfun) {
  182. if (app.globalToken == null) {
  183. wx.showToast({
  184. title: '尚未登录,登录后即可使用',
  185. icon: 'none'
  186. })
  187. return
  188. }
  189. wx.request({
  190. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  191. method: "GET",
  192. header: {
  193. 'content-type': 'application/json',
  194. 'Authorization': 'Bearer ' + app.globalToken
  195. },
  196. data: data,
  197. success(res) {
  198. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  199. // var json = JSON.parse(d)
  200. authToken(res.data)
  201. successfun(res.data)
  202. },
  203. fail(res){
  204. wx.showToast({
  205. title: "网络请求失败",
  206. icon:'none',
  207. })
  208. }
  209. })
  210. }
  211. /**
  212. * 无token 可使用的 GET 请求
  213. * @param {*} url
  214. * @param {*} data
  215. * @param {*} successfun
  216. */
  217. function get_token(url, data, successfun) {
  218. wx.request({
  219. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  220. method: "GET",
  221. header: {
  222. 'content-type': 'application/json',
  223. 'Authorization': 'Bearer ' + app.globalToken
  224. },
  225. data: data,
  226. success(res) {
  227. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  228. // var json = JSON.parse(d)
  229. authToken(res.data)
  230. successfun(res.data)
  231. },
  232. fail(res){
  233. wx.showToast({
  234. title: "网络请求失败",
  235. icon:'none',
  236. })
  237. }
  238. })
  239. }
  240. function send_postdecode(url, data, successfun) {
  241. wx.request({
  242. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  243. method: "POST",
  244. header: {
  245. 'content-type': 'application/x-www-form-urlencoded'
  246. },
  247. data: data,
  248. success(res) {
  249. var json = JSON.parse(res.data)
  250. successfun(json)
  251. }
  252. })
  253. }
  254. function send_photo(data, successfun) {
  255. var that = this
  256. for (let i = 0; i < data.length; i++) {
  257. wx.uploadFile({
  258. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  259. filePath: data[i] + "",
  260. name: 'file',
  261. success: function (res) {
  262. var json = JSON.parse(res.data)
  263. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  264. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  265. successfun(sss)
  266. }
  267. })
  268. }
  269. }
  270. function send(data, successfun) {
  271. var that = this
  272. wx.uploadFile({
  273. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  274. filePath: data[0] + "",
  275. name: 'file',
  276. success: function (res) {
  277. var json = JSON.parse(res.data)
  278. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  279. // wx.setStorageSync("carnumber", sss.data.carNumber)
  280. successfun(sss)
  281. },
  282. fail: function (res) {}
  283. })
  284. }
  285. module.exports = {
  286. BASE_Server: BASE_Server,
  287. send_photo: send_photo,
  288. send: send,
  289. post: post,
  290. send_post: send_post,
  291. post_token: post_token,
  292. // wxpost: wxpost,
  293. get: get,
  294. send_get:send_get,
  295. get_token:get_token,
  296. send_postdecode: send_postdecode,
  297. // send_post_login,
  298. showLoading,
  299. hideLoading,
  300. }
  301. function showLoading() {
  302. wx.showLoading({
  303. title: '加载中...',
  304. mask: true,
  305. })
  306. }
  307. function hideLoading() {
  308. wx.hideLoading()
  309. }
  310. function authToken(res){
  311. if(res.code==401){//登录token过期状态码
  312. wx.clearStorage({
  313. success:(res)=>{
  314. wx.reLaunch({
  315. url: '../denglu/denglu',
  316. })
  317. }
  318. })
  319. }
  320. }
  321. // function wxpost(url, data, successfun) {
  322. // wx.request({
  323. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  324. // method: "POST",
  325. // header: {
  326. // 'content-type': 'application/json',
  327. // 'Authorization': 'Bearer ' + app.globalToken
  328. // },
  329. // data: data,
  330. // success(res) {
  331. // successfun(res.data)
  332. // }
  333. // })
  334. // }