httputil.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. wx.request({
  72. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  73. method: "POST",
  74. header: {
  75. 'content-type': 'application/json',
  76. 'Authorization': 'Bearer ' + app.globalToken
  77. },
  78. data: data,
  79. success(res) {
  80. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  81. // var json = JSON.parse(d)
  82. console.log("TAG","返回数据:"+res.data)
  83. authToken(res.data)
  84. successfun(res.data)
  85. },
  86. fail(res){
  87. wx.showToast({
  88. title: "网络请求失败",
  89. })
  90. }
  91. })
  92. }
  93. function post(url, data, successfun) {
  94. wx.request({
  95. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  96. method: "POST",
  97. header: {
  98. 'content-type': 'application/x-www-form-urlencoded',
  99. 'Authorization': 'Bearer ' + app.globalToken
  100. },
  101. data: data,
  102. success(res) {
  103. successfun(res.data)
  104. },
  105. fail(res){
  106. wx.showToast({
  107. title: "网络请求失败",
  108. })
  109. }
  110. })
  111. }
  112. function get(url, data, successfun) {
  113. wx.request({
  114. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  115. method: "GET",
  116. header: {
  117. 'content-type': 'application/x-www-form-urlencoded',
  118. 'Authorization': 'Bearer ' + app.globalToken
  119. },
  120. params: data,
  121. success(res) {
  122. successfun(res.data)
  123. },
  124. fail(res){
  125. wx.showToast({
  126. title: "网络请求失败",
  127. })
  128. }
  129. })
  130. }
  131. function send_get(url, data, successfun) {
  132. console.log("令牌:", app.globalToken)
  133. wx.request({
  134. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  135. method: "GET",
  136. header: {
  137. 'content-type': 'application/json',
  138. 'Authorization': 'Bearer ' + app.globalToken
  139. },
  140. data: data,
  141. success(res) {
  142. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  143. // var json = JSON.parse(d)
  144. console.log("TAG","返回数据:"+res.data)
  145. authToken(res.data)
  146. successfun(res.data)
  147. },
  148. fail(res){
  149. wx.showToast({
  150. title: "网络请求失败",
  151. })
  152. }
  153. })
  154. }
  155. function send_postdecode(url, data, successfun) {
  156. wx.request({
  157. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  158. method: "POST",
  159. header: {
  160. 'content-type': 'application/x-www-form-urlencoded'
  161. },
  162. data: data,
  163. success(res) {
  164. var json = JSON.parse(res.data)
  165. successfun(json)
  166. }
  167. })
  168. }
  169. function send_photo(data, successfun) {
  170. var that = this
  171. for (let i = 0; i < data.length; i++) {
  172. console.log("data长度=" + data.length)
  173. console.log(data)
  174. wx.uploadFile({
  175. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  176. filePath: data[i] + "",
  177. name: 'file',
  178. success: function (res) {
  179. var json = JSON.parse(res.data)
  180. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  181. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  182. successfun(sss)
  183. }
  184. })
  185. }
  186. }
  187. function send(data, successfun) {
  188. var that = this
  189. wx.uploadFile({
  190. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  191. filePath: data[0] + "",
  192. name: 'file',
  193. success: function (res) {
  194. var json = JSON.parse(res.data)
  195. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  196. // wx.setStorageSync("carnumber", sss.data.carNumber)
  197. successfun(sss)
  198. },
  199. fail: function (res) {}
  200. })
  201. }
  202. module.exports = {
  203. BASE_Server: BASE_Server,
  204. send_post: send_post,
  205. send_photo: send_photo,
  206. send: send,
  207. post: post,
  208. // wxpost: wxpost,
  209. get: get,
  210. send_get:send_get,
  211. send_postdecode: send_postdecode,
  212. // send_post_login,
  213. showLoading,
  214. hideLoading,
  215. }
  216. function showLoading() {
  217. wx.showLoading({
  218. title: '加载中...',
  219. mask: true,
  220. })
  221. }
  222. function hideLoading() {
  223. wx.hideLoading()
  224. }
  225. function authToken(res){
  226. if(res.code==401){//登录token过期状态码
  227. wx.clearStorage({
  228. success:(res)=>{
  229. wx.reLaunch({
  230. url: '../denglu/denglu',
  231. })
  232. }
  233. })
  234. }
  235. }
  236. // function wxpost(url, data, successfun) {
  237. // wx.request({
  238. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  239. // method: "POST",
  240. // header: {
  241. // 'content-type': 'application/json',
  242. // 'Authorization': 'Bearer ' + app.globalToken
  243. // },
  244. // data: data,
  245. // success(res) {
  246. // successfun(res.data)
  247. // }
  248. // })
  249. // }