百富门答题小程序
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.

167 lines
3.1 KiB

5 months ago
3 months ago
5 months ago
3 months ago
5 months ago
3 months ago
5 months ago
3 months ago
5 months ago
3 months ago
5 months ago
3 months ago
5 months ago
3 months ago
5 months ago
3 months ago
5 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(!/^1\d{10}$/.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. //深度对比合并两个对象,相同属性b会覆盖a
  79. export function deepMergeObject(a, b){
  80. let data = JSON.parse(JSON.stringify(a))
  81. function mergeObject(obj1, obj2){
  82. for(let key in obj2){
  83. if(typeof obj1[key] == 'object'){
  84. obj1[key] = mergeObject(obj1[key], obj2[key])
  85. }else{
  86. obj1[key] = obj2[key]
  87. }
  88. }
  89. return obj1
  90. }
  91. return mergeObject(data, b)
  92. }
  93. function params(url){
  94. if(typeof url == 'object'){
  95. return url
  96. }
  97. let data = {
  98. url
  99. }
  100. if(!data.url.includes('/pages')){
  101. data.url = '/pages' + data.url
  102. }
  103. return data
  104. }
  105. export function navigateTo(...args){
  106. uni.navigateTo(params(...args))
  107. }
  108. export function navigateBack(num = -1){
  109. uni.navigateBack(num)
  110. }
  111. export function redirectTo(...args){
  112. uni.redirectTo(params(...args))
  113. }
  114. // 将字符串中的文本格式化html
  115. export function stringFormatHtml(str){
  116. return str && str.replace(/\n/gi, '<br>')
  117. .replace(/ /gi, ' ')
  118. }
  119. export const toLogin = function(){
  120. let time = 0
  121. return () => {
  122. if(new Date().getTime() - time < 1000){
  123. return
  124. }
  125. time = new Date().getTime()
  126. uni.navigateTo({
  127. url: '/pages_order/auth/wxLogin'
  128. })
  129. }
  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. stringFormatHtml,
  144. toLogin,
  145. }