app.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import * as util from './util'
  2. import storage from "@/utils/storage";
  3. import {deletePageStyle, setPageStyle} from "@/utils/uniFunction";
  4. /**
  5. * 显示成功提示框
  6. */
  7. export const showSuccess = (msg, callback = null, duration = 1500) => {
  8. uni.showToast({
  9. title: `${msg}`,
  10. icon: 'success',
  11. mask: true,
  12. duration,
  13. success() {
  14. if (callback)
  15. setTimeout(() => {
  16. callback()
  17. }, duration)
  18. }
  19. })
  20. }
  21. /**
  22. * 显示失败提示框
  23. */
  24. export const showError = (msg, callback = null, duration = 1500) => {
  25. uni.showModal({
  26. title: '友情提示',
  27. content: `${msg}`,
  28. showCancel: false,
  29. success(res) {
  30. if (callback)
  31. setTimeout(() => {
  32. callback()
  33. }, duration)
  34. },
  35. fail(err) {
  36. console.log('showError fail', err)
  37. },
  38. })
  39. }
  40. /**
  41. * 显示失败提示框
  42. */
  43. export const confirmModal = (msg, options = {}) => {
  44. return new Promise((resolve, reject) => {
  45. let allOptions = Object.assign({
  46. title: '温馨提示',
  47. content: `${msg}`,
  48. showCancel: true,
  49. success: res => {
  50. if (res.confirm) {
  51. resolve(res)
  52. } else {
  53. reject(res)
  54. }
  55. },
  56. fail: res => reject(res)
  57. }, options)
  58. uni.showModal(allOptions)
  59. })
  60. }
  61. /**
  62. * 显示纯文字提示框
  63. */
  64. export const showToast = (msg, duration = 1500) => {
  65. uni.showToast({
  66. title: `${msg}`,
  67. icon: 'none',
  68. duration
  69. })
  70. }
  71. /**
  72. * tabBar页面路径列表 (用于链接跳转时判断)
  73. * tabBarLinks为常量, 无需修改
  74. */
  75. export const getTabBarLinks = () => {
  76. const tabBarLinks = [
  77. // 'pages/index/index',
  78. ]
  79. return tabBarLinks
  80. }
  81. /**
  82. * 生成完整的H5地址 [带参数]
  83. * @param {string} h5Url H5访问地址
  84. * @param {string} path 页面路径
  85. * @param {object} params 页面参数
  86. * @return {string}
  87. */
  88. export const buildUrL = (h5Url, path, params) => {
  89. let complete = h5Url
  90. if (!util.isEmpty(path)) {
  91. complete += '#/' + path
  92. const shareParamsStr = getShareUrlParams(params)
  93. if (!util.isEmpty(shareParamsStr)) {
  94. complete += '?' + shareParamsStr
  95. }
  96. }
  97. return complete
  98. }
  99. /**
  100. * 跳转到指定页面url
  101. * 支持tabBar页面
  102. * @param {string} url
  103. * @param {object} query
  104. */
  105. export const navTo = (url, query = {}, isLogin = false) => {
  106. if (!url || url.length == 0) {
  107. return false
  108. }
  109. if (isLogin && !checkLogin()) {
  110. checkLogin(true)
  111. return;
  112. }
  113. // 生成query参数
  114. const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
  115. // tabBar页面, 使用switchTab
  116. if (util.inArray(url, getTabBarLinks())) {
  117. if (queryStr) {
  118. uni.reLaunch({
  119. url: `/${url}${queryStr}`
  120. })
  121. } else {
  122. uni.switchTab({
  123. url: `/${url}`
  124. })
  125. }
  126. return true
  127. }
  128. // 普通页面, 使用navigateTo
  129. uni.navigateTo({
  130. url: `/${url}${queryStr}`
  131. })
  132. return true
  133. }
  134. /**
  135. * 跳转到指定页面url 并关闭当前页面
  136. * 支持tabBar页面
  137. * @param {string} url
  138. * @param {object} query
  139. * @param type
  140. */
  141. export const redirectTo = (url, query = {}, type = 1) => {
  142. if (!url || url.length == 0) {
  143. return false
  144. }
  145. // tabBar页面, 使用switchTab
  146. if (util.inArray(url, getTabBarLinks())) {
  147. uni.switchTab({
  148. url: `/${url}`
  149. })
  150. return true
  151. }
  152. // 生成query参数
  153. const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
  154. if (type === 1) {
  155. uni.redirectTo({
  156. url: `/${url}${queryStr}`
  157. })
  158. return true
  159. } else if (type === 2) {
  160. uni.reLaunch({
  161. url: `/${url}${queryStr}`
  162. })
  163. return true
  164. }
  165. return true
  166. }
  167. /**
  168. * 验证是否已登录
  169. */
  170. export const checkLogin = (isLogin = false, is_confirm = false) => {
  171. let token = storage.getKey('LOGIN_TEACHER_TOKEN');
  172. if (token) return !!token;
  173. if (isLogin) {
  174. // 要登录
  175. if (is_confirm == true) {
  176. uni.showModal({
  177. title: '温馨提示',
  178. content: '此时此刻需要您登录喔~',
  179. // showCancel: false,
  180. confirmText: "去登录",
  181. cancelText: "再逛会",
  182. success: res => {
  183. if (res.confirm) {
  184. uni.navigateTo({
  185. url: "/pages/auth/index"
  186. })
  187. }
  188. if (res.cancel && getCurrentPages().length > 1) {
  189. uni.navigateBack()
  190. }
  191. }
  192. })
  193. } else {
  194. redirectTo('/pages/auth/index')
  195. }
  196. } else {
  197. // 无需登录
  198. return !!token
  199. }
  200. }
  201. export const navigateBack = (num = 1, callback) => {
  202. uni.navigateBack({
  203. delta: num,
  204. success(res) {
  205. if (callback) callback()
  206. }
  207. })
  208. }
  209. /**
  210. * 设置页面标题
  211. * @param title
  212. */
  213. export const setPageTitle = (title = '标题') => {
  214. uni.setNavigationBarTitle({
  215. title
  216. });
  217. }
  218. /**
  219. * 获取指定页面信息
  220. * @param num
  221. * @returns {Page.PageInstance<AnyObject, {}>}
  222. */
  223. export const getBeforePage = (num = 1) => {
  224. let pages = getCurrentPages(); // 当前页面
  225. let index = pages.length - 1
  226. let beforePage = pages[index - num]; // 前一个页面
  227. return beforePage
  228. }
  229. /**
  230. * 锁定滚动
  231. * @param pageStyle
  232. * @returns {string}
  233. */
  234. export const lockScroll = (pageStyle) => {
  235. return setPageStyle(pageStyle, {
  236. overflow: 'hidden',
  237. 'max-height': '100vh', // h5 问题
  238. })
  239. }
  240. /**
  241. * 解除锁定滚动
  242. * @param pageStyle
  243. * @returns {string}
  244. */
  245. export const unLockScroll = (pageStyle) => {
  246. return deletePageStyle(pageStyle, ['overflow', 'max-height'])
  247. }
  248. export const adMethod = (v) => {
  249. if (!v.method) return !1
  250. switch (v.method) {
  251. case 'adgo':
  252. if (v.line && checkLogin()) {
  253. uni.navigateTo({
  254. url: v.line,
  255. fail: function () {
  256. uni.switchTab({
  257. url: v.line
  258. })
  259. }
  260. })
  261. }
  262. break;
  263. }
  264. }