httputil.js 6.8 KB

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