tools.js 9.0 KB

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