tools.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // import wxJs from "jweixin-module"
  2. // import jWeixin from "wecomjsdk"
  3. import {
  4. Base64
  5. } from '@/js_sdk/js-base64/base64';
  6. // import {getWxJsSign} from "@/api/weChat";
  7. let tools = {}
  8. /**
  9. * 大小判断
  10. * @param a
  11. * @param b
  12. * @returns {number}
  13. */
  14. tools.sortNumber = function (a, b) {
  15. return a - b;
  16. }
  17. //邮箱/手机***过滤
  18. tools.starPhone = function(value, type) {
  19. const start = value.slice(0, 3)
  20. const end = value.slice(-type)
  21. return `${start}****${end}`
  22. }
  23. /**
  24. * 保留两位小数
  25. * @param num
  26. * @returns {string}
  27. */
  28. tools.twoFloating = function (num) {
  29. // 获取两位小数
  30. let price = "";
  31. price = num * 1;
  32. price = String(price).split(".")[1];
  33. if (price !== undefined && price.length === 1) {
  34. price = `.${price}0`;
  35. } else {
  36. price === undefined ? (price = ".00") : (price = `.${price}`);
  37. }
  38. return price;
  39. }
  40. tools.formatDecimal = function (num, decimal) {
  41. num = num.toString()
  42. let index = num.indexOf('.')
  43. if (index !== -1) {
  44. num = num.substring(0, decimal + index + 1)
  45. } else {
  46. num = num.substring(0)
  47. }
  48. return parseFloat(num).toFixed(decimal)
  49. }
  50. /**
  51. * 错误提示
  52. * @param msg
  53. */
  54. tools.error = function (msg) {
  55. let type = 'error'
  56. if(msg.length > 7){
  57. type = 'none'
  58. }
  59. uni.showToast({
  60. 'title': msg,
  61. 'icon': type,
  62. 'mask': true,
  63. 'duration': 1500
  64. })
  65. }
  66. /**
  67. * 成功提示
  68. * @param msg
  69. */
  70. tools.success = function (msg) {
  71. uni.showToast({
  72. 'title': msg,
  73. 'icon': 'success',
  74. 'mask': true,
  75. 'duration': 1500
  76. })
  77. }
  78. /**
  79. * 显示Loading
  80. */
  81. tools.showLoading = function () {
  82. uni.showLoading({
  83. title: '加载中...',
  84. mask: true
  85. });
  86. }
  87. /**
  88. * 关闭Loading
  89. */
  90. tools.hideLoading = function () {
  91. uni.hideLoading();
  92. }
  93. /**
  94. * 获取时间戳(毫秒)
  95. * @returns {number}
  96. */
  97. tools.getTime = function () {
  98. return new Date().getTime();
  99. }
  100. // 返回上级
  101. tools.leftClick = function () {
  102. let pages = getCurrentPages();
  103. if (pages.length > 1) {
  104. uni.navigateBack({
  105. delta: 1
  106. })
  107. } else {
  108. uni.reLaunch({
  109. url: '/pages/index/index'
  110. });
  111. }
  112. }
  113. tools.updateVersion = function (sysVersion, appUrl) {
  114. let app_version = plus.runtime.version;
  115. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  116. console.log(app_version < sysVersion)
  117. if (app_version < sysVersion) {
  118. uni.showLoading({
  119. title: '更新中……'
  120. })
  121. uni.downloadFile({//执行下载
  122. url: appUrl, //下载地址
  123. success: (downloadResult) => {//下载成功
  124. uni.hideLoading();
  125. if (downloadResult.statusCode === 200) {
  126. uni.showModal({
  127. title: '',
  128. content: '更新成功,确定现在重启吗?',
  129. confirmText: '重启',
  130. confirmColor: '#EE8F57',
  131. success: function (res) {
  132. if (res.confirm === true) {
  133. plus.runtime.install(//安装
  134. downloadResult.tempFilePath, {
  135. force: true
  136. },
  137. function (res) {
  138. tools.success('更新成功,重启中')
  139. plus.runtime.restart();
  140. }
  141. );
  142. }
  143. }
  144. });
  145. }
  146. }
  147. });
  148. } else {
  149. // tools.success('你已是最新版本')
  150. }
  151. }
  152. /**
  153. * uniapp html图片显示控制
  154. * @param str
  155. * @returns {*}
  156. */
  157. tools.imgDeal = function (str) {
  158. console.log(str)
  159. if(str===null || str===undefined){
  160. return '';
  161. }else {
  162. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  163. }
  164. }
  165. /**
  166. * 获取平台类型 1:微信,2:支付宝
  167. * @returns {boolean|number}
  168. */
  169. tools.platformType = function () {
  170. let ua = window.navigator.userAgent.toLowerCase();
  171. if (ua.indexOf('micromessenger') != -1) {
  172. return 1;
  173. } else if (ua.indexOf('alipay') != -1) {
  174. return 2;
  175. } else {
  176. return 0;
  177. }
  178. }
  179. /**
  180. * 获取开发平台
  181. * @returns {string}
  182. */
  183. tools.getPlatform = function () {
  184. let platForm = undefined;
  185. // #ifdef H5
  186. platForm = 'H5';
  187. //#endif
  188. // #ifdef APP-PLUS
  189. platForm = 'APP';
  190. //#endif
  191. // #ifdef APP-PLUS-NVUE
  192. platForm = 'APP';
  193. //#endif
  194. // #ifdef APP-NVUE
  195. platForm = 'APP';
  196. //#endif
  197. // #ifdef MP-WEIXIN
  198. platForm = 'MP-WEIXIN'; // 小程序
  199. //#endif
  200. // #ifdef MP-ALIPAY
  201. platForm = 'MP-ALIPAY';
  202. //#endif
  203. // #ifdef MP-BAIDU
  204. platForm = 'MP-BAIDU';
  205. //#endif
  206. // #ifdef MP-TOUTIAO
  207. platForm = 'MP-TOUTIAO';
  208. //#endif
  209. // #ifdef MP-LARK
  210. platForm = 'MP-LARK';
  211. //#endif
  212. // #ifdef MP-QQ
  213. platForm = 'MP-QQ';
  214. //#endif
  215. // #ifdef MP-KUAISHOU
  216. platForm = 'MP-KUAISHOU';
  217. //#endif
  218. // #ifdef QUICKAPP-WEBVIEW
  219. platForm = 'QUICKAPP-WEBVIEW';
  220. //#endif
  221. return platForm;
  222. }
  223. /**
  224. * 记录用户登录信息
  225. * @param data
  226. * @param type
  227. */
  228. tools.setLoginData = function (data) {
  229. uni.setStorageSync('m_id', data.m_id)
  230. uni.setStorageSync('token', data.token)
  231. tools.success('登陆成功')
  232. setTimeout(() => {
  233. uni.reLaunch({
  234. url: '/pages/index/index'
  235. });
  236. }, 1500)
  237. }
  238. /**
  239. * 微信JSDK授权
  240. * @param jsApiList
  241. * @returns {Promise<*>}
  242. */
  243. tools.wxInitialize = async function (jsApiList) {
  244. return await getWxJsSign({ 'url': window.location.href,'type':1 }).then((ret) => {
  245. if (ret.code === 1) {
  246. wxJs.config({
  247. beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  248. debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  249. appId: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  250. timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
  251. nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
  252. signature: ret.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
  253. jsApiList: jsApiList,// 必填,需要使用的JS接口列表
  254. success: function(res) {
  255. console.log(res)
  256. console.log('config:---------------OK')
  257. tools.success('成功')
  258. },
  259. fail: function(res) {
  260. console.log('config-error')
  261. console.log(res)
  262. }
  263. });
  264. return true;
  265. } else {
  266. return false;
  267. }
  268. })
  269. }
  270. /**
  271. * 企业微信信息授权
  272. * @param jsApiList
  273. * @returns {Promise<unknown>}
  274. */
  275. tools.wxAgentInitialize=async function (jsApiList){
  276. return await getWxJsSign({ 'url': window.location.href ,'type':2}).then((ret) => {
  277. if (ret.code === 1) {
  278. jWeixin.agentConfig({
  279. corpid: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  280. agentid: ret.data.agentid, // 必填,企业微信的应用id (e.g. 1000247)
  281. timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
  282. nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
  283. signature: ret.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
  284. jsApiList: jsApiList,// 必填,需要使用的JS接口列表
  285. success: function(res) {
  286. console.log('agentConfig:---------------OK')
  287. console.log(res)
  288. },
  289. fail: function(res) {
  290. console.log('agentConfig-error')
  291. console.log(res)
  292. if(res.errMsg.indexOf('function not exist') > -1){
  293. alert('版本过低请升级')
  294. }
  295. }
  296. });
  297. return true;
  298. } else {
  299. return false;
  300. }
  301. })
  302. }
  303. tools.wxLogin = function () {
  304. console.log(Base64.encode('aaa'));
  305. // 微信浏览器
  306. let url = encodeURIComponent(window.location.href);
  307. window.location.href = `https://test-api.wealfavor.cn/api/weChat/wx?redirect_url=${Base64.encode(url)}`
  308. }
  309. export default tools