tools.js 8.3 KB

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