vk.navigate.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * 函数 - 页面导航
  3. */
  4. // #ifndef VUE3
  5. var config;
  6. try {
  7. config = require('@/app.config.js');
  8. if (typeof config.default === "object") {
  9. config = config.default;
  10. }
  11. } catch (e) {
  12. config = {};
  13. }
  14. // #endif
  15. // #ifdef VUE3
  16. import config from '@/app.config.js'
  17. // #endif
  18. var util = {};
  19. /**
  20. * 保留当前页面,跳转到应用内的某个页面,使用vk.navigateBack可以返回到原页面。
  21. * vk.navigateTo(url);
  22. */
  23. util.navigateTo = function(obj) {
  24. let vk = uni.vk;
  25. if (typeof obj == "string") {
  26. let url = obj;
  27. obj = {
  28. url: url
  29. };
  30. } else if (typeof obj == "undefined") {
  31. obj = {};
  32. }
  33. if (!obj.url) {
  34. vk.toast("url不能为空!");
  35. return false;
  36. }
  37. util.checkNeedLogin({
  38. url: obj.url,
  39. success: function(res) {
  40. if (res.needLogin) {
  41. obj.url = vk.pubfn.getPageFullPath(obj.url);
  42. vk.navigate.originalPage = vk.pubfn.copyObject(obj);
  43. obj.url = config.login.url;
  44. // login拦截器开始-----------------------------------------------------------
  45. let { interceptor = {} } = config;
  46. if (typeof interceptor.login === "function") {
  47. let key = interceptor.login({
  48. vk,
  49. params: obj,
  50. res:{
  51. ...res,
  52. code: 30204,
  53. msg: "本地token校验未通过"
  54. }
  55. });
  56. if (typeof key === "undefined" || key !== true) return false;
  57. }
  58. // login拦截器结束-----------------------------------------------------------
  59. } else {
  60. vk.navigate.originalPage = null;
  61. }
  62. util._navigateTo(obj);
  63. }
  64. });
  65. };
  66. util._navigateTo = function(obj) {
  67. let { interceptor = {} } = config;
  68. if (typeof interceptor.navigateTo === "function") {
  69. let vk = uni.vk;
  70. obj.pagePath = vk.pubfn.getPageFullPath(obj.url);
  71. let key = interceptor.navigateTo({
  72. ...obj,
  73. vk
  74. });
  75. if (typeof key == "boolean" && key === false) return false;
  76. }
  77. let {
  78. url,
  79. animationType = "pop-in",
  80. animationDuration = 300,
  81. events,
  82. mode = "navigateTo"
  83. } = obj;
  84. // 此处写法仅为支持vue3,vue3不支持uni[apiName]的形式调用
  85. let navigateFn;
  86. if (mode === "navigateTo") {
  87. navigateFn = uni.navigateTo;
  88. } else if (mode === "redirectTo") {
  89. navigateFn = uni.redirectTo;
  90. } else if (mode === "reLaunch") {
  91. navigateFn = uni.reLaunch;
  92. } else if (mode === "switchTab") {
  93. navigateFn = uni.switchTab;
  94. } else {
  95. navigateFn = uni.navigateTo;
  96. }
  97. // 此处写法仅为支持vue3,vue3不支持uni[apiName]的形式调用
  98. navigateFn({
  99. url: url,
  100. animationType: animationType,
  101. animationDuration: animationDuration,
  102. events: events, // 参考 https://uniapp.dcloud.io/api/router?id=navigateto
  103. success: function(res) {
  104. if (typeof obj.success == "function") obj.success(res);
  105. },
  106. fail: function(err) {
  107. if (err.errMsg.indexOf("not found") > -1) {
  108. let vk = uni.vk;
  109. let errUrl = vk.pubfn.getPageFullPath(url);
  110. vk.toast(`页面 ${errUrl} 不存在`, "none");
  111. console.error(err);
  112. return false;
  113. }
  114. uni.switchTab({
  115. url: url,
  116. success: obj.success,
  117. fail: function() {
  118. uni.redirectTo({
  119. url: url,
  120. success: obj.success,
  121. fail: function(err) {
  122. console.error(err);
  123. if (typeof obj.fail == "function") obj.fail(err);
  124. }
  125. });
  126. }
  127. });
  128. },
  129. complete: function(res) {
  130. if (typeof obj.complete == "function") obj.complete(res);
  131. }
  132. });
  133. };
  134. /**
  135. * 关闭当前页面,跳转到应用内的某个页面。
  136. * vk.redirectTo(url);
  137. */
  138. util.redirectTo = function(obj) {
  139. obj = util.paramsInit(obj);
  140. obj.mode = "redirectTo";
  141. util.navigateTo(obj);
  142. };
  143. /**
  144. * 关闭所有页面,打开到应用内的某个页面。
  145. * vk.reLaunch(url);
  146. */
  147. util.reLaunch = function(obj) {
  148. obj = util.paramsInit(obj);
  149. obj.mode = "reLaunch";
  150. util.navigateTo(obj);
  151. };
  152. /**
  153. * 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
  154. * vk.switchTab(url);
  155. */
  156. util.switchTab = function(obj) {
  157. obj = util.paramsInit(obj);
  158. obj.mode = "switchTab";
  159. util.navigateTo(obj);
  160. };
  161. /**
  162. * 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
  163. * vk.navigateBack();
  164. */
  165. util.navigateBack = function(obj) {
  166. let vk = uni.vk;
  167. if (typeof obj == "number") {
  168. let delta = obj;
  169. obj = {
  170. delta: delta
  171. };
  172. } else if (typeof obj == "undefined") {
  173. obj = {};
  174. }
  175. let {
  176. delta = 1,
  177. animationType = "pop-out",
  178. animationDuration = 300
  179. } = obj;
  180. uni.navigateBack({
  181. delta: delta,
  182. animationType: animationType,
  183. animationDuration: animationDuration,
  184. success: function() {
  185. if (typeof obj.success == "function") obj.success();
  186. },
  187. fail: function(res) {
  188. console.error(res);
  189. if (typeof obj.fail == "function") obj.fail();
  190. },
  191. complete: function() {
  192. if (typeof obj.complete == "function") obj.complete();
  193. }
  194. });
  195. };
  196. /**
  197. * 跳转到登录前的页面
  198. * vk.navigate.originalTo();
  199. */
  200. util.originalTo = function() {
  201. let vk = uni.vk;
  202. let originalPage = vk.pubfn.copyObject(vk.navigate.originalPage);
  203. vk.navigate.originalPage = null;
  204. util.redirectTo(originalPage);
  205. };
  206. /**
  207. * 跳转到首页
  208. * vk.navigateToHome();
  209. */
  210. util.navigateToHome = function(obj = {}) {
  211. let vk = uni.vk;
  212. let {
  213. mode = "reLaunch"
  214. } = obj;
  215. vk[mode](config.index.url);
  216. };
  217. /**
  218. * 跳转到登录页
  219. * vk.navigateToLogin();
  220. */
  221. util.navigateToLogin = function(obj = {}) {
  222. let vk = uni.vk;
  223. let {
  224. mode = "reLaunch"
  225. } = obj;
  226. vk[mode](config.login.url);
  227. };
  228. /**
  229. * 检测是否满足条件(内部方法)
  230. util.checkWildcardTest({
  231. url:url,
  232. pagesRule:config.checkTokenPages,
  233. success:function(res){
  234. if(res.success){
  235. }
  236. }
  237. })
  238. */
  239. util.checkWildcardTest = function(obj) {
  240. let vk = uni.vk;
  241. let {
  242. url,
  243. pagesRule
  244. } = obj;
  245. // ../ 转成绝对路径
  246. url = vk.pubfn.getPageFullPath(url);
  247. let key = false;
  248. if (vk.pubfn.isNotNull(pagesRule)) {
  249. let { mode = 0, list = [] } = pagesRule;
  250. if (mode > 0) {
  251. let regExpKey = false;
  252. let path = util.getPagePath(url);
  253. for (let i = 0; i < list.length; i++) {
  254. let pageRegExp = list[i];
  255. regExpKey = vk.pubfn.wildcardTest(path, pageRegExp);
  256. if (regExpKey) {
  257. break;
  258. }
  259. }
  260. if (mode === 1 && regExpKey) {
  261. key = true;
  262. } else if (mode === 2 && !regExpKey) {
  263. key = true;
  264. }
  265. }
  266. }
  267. return {
  268. url,
  269. key
  270. };
  271. };
  272. /**
  273. * 检测是否需要登录(内部方法)
  274. util.checkNeedLogin({
  275. url:url,
  276. success:function(res){
  277. if(res.needLogin){
  278. }
  279. }
  280. })
  281. */
  282. util.checkNeedLogin = function(obj) {
  283. let vk = uni.vk;
  284. let { url, success } = obj;
  285. let needLogin = false;
  286. let pagesRule = config.checkTokenPages;
  287. if (pagesRule) {
  288. let res = util.checkWildcardTest({
  289. url,
  290. pagesRule
  291. });
  292. if (res.key) {
  293. // 本地判断token有效期(联网会浪费性能)
  294. if (!vk.callFunctionUtil.checkToken()) {
  295. needLogin = true;
  296. }
  297. }
  298. }
  299. success({
  300. url,
  301. needLogin
  302. });
  303. };
  304. // 获取?前面的地址
  305. util.getPagePath = function(url) {
  306. let pathIndex = url.indexOf("?");
  307. let path = url;
  308. if (pathIndex > -1) {
  309. path = path.substring(0, pathIndex);
  310. }
  311. return path;
  312. };
  313. util.paramsInit = function(obj) {
  314. let vk = uni.vk;
  315. if (typeof obj == "string") {
  316. let url = obj;
  317. obj = {
  318. url: url
  319. };
  320. } else if (typeof obj == "undefined") {
  321. obj = {};
  322. }
  323. if (!obj.url) {
  324. vk.toast("url不能为空!");
  325. return false;
  326. }
  327. return obj;
  328. }
  329. /**
  330. * 跳转到小程序
  331. vk.navigateToMiniProgram({
  332. appId: 'appId',
  333. path: 'pages/index/index',
  334. extraData:{
  335. //发送数据携带的参数
  336. },
  337. success(res) {
  338. // 打开成功
  339. }
  340. })
  341. */
  342. util.navigateToMiniProgram = function(obj) {
  343. let vk = uni.vk;
  344. // #ifdef H5
  345. vk.toast("不支持打开小程序", "none");
  346. // #endif
  347. // #ifndef H5
  348. uni.navigateToMiniProgram(obj);
  349. // #endif
  350. };
  351. /**
  352. * 检测是否可以分享(内部方法)
  353. util.checkAllowShare({
  354. url:url,
  355. });
  356. */
  357. util.checkAllowShare = function(obj) {
  358. let vk = uni.vk;
  359. let { url, success } = obj;
  360. let pagesRule = config.checkSharePages || {};
  361. if (pagesRule && pagesRule.mode > 0) {
  362. let res = util.checkWildcardTest({
  363. url,
  364. pagesRule
  365. });
  366. // #ifdef MP
  367. let menus = pagesRule.menus || ['shareAppMessage', 'shareTimeline'];
  368. if (res.key) {
  369. //console.log("允许分享");
  370. uni.showShareMenu({
  371. withShareTicket: false,
  372. menus
  373. });
  374. } else {
  375. //console.log("禁止分享");
  376. uni.hideShareMenu({
  377. menus
  378. })
  379. }
  380. // #endif
  381. }
  382. };
  383. export default util;