routeJump.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import * as utils from './utils.js';
  2. import store from '../store/index.js';
  3. import user from '../core/user.js';
  4. import { clearStorage } from '../core/cache.js';
  5. const jump = function(data) {
  6. console.log(data);
  7. if (!data.open_type && !data.params && !data.page_url) return;
  8. let open_type = data.open_type;
  9. let params = data.params;
  10. let page_url = data.page_url;
  11. if (!Array.isArray(params) && Object.prototype.toString.call(params) ==="[object String]" && params) {
  12. params = JSON.parse(params);
  13. }
  14. switch (open_type) {
  15. case 'reLaunch':
  16. uni.reLaunch({
  17. url: params[0].value,
  18. });
  19. break;
  20. case 'redirect':
  21. uni.redirectTo({
  22. url: params[0].value,
  23. });
  24. break;
  25. case 'navigate':
  26. let new_page_url = page_url.split('?')[0];
  27. let options = `?`;
  28. // console.log(page_url.split('?'));
  29. // if (page_url.split('?').length > 1) {
  30. // options = '&'
  31. // }
  32. for (let i = 0; i < params.length; i++) {
  33. options += `${params[i].key}=${params[i].value}&`;
  34. }
  35. // console.log(options);
  36. new_page_url += options.slice(0, options.length - 1);
  37. if (options === '?') {
  38. new_page_url = page_url;
  39. }
  40. console.log(new_page_url);
  41. /* #ifdef MP-BAIDU || MP-TOUTIAO */
  42. if (page_url.split('?')[0] !== '/plugins/step/index/index') {
  43. uni.navigateTo({
  44. url: new_page_url
  45. });
  46. }
  47. /* #endif */
  48. /* #ifdef MP-WEIXIN || MP-ALIPAY */
  49. uni.navigateTo({
  50. url: new_page_url
  51. });
  52. /* #endif */
  53. break;
  54. case 'app_admin':
  55. if (store.state.user.info.identity.is_admin == 1) {
  56. uni.navigateTo({
  57. url: data.url,
  58. });
  59. }
  60. break;
  61. case 'back':
  62. uni.navigateBack({});
  63. break;
  64. case 'tel':
  65. uni.makePhoneCall({
  66. phoneNumber: params[0].value,
  67. });
  68. break;
  69. case 'web':
  70. uni.navigateTo({
  71. url: `${page_url.split('?')[0]}?url=${encodeURIComponent(params[0].value)}`
  72. });
  73. break;
  74. case 'app':
  75. let originalPath = '';
  76. for (let i = 0; i < params.length; i++) {
  77. originalPath += `${params[i].key}=${params[i].value}&`;
  78. }
  79. if (typeof originalPath !== 'string') return;
  80. let groups = originalPath.split('&');
  81. let obj = {};
  82. for (let i in groups) {
  83. if (typeof groups[i] !== 'string') continue;
  84. if (!groups[i].length) continue;
  85. let kvs = groups[i].split('=');
  86. if (kvs.length !== 2) {
  87. let s = '';
  88. for (let i = 1; i < kvs.length; i++) {
  89. if (i+1 === kvs.length) {
  90. continue;
  91. } else {
  92. s += `${kvs[i]}=${kvs[i+1]}`
  93. }
  94. }
  95. obj[kvs[0]] = s;
  96. } else {
  97. obj[kvs[0]] = kvs[1];
  98. }
  99. }
  100. let new_params = obj;
  101. let appId = '';
  102. let path = '';
  103. // #ifdef MP-WEIXIN
  104. appId = new_params.app_id || '';
  105. path = new_params.path || '';
  106. // #endif
  107. // #ifdef MP-ALIPAY
  108. appId = new_params.ali_app_id || '';
  109. path = new_params.ali_path || '';
  110. // #endif
  111. // #ifdef MP-TOUTIAO
  112. appId = new_params.tt_app_id || '';
  113. path = new_params.tt_path || '';
  114. // #endif
  115. // #ifdef MP-BAIDU
  116. appId = new_params.bd_app_key || '';
  117. path = new_params.bd_path || '';
  118. // #endif
  119. console.log(path);
  120. uni.navigateToMiniProgram({
  121. appId: appId,
  122. path: path,
  123. success(e) {
  124. console.log('打开小程序成功', e);
  125. },
  126. fail(e) {
  127. console.log('打开小程序失败', e);
  128. }
  129. });
  130. break;
  131. case 'clear_cache':
  132. uni.showModal({
  133. title: '提示',
  134. content: '确认清理缓存?',
  135. cancelText: '取消',
  136. confirmText: '确认',
  137. success: (e) => {
  138. if (e.confirm) {
  139. uni.showLoading({
  140. title: '清理缓存...',
  141. });
  142. clearStorage();
  143. if (user && store && store.state.user.accessToken) {
  144. user.loginByToken(store.state.user.accessToken);
  145. }
  146. store.dispatch('mallConfig/actionResetConfig');
  147. uni.hideLoading();
  148. uni.showToast({
  149. title: '清理完成',
  150. duration: 1000,
  151. });
  152. }
  153. },
  154. });
  155. break;
  156. case 'map':
  157. // uni.openLocation({
  158. // latitude: Number(this.latitude),
  159. // longitude: Number(this.longitude),
  160. // name: this.address,
  161. // address: this.address,
  162. // success: function () {
  163. // },
  164. // fail: function () {
  165. // }
  166. // });
  167. break;
  168. // case 'share':
  169. }
  170. };
  171. export default jump;