tools.js 8.6 KB

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