tools.js 9.1 KB

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