App.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <script>
  2. export default {
  3. globalData: {
  4. serverUrl: 'https://mdyg.sdmingde.cn/api/h5/',
  5. uploadServerUrl: 'https://mdyg.sdmingde.cn/api/h5/',
  6. appId: 'wx2f0195c58ad8c3ec',
  7. redirectUri: 'https://mdyg.sdmingde.cn/wap/visitor/#/pages/index/wechat',
  8. storagePre: 'visit_',
  9. host: 'https://mdyg.sdmingde.cn/wap/visitor/',
  10. userinfo:{}
  11. },
  12. onLaunch: function() {
  13. console.log('App Launch')
  14. var app = getApp();
  15. let token = uni.getStorageSync(app.globalData.storagePre+'token');
  16. if(token){
  17. app.globalData.userinfo.token = token;
  18. app.globalData.userinfo.userId = uni.getStorageSync(app.globalData.storagePre+'userId');
  19. }
  20. // app.globalData.userinfo.token = '1914162463525';
  21. // app.globalData.userinfo.userId = 1;
  22. },
  23. onShow: function() {
  24. console.log('App Show')
  25. },
  26. onHide: function() {
  27. console.log('App Hide')
  28. },
  29. methods: {
  30. getUrlCode (name) {
  31. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
  32. },
  33. checkLogin: function(){
  34. var app = getApp();
  35. var token = app.globalData.userinfo.token;
  36. if(!token){
  37. let routes = getCurrentPages();
  38. let curRoute = routes[routes.length - 1].route //获取当前页面路由
  39. let curParam = routes[routes.length - 1].options; //获取路由参数
  40. let orgId = curParam.orgId?curParam.orgId:0;
  41. let type = curParam.type?curParam.type:0;
  42. if(curRoute == 'pages/index/add'&&orgId > 0){
  43. uni.reLaunch({ //关闭所有页面,跳转到闪屏页
  44. url: '/pages/index/wechat?state=' + orgId + '_' + type
  45. })
  46. }else{
  47. uni.reLaunch({ //关闭所有页面,跳转到闪屏页
  48. url: '/pages/index/wechat'
  49. })
  50. }
  51. }
  52. },
  53. checkCode:function(code){
  54. if(code == -100){
  55. uni.showToast({
  56. title: '登录超时,请重新登录',
  57. icon: 'none',
  58. duration: 2000
  59. })
  60. var app = getApp();
  61. uni.removeStorageSync(app.globalData.storagePre+'token');
  62. uni.removeStorageSync(app.globalData.storagePre+'userId');
  63. setTimeout(function(){
  64. uni.reLaunch({ //关闭所有页面,打开到应用内的某个页面
  65. url: '/pages/index/wechat'
  66. })
  67. },2000);
  68. return false;
  69. }
  70. return true;
  71. },
  72. ajax: function(params){
  73. if (!params.type) {
  74. var type = 'POST';
  75. }else{
  76. var type = params.type;
  77. }
  78. if (!params.apiname) {
  79. var apiname = '1';
  80. } else {
  81. var apiname = params.apiname;
  82. }
  83. if (!params.data) {
  84. var data = [];
  85. }else{
  86. var data = params.data;
  87. }
  88. var app = getApp();
  89. var token = app.globalData.userinfo.token;
  90. var userId = app.globalData.userinfo.userId;
  91. if (!token || token == undefined) { //未登录
  92. uni.showToast({
  93. title: '未登录',
  94. icon: 'none',
  95. duration: 2000
  96. });
  97. uni.removeStorageSync(app.globalData.storagePre+'token');
  98. uni.removeStorageSync(app.globalData.storagePre+'userId');
  99. setTimeout(function () {
  100. uni.reLaunch({ //关闭所有页面,跳转到闪屏页
  101. url: '/pages/index/wechat'
  102. })
  103. }, 2000);
  104. return false;
  105. }
  106. data['token'] = token;
  107. data['userId'] = userId;
  108. uni.showLoading({
  109. title: '加载中',
  110. mask: true
  111. });
  112. uni.request({
  113. url: params.url,
  114. data: data,
  115. method: type,
  116. header: {
  117. 'content-type': 'application/x-www-form-urlencoded'
  118. },
  119. success: function (res) {
  120. uni.hideLoading();
  121. uni.stopPullDownRefresh();
  122. var ret = app.checkCode(res.data.code); //检查token是否失效
  123. if(ret == false){
  124. return false;
  125. }
  126. if (res.data.code == 0) {
  127. if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖
  128. res.data['apiname'] = apiname;
  129. app.ajaxReadyCallback(res)
  130. }
  131. } else {
  132. uni.showToast({
  133. title: res.data.message,
  134. icon: 'none',
  135. duration: 2000
  136. })
  137. }
  138. },
  139. fail: function (err) {
  140. uni.hideLoading();
  141. uni.showToast({
  142. title: '请求失败',
  143. icon: 'none',
  144. duration: 2000
  145. })
  146. }
  147. })
  148. }
  149. }
  150. }
  151. </script>
  152. <style>
  153. /*每个页面公共css */
  154. page{
  155. background-color: #F3F3F3;
  156. }
  157. .no-data{
  158. width: 100%;
  159. height: auto;
  160. margin: 0 auto;
  161. margin-top: 20%;
  162. text-align: center;
  163. }
  164. .no-data image{
  165. width: 430rpx;
  166. height: 316rpx;
  167. }
  168. .no-data .no-data-text{
  169. color: #C5C8D9;
  170. font-size: 25rpx;
  171. }
  172. </style>