Newer
Older
smartwell_app_front / src / assets / js / plusReady.js
StephanieGitHub on 6 Aug 2019 1 KB first commit
import { Toast } from 'mint-ui'
import { setCid } from '@/utils/auth'
import store from '@/store'

// 这个方法,是用来确保 5+ API 扩展成功了,在这个方法的回调中调用 5+ 扩展 API,不会出现 plus is not defined 错误。
export const plusReady = (fn) => {
  if (window.plus) {
    setTimeout(fn, 0)
  } else {
    document.addEventListener("plusready", fn, false)
  }
}

var first = null
/*重写返回按钮事件*/
plusReady(() => {
  console.log('plusReady')
  // 页面加载时触发
  var pinf = plus.push.getClientInfo();
  var cid = pinf.clientid;//客户端标识
  console.log('plusReady-cid:'+cid)
  setCid(cid)

  plus.key.addEventListener('backbutton',function () {
    //首次按键,提示‘再按一次退出应用’
    console.log(location.hash)
    console.log(first)
    var urls = location.hash
    if(urls=='#/'||urls=='#/login'){
      if (!first) {
        first = new Date().getTime();
        Toast({
          message: '再按一次退出应用',
          position: 'bottom',
          duration: 3000
        })
        console.log(first)
        setTimeout(function() {
          first = null;
        }, 2000);
      } else {
        if (new Date().getTime() - first < 2000) {
          var webview = plus.webview.currentWebview()
          webview.close(); //hide,quit按手机返回键直接退出APP
          plus.runtime.quit();
        }
      }
    }else{
      var webview = plus.webview.currentWebview()
      webview.back()
    }
  })
})