tools.js 9.1 KB

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