core.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import Vue from 'vue'
  2. import config from './config'
  3. import page from './page'
  4. import pic from './pic'
  5. const dever = {
  6. // 运行平台 h5、weixin、alipay、toutiao、baidu、ios、android
  7. platform: 'h5',
  8. // 运行环境标识 2是h5独立浏览器、3是jsapi、4是app、5是小程序、6是pc
  9. env: 1,
  10. // 当前主机域名
  11. host: '',
  12. page,
  13. pic,
  14. // 获取当前时间戳
  15. curTime: () => Math.floor(Date.now() / 1000),
  16. // 更新全局配置
  17. upConfig(update) {
  18. if (!config.base.init) return
  19. return new Promise((resolve) => {
  20. const $config = this.data('dever_config')
  21. if (!$config || (update && $config.u != update)) {
  22. Vue.prototype.DeverApi.request('GET', config.base.init, {
  23. noloading: 1
  24. }).then((res) => {
  25. this.data('dever_config', res);
  26. resolve(res)
  27. }).catch((err) => {
  28. dever.alert(err.msg)
  29. Vue.prototype.DeverApi.setCode('')
  30. this.goHome()
  31. })
  32. } else {
  33. resolve($config)
  34. }
  35. })
  36. },
  37. // 获取全局配置
  38. config() {
  39. return new Promise((resolve) => {
  40. const config = this.data('dever_config')
  41. if (config) {
  42. resolve(config)
  43. } else {
  44. this.goHome();
  45. }
  46. })
  47. },
  48. //关闭弹窗
  49. close() {
  50. uni.$emit('u-popover-close')
  51. },
  52. // 初始化来源
  53. init() {
  54. //#ifdef WEB
  55. const ua = navigator.userAgent.toLowerCase()
  56. if (ua.includes('micromessenger')) {
  57. this.platform = 'weixin'
  58. this.env = 3
  59. } else if (ua.includes('alipayclient')) {
  60. this.platform = 'alipay'
  61. this.env = 3
  62. } else {
  63. this.platform = 'weixin'
  64. this.env = 2
  65. }
  66. this.host = `${window.location.protocol}//${window.location.host}/#`
  67. //#endif
  68. //#ifdef APP-PLUS
  69. //ios或者android
  70. this.platform = uni.getSystemInfoSync().platform
  71. this.env = 4
  72. //#endif
  73. //#ifdef MP-WEIXIN
  74. this.platform = 'weixin'
  75. this.env = 5
  76. //#endif
  77. //#ifdef MP-ALIPAY
  78. this.platform = 'alipay'
  79. this.env = 5
  80. //#endif
  81. //#ifdef MP-BAIDU
  82. this.platform = 'baidu'
  83. this.env = 5
  84. //#endif
  85. //#ifdef MP-TOUTIAO
  86. this.platform = 'toutiao'
  87. this.env = 5
  88. //#endif
  89. //#ifdef MP-QQ
  90. this.platform = 'qq'
  91. this.env = 5
  92. //#endif
  93. //#ifdef MP-KUAISHOU
  94. this.platform = 'kuaishou'
  95. this.env = 5
  96. //#endif
  97. //#ifdef MP-LARK
  98. this.platform = 'feishu'
  99. this.env = 5
  100. //#endif
  101. //#ifdef MP-JD
  102. this.platform = 'jd'
  103. this.env = 5
  104. //#endif
  105. //#ifdef MP-HARMONY
  106. this.platform = 'hm'
  107. this.env = 5
  108. //#endif
  109. //#ifdef MP-XHS
  110. this.platform = 'xhs'
  111. this.env = 5
  112. //#endif
  113. },
  114. back(path) {
  115. const route = 'pages/' + path
  116. const pages = getCurrentPages()
  117. const index = pages.findIndex(p => p.route == route)
  118. if (index !== -1) {
  119. const delta = pages.length - 1 - index
  120. console.info(route, pages, index, delta)
  121. uni.navigateBack({
  122. delta
  123. })
  124. } else {
  125. this.location(path)
  126. }
  127. },
  128. // 页面跳转
  129. location(path, go = '', title) {
  130. if (path.includes('http')) {
  131. this.debug('http:' + path)
  132. this.data('web_view', path)
  133. title && this.data('web_view_title', title)
  134. if ([4, 5].includes(this.env) || go === 'webview') {
  135. uni.navigateTo({
  136. url: config.path.webView
  137. })
  138. } else if (go.includes('/')) {
  139. window.location.href = path + (path.includes('?') ? '&' : '?') + 'refer=' + btoa(this.host + go)
  140. } else {
  141. window.location.href = path + (path.includes('?') ? '&' : '?') + 'refer=' + btoa(window.location
  142. .href + go)
  143. }
  144. } else {
  145. path = path.includes('/pages/') ? path : '/pages/' + path
  146. if (config.switchTab.list.includes(path)) {
  147. uni.switchTab({
  148. url: path,
  149. success: (e) => config.switchTab.call[path]?.(this, e)
  150. })
  151. } else if (go && go == 'clear') {
  152. uni.reLaunch({
  153. url: path
  154. })
  155. } else if (go && go !== 'webview') {
  156. uni.redirectTo({
  157. url: path
  158. })
  159. } else {
  160. uni.navigateTo({
  161. url: path
  162. })
  163. }
  164. }
  165. },
  166. goHome() {
  167. this.location(config.path.home, 'clear')
  168. },
  169. goLogin(refer) {
  170. this.data('refer', page.refer(refer))
  171. this.location(config.path.login)
  172. },
  173. goUser(refer) {
  174. this.data('refer', page.refer(refer))
  175. this.location(config.path.user)
  176. },
  177. goBindMobile(refer) {
  178. this.data('refer', page.refer(refer))
  179. this.location(config.path.bindMobile)
  180. },
  181. goBindEmail(refer) {
  182. this.data('refer', page.refer(refer))
  183. this.location(config.path.bindEmail)
  184. },
  185. goTabbar(item, index) {
  186. uni.setStorage({
  187. key: 'nav',
  188. data: index,
  189. });
  190. config.tabbar.go(this, item, index)
  191. },
  192. success(info, callback) {
  193. this.alert(info, 'success', callback)
  194. },
  195. alert(info, icon = 'none', callback) {
  196. this.debug('alert:' + info)
  197. info && uni.showToast({
  198. title: info,
  199. icon,
  200. duration: 1500,
  201. success: callback
  202. })
  203. return false
  204. },
  205. confirm(text, callback, errCallback) {
  206. uni.showModal({
  207. content: text,
  208. cancelText: '取消',
  209. confirmText: '确定',
  210. success: res => {
  211. if (res.confirm) {
  212. callback()
  213. } else if (res.cancel && errCallback) {
  214. errCallback()
  215. }
  216. }
  217. })
  218. },
  219. debug(string) {
  220. config.base.debug && console.log(string)
  221. },
  222. log: console.log.bind(console),
  223. data(key, value) {
  224. if (value !== undefined) {
  225. if (value === 'del') return uni.removeStorageSync(key)
  226. return uni.setStorageSync(key, value)
  227. }
  228. return uni.getStorageSync(key)
  229. },
  230. jump() {
  231. let refer = this.data('refer') || config.path.home
  232. const id = this.data('invite')
  233. if (id && refer !== config.path.home) refer += `?id=${id}&type=${type}`
  234. this.data('refer', 'del')
  235. this.data('invite', 'del')
  236. this.location(refer, 'go')
  237. },
  238. xing: s => s.replace(/./g, '*'),
  239. quit(page, call) {
  240. if (this.env == 4) {
  241. const main = plus.android.runtimeMainActivity()
  242. plus.nativeUI.toast = str => {
  243. if (str === '再按一次退出应用') {
  244. call ? call(main) : page.$mp.page.$getAppWebview().children()[0].back()
  245. return false
  246. } else this.alert(str)
  247. }
  248. }
  249. },
  250. sleep: time => new Promise(resolve => setTimeout(resolve, time)),
  251. copy(content) {
  252. uni.setClipboardData({
  253. data: content,
  254. success: () => uni.showToast({
  255. title: '复制成功'
  256. })
  257. })
  258. },
  259. formatDate: function(date) {
  260. var date = new Date(date);
  261. var YY = date.getFullYear() + '-';
  262. var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  263. var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  264. var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  265. var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  266. var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  267. return YY + MM + DD + " " + hh + mm + ss;
  268. },
  269. }
  270. export default dever