app.js 5.8 KB

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