tools.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. tools.updateVersion = function (sysVersion, appUrl) {
  141. let app_version = plus.runtime.version;
  142. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  143. console.log(app_version < sysVersion)
  144. if (app_version < sysVersion) {
  145. uni.showLoading({
  146. title: '更新中……'
  147. })
  148. uni.downloadFile({//执行下载
  149. url: appUrl, //下载地址
  150. success: (downloadResult) => {//下载成功
  151. uni.hideLoading();
  152. if (downloadResult.statusCode === 200) {
  153. uni.showModal({
  154. title: '',
  155. content: '更新成功,确定现在重启吗?',
  156. confirmText: '重启',
  157. confirmColor: '#EE8F57',
  158. success: function (res) {
  159. if (res.confirm === true) {
  160. plus.runtime.install(//安装
  161. downloadResult.tempFilePath, {
  162. force: true
  163. },
  164. function (res) {
  165. tools.success('更新成功,重启中')
  166. plus.runtime.restart();
  167. }
  168. );
  169. }
  170. }
  171. });
  172. }
  173. }
  174. });
  175. } else {
  176. // tools.success('你已是最新版本')
  177. }
  178. }
  179. /**
  180. * uniapp html图片显示控制
  181. * @param str
  182. * @returns {*}
  183. */
  184. tools.imgDeal = function (str) {
  185. console.log(str)
  186. if(str===null || str===undefined){
  187. return '';
  188. }else {
  189. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  190. }
  191. }
  192. /**
  193. * 获取平台类型 1:微信,2:支付宝
  194. * @returns {boolean|number}
  195. */
  196. tools.platformType = function () {
  197. let ua = window.navigator.userAgent.toLowerCase();
  198. if (ua.indexOf('micromessenger') != -1) {
  199. return 1;
  200. } else if (ua.indexOf('alipay') != -1) {
  201. return 2;
  202. } else {
  203. return 0;
  204. }
  205. }
  206. /**
  207. * 获取开发平台
  208. * @returns {string}
  209. */
  210. tools.getPlatform = function () {
  211. let platForm = undefined;
  212. // #ifdef H5
  213. platForm = 'H5';
  214. //#endif
  215. // #ifdef APP-PLUS
  216. platForm = 'APP';
  217. //#endif
  218. // #ifdef APP-PLUS-NVUE
  219. platForm = 'APP';
  220. //#endif
  221. // #ifdef APP-NVUE
  222. platForm = 'APP';
  223. //#endif
  224. // #ifdef MP-WEIXIN
  225. platForm = 'MP-WEIXIN'; // 小程序
  226. //#endif
  227. // #ifdef MP-ALIPAY
  228. platForm = 'MP-ALIPAY';
  229. //#endif
  230. // #ifdef MP-BAIDU
  231. platForm = 'MP-BAIDU';
  232. //#endif
  233. // #ifdef MP-TOUTIAO
  234. platForm = 'MP-TOUTIAO';
  235. //#endif
  236. // #ifdef MP-LARK
  237. platForm = 'MP-LARK';
  238. //#endif
  239. // #ifdef MP-QQ
  240. platForm = 'MP-QQ';
  241. //#endif
  242. // #ifdef MP-KUAISHOU
  243. platForm = 'MP-KUAISHOU';
  244. //#endif
  245. // #ifdef QUICKAPP-WEBVIEW
  246. platForm = 'QUICKAPP-WEBVIEW';
  247. //#endif
  248. return platForm;
  249. }
  250. /**
  251. * 记录用户登录信息
  252. * @param data
  253. * @param type
  254. */
  255. tools.setLoginData = function (data) {
  256. uni.setStorageSync('m_id', data.m_id)
  257. uni.setStorageSync('token', data.token)
  258. tools.success('登陆成功')
  259. setTimeout(() => {
  260. uni.reLaunch({
  261. url: '/pages/index/index'
  262. });
  263. }, 1500)
  264. }
  265. /**
  266. * 微信JSDK授权
  267. * @param jsApiList
  268. * @returns {Promise<*>}
  269. */
  270. tools.wxInitialize = async function (jsApiList) {
  271. return await getWxJsSign({ 'url': window.location.href,'type':1 }).then((ret) => {
  272. if (ret.code === 1) {
  273. wxJs.config({
  274. beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  275. debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  276. appId: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  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(res)
  283. console.log('config:---------------OK')
  284. tools.success('成功')
  285. },
  286. fail: function(res) {
  287. console.log('config-error')
  288. console.log(res)
  289. }
  290. });
  291. return true;
  292. } else {
  293. return false;
  294. }
  295. })
  296. }
  297. /**
  298. * 企业微信信息授权
  299. * @param jsApiList
  300. * @returns {Promise<unknown>}
  301. */
  302. tools.wxAgentInitialize=async function (jsApiList){
  303. return await getWxJsSign({ 'url': window.location.href ,'type':2}).then((ret) => {
  304. if (ret.code === 1) {
  305. jWeixin.agentConfig({
  306. corpid: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  307. agentid: ret.data.agentid, // 必填,企业微信的应用id (e.g. 1000247)
  308. timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
  309. nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
  310. signature: ret.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
  311. jsApiList: jsApiList,// 必填,需要使用的JS接口列表
  312. success: function(res) {
  313. console.log('agentConfig:---------------OK')
  314. console.log(res)
  315. },
  316. fail: function(res) {
  317. console.log('agentConfig-error')
  318. console.log(res)
  319. if(res.errMsg.indexOf('function not exist') > -1){
  320. alert('版本过低请升级')
  321. }
  322. }
  323. });
  324. return true;
  325. } else {
  326. return false;
  327. }
  328. })
  329. }
  330. tools.wxLogin = function () {
  331. console.log(Base64.encode('aaa'));
  332. // 微信浏览器
  333. let url = encodeURIComponent(window.location.href);
  334. window.location.href = `https://test-api.wealfavor.cn/api/weChat/wx?redirect_url=${Base64.encode(url)}`
  335. }
  336. export default tools