瑶都万能墙
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.

208 lines
3.8 KiB

4 months ago
4 months ago
4 months ago
3 months ago
4 months ago
4 months ago
3 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. // let Msgs = {
  54. // default : msg || '表单数据未填写'
  55. // }
  56. // if(typeof msg == 'object'){
  57. // Msgs = {
  58. // default : '表单数据未填写',
  59. // ...msg,
  60. // }
  61. // }
  62. // if (!data){
  63. // uni.showToast({
  64. // title: Msgs.default,
  65. // icon: "none"
  66. // })
  67. // return true
  68. // }
  69. // for (let key in data) {
  70. // if (!data[key] || data[key] === "") {
  71. // uni.showToast({
  72. // title: (Msgs[key] || Msgs.default),
  73. // icon: "none"
  74. // })
  75. // return true
  76. // }
  77. // }
  78. return false
  79. }
  80. //验证手机号是否合法
  81. function verificationPhone(phone){
  82. 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)){
  83. return false
  84. }
  85. return true
  86. }
  87. //获取url中参数的方法
  88. export function getHrefParams(name) {
  89. var url = window.location.href;
  90. try {
  91. var cs = url.split('?')[1]; //获取?之后的参数字符串
  92. var cs_arr = cs.split('&'); //参数字符串分割为数组
  93. for (var i = 0; i < cs_arr.length; i++) { //遍历数组,拿到json对象
  94. if (cs_arr[i].split('=')[0] == name) {
  95. return cs_arr[i].split('=')[1];
  96. }
  97. }
  98. return "";
  99. } catch {
  100. return "";
  101. }
  102. }
  103. //深度对比合并两个对象,相同属性b会覆盖a
  104. export function deepMergeObject(a, b){
  105. let data = JSON.parse(JSON.stringify(a))
  106. function mergeObject(obj1, obj2){
  107. for(let key in obj2){
  108. if(typeof obj1[key] == 'object'){
  109. obj1[key] = mergeObject(obj1[key], obj2[key])
  110. }else{
  111. obj1[key] = obj2[key]
  112. }
  113. }
  114. return obj1
  115. }
  116. return mergeObject(data, b)
  117. }
  118. //复制内容
  119. export function copyText(content) {
  120. uni.setClipboardData({
  121. data: content,
  122. success: () => {
  123. uni.showToast({
  124. title: '复制成功',
  125. icon: 'none'
  126. })
  127. }
  128. })
  129. }
  130. // 将字符串中的文本格式化html
  131. export function stringFormatHtml(str){
  132. return str && str.replace(/\n/gi, '<br>')
  133. .replace(/ /gi, ' ')
  134. }
  135. function params(url){
  136. if(typeof url == 'object'){
  137. return url
  138. }
  139. let data = {
  140. url
  141. }
  142. if(!data.url.includes('/pages')){
  143. data.url = '/pages' + data.url
  144. }
  145. return data
  146. }
  147. export function navigateTo(...args){
  148. uni.navigateTo(params(...args))
  149. }
  150. export function navigateBack(num = -1){
  151. uni.navigateBack(num)
  152. }
  153. export function redirectTo(...args){
  154. uni.redirectTo(params(...args))
  155. }
  156. export const toLogin = function(){
  157. let time = 0
  158. return () => {
  159. if(new Date().getTime() - time < 1000){
  160. return
  161. }
  162. time = new Date().getTime()
  163. uni.navigateTo({
  164. url: '/pages_order/auth/wxLogin'
  165. })
  166. }
  167. }()
  168. export default {
  169. toArray,
  170. generateUUID,
  171. verificationAll,
  172. generateRandomColor,
  173. generateLightRandomColor,
  174. verificationPhone,
  175. getHrefParams,
  176. deepMergeObject,
  177. navigateTo,
  178. navigateBack,
  179. redirectTo,
  180. copyText,
  181. stringFormatHtml,
  182. toLogin
  183. }