康巴易测肤/伤疤uniapp小程序类
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

299 lines
7.3KB

  1. import Vue from 'vue'
  2. const FormData = require('./formData.js')
  3. function formateDate(fmt = 'yyyy-mm-dd', date = '') {
  4. if (!date) {
  5. date = new Date()
  6. }
  7. try {
  8. if (typeof date == 'string') {
  9. date = date.replace(/-/g, '/').replace(/T/g, ' ')
  10. if (date.indexOf('/') == -1) {
  11. date = new Date(parseFloat(date))
  12. }
  13. date = new Date(date)
  14. } else if (typeof date == 'number') {
  15. date = new Date(date)
  16. }
  17. } catch (error) {
  18. console.log('时间格式化出错', error)
  19. date = new Date()
  20. }
  21. let ret
  22. let weak = (function (date) {
  23. let days = date.getDay()
  24. let weekArrTxt = [
  25. '星期天',
  26. '星期一',
  27. '星期二',
  28. '星期三',
  29. '星期四',
  30. '星期五',
  31. '星期六',
  32. ]
  33. let weekArrTxt2 = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  34. return [weekArrTxt[days], weekArrTxt2[days]]
  35. })(date)
  36. const opt = {
  37. 'y+': date.getFullYear().toString(), // 年
  38. 'm+': (date.getMonth() + 1).toString(), // 月
  39. 'd+': date.getDate().toString(), // 日
  40. 'H+': date.getHours().toString(), // 时
  41. 'M+': date.getMinutes().toString(), // 分
  42. 'S+': date.getSeconds().toString(), // 秒
  43. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  44. 'W+': weak[0],
  45. 'w+': weak[1],
  46. }
  47. for (let k in opt) {
  48. ret = new RegExp('(' + k + ')').exec(fmt)
  49. if (ret) {
  50. fmt = fmt.replace(
  51. ret[1],
  52. ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
  53. )
  54. }
  55. }
  56. return fmt
  57. }
  58. function getDateDiff(time = new Date()) {
  59. if (time == '') return '未知'
  60. // 当前时间
  61. let now = new Date()
  62. let ny = formateDate('yyyy', now)
  63. let nm = formateDate('mm', now)
  64. let nd = formateDate('dd', now)
  65. let nH = formateDate('HH', now)
  66. let nM = formateDate('MM', now)
  67. let nS = formateDate('SS', now)
  68. let oDate = new Date(formateDate('yyyy/mm/dd HH:MM:SS', time))
  69. let oy = formateDate('yyyy', oDate)
  70. let om = formateDate('mm', oDate)
  71. let od = formateDate('dd', oDate)
  72. let oH = formateDate('HH', oDate)
  73. let oM = formateDate('MM', oDate)
  74. let oS = formateDate('SS', oDate)
  75. // console.log(parseInt(nm), parseInt(om));
  76. if ('' + ny + nm + nd + nH + nM == '' + oy + om + od + oH + oM) {
  77. //同分
  78. return '刚刚'
  79. } else if ('' + ny + nm + nd + nH == '' + oy + om + od + oH) {
  80. //同时
  81. return parseInt(nM) - parseInt(oM) + '分钟前'
  82. } else if ('' + ny + nm + nd == '' + oy + om + od) {
  83. //同天
  84. return oH + ':' + oM
  85. } else if ('' + ny + nm == '' + oy + om) {
  86. //同月
  87. return om + '-' + od + ' ' + oH + ':' + oM
  88. } else if ('' + ny == '' + oy) {
  89. //同年
  90. return om + '-' + od + ' ' + oH + ':' + oM
  91. } else {
  92. return oy + '-' + om + '-' + od
  93. }
  94. }
  95. //检测url是否合法
  96. function TestUrl(url) {
  97. let regex = new RegExp(
  98. '(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]'
  99. )
  100. if (regex.test(url)) {
  101. return true
  102. } else {
  103. return false
  104. }
  105. }
  106. var ThrottleObject = new Map()
  107. //节流器 程序名 执行函数 节流时间
  108. function InitThrottle(name, event, time) {
  109. if (ThrottleObject.get(name)) clearTimeout(ThrottleObject.get(name)) //清除前程序
  110. //执行方法
  111. let obj = setTimeout(() => {
  112. ThrottleObject.delete(name)
  113. event()
  114. }, time)
  115. //存入栈
  116. ThrottleObject.set(name, obj)
  117. }
  118. //页面数据持久化工具 , app 使用 页面名称 this对象 带 async 的回调函数
  119. async function PersistencePages(name, vdom, callback) {
  120. // #ifndef APP-PLUS
  121. callback()
  122. // #endif
  123. // #ifdef APP-PLUS
  124. let key = 'Persistence_' + name
  125. let odata = await uni.getStorageSync(key)
  126. let net = await getNetWork()
  127. if (odata && !net) {
  128. //取
  129. Object.keys(odata).forEach((item) => {
  130. vdom[item] = odata[item]
  131. })
  132. } else if (net) {
  133. //存
  134. //先执行页面数据获取
  135. await callback()
  136. let data = {}
  137. Object.keys(vdom).forEach((item) => {
  138. if (item.indexOf('_') == -1 && item.indexOf('$') == -1) {
  139. //排除原生、全局参数
  140. if (typeof vdom[item] != 'function') {
  141. //排除方法
  142. data[item] = vdom[item]
  143. }
  144. }
  145. })
  146. //缓存数据
  147. uni.setStorageSync(key, data)
  148. }
  149. // #endif
  150. }
  151. async function getNetWork() {
  152. return new Promise((res, rej) => {
  153. uni.getNetworkType({
  154. success: (re) => {
  155. if (re.networkType == 'none') {
  156. res(false)
  157. } else {
  158. res(true)
  159. }
  160. },
  161. fail() {
  162. res(false)
  163. },
  164. })
  165. })
  166. }
  167. // 文件tempUrl 转 上传参数
  168. function tempUrlToUpload(tempUrl, fileName, other = {}) {
  169. let formData = new FormData()
  170. if (tempUrl) {
  171. formData.appendFile('File', tempUrl, fileName)
  172. }
  173. // 遍历增加额外参数
  174. Object.keys(other).forEach((key) => {
  175. formData.append(key, other[`${key}`])
  176. })
  177. let data = formData.getData()
  178. return {
  179. data: data.buffer,
  180. header: {
  181. 'content-type': data.contentType,
  182. },
  183. }
  184. }
  185. // 列表追加key
  186. function listAddKey(list) {
  187. console.log(list)
  188. return list.map((item) => {
  189. return {
  190. ...item,
  191. key: Vue.prototype.$u.guid(),
  192. }
  193. })
  194. }
  195. function getColorForStr(str) {
  196. if (!str) {
  197. return '#ffffff'
  198. }
  199. // 姓名拼音首字母为基础生成背景色,过滤白色及相近色
  200. // 定义26个颜色
  201. const colors = [
  202. '#000000', // 黑色
  203. '#1C1C1C', // 深灰色
  204. '#333333', // 较深灰色
  205. '#400000', // 深红色
  206. '#004000', // 深绿色
  207. '#000040', // 深蓝色
  208. '#590059', // 深紫红色
  209. '#404000', // 深橄榄色
  210. '#004040', // 深青色
  211. '#660000', // 更深红色
  212. '#006600', // 更浅绿色
  213. '#000066', // 更深蓝色
  214. '#7F0000', // 极深红色
  215. '#007F00', // 极深绿色
  216. '#00007F', // 极深蓝色
  217. '#8B008B', // 深洋红色
  218. '#990000', // 非常深红色
  219. '#009900', // 非常深绿色
  220. '#000099', // 非常深蓝色
  221. '#A52A2A', // 褐红色
  222. '#00A500', // 深草绿色
  223. '#0000A5', // 深宝蓝色
  224. '#B22222', // 耐火砖色
  225. '#00B200', // 深翠绿色
  226. '#0000B2', // 深海军蓝色
  227. '#C71585', // 洋红色
  228. ]
  229. const index = str.charCodeAt(0) % 26
  230. return colors[index]
  231. }
  232. function calculateAge(birthdate) {
  233. const birthdateObj = new Date(birthdate); // 将出生日期转换为 Date 对象
  234. const currentDate = new Date(); // 获取当前日期
  235. let age = currentDate.getFullYear() - birthdateObj.getFullYear(); // 计算年份差
  236. // 检查当前日期是否在出生日期的月份和日期之前
  237. if (currentDate.getMonth() < birthdateObj.getMonth() ||
  238. (currentDate.getMonth() === birthdateObj.getMonth() && currentDate.getDate() < birthdateObj.getDate())) {
  239. age--; // 如果是,则岁数减 1
  240. }
  241. return age; // 返回计算出的岁数
  242. }
  243. function getDayName() {
  244. const daysOfWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  245. const currentDate = new Date(); // 获取当前日期
  246. const dayIndex = currentDate.getDay(); // 获取当前日期是星期几(0-6)
  247. return daysOfWeek[dayIndex]; // 返回对应的星期名称
  248. }
  249. function getPaddTopheight() {
  250. const systemInfo = uni.getSystemInfoSync();
  251. let statusBarHeight = systemInfo.statusBarHeight;
  252. let navBarHeight = 44;
  253. let paddTopheight = '';
  254. if (systemInfo.platform === 'ios') {
  255. navBarHeight = 44;
  256. } else if (systemInfo.platform === 'android') {
  257. navBarHeight = 50;
  258. }
  259. paddTopheight = navBarHeight + statusBarHeight;
  260. return paddTopheight || 70
  261. }
  262. const util = {
  263. formateDate,
  264. PersistencePages,
  265. getNetWork,
  266. getDateDiff,
  267. TestUrl,
  268. tempUrlToUpload,
  269. listAddKey,
  270. getColorForStr,
  271. calculateAge,
  272. getDayName,
  273. getPaddTopheight
  274. }
  275. export default util