tools.js 8.7 KB

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