铝交易,微信公众号
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.

202 lines
3.7 KiB

4 months ago
1 month ago
4 months ago
1 month 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. function params(url){
  131. if(typeof url == 'object'){
  132. return url
  133. }
  134. let data = {
  135. url
  136. }
  137. if(!data.url.includes('/pages')){
  138. data.url = '/pages' + data.url
  139. }
  140. return data
  141. }
  142. export function navigateTo(...args){
  143. uni.navigateTo(params(...args))
  144. }
  145. export function navigateBack(num = -1){
  146. uni.navigateBack(num)
  147. }
  148. export function redirectTo(...args){
  149. uni.redirectTo(params(...args))
  150. }
  151. export const toLogin = function(){
  152. let time = 0
  153. return () => {
  154. if(new Date().getTime() - time < 1000){
  155. return
  156. }
  157. time = new Date().getTime()
  158. uni.navigateTo({
  159. url: '/pages_order/auth/loginAndRegisterAndForgetPassword'
  160. })
  161. }
  162. }()
  163. export default {
  164. toArray,
  165. generateUUID,
  166. verificationAll,
  167. generateRandomColor,
  168. generateLightRandomColor,
  169. verificationPhone,
  170. getHrefParams,
  171. deepMergeObject,
  172. navigateTo,
  173. navigateBack,
  174. redirectTo,
  175. copyText,
  176. toLogin
  177. }