util.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. function friendlyDate(timestamp) {
  2. var formats = {
  3. 'year': '%n% 年前',
  4. 'month': '%n% 月前',
  5. 'day': '%n% 天前',
  6. 'hour': '%n% 小时前',
  7. 'minute': '%n% 分钟前',
  8. 'second': '%n% 秒前',
  9. };
  10. var now = Date.now();
  11. var seconds = Math.floor((now - timestamp) / 1000);
  12. var minutes = Math.floor(seconds / 60);
  13. var hours = Math.floor(minutes / 60);
  14. var days = Math.floor(hours / 24);
  15. var months = Math.floor(days / 30);
  16. var years = Math.floor(months / 12);
  17. var diffType = '';
  18. var diffValue = 0;
  19. if (years > 0) {
  20. diffType = 'year';
  21. diffValue = years;
  22. } else {
  23. if (months > 0) {
  24. diffType = 'month';
  25. diffValue = months;
  26. } else {
  27. if (days > 0) {
  28. diffType = 'day';
  29. diffValue = days;
  30. } else {
  31. if (hours > 0) {
  32. diffType = 'hour';
  33. diffValue = hours;
  34. } else {
  35. if (minutes > 0) {
  36. diffType = 'minute';
  37. diffValue = minutes;
  38. } else {
  39. diffType = 'second';
  40. diffValue = seconds === 0 ? (seconds = 1) : seconds;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. return formats[diffType].replace('%n%', diffValue);
  47. }
  48. function getStorage(key) {
  49. //#ifdef H5
  50. const value = localStorage.getItem(key);
  51. return value !== null && value !== undefined ? value : undefined;
  52. //#endif
  53. //#ifndef H5
  54. const value = uni.getStorageSync(key);
  55. return value !== null && value !== undefined ? value : undefined;
  56. //#endif
  57. }
  58. function setStorage(key, value) {
  59. //#ifdef H5
  60. localStorage.setItem(key, value);
  61. //#endif
  62. //#ifndef H5
  63. return uni.setStorageSync(key, value);
  64. //#endif
  65. }
  66. function removeStorage(key) {
  67. //#ifdef H5
  68. localStorage.removeItem(key);
  69. //#endif
  70. //#ifndef H5
  71. return uni.removeStorageSync(key);
  72. //#endif
  73. }
  74. // #ifdef APP-PLUS
  75. // 文字换行
  76. function drawtext(text, maxWidth) {
  77. let textArr = text.split("");
  78. let len = textArr.length;
  79. // 上个节点
  80. let previousNode = 0;
  81. // 记录节点宽度
  82. let nodeWidth = 0;
  83. // 文本换行数组
  84. let rowText = [];
  85. // 如果是字母,侧保存长度
  86. let letterWidth = 0;
  87. // 汉字宽度
  88. let chineseWidth = 16;
  89. // otherFont宽度
  90. let otherWidth = 8;
  91. for (let i = 0; i < len; i++) {
  92. if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])) {
  93. if (letterWidth > 0) {
  94. if (nodeWidth + chineseWidth + letterWidth * otherWidth > maxWidth) {
  95. rowText.push({
  96. type: "text",
  97. content: text.substring(previousNode, i)
  98. });
  99. previousNode = i;
  100. nodeWidth = chineseWidth;
  101. letterWidth = 0;
  102. } else {
  103. nodeWidth += chineseWidth + letterWidth * otherWidth;
  104. letterWidth = 0;
  105. }
  106. } else {
  107. if (nodeWidth + chineseWidth > maxWidth) {
  108. rowText.push({
  109. type: "text",
  110. content: text.substring(previousNode, i)
  111. });
  112. previousNode = i;
  113. nodeWidth = chineseWidth;
  114. } else {
  115. nodeWidth += chineseWidth;
  116. }
  117. }
  118. } else {
  119. if (/\n/g.test(textArr[i])) {
  120. rowText.push({
  121. type: "break",
  122. content: text.substring(previousNode, i)
  123. });
  124. previousNode = i + 1;
  125. nodeWidth = 0;
  126. letterWidth = 0;
  127. } else if (textArr[i] == "\\" && textArr[i + 1] == "n") {
  128. rowText.push({
  129. type: "break",
  130. content: text.substring(previousNode, i)
  131. });
  132. previousNode = i + 2;
  133. nodeWidth = 0;
  134. letterWidth = 0;
  135. } else if (/[a-zA-Z0-9]/g.test(textArr[i])) {
  136. letterWidth += 1;
  137. if (nodeWidth + letterWidth * otherWidth > maxWidth) {
  138. rowText.push({
  139. type: "text",
  140. content: text.substring(previousNode, i + 1 - letterWidth)
  141. });
  142. previousNode = i + 1 - letterWidth;
  143. nodeWidth = letterWidth * otherWidth;
  144. letterWidth = 0;
  145. }
  146. } else {
  147. if (nodeWidth + otherWidth > maxWidth) {
  148. rowText.push({
  149. type: "text",
  150. content: text.substring(previousNode, i)
  151. });
  152. previousNode = i;
  153. nodeWidth = otherWidth;
  154. } else {
  155. nodeWidth += otherWidth;
  156. }
  157. }
  158. }
  159. }
  160. if (previousNode < len) {
  161. rowText.push({
  162. type: "text",
  163. content: text.substring(previousNode, len)
  164. });
  165. }
  166. return rowText;
  167. }
  168. // 重写app弹窗
  169. uni.showModal = function(options) {
  170. let optionsObj = Object.assign({
  171. title: "提示",
  172. content: "自定义内容",
  173. align: "center", // 对齐方式 left/center/right
  174. cancelText: "取消", // 取消按钮的文字
  175. cancelColor: "#8F8F8F", // 取消按钮颜色
  176. confirmText: "确定", // 确认按钮文字
  177. confirmColor: "#1C79D6", // 确认按钮颜色
  178. showCancel: true, // 是否显示取消按钮,默认为 true
  179. }, options);
  180. // 以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  181. const screenWidth = plus.screen.resolutionWidth;
  182. const screenHeight = plus.screen.resolutionHeight;
  183. //弹窗容器宽度
  184. const popupViewWidth = screenWidth * 0.8;
  185. // 弹窗容器的Padding
  186. const viewContentPadding = 20;
  187. // 弹窗容器的宽度
  188. const viewContentWidth = parseInt(popupViewWidth - (viewContentPadding * 2));
  189. // 描述的列表
  190. const descriptionList = drawtext(optionsObj.content, viewContentWidth);
  191. // 弹窗高度
  192. let popupViewHeight = 168;
  193. // 弹窗遮罩层
  194. let maskLayer = new plus.nativeObj.View("maskLayer", { //先创建遮罩层
  195. top: '0px',
  196. left: '0px',
  197. height: '100%',
  198. width: '100%',
  199. backgroundColor: 'rgba(0,0,0,0.5)'
  200. });
  201. let popupViewContentList = [{
  202. tag: 'font',
  203. id: 'title',
  204. text: optionsObj.title,
  205. textStyles: {
  206. size: '18px',
  207. color: "#333",
  208. weight: "bold",
  209. whiteSpace: "normal"
  210. },
  211. position: {
  212. top: viewContentPadding + "px",
  213. left: viewContentPadding + "px",
  214. width: viewContentWidth + "px",
  215. height: "30px",
  216. }
  217. }];
  218. const textHeight = 22;
  219. let contentTop = 65;
  220. descriptionList.forEach((item, index) => {
  221. if (index > 0) {
  222. popupViewHeight += textHeight;
  223. contentTop += textHeight;
  224. }
  225. popupViewContentList.push({
  226. tag: 'font',
  227. id: 'content' + index + 1,
  228. text: item.content,
  229. textStyles: {
  230. size: '16px',
  231. color: "#333",
  232. lineSpacing: "50%",
  233. align: optionsObj.align
  234. },
  235. position: {
  236. top: contentTop + "px",
  237. left: viewContentPadding + "px",
  238. width: viewContentWidth + "px",
  239. height: textHeight + "px",
  240. }
  241. });
  242. if (item.type == "break") {
  243. contentTop += 10;
  244. popupViewHeight += 10;
  245. }
  246. });
  247. popupViewContentList.push({
  248. tag: 'rect',
  249. id: 'lineTop',
  250. rectStyles: {
  251. color: "#f1f1f1",
  252. },
  253. position: {
  254. top: contentTop + 50 + "px",
  255. left: "0px",
  256. width: "100%",
  257. height: "1px",
  258. }
  259. });
  260. if (optionsObj.showCancel) {
  261. popupViewContentList.push({
  262. tag: 'rect',
  263. id: 'line',
  264. rectStyles: {
  265. color: "#f1f1f1",
  266. },
  267. position: {
  268. top: contentTop + 50 + "px",
  269. left: popupViewWidth / 2 + "px",
  270. width: "1px",
  271. height: "50px",
  272. }
  273. });
  274. popupViewContentList.push({
  275. tag: 'font',
  276. id: 'cancelText',
  277. text: optionsObj.cancelText,
  278. textStyles: {
  279. size: '16px',
  280. color: optionsObj.cancelColor,
  281. },
  282. position: {
  283. top: contentTop + 50 + "px",
  284. left: "0px",
  285. width: popupViewWidth / 2 + "px",
  286. height: "50px",
  287. }
  288. });
  289. popupViewContentList.push({
  290. tag: 'font',
  291. id: 'confirmText',
  292. text: optionsObj.confirmText,
  293. textStyles: {
  294. size: '16px',
  295. color: optionsObj.confirmColor,
  296. },
  297. position: {
  298. top: contentTop + 50 + "px",
  299. left: popupViewWidth / 2 + "px",
  300. width: popupViewWidth / 2 + "px",
  301. height: "50px",
  302. }
  303. });
  304. } else {
  305. popupViewContentList.push({
  306. tag: 'font',
  307. id: 'confirmText',
  308. text: optionsObj.confirmText,
  309. textStyles: {
  310. size: '16px',
  311. color: optionsObj.confirmColor,
  312. },
  313. position: {
  314. top: contentTop + 50 + "px",
  315. left: "0px",
  316. width: "100%",
  317. height: "50px",
  318. }
  319. });
  320. }
  321. // 弹窗内容
  322. let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
  323. tag: "rect",
  324. top: (screenHeight - popupViewHeight) / 2 + "px",
  325. left: '10%',
  326. height: popupViewHeight + "px",
  327. width: "80%"
  328. });
  329. // 绘制白色背景
  330. popupView.drawRect({
  331. color: "#FFFFFF",
  332. radius: "8px"
  333. }, {
  334. top: "0px",
  335. height: popupViewHeight + "px",
  336. });
  337. popupView.draw(popupViewContentList);
  338. popupView.addEventListener("click", function(e) {
  339. if (optionsObj.showCancel) {
  340. if (e.clientY > popupViewHeight - 50 && e.clientX < popupViewWidth / 2) {
  341. // 取消
  342. maskLayer.close();
  343. popupView.close();
  344. options.success && options.success({
  345. confirm: false,
  346. cancel: true
  347. });
  348. } else if (e.clientY > popupViewHeight - 50 && e.clientX > popupViewWidth / 2) {
  349. // 确定
  350. maskLayer.close();
  351. popupView.close();
  352. options.success && options.success({
  353. confirm: true,
  354. cancel: false
  355. });
  356. }
  357. } else {
  358. if (e.clientY > popupViewHeight - 50) {
  359. // 确定
  360. maskLayer.close();
  361. popupView.close();
  362. options.success && options.success({
  363. confirm: true,
  364. cancel: false
  365. });
  366. }
  367. }
  368. });
  369. // 显示弹窗
  370. maskLayer.show();
  371. popupView.show();
  372. options.complete && options.complete();
  373. };
  374. // #endif
  375. export {
  376. friendlyDate,
  377. getStorage,
  378. setStorage,
  379. removeStorage
  380. }