app.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // app.js
  2. App({
  3. onLaunch() {
  4. var _this=this;
  5. //小程序初始化时获取token缓存
  6. var token=wx.getStorageSync('token');
  7. _this.globalData.token=token;
  8. this.checkToken(token);
  9. },
  10. globalData: {
  11. userInfo: null,
  12. rootUrl:"http://localhost:8080/eorder/app",
  13. fileUrl:"http://localhost:8080/eorder/file",
  14. staticUrl:"http://localhost:8080/eorder/static",
  15. serverPrefix:"http://localhost:8080",
  16. token:''
  17. },
  18. checkToken(token){
  19. if(!token)
  20. return;
  21. var _this=this;
  22. wx.request({
  23. url: this.globalData.rootUrl+'/user/token/check',
  24. method:"GET",
  25. header:{
  26. token:token
  27. },
  28. success(e){
  29. if(!e.data.success){
  30. _this.showErrMsg("登录状态失效")
  31. wx.removeStorageSync('token')
  32. wx.removeStorageSync('userInfo')
  33. }
  34. }
  35. })
  36. },
  37. /**
  38. * 显示成功消息
  39. * @param {*} msg
  40. */
  41. showMsg(msg){
  42. wx.showToast({
  43. title: msg,
  44. icon:'success'
  45. })
  46. },
  47. /**
  48. * 显示错误消息
  49. * @param {*} msg
  50. */
  51. showErrMsg(msg){
  52. wx.showToast({
  53. title: msg,
  54. icon:'error'
  55. })
  56. },
  57. /**
  58. *
  59. * @param {显示加载} msg
  60. */
  61. showLoading(msg){
  62. wx.showLoading({
  63. title: msg,
  64. })
  65. },
  66. //获取token
  67. getToken() {
  68. var token = wx.getStorageSync('token');
  69. return token;
  70. },
  71. /**
  72. * 判断是否是url
  73. */
  74. testUrl(url){
  75. var reg='(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]'
  76. var re=new RegExp(reg)
  77. return re.test(url);
  78. }
  79. })