interface.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import app from '@system.app'
  2. import device from '@system.device'
  3. import prompt from '@system.prompt'
  4. import shortcut from '@system.shortcut'
  5. import router from '@system.router'
  6. import push from '@service.push'
  7. const SHORT_CUT_FORBIDDEN_INFO = 'User forbidden'
  8. // 获取设备信息
  9. const getDeviceInfo = () => {
  10. device.getDeviceId({
  11. success: ret => {
  12. console.log(ret)
  13. }
  14. })
  15. }
  16. //获取订阅推送
  17. const getPushRedId = fn => {
  18. push.subscribe({
  19. success: ret => {
  20. fn && fn(ret ? ret.regId : '')
  21. }
  22. })
  23. }
  24. //获取应用信息
  25. const getAppInfo = () => {
  26. const appInfo = app.getInfo()
  27. return appInfo
  28. }
  29. //获取provider
  30. const getProvider = () => {
  31. const provider = push.getProvider()
  32. return provider
  33. }
  34. //获取设备明细
  35. const getAppDev = fn => {
  36. device.getInfo({
  37. success: ret => {
  38. fn && fn(ret)
  39. }
  40. })
  41. }
  42. //校验手机号
  43. const validatePhone = phone => {
  44. 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}$/
  45. const isMobile = VALIDATE_REG.test(phone)
  46. return isMobile
  47. }
  48. //获取是否创建图标
  49. const getShortCut = fn => {
  50. shortcut.hasInstalled({
  51. success: ret => {
  52. fn(ret)
  53. }
  54. })
  55. }
  56. // 判断用户是否创建图标
  57. const hasCreateShortCut = (isPayPage = false, fn) => {
  58. shortcut.hasInstalled({
  59. success: ret => {
  60. if (!ret) {
  61. shortcut.install({
  62. message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
  63. success: () => {
  64. prompt.showToast('添加成功!')
  65. fn && fn(true)
  66. },
  67. fail: code => {
  68. fn && fn(false)
  69. if (code === SHORT_CUT_FORBIDDEN_INFO && !isPayPage) app.exit()
  70. }
  71. })
  72. } else {
  73. fn && fn(false)
  74. }
  75. }
  76. })
  77. }
  78. //弱加桌
  79. const loseLevelShortCut = fn => {
  80. shortcut.hasInstalled({
  81. success: ret => {
  82. if (!ret) {
  83. shortcut.install({
  84. message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
  85. complete: () => {
  86. fn && fn(true)
  87. }
  88. })
  89. } else {
  90. fn && fn(false)
  91. }
  92. }
  93. })
  94. }
  95. //强制加桌
  96. const mustCreateShort = fn => {
  97. shortcut.hasInstalled({
  98. success: ret => {
  99. if (!ret) {
  100. shortcut.install({
  101. message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
  102. success: () => {
  103. prompt.showToast('添加成功!')
  104. fn && fn(true)
  105. },
  106. fail: code => {
  107. fn && fn(false, code)
  108. prompt.showToast('取消操作')
  109. }
  110. })
  111. } else {
  112. fn && fn(false)
  113. }
  114. }
  115. })
  116. }
  117. //用于返回时提醒加桌
  118. const backCreateShortCut = fn => {
  119. shortcut.hasInstalled({
  120. success: ret => {
  121. if (!ret) {
  122. shortcut.install({
  123. message: '添加【'+getAppName()+'】到桌面,方便下次阅读',
  124. success: () => {
  125. prompt.showToast('添加成功!')
  126. fn && fn('back')
  127. },
  128. fail: code => {
  129. console.log('我失败了我失败了')
  130. fn && fn('back')
  131. }
  132. })
  133. } else {
  134. fn && fn('back')
  135. }
  136. }
  137. })
  138. }
  139. // 右上角菜单点击
  140. const showMenu = () => {
  141. const appInfo = app.getInfo()
  142. prompt.showContextMenu({
  143. itemList: ['保存桌面', '关于', '取消'],
  144. success: function(ret) {
  145. switch (ret.index) {
  146. case 0:
  147. // 保存桌面
  148. hasCreateShortCut()
  149. break
  150. case 1:
  151. // 关于
  152. router.push({
  153. uri: '/views/About',
  154. params: {
  155. name: appInfo.name,
  156. icon: appInfo.icon
  157. }
  158. })
  159. break
  160. case 2:
  161. // 取消
  162. break
  163. default:
  164. prompt.showToast({
  165. message: '错误'
  166. })
  167. }
  168. }
  169. })
  170. }
  171. const getAppName = () => {
  172. let appInfo = getAppInfo();
  173. return appInfo.name;
  174. }
  175. export default {
  176. shortcut,
  177. getDeviceInfo,
  178. hasCreateShortCut,
  179. backCreateShortCut,
  180. showMenu,
  181. getShortCut,
  182. getAppInfo,
  183. getAppDev,
  184. validatePhone,
  185. getPushRedId,
  186. getProvider,
  187. mustCreateShort,
  188. loseLevelShortCut,
  189. getAppName
  190. }