tools.js 9.9 KB

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