utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. export default {
  2. // 页面提示信息函数
  3. showPrompt(msg) {
  4. uni.showToast({
  5. title: msg,
  6. icon: 'none',
  7. mask: true
  8. })
  9. },
  10. // APP名称
  11. appName() {
  12. return "贞达市政工程"
  13. },
  14. formatRichText(html){
  15. let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
  16. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  17. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  18. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  19. return match;
  20. });
  21. newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
  22. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  23. return match;
  24. });
  25. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  26. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
  27. return newContent;
  28. },
  29. getHeight(e) {
  30. console.log(e)
  31. },
  32. changeTwoDecimal_f(x) {
  33. var f_x = parseFloat(x);
  34. if (isNaN(f_x)) {
  35. return 0;
  36. }
  37. var f_x = Math.round(x * 100) / 100;
  38. var s_x = f_x.toString();
  39. var pos_decimal = s_x.indexOf('.');
  40. if (pos_decimal < 0) {
  41. pos_decimal = s_x.length;
  42. s_x += '.';
  43. }
  44. while (s_x.length <= pos_decimal + 2) {
  45. s_x += '0';
  46. }
  47. return s_x;
  48. },
  49. // token过期
  50. tokenOverdue() {
  51. uni.setStorage({
  52. key: "jumpUrl",
  53. data: ''
  54. })
  55. uni.setStorage({
  56. key: "jumpType",
  57. data: ''
  58. })
  59. uni.navigateTo({
  60. url: "/pages/login/login"
  61. })
  62. },
  63. checkLoginTo(url, type) {
  64. console.log(type)
  65. let status = uni.getStorageSync('token')
  66. console.log(status)
  67. if (status) {
  68. uni.navigateTo({
  69. url: url
  70. })
  71. } else {
  72. uni.showModal({
  73. title: '',
  74. content: '您未登录,是否登录?',
  75. success: res => {
  76. if (res.confirm) {
  77. uni.setStorage({
  78. key: "jumpUrl",
  79. data: url
  80. })
  81. uni.setStorage({
  82. key: "jumpType",
  83. data: type
  84. })
  85. uni.navigateTo({
  86. url: "/pages/login/login"
  87. })
  88. }
  89. }
  90. })
  91. }
  92. },
  93. // 拨打电话
  94. toCall(tel) {
  95. if (!tel) {
  96. this.showPrompt('暂无商家电话')
  97. return
  98. }
  99. uni.makePhoneCall({
  100. phoneNumber: tel
  101. });
  102. },
  103. toBack() {
  104. uni.navigateBack({
  105. delta: 1
  106. })
  107. },
  108. // 图片预览
  109. previewImage(url) {
  110. let urlList = []
  111. if (typeof url == 'string') {
  112. urlList.push(url)
  113. } else {
  114. urlList = url
  115. }
  116. uni.previewImage({
  117. urls: urlList
  118. })
  119. },
  120. // 登录成功后的跳转
  121. loginSuccess() {
  122. let type = uni.getStorageSync('jumpType')
  123. // console.log(type)
  124. if (type) {
  125. let url = uni.getStorageSync('jumpUrl')
  126. // console.log(url)
  127. if (!url) {
  128. uni.navigateBack({
  129. delta: 1
  130. });
  131. return
  132. }
  133. if (type == 'page') {
  134. uni.redirectTo({
  135. url: url
  136. })
  137. } else {
  138. uni.switchTab({
  139. url: url,
  140. fail: error => {
  141. console.log(error)
  142. }
  143. })
  144. }
  145. } else {
  146. // uni.switchTab({
  147. // url: "/pages/index/index"
  148. // })
  149. uni.navigateBack({
  150. delta: 1
  151. });
  152. }
  153. },
  154. parseNumber(num) {
  155. return num < 10 ? "0" + num : num;
  156. },
  157. // 时间格式化基类
  158. dateFormat(date, formatStr) {
  159. let dateObj = {},
  160. rStr = /\{([^}]+)\}/,
  161. mons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
  162. dateObj["Y"] = date.getFullYear();
  163. dateObj["M"] = date.getMonth() + 1;
  164. dateObj["MM"] = this.parseNumber(dateObj["M"]);
  165. dateObj["Mon"] = mons[dateObj['M'] - 1];
  166. dateObj["D"] = date.getDate();
  167. dateObj["DD"] = this.parseNumber(dateObj["D"]);
  168. dateObj["h"] = date.getHours();
  169. dateObj["hh"] = this.parseNumber(dateObj["h"]);
  170. // dateObj["t"] = dateObj["h"] > 12 ? dateObj["h"] - 12 : dateObj["h"];
  171. dateObj["t"] = dateObj["h"]
  172. dateObj["tt"] = this.parseNumber(dateObj["t"]);
  173. // dateObj["A"] = dateObj["h"] > 12 ? '下午' : '上午';
  174. dateObj["i"] = date.getMinutes();
  175. dateObj["ii"] = this.parseNumber(dateObj["i"]);
  176. dateObj["s"] = date.getSeconds();
  177. dateObj["ss"] = this.parseNumber(dateObj["s"]);
  178. while (rStr.test(formatStr)) {
  179. formatStr = formatStr.replace(rStr, dateObj[RegExp.$1]);
  180. }
  181. return formatStr;
  182. },
  183. // 时间格式 年月日时分秒
  184. gettime(shorttime, type) {
  185. shorttime = shorttime.toString().length < 13 ? shorttime * 1000 : shorttime;
  186. let now = (new Date()).getTime();
  187. let cha = (now - parseInt(shorttime)) / 1000
  188. // type: 格式化类型 1为年月日时分秒 2为年月日时分 3为年月日
  189. if (type == 1) {
  190. return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD} {t}:{ii}:{ss}");
  191. } else if (type == 2) {
  192. return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD} {t}:{ii}");
  193. } else if (type == 3) {
  194. return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD}");
  195. }
  196. },
  197. // 时间格式 年月日
  198. getDate(shorttime) {
  199. shorttime = shorttime.toString().length < 13 ? shorttime * 1000 : shorttime
  200. let now = (new Date()).getTime()
  201. let cha = (now - parseInt(shorttime)) / 1000
  202. return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD}");
  203. },
  204. // 时间格式 月日
  205. getDateMd(shorttime) {
  206. shorttime = shorttime.toString().length < 13 ? shorttime * 1000 : shorttime
  207. let now = (new Date()).getTime()
  208. let cha = (now - parseInt(shorttime)) / 1000
  209. return this.dateFormat(new Date(shorttime), "{MM}-{DD}");
  210. },
  211. getSevenDate(num) {
  212. let date1 = new Date();
  213. //今天时间
  214. let time1 = ((date1.getMonth() + 1) < 10 ? '0' + (date1.getMonth() + 1).toString() : (date1.getMonth() + 1)) +
  215. "-" +
  216. (date1.getDate() < 10 ? '0' + date1.getDate().toString() : date1.getDate())
  217. let date2 = new Date(date1);
  218. date2.setDate(date1.getDate() + num);
  219. //num是正数表示之后的时间,num负数表示之前的时间,0表示今天
  220. let time2 = (date2.getMonth() + 1) + "-" + date2.getDate();
  221. return this.getdiffdate(time1, time2, num)
  222. },
  223. //获取两日期之间日期列表函数
  224. getdiffdate(stime, etime, num) {
  225. //初始化日期列表,数组
  226. let diffdate = new Array()
  227. let i = 0
  228. //开始日期小于等于结束日期,并循环
  229. while (i <= num - 1) {
  230. diffdate.push({
  231. dateStr: stime,
  232. weekStr: this.getWeek(new Date().getFullYear().toString() + "-" + stime)
  233. })
  234. //获取开始日期时间戳
  235. let stime_ts = new Date(stime).getTime()
  236. // console.log('当前日期:'+stime +'当前时间戳:'+stime_ts)
  237. //增加一天时间戳后的日期
  238. let next_date = stime_ts + (24 * 60 * 60 * 1000)
  239. //拼接年月日,这里的月份会返回(0-11),所以要+1
  240. let next_dates_y = new Date(next_date).getFullYear() + '-'
  241. let next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
  242. '-' : (
  243. new Date(next_date).getMonth() + 1) + '-'
  244. let next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
  245. next_date).getDate()
  246. stime = next_dates_m + next_dates_d
  247. // 增加数组key
  248. i++
  249. }
  250. return diffdate
  251. },
  252. // 根据日期转换星期
  253. getWeek(dateString) {
  254. let dateArray = dateString.split("-");
  255. let date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
  256. return "周" + "日一二三四五六".charAt(date.getDay());
  257. },
  258. // 获取当前位置
  259. getNowAddress() {
  260. console.log(222)
  261. return new Promise((resolve, reject) => {
  262. let mapkey = ''
  263. // #ifdef MP-WEIXIN
  264. mapkey = 'c827c04d1d74d8589ec826fce02a135a'
  265. // #endif
  266. // #ifdef APP-PLUS
  267. mapkey = '7923ab86c887ab9c77e9e28828567622 '
  268. // #endif
  269. console.log(mapkey)
  270. let amapPlugin = new amap.AMapWX({
  271. key: mapkey
  272. })
  273. amapPlugin.getRegeo({
  274. success: res => {
  275. resolve(res[0])
  276. },
  277. fail: error => {
  278. console.log(error)
  279. uni.showToast({
  280. mask: true,
  281. icon: "none",
  282. title: error
  283. })
  284. }
  285. })
  286. })
  287. }
  288. }