App.vue 4.5 KB

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