index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var handle_1 = require("./handle");
  15. var config_1 = require("./config");
  16. var config_2 = require("../config/config");
  17. exports.default = {
  18. navigateBack: function (data) {
  19. return uni.navigateBack(data);
  20. },
  21. switchTab: function (data) {
  22. return this.router('switchTab', data, false);
  23. },
  24. reLaunch: function (data) {
  25. return this.router('reLaunch', data);
  26. },
  27. navigateTo: function (data) {
  28. return this.router('navigateTo', data);
  29. },
  30. redirectTo: function (data) {
  31. return this.router('redirectTo', data);
  32. },
  33. autoRouter: function (data, defaultMode) {
  34. if (defaultMode === void 0) { defaultMode = 'navigateTo'; }
  35. var resultData;
  36. if (typeof data === 'string') {
  37. resultData = { url: data };
  38. }
  39. else {
  40. resultData = data;
  41. }
  42. var mode = undefined;
  43. if (typeof data === 'string') {
  44. var url = resultData.url;
  45. var urls = url.split('?') || [];
  46. resultData.params = urls[1];
  47. url = urls[0] || '';
  48. if (url.indexOf('/') === 0) {
  49. url = url.substr(1);
  50. }
  51. urls = url.split('/') || [];
  52. url = urls[urls.length - 1] || '';
  53. resultData.name = url;
  54. if (config_1.default.tabBar.indexOf(url) >= 0) {
  55. mode = 'switchTab';
  56. }
  57. }
  58. console.log(mode || defaultMode, resultData);
  59. return this.router(mode || defaultMode, resultData);
  60. },
  61. getLivePath: function (roomId, params) {
  62. var path = config_1.default.live_path;
  63. var useParams = [];
  64. if (roomId) {
  65. useParams.push('room_id=' + roomId);
  66. }
  67. if (params) {
  68. var toStringParams = '';
  69. if (typeof params === 'object')
  70. toStringParams = JSON.stringify(params);
  71. useParams.push('custom_params=' + encodeURIComponent(toStringParams));
  72. }
  73. if (useParams.length > 0) {
  74. path += '?' + useParams.join('&');
  75. }
  76. return path;
  77. },
  78. getMapPath: function (params) {
  79. var resultParams;
  80. if (typeof params === "string") {
  81. resultParams = params;
  82. }
  83. else {
  84. resultParams = this.getParamsString(__assign(__assign({}, params), config_2.default.map));
  85. }
  86. return config_1.default.map_path + resultParams;
  87. },
  88. routerParams: {
  89. next: true,
  90. delay: config_1.default.routerDelay,
  91. },
  92. router: function (name, data, params) {
  93. var _this = this;
  94. if (params === void 0) { params = true; }
  95. if (this.routerParams.next === false)
  96. return false;
  97. this.routerParams.next = false;
  98. setTimeout(function () {
  99. _this.routerParams.next = true;
  100. }, this.routerParams.delay);
  101. var resultData;
  102. if (typeof data === 'string') {
  103. resultData = { name: data, url: '' };
  104. }
  105. else {
  106. resultData = data;
  107. }
  108. resultData.url = resultData.path || resultData.name || '';
  109. if (resultData.name && handle_1.default[resultData.name]) {
  110. resultData = handle_1.default[resultData.name].call(this, data);
  111. }
  112. else if (resultData.url.indexOf('/') < 0) {
  113. resultData.url = '/pages/' + resultData.url + '/' + resultData.url;
  114. }
  115. if (resultData.subcontract === undefined) {
  116. resultData.subcontract = this.inferSubPages(resultData.url);
  117. }
  118. if (resultData.subcontract) {
  119. resultData.url = '/' + resultData.subcontract + resultData.url;
  120. }
  121. if (resultData.params && params) {
  122. resultData.url += this.getParamsString(resultData.params);
  123. delete resultData.params;
  124. }
  125. if (resultData.memoryParams) {
  126. this.memorySet(resultData.name || this.getRouterName(resultData.url), resultData.memoryParams);
  127. }
  128. return uni[name](resultData);
  129. },
  130. getParamsString: function (params) {
  131. if (params) {
  132. if (typeof params === 'string') {
  133. if (params.indexOf('?') === 0) {
  134. return params;
  135. }
  136. else {
  137. return '?' + params;
  138. }
  139. }
  140. var params_string = '';
  141. for (var key in params) {
  142. if (params.hasOwnProperty(key)) {
  143. params_string += (params_string ? '&' : '?') + key + '=' + (typeof params[key] === 'object' ? JSON.stringify(params[key]) : params[key]);
  144. }
  145. }
  146. return params_string;
  147. }
  148. else {
  149. return '';
  150. }
  151. },
  152. getRouterName: function (path) {
  153. if (path === void 0) { path = ''; }
  154. return path.split('/').pop();
  155. },
  156. inferSubPages: function (url) {
  157. url = url || '';
  158. var urls = url.split('/');
  159. var name = urls[urls.length - 1];
  160. if (config_1.default.subcontractIndexes[name]) {
  161. return config_1.default.subcontract[config_1.default.subcontractIndexes[name]];
  162. }
  163. else {
  164. return undefined;
  165. }
  166. },
  167. memoryData: {},
  168. memoryGet: function (name) {
  169. if (this.memoryData[name]) {
  170. var resultData = this.memoryData[name];
  171. delete this.memoryData[name];
  172. return resultData;
  173. }
  174. else {
  175. return undefined;
  176. }
  177. },
  178. memorySet: function (name, params) {
  179. this.memoryData[name] = params;
  180. }
  181. };