特易招,招聘小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.1 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. function toArray(data) {
  2. if (!data) return []
  3. if (data instanceof Array){
  4. return data
  5. } else {
  6. return [data]
  7. }
  8. }
  9. function generateUUID() {
  10. return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  11. var r = Math.random() * 16 | 0,
  12. v = c == 'x' ? r : (r & 0x3 | 0x8);
  13. return v.toString(16);
  14. });
  15. }
  16. function generateRandomColor() {
  17. const letters = '0123456789ABCDEF';
  18. let color = '#';
  19. for (let i = 0; i < 6; i++) {
  20. color += letters[Math.floor(Math.random() * 16)];
  21. }
  22. return color;
  23. }
  24. function generateLightRandomColor() {
  25. const min = 150;
  26. const range = 105;
  27. const r = Math.floor(Math.random() * range + min);
  28. const g = Math.floor(Math.random() * range + min);
  29. const b = Math.floor(Math.random() * range + min);
  30. const color = 'rgb(' + r + ',' + g + ',' + b + ')';
  31. return color;
  32. }
  33. function verificationAll(data, msg){
  34. if (!msg){
  35. console.log(msg);
  36. return false
  37. }
  38. if (!data){
  39. uni.showToast({
  40. title: '表单数据未填写',
  41. icon: "none"
  42. })
  43. }
  44. for (let key in msg) {
  45. if (!data[key]) {
  46. uni.showToast({
  47. title: msg[key],
  48. icon: "none"
  49. })
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55. //验证手机号是否合法
  56. function verificationPhone(phone){
  57. if(!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone)){
  58. return false
  59. }
  60. return true
  61. }
  62. //获取url中参数的方法
  63. export function getHrefParams(name) {
  64. var url = window.location.href;
  65. try {
  66. var cs = url.split('?')[1]; //获取?之后的参数字符串
  67. var cs_arr = cs.split('&'); //参数字符串分割为数组
  68. for (var i = 0; i < cs_arr.length; i++) { //遍历数组,拿到json对象
  69. if (cs_arr[i].split('=')[0] == name) {
  70. return cs_arr[i].split('=')[1];
  71. }
  72. }
  73. return "";
  74. } catch {
  75. return "";
  76. }
  77. }
  78. // 将字符串中的文本格式化html
  79. export function stringFormatHtml(str){
  80. return str && str.replace(/\n/gi, '<br>')
  81. .replace(/ /gi, ' ')
  82. }
  83. //深度对比合并两个对象,相同属性b会覆盖a
  84. export function deepMergeObject(a, b){
  85. let data = JSON.parse(JSON.stringify(a))
  86. function mergeObject(obj1, obj2){
  87. for(let key in obj2){
  88. if(typeof obj1[key] == 'object'){
  89. obj1[key] = mergeObject(obj1[key], obj2[key])
  90. }else{
  91. obj1[key] = obj2[key]
  92. }
  93. }
  94. return obj1
  95. }
  96. return mergeObject(data, b)
  97. }
  98. //复制内容
  99. export function copyText(content) {
  100. uni.setClipboardData({
  101. data: content,
  102. success: () => {
  103. uni.showToast({
  104. title: '复制成功',
  105. icon: 'none'
  106. })
  107. }
  108. })
  109. }
  110. function params(url){
  111. if(typeof url == 'object'){
  112. return url
  113. }
  114. let data = {
  115. url
  116. }
  117. if(!data.url.includes('/pages')){
  118. data.url = '/pages' + data.url
  119. }
  120. return data
  121. }
  122. export function navigateTo(...args){
  123. uni.navigateTo(params(...args))
  124. }
  125. export function navigateBack(num = -1){
  126. uni.navigateBack(num)
  127. }
  128. export function redirectTo(...args){
  129. uni.redirectTo(params(...args))
  130. }
  131. export default {
  132. toArray,
  133. generateUUID,
  134. verificationAll,
  135. generateRandomColor,
  136. generateLightRandomColor,
  137. verificationPhone,
  138. getHrefParams,
  139. deepMergeObject,
  140. navigateTo,
  141. navigateBack,
  142. redirectTo,
  143. copyText,
  144. stringFormatHtml
  145. }