httputil.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // let BASE_Server = "http://127.0.0.1:8329"
  2. //let BASE_Server = "https://192.168.1.100:18080"
  3. let BASE_Server = "https://sookajs.top:18080"
  4. let app = getApp()
  5. // function send_post_login(url, data, successfun) {
  6. // wx.request({
  7. // url: BASE_Server_Login + url, //仅为示例,并非真实的接口地址
  8. // method: "POST",
  9. // header: {
  10. // 'content-type': 'application/json'
  11. // },
  12. // data: data,
  13. // success(res) {
  14. // successfun(res)
  15. // // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  16. // // console.log("*****", d)
  17. // // var json = JSON.parse(d)
  18. // // successfun(json)
  19. // }
  20. // })
  21. // }
  22. //-----------------------------登录,获取token-----------------------------------
  23. function init_userInfo() {
  24. if (app.globalToken == null) {
  25. wx.getUserProfile({
  26. desc: 'desc',
  27. success: (res) => {
  28. getSysUserInfo(res.userInfo)
  29. },
  30. fail: res => {
  31. console.log(res)
  32. }
  33. })
  34. }
  35. }
  36. function getSysUserInfo(info) {
  37. console.log(2222)
  38. let that = this
  39. wx.login({
  40. success(res) {
  41. var code = res.code
  42. var data = {
  43. wxCode: code,
  44. wxNickName: info.nickName,
  45. wxAvatarUrl: info.avatarUrl
  46. }
  47. // http.send_post_login("/minapp/AppLoginController/appLogin",data)
  48. send_post("/auth/applogin", data, loginSuccess)
  49. },
  50. fail(res) {
  51. console.log("ffff", res)
  52. }
  53. })
  54. }
  55. function loginSuccess(res) {
  56. if (res.data.code != 200) {
  57. wx.showToast({
  58. title: res.data.msg != null ? res.data.msg : '',
  59. icon: "none"
  60. })
  61. } else {
  62. app.globalToken = res.data.access_token
  63. //登录成功,获取首页数据
  64. this.initIndexPage()
  65. }
  66. console.log("!!!!!", app.globalToken)
  67. }
  68. //------------------------------------------------------------------
  69. function send_post(url, data, successfun) {
  70. console.log("令牌:", app.globalToken)
  71. // if(app.globalToken==null){
  72. // wx.reLaunch({
  73. // url: '../denglu/denglu',
  74. // })
  75. // }
  76. //如果令牌为空或过期,需要重新请求
  77. wx.request({
  78. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  79. method: "POST",
  80. header: {
  81. 'content-type': 'application/json',
  82. 'Authorization': 'Bearer ' + app.globalToken
  83. },
  84. data: data,
  85. success(res) {
  86. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  87. // var json = JSON.parse(d)
  88. console.log("TAG","返回数据:"+res.data)
  89. authToken(res.data)
  90. successfun(res.data)
  91. },
  92. fail(res){
  93. wx.showToast({
  94. title: "网络请求失败",
  95. })
  96. }
  97. })
  98. }
  99. function post(url, data, successfun) {
  100. wx.request({
  101. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  102. method: "POST",
  103. header: {
  104. 'content-type': 'application/x-www-form-urlencoded',
  105. 'Authorization': 'Bearer ' + app.globalToken
  106. },
  107. data: data,
  108. success(res) {
  109. successfun(res.data)
  110. },
  111. fail(res){
  112. wx.showToast({
  113. title: "网络请求失败",
  114. })
  115. }
  116. })
  117. }
  118. // function wxpost(url, data, successfun) {
  119. // wx.request({
  120. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  121. // method: "POST",
  122. // header: {
  123. // 'content-type': 'application/json',
  124. // 'Authorization': 'Bearer ' + app.globalToken
  125. // },
  126. // data: data,
  127. // success(res) {
  128. // successfun(res.data)
  129. // }
  130. // })
  131. // }
  132. function get(url, data, successfun) {
  133. wx.request({
  134. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  135. method: "GET",
  136. header: {
  137. 'content-type': 'application/x-www-form-urlencoded',
  138. 'Authorization': 'Bearer ' + app.globalToken
  139. },
  140. params: data,
  141. success(res) {
  142. successfun(res.data)
  143. },
  144. fail(res){
  145. wx.showToast({
  146. title: "网络请求失败",
  147. })
  148. }
  149. })
  150. }
  151. function send_get(url, data, successfun) {
  152. console.log("令牌:", app.globalToken)
  153. // if(app.globalToken==null){
  154. // wx.reLaunch({
  155. // url: '../denglu/denglu',
  156. // })
  157. // }
  158. //如果令牌为空或过期,需要重新请求
  159. wx.request({
  160. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  161. method: "GET",
  162. header: {
  163. 'content-type': 'application/json',
  164. 'Authorization': 'Bearer ' + app.globalToken
  165. },
  166. data: data,
  167. success(res) {
  168. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  169. // var json = JSON.parse(d)
  170. console.log("TAG","返回数据:"+res.data)
  171. authToken(res.data)
  172. successfun(res.data)
  173. },
  174. fail(res){
  175. wx.showToast({
  176. title: "网络请求失败",
  177. })
  178. }
  179. })
  180. }
  181. function send_postdecode(url, data, successfun) {
  182. wx.request({
  183. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  184. method: "POST",
  185. header: {
  186. 'content-type': 'application/x-www-form-urlencoded'
  187. },
  188. data: data,
  189. success(res) {
  190. var json = JSON.parse(res.data)
  191. successfun(json)
  192. }
  193. })
  194. }
  195. function send_photo(data, successfun) {
  196. var that = this
  197. for (let i = 0; i < data.length; i++) {
  198. console.log("data长度=" + data.length)
  199. console.log(data)
  200. wx.uploadFile({
  201. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  202. filePath: data[i] + "",
  203. name: 'file',
  204. success: function (res) {
  205. var json = JSON.parse(res.data)
  206. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  207. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  208. successfun(sss)
  209. }
  210. })
  211. }
  212. }
  213. function send(data, successfun) {
  214. var that = this
  215. wx.uploadFile({
  216. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  217. filePath: data[0] + "",
  218. name: 'file',
  219. success: function (res) {
  220. var json = JSON.parse(res.data)
  221. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  222. // wx.setStorageSync("carnumber", sss.data.carNumber)
  223. successfun(sss)
  224. },
  225. fail: function (res) {}
  226. })
  227. }
  228. module.exports = {
  229. BASE_Server: BASE_Server,
  230. send_post: send_post,
  231. send_photo: send_photo,
  232. send: send,
  233. post: post,
  234. // wxpost: wxpost,
  235. get: get,
  236. send_get:send_get,
  237. send_postdecode: send_postdecode,
  238. // send_post_login,
  239. showLoading,
  240. hideLoading,
  241. }
  242. function showLoading() {
  243. wx.showLoading({
  244. title: '加载中...',
  245. mask: true,
  246. })
  247. }
  248. function hideLoading() {
  249. wx.hideLoading()
  250. }
  251. function authToken(res){
  252. if(res.code==300){
  253. wx.reLaunch({
  254. url: '../denglu/denglu',
  255. })
  256. }
  257. }