app.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // app.js
  2. App({
  3. //全局变量
  4. globalData: {
  5. //服务器地址
  6. serverUrl: 'https://wy.dazhengyun.com/api/h5/',
  7. h5Url: 'https://wy.dazhengyun.com/public/wap/ph2',
  8. appVersion: '1.0.0',
  9. platform: 'wxapp',
  10. userinfo:{}
  11. },
  12. onLaunch: function (options) {
  13. // let orgId = 3
  14. // wx.setStorageSync("orgId", orgId);
  15. this.getScene(options);
  16. },
  17. getScene(options){
  18. const scene = decodeURIComponent(options.scene);
  19. let ss = scene.split('-');
  20. if(ss.length == 2){
  21. var orgId = wx.getStorageSync("orgId");
  22. if(!orgId || (orgId && Number(orgId) != Number(ss[1]))){
  23. wx.setStorageSync("orgId", ss[1]);
  24. this.logout();
  25. }
  26. }
  27. var orgId = wx.getStorageSync("orgId");
  28. if(!orgId){
  29. var orgId = 3;
  30. wx.setStorageSync("orgId", 3);
  31. }
  32. },
  33. getCommonParam(){
  34. var orgId = wx.getStorageSync("orgId");
  35. var userId = wx.getStorageSync("userId");
  36. var token = wx.getStorageSync("token");
  37. return "orgId="+orgId+"&userId="+userId+"&token="+token;
  38. },
  39. logout(){
  40. wx.removeStorageSync("token");
  41. wx.removeStorageSync("userId");
  42. },
  43. checkCode:function(code){
  44. if(code == -100){
  45. wx.showToast({
  46. title: '登录超时,请重新登录',
  47. icon: 'none',
  48. duration: 2000
  49. })
  50. this.logout();
  51. setTimeout(function(){
  52. wx.reLaunch({ //关闭所有页面,打开到应用内的某个页面
  53. url: '/pages/splash/splash'
  54. })
  55. },2000);
  56. return false;
  57. }
  58. return true;
  59. },
  60. ajax:function(params){
  61. console.log('params',params)
  62. if (!params.type) {
  63. var type = 'POST';
  64. }else{
  65. var type = params.type;
  66. }
  67. if (!params.apiname) {
  68. var apiname = '1';
  69. } else {
  70. var apiname = params.apiname;
  71. }
  72. if (!params.data) {
  73. var data = [];
  74. }else{
  75. var data = params.data;
  76. }
  77. var app = getApp();
  78. var userId = wx.getStorageSync("userId");
  79. var token = wx.getStorageSync("token");
  80. var orgId = wx.getStorageSync("orgId");
  81. console.log('params2',userId,token,token);
  82. if (!userId || userId == undefined) { //未登录
  83. wx.showToast({
  84. title: '未登录',
  85. icon: 'none',
  86. duration: 2000
  87. });
  88. this.logout();
  89. setTimeout(function () {
  90. wx.reLaunch({ //关闭所有页面,跳转到闪屏页
  91. url: '/pages/splash/splash'
  92. })
  93. }, 2000);
  94. return false;
  95. }
  96. data['userId'] = userId;
  97. data['token'] = token;
  98. data['orgId'] = orgId;
  99. console.log('params3',data);
  100. wx.showLoading({
  101. title: '加载中',
  102. mask: true
  103. });
  104. wx.request({
  105. url: params.url,
  106. data: data,
  107. method: type,
  108. header: {
  109. 'content-type': 'application/x-www-form-urlencoded'
  110. },
  111. success: function (res) {
  112. console.log('params4',res);
  113. wx.hideLoading();
  114. wx.stopPullDownRefresh();
  115. var ret = app.checkCode(res.data.code); //检查token是否失效
  116. if(ret == false){
  117. return false;
  118. }
  119. if (res.data.code == 0) {
  120. if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖
  121. res.data['apiname'] = apiname;
  122. app.ajaxReadyCallback(res)
  123. }
  124. } else {
  125. let showMsg = true;
  126. if(params.showMsg != undefined){
  127. showMsg = params.showMsg;
  128. }
  129. if(showMsg){
  130. wx.showToast({
  131. title: res.data.message,
  132. icon: 'none',
  133. duration: 2000
  134. })
  135. }
  136. }
  137. },
  138. fail: function (err) {
  139. wx.hideLoading();
  140. wx.showToast({
  141. title: '请求失败',
  142. icon: 'none',
  143. duration: 2000
  144. })
  145. }
  146. })
  147. },
  148. })