form-mixins.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // 表单相关的工具函数
  2. export const formUtils = {
  3. hideKeyboard(): void {
  4. uni.hideKeyboard();
  5. },
  6. selectPlace(): Promise<UniApp.ChooseLocationSuccess> {
  7. return new Promise((resolve, reject) => {
  8. uni.chooseLocation({
  9. success: (res) => {
  10. resolve(res);
  11. },
  12. fail: (error) => {
  13. console.log(error);
  14. reject(error);
  15. }
  16. });
  17. });
  18. },
  19. formatter(type: string, value: number): string {
  20. if (type === 'year') {
  21. return `${value}年`;
  22. }
  23. if (type === 'month') {
  24. return `${value}月`;
  25. }
  26. if (type === 'day') {
  27. return `${value}日`;
  28. }
  29. return value.toString();
  30. },
  31. showOptions(itemList: string[]): Promise<number> {
  32. return new Promise((resolve, reject) => {
  33. uni.showActionSheet({
  34. itemList,
  35. success(res) {
  36. resolve(res.tapIndex);
  37. },
  38. fail(error) {
  39. reject(error);
  40. }
  41. });
  42. });
  43. },
  44. setCipByNum(val: string, startNum: number, num: number, isCip: boolean = false): string {
  45. if (isCip) {
  46. return val;
  47. }
  48. if (!val) return '-';
  49. const a = val.slice(0, startNum);
  50. const b = val.substring(startNum + num);
  51. return a + '*'.repeat(num) + b;
  52. }
  53. };
  54. // 向后兼容的导出
  55. export const formMixins = {
  56. components: {},
  57. data() {
  58. return {};
  59. },
  60. methods: formUtils
  61. };