123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- import app from '@system.app'
- import device from '@system.device'
- import prompt from '@system.prompt'
- import shortcut from '@system.shortcut'
- import router from '@system.router'
- import push from '@service.push'
- const SHORT_CUT_FORBIDDEN_INFO = 'User forbidden'
- // 获取设备信息
- const getDeviceInfo = () => {
- device.getDeviceId({
- success: ret => {
- console.log(ret)
- }
- })
- }
- //获取订阅推送
- const getPushRedId = fn => {
- push.subscribe({
- success: ret => {
- fn && fn(ret ? ret.regId : '')
- }
- })
- }
- //获取应用信息
- const getAppInfo = () => {
- const appInfo = app.getInfo()
- return appInfo
- }
- //获取provider
- const getProvider = () => {
- const provider = push.getProvider()
- return provider
- }
- //获取设备明细
- const getAppDev = fn => {
- device.getInfo({
- success: ret => {
- fn && fn(ret)
- }
- })
- }
- //校验手机号
- const validatePhone = phone => {
- const VALIDATE_REG = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[0-9]|18[0-9]|14[57]|19[89])[0-9]{8}$/
- const isMobile = VALIDATE_REG.test(phone)
- return isMobile
- }
- //获取是否创建图标
- const getShortCut = fn => {
- shortcut.hasInstalled({
- success: ret => {
- fn(ret)
- }
- })
- }
- // 判断用户是否创建图标
- const hasCreateShortCut = (isPayPage = false, fn) => {
- shortcut.hasInstalled({
- success: ret => {
- if (!ret) {
- shortcut.install({
- message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
- success: () => {
- prompt.showToast('添加成功!')
- fn && fn(true)
- },
- fail: code => {
- fn && fn(false)
- if (code === SHORT_CUT_FORBIDDEN_INFO && !isPayPage) app.exit()
- }
- })
- } else {
- fn && fn(false)
- }
- }
- })
- }
- //弱加桌
- const loseLevelShortCut = fn => {
- shortcut.hasInstalled({
- success: ret => {
- if (!ret) {
- shortcut.install({
- message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
- complete: () => {
- fn && fn(true)
- }
- })
- } else {
- fn && fn(false)
- }
- }
- })
- }
- //强制加桌
- const mustCreateShort = fn => {
- shortcut.hasInstalled({
- success: ret => {
- if (!ret) {
- shortcut.install({
- message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
- success: () => {
- prompt.showToast('添加成功!')
- fn && fn(true)
- },
- fail: code => {
- fn && fn(false, code)
- prompt.showToast('取消操作')
- }
- })
- } else {
- fn && fn(false)
- }
- }
- })
- }
- //用于返回时提醒加桌
- const backCreateShortCut = fn => {
- shortcut.hasInstalled({
- success: ret => {
- if (!ret) {
- shortcut.install({
- message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
- success: () => {
- prompt.showToast('添加成功!')
- fn && fn('back')
- },
- fail: code => {
- console.log('我失败了我失败了')
- fn && fn('back')
- }
- })
- } else {
- fn && fn('back')
- }
- }
- })
- }
- // 右上角菜单点击
- const showMenu = () => {
- const appInfo = app.getInfo()
- prompt.showContextMenu({
- itemList: ['保存桌面', '关于', '取消'],
- success: function(ret) {
- switch (ret.index) {
- case 0:
- // 保存桌面
- hasCreateShortCut()
- break
- case 1:
- // 关于
- router.push({
- uri: '/views/About',
- params: {
- name: appInfo.name,
- icon: appInfo.icon
- }
- })
- break
- case 2:
- // 取消
- break
- default:
- prompt.showToast({
- message: '错误'
- })
- }
- }
- })
- }
- const getAppName = () => {
- let appInfo = getAppInfo();
- return appInfo.name;
- }
- export default {
- shortcut,
- getDeviceInfo,
- hasCreateShortCut,
- backCreateShortCut,
- showMenu,
- getShortCut,
- getAppInfo,
- getAppDev,
- validatePhone,
- getPushRedId,
- getProvider,
- mustCreateShort,
- loseLevelShortCut,
- getAppName
- }
|