core.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import Vue from 'vue'
  2. import config from './config'
  3. import page from './page'
  4. import pic from './pic'
  5. const dever = {
  6. // 运行平台
  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. })
  28. } else {
  29. resolve($config)
  30. }
  31. })
  32. },
  33. // 获取全局配置
  34. config() {
  35. return new Promise((resolve) => {
  36. const config = this.data('dever_config')
  37. if (config) {
  38. resolve(config)
  39. } else {
  40. this.goHome();
  41. }
  42. })
  43. },
  44. // 初始化来源
  45. init() {
  46. //#ifdef H5
  47. const ua = navigator.userAgent.toLowerCase()
  48. if (ua.includes('micromessenger')) {
  49. this.platform = 'weixin'
  50. this.env = 3
  51. } else if (ua.includes('alipayclient')) {
  52. this.platform = 'alipay'
  53. this.env = 3
  54. } else {
  55. this.platform = 'h5'
  56. this.env = 2
  57. }
  58. this.host = `${window.location.protocol}//${window.location.host}/#`
  59. //#endif
  60. //#ifdef APP-PLUS
  61. //ios或者android
  62. this.platform = uni.getSystemInfoSync().platform
  63. this.env = 4
  64. //#endif
  65. //#ifdef MP-WEIXIN
  66. this.platform = 'weixin'
  67. this.env = 5
  68. //#endif
  69. //#ifdef MP-ALIPAY
  70. this.platform = 'alipay'
  71. this.env = 5
  72. //#endif
  73. //#ifdef MP-BAIDU
  74. this.platform = 'baidu'
  75. this.env = 5
  76. //#endif
  77. //#ifdef MP-TOUTIAO
  78. this.platform = 'toutiao'
  79. this.env = 5
  80. //#endif
  81. },
  82. // 页面跳转
  83. location(path, go = '', title) {
  84. if (path.includes('http')) {
  85. this.debug('http:' + path)
  86. this.data('web_view', path)
  87. title && this.data('web_view_title', title)
  88. if ([4, 5].includes(this.env) || go === 'webview') {
  89. uni.navigateTo({
  90. url: config.path.webView
  91. })
  92. } else if (go.includes('/')) {
  93. window.location.href = path + (path.includes('?') ? '&' : '?') + 'refer=' + btoa(this.host + go)
  94. } else {
  95. window.location.href = path + (path.includes('?') ? '&' : '?') + 'refer=' + btoa(window.location
  96. .href + go)
  97. }
  98. } else {
  99. const route = 'pages/' + path
  100. const pages = getCurrentPages()
  101. const index = pages.findIndex(p => p.route == route)
  102. if (index !== -1) {
  103. const delta = pages.length - 1 - index
  104. console.info(route, pages, index, delta)
  105. uni.navigateBack({ delta })
  106. } else {
  107. path = path.includes('/pages/') ? path : '/pages/' + path
  108. if (config.switchTab.list.includes(path)) {
  109. uni.switchTab({
  110. url: path,
  111. success: (e) => config.switchTab.call[path]?.(this, e)
  112. })
  113. } else if (go && go !== 'webview') {
  114. uni.redirectTo({
  115. url: path
  116. })
  117. } else {
  118. uni.navigateTo({
  119. url: path
  120. })
  121. }
  122. }
  123. }
  124. },
  125. goHome() {
  126. this.location(config.path.home, 'go')
  127. },
  128. goLogin(refer) {
  129. this.data('refer', page.refer(refer))
  130. this.location(config.path.login)
  131. },
  132. goUser(refer) {
  133. this.data('refer', page.refer(refer))
  134. this.location(config.path.user)
  135. },
  136. goBindMobile(refer) {
  137. this.data('refer', page.refer(refer))
  138. this.location(config.path.bindMobile)
  139. },
  140. goBindEmail(refer) {
  141. this.data('refer', page.refer(refer))
  142. this.location(config.path.bindEmail)
  143. },
  144. goTabbar(item) {
  145. config.tabbar.go(this, item)
  146. },
  147. success(info, callback) {
  148. this.alert(info, 'success', callback)
  149. },
  150. alert(info, icon = 'none', callback) {
  151. this.debug('alert:' + info)
  152. info && uni.showToast({
  153. title: info,
  154. icon,
  155. duration: 1000,
  156. success: callback
  157. })
  158. return false
  159. },
  160. confirm(text, callback, errCallback) {
  161. uni.showModal({
  162. content: text,
  163. cancelText: '取消',
  164. confirmText: '确定',
  165. success: res => {
  166. if (res.confirm) {
  167. callback()
  168. } else if(res.cancel && errCallback) {
  169. errCallback()
  170. }
  171. }
  172. })
  173. },
  174. debug(string) {
  175. config.base.debug && console.log(string)
  176. },
  177. log: console.log.bind(console),
  178. data(key, value) {
  179. if (value !== undefined) {
  180. if (value === 'del') return uni.removeStorageSync(key)
  181. return uni.setStorageSync(key, value)
  182. }
  183. return uni.getStorageSync(key)
  184. },
  185. jump() {
  186. let refer = this.data('refer') || config.path.home
  187. const id = this.data('invite')
  188. if (id && refer !== config.path.home) refer += `?id=${id}&type=${type}`
  189. this.data('refer', 'del')
  190. this.data('invite', 'del')
  191. this.location(refer, 'go')
  192. },
  193. xing: s => s.replace(/./g, '*'),
  194. quit(page, call) {
  195. if (this.env == 4) {
  196. const main = plus.android.runtimeMainActivity()
  197. plus.nativeUI.toast = str => {
  198. if (str === '再按一次退出应用') {
  199. call ? call(main) : page.$mp.page.$getAppWebview().children()[0].back()
  200. return false
  201. } else this.alert(str)
  202. }
  203. }
  204. },
  205. sleep: time => new Promise(resolve => setTimeout(resolve, time)),
  206. copy(content) {
  207. uni.setClipboardData({
  208. data: content,
  209. success: () => uni.showToast({
  210. title: '复制成功'
  211. })
  212. })
  213. },
  214. formatDate: function(date) {
  215. var date = new Date(date);
  216. var YY = date.getFullYear() + '-';
  217. var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  218. var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  219. var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  220. var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  221. var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  222. return YY + MM + DD + " " + hh + mm + ss;
  223. },
  224. }
  225. export default dever