tools.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // 播放音频
  2. export function playAudio(type){
  3. const innerAudioContext = uni.createInnerAudioContext();
  4. // innerAudioContext.autoplay = true;
  5. innerAudioContext.src = '/static/'+type+'.mp3';
  6. innerAudioContext.onCanplay(res => {
  7. innerAudioContext.play()
  8. })
  9. innerAudioContext.onPlay(() => {
  10. console.log('开始播放');
  11. });
  12. innerAudioContext.onError((res) => {
  13. console.log(res.errCode);
  14. });
  15. innerAudioContext.onEnded(res => {
  16. innerAudioContext.destroy()
  17. })
  18. }
  19. /**
  20. * //设置应用版本号对应的缓存信息
  21. * @param {Object} currTimeStamp 当前获取的时间戳
  22. */
  23. export const setStorageForAppVersion = function(currTimeStamp){
  24. uni.setStorage({
  25. key: 'tip_version_update_time',
  26. data: currTimeStamp,
  27. success: function () {
  28. console.log('setStorage-success');
  29. }
  30. });
  31. }
  32. /**
  33. * 进行版本型号的比对 以及下载更新请求
  34. * @param {Object} server_version 服务器最新 应用版本号
  35. * @param {Object} curr_version 当前应用版本号
  36. * isQzgx 是否强制更新
  37. * callback 登录
  38. */
  39. export const checkVersionToLoadUpdate = function(newVersion,type,callback){
  40. if(newVersion.version == '2001'||newVersion.version == '2002'){
  41. newVersion.version = 201
  42. }
  43. const curVer = getApp().globalData.version // 当前版本信息
  44. console.log(newVersion)
  45. console.log(curVer)
  46. if(!newVersion || !curVer){
  47. callback()
  48. return
  49. }
  50. const curr_version = curVer && curVer.version ? curVer.version.replace(/\./g,'') : ''
  51. const server_version = newVersion && newVersion.version ? newVersion.version : '' // 最新版本信息
  52. const isQzgx = newVersion && newVersion.forceUpgrade != 1
  53. let isWifi = plus.networkinfo.getCurrentType()!=3 ? '有新的版本发布,检测到您目前非Wifi连接,' : '有新的版本发布,'
  54. let msg = isWifi + (!isQzgx ? '请立即更新?' : '是否立即更新版本?')
  55. // 下载app并安装
  56. let downloadApp = function(downloadApkUrl){
  57. uni.showLoading({
  58. title: '正在更新中...',
  59. mask: true,
  60. });
  61. var dtask = plus.downloader.createDownload( downloadApkUrl, {}, function ( d, status ) {
  62. // 下载完成
  63. if ( status == 200 ) {
  64. plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){
  65. uni.showToast({
  66. icon: 'none',
  67. title: '安装失败',
  68. duration: 1500
  69. });
  70. })
  71. uni.hideLoading()
  72. } else {
  73. uni.showToast({
  74. icon: 'none',
  75. title: '更新失败',
  76. duration: 1500
  77. });
  78. }
  79. });
  80. dtask.start();
  81. }
  82. // 打开ios 应用市场
  83. let openIosAppSotre = function(downloadUrl){
  84. //在App Store Connect中的App Store下的app信息,可找到appleId
  85. plus.runtime.launchApplication({
  86. action: downloadUrl
  87. }, function(e) {
  88. console.log('Open system default browser failed: ' + e.message);
  89. })
  90. }
  91. console.log(server_version,curr_version)
  92. if(Number(server_version) > Number(curr_version)){
  93. uni.showModal({
  94. title: msg,
  95. content: '更新内容:'+newVersion.upgradeContent,
  96. showCancel: isQzgx,
  97. confirmText:'立即更新',
  98. cancelText:'稍后进行',
  99. success: function (res) {
  100. if (res.confirm) {
  101. // 清空缓存token
  102. let tmp = uni.getStorageSync('lifeData');
  103. tmp = tmp ? tmp : {};
  104. tmp['vuex_token'] = '';
  105. uni.setStorageSync('lifeData', tmp);
  106. if(curVer.platform == 'android'){
  107. //设置 最新版本apk的下载链接
  108. downloadApp(newVersion.attachment);
  109. }else{
  110. openIosAppSotre(newVersion.downloadUrl)
  111. }
  112. } else if (res.cancel) {
  113. console.log('稍后更新');
  114. callback()
  115. }
  116. }
  117. });
  118. uni.hideLoading();
  119. }else{
  120. if(type){
  121. uni.showToast({
  122. icon: 'none',
  123. title: '已是最新版本',
  124. duration: 1500
  125. });
  126. }else{
  127. callback()
  128. }
  129. }
  130. }
  131. // 处理数字千位分隔符 num 数值,decimal要保留的小数位数
  132. export const toThousands = (num, decimal) => {
  133. if (decimal) {
  134. num = formatDecimal(num, decimal)
  135. }
  136. return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
  137. return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
  138. return $1 + ','
  139. })
  140. })
  141. }
  142. // 保留decimal位小数(不四舍五入) num 数值,decimal要保留的小数位数
  143. export const formatDecimal = function (num, decimal) {
  144. num = num.toString()
  145. const index = num.indexOf('.')
  146. if (index !== -1) {
  147. num = num.substring(0, decimal + index + 1)
  148. } else {
  149. num = num.substring(0)
  150. }
  151. return parseFloat(num).toFixed(decimal)
  152. }