httputil.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // let BASE_Server = "http://127.0.0.1:8329"
  2. let BASE_Server = "http://192.168.1.100:8080"
  3. let BASE_Server_Login = "http://192.168.1.100:9200"
  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. console.log(1111111)
  26. wx.getUserProfile({
  27. desc: 'desc',
  28. success: (res) => {
  29. getSysUserInfo(res.userInfo)
  30. },
  31. fail: res => {
  32. console.log(res)
  33. }
  34. })
  35. }
  36. }
  37. function getSysUserInfo(info) {
  38. console.log(2222)
  39. let that = this
  40. wx.login({
  41. success(res) {
  42. var code = res.code
  43. var data = {
  44. wxCode: code,
  45. wxNickName: info.nickName,
  46. wxAvatarUrl: info.avatarUrl
  47. }
  48. // http.send_post_login("/minapp/AppLoginController/appLogin",data)
  49. send_post_login("/applogin", data, loginSuccess)
  50. },
  51. fail(res) {
  52. console.log("ffff", res)
  53. }
  54. })
  55. }
  56. function loginSuccess(res) {
  57. if (res.data.code != 200) {
  58. wx.showToast({
  59. title: res.data.msg,
  60. icon: "none"
  61. })
  62. } else {
  63. app.globalToken = res.data.data.access_token
  64. //登录成功,获取首页数据
  65. this.initIndexPage()
  66. }
  67. console.log("!!!!!", app.globalToken)
  68. }
  69. //------------------------------------------------------------------
  70. function send_post(url, data, successfun) {
  71. console.log("令牌:", app.globalToken)
  72. //如果令牌为空或过期,需要重新请求
  73. wx.request({
  74. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  75. method: "POST",
  76. header: {
  77. 'content-type': 'application/json',
  78. 'Authorization': 'Bearer ' + app.globalToken
  79. },
  80. data: data,
  81. success(res) {
  82. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  83. // var json = JSON.parse(d)
  84. successfun(res)
  85. }
  86. })
  87. }
  88. function post(url, data, successfun) {
  89. wx.request({
  90. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  91. method: "POST",
  92. header: {
  93. 'content-type': 'application/x-www-form-urlencoded',
  94. 'Authorization': 'Bearer ' + app.globalToken
  95. },
  96. data: data,
  97. success(res) {
  98. successfun(res.data)
  99. }
  100. })
  101. }
  102. function wxpost(url, data, successfun) {
  103. wx.request({
  104. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  105. method: "POST",
  106. header: {
  107. 'content-type': 'application/json',
  108. 'Authorization': 'Bearer ' + app.globalToken
  109. },
  110. data: data,
  111. success(res) {
  112. successfun(res.data)
  113. }
  114. })
  115. }
  116. function get(url, data, successfun) {
  117. wx.request({
  118. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  119. method: "Get",
  120. header: {
  121. 'content-type': 'application/x-www-form-urlencoded'
  122. },
  123. params: data,
  124. success(res) {
  125. successfun(res.data)
  126. }
  127. })
  128. }
  129. function send_postdecode(url, data, successfun) {
  130. wx.request({
  131. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  132. method: "POST",
  133. header: {
  134. 'content-type': 'application/x-www-form-urlencoded'
  135. },
  136. data: data,
  137. success(res) {
  138. var json = JSON.parse(res.data)
  139. successfun(json)
  140. }
  141. })
  142. }
  143. function send_photo(data, successfun) {
  144. var that = this
  145. for (let i = 0; i < data.length; i++) {
  146. console.log("data长度=" + data.length)
  147. console.log(data)
  148. wx.uploadFile({
  149. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  150. filePath: data[i] + "",
  151. name: 'file',
  152. success: function (res) {
  153. var json = JSON.parse(res.data)
  154. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  155. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  156. successfun(sss)
  157. }
  158. })
  159. }
  160. }
  161. function send(data, successfun) {
  162. var that = this
  163. wx.uploadFile({
  164. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  165. filePath: data[0] + "",
  166. name: 'file',
  167. success: function (res) {
  168. var json = JSON.parse(res.data)
  169. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  170. // wx.setStorageSync("carnumber", sss.data.carNumber)
  171. successfun(sss)
  172. },
  173. fail: function (res) {}
  174. })
  175. }
  176. module.exports = {
  177. BASE_Server: BASE_Server,
  178. send_post: send_post,
  179. send_photo: send_photo,
  180. send: send,
  181. post: post,
  182. wxpost: wxpost,
  183. get: get,
  184. send_postdecode: send_postdecode,
  185. send_post_login,
  186. showLoading,
  187. hideLoading,
  188. }
  189. function showLoading() {
  190. wx.showLoading({
  191. title: '加载中...',
  192. mask: true,
  193. })
  194. }
  195. function hideLoading() {
  196. wx.hideLoading()
  197. }