tools.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import permision from "@/js_sdk/wa-permission/permission.js"
  2. let tools = {}
  3. /**
  4. * 大小判断
  5. * @param a
  6. * @param b
  7. * @returns {number}
  8. */
  9. tools.sortNumber = function (a, b) {
  10. return a - b;
  11. }
  12. /**
  13. * 保留两位小数
  14. * @param num
  15. * @returns {string}
  16. */
  17. tools.twoFloating = function (num) {
  18. // 获取两位小数
  19. let price = "";
  20. price = num * 1;
  21. price = String(price).split(".")[1];
  22. if (price !== undefined && price.length === 1) {
  23. price = `.${price}0`;
  24. } else {
  25. price === undefined ? (price = ".00") : (price = `.${price}`);
  26. }
  27. return price;
  28. }
  29. tools.formatDecimal = function (num, decimal) {
  30. num = num.toString()
  31. let index = num.indexOf('.')
  32. if (index !== -1) {
  33. num = num.substring(0, decimal + index + 1)
  34. } else {
  35. num = num.substring(0)
  36. }
  37. return parseFloat(num).toFixed(decimal)
  38. }
  39. /**
  40. * 错误提示
  41. * @param msg
  42. */
  43. tools.error = function (msg) {
  44. uni.showToast({
  45. 'title': msg,
  46. 'icon': 'error',
  47. 'mask': true,
  48. 'duration': 1500
  49. })
  50. }
  51. /**
  52. * 成功提示
  53. * @param msg
  54. */
  55. tools.success = function (msg) {
  56. uni.showToast({
  57. 'title': msg,
  58. 'icon': 'success',
  59. 'mask': true,
  60. 'duration': 1500
  61. })
  62. }
  63. /**
  64. * 显示Loading
  65. */
  66. tools.showLoading = function () {
  67. uni.showLoading({
  68. title: '加载中...',
  69. mask: true
  70. });
  71. }
  72. /**
  73. * 关闭Loading
  74. */
  75. tools.hideLoading = function () {
  76. uni.hideLoading();
  77. }
  78. /**
  79. * 获取时间戳(毫秒)
  80. * @returns {number}
  81. */
  82. tools.getTime = function () {
  83. return new Date().getTime();
  84. }
  85. tools.getCountDays=function (date){
  86. let curDate = new Date(date);
  87. // 获取当前月份
  88. curDate.setDate(32);
  89. // 返回当前月份的天数
  90. return 32-curDate.getDate();
  91. }
  92. /**
  93. * 获取当前年
  94. * @returns {number}
  95. */
  96. tools.getYear=function (){
  97. return new Date().getFullYear()
  98. }
  99. tools.updateVersion = function (sysVersion, appUrl) {
  100. let app_version = plus.runtime.version;
  101. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  102. console.log(app_version < sysVersion)
  103. if (app_version < sysVersion) {
  104. uni.showLoading({
  105. title: '更新中……'
  106. })
  107. uni.downloadFile({//执行下载
  108. url: appUrl, //下载地址
  109. success: (downloadResult) => {//下载成功
  110. uni.hideLoading();
  111. if (downloadResult.statusCode === 200) {
  112. uni.showModal({
  113. title: '',
  114. content: '更新成功,确定现在重启吗?',
  115. confirmText: '重启',
  116. confirmColor: '#EE8F57',
  117. success: function (res) {
  118. if (res.confirm === true) {
  119. plus.runtime.install(//安装
  120. downloadResult.tempFilePath, {
  121. force: true
  122. },
  123. function (res) {
  124. tools.success('更新成功,重启中')
  125. plus.runtime.restart();
  126. }
  127. );
  128. }
  129. }
  130. });
  131. }
  132. }
  133. });
  134. } else {
  135. // tools.success('你已是最新版本')
  136. }
  137. }
  138. /**
  139. * uniapp html图片显示控制
  140. * @param str
  141. * @returns {*}
  142. */
  143. tools.imgDeal = function (str) {
  144. console.log(str)
  145. if(str===null || str===undefined){
  146. return '';
  147. }else {
  148. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  149. }
  150. }
  151. /**
  152. * 获取平台类型 1:微信,2:支付宝
  153. * @returns {boolean|number}
  154. */
  155. tools.platformType = function () {
  156. let ua = window.navigator.userAgent.toLowerCase();
  157. if (ua.indexOf('micromessenger') != -1) {
  158. return 1;
  159. } else if (ua.indexOf('alipay') != -1) {
  160. return 2;
  161. } else {
  162. return 0;
  163. }
  164. }
  165. /**
  166. * 手机震动
  167. */
  168. tools.vibrate=function (){
  169. console.log('vibrate')
  170. // #ifndef H5
  171. let platform=uni.getSystemInfoSync().platform
  172. // #ifdef APP-PLUS
  173. if (platform === "ios") {
  174. let UIImpactFeedbackGenerator = plus.ios.importClass('UIImpactFeedbackGenerator');
  175. let impact = new UIImpactFeedbackGenerator();
  176. impact.prepare();
  177. impact.init(1);
  178. impact.impactOccurred();
  179. }
  180. if (platform === "android") {
  181. uni.vibrateShort();
  182. }
  183. // #endif
  184. //#endif
  185. }
  186. /**
  187. * 获取权限
  188. */
  189. tools.getAndroidPermission = async function(permissionName) {
  190. let ret = await permision.requestAndroidPermission(permissionName);
  191. console.log(ret)
  192. return ret;
  193. }
  194. /**
  195. * ios权限验证
  196. * @param permissionName
  197. * @returns {Promise<any>}
  198. */
  199. tools.getIosPermission = async function(permissionName) {
  200. let ret = await permision.judgeIosPermission(permissionName);
  201. console.log(ret)
  202. return ret;
  203. }
  204. /**
  205. * 获取开发平台
  206. * @returns {string}
  207. */
  208. tools.getPlatform = function () {
  209. let platForm = undefined;
  210. // #ifdef H5
  211. platForm = 'H5';
  212. //#endif
  213. // #ifdef APP-PLUS
  214. platForm = 'APP';
  215. //#endif
  216. // #ifdef APP-PLUS-NVUE
  217. platForm = 'APP';
  218. //#endif
  219. // #ifdef APP-NVUE
  220. platForm = 'APP';
  221. //#endif
  222. // #ifdef MP-WEIXIN
  223. platForm = 'MP-WEIXIN'; // 小程序
  224. //#endif
  225. // #ifdef MP-ALIPAY
  226. platForm = 'MP-ALIPAY';
  227. //#endif
  228. // #ifdef MP-BAIDU
  229. platForm = 'MP-BAIDU';
  230. //#endif
  231. // #ifdef MP-TOUTIAO
  232. platForm = 'MP-TOUTIAO';
  233. //#endif
  234. // #ifdef MP-LARK
  235. platForm = 'MP-LARK';
  236. //#endif
  237. // #ifdef MP-QQ
  238. platForm = 'MP-QQ';
  239. //#endif
  240. // #ifdef MP-KUAISHOU
  241. platForm = 'MP-KUAISHOU';
  242. //#endif
  243. // #ifdef QUICKAPP-WEBVIEW
  244. platForm = 'QUICKAPP-WEBVIEW';
  245. //#endif
  246. return platForm;
  247. }
  248. tools.leftClick = function () {
  249. let pages = getCurrentPages();
  250. if (pages.length > 1) {
  251. uni.navigateBack({
  252. delta: 1
  253. })
  254. } else {
  255. uni.reLaunch({
  256. url: '/pages/index/index'
  257. });
  258. }
  259. }
  260. export default tools