tools.js 9.0 KB

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