index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const formLayout = {
  5. fieldId: '101', //对应一级表单层级
  6. fieldName: '基本信息',
  7. formInfo: [ //每个层级下面 具体表单元素
  8. {
  9. label: '名字',//标题
  10. type: 'text', //表单类型 text,upload,picker,time
  11. id: 'input-name-form', //表单id
  12. placeholder: '输入您的姓名',//设置文本框默认提示
  13. data: [], //填充表单的数据 例如下拉框
  14. role: {
  15. type: 'reg',
  16. value: '',//正则表达式
  17. },
  18. force: true,//是否必输入
  19. },
  20. {
  21. label: '手机号',//标题
  22. type: 'text', //表单类型 text,upload,picker,time
  23. id: 'input-id-form', //表单id
  24. placeholder: '输入您的手机号',//设置文本框默认提示
  25. data: [], //填充表单的数据 例如下拉框
  26. role: {
  27. type: 'reg',
  28. formatter:(v)=>{
  29. let reg = /^1[3-9]\d{9}$/
  30. if (!reg.test(v)) return '手机号输入有误'
  31. return ''
  32. }
  33. },
  34. force: true,//是否必输入
  35. },
  36. {
  37. label: '性别',//标题
  38. type: 'picker', //表单类型 text,upload,picker,time
  39. id: 'input-sex-form', //表单id
  40. inputValue:'男',
  41. placeholder: '输入您的姓名',//设置文本框默认提示
  42. data: [
  43. { value: 1, label: '男' },
  44. { value: 2, label: '女' },
  45. ], //填充表单的数据 例如下拉框
  46. role: {
  47. type: 'reg',
  48. value: '',//正则表达式
  49. },
  50. force: false,//是否必输入
  51. },
  52. {
  53. label: '上传',//标题
  54. type: 'upload', //表单类型 text,upload,picker,time
  55. id: 'input-sex-form', //表单id
  56. inputValue:'男',
  57. placeholder: '输入您的姓名',//设置文本框默认提示
  58. data: [
  59. { value: 1, label: '男' },
  60. { value: 2, label: '女' },
  61. ], //填充表单的数据 例如下拉框
  62. role: {
  63. type: 'reg',
  64. value: '',//正则表达式
  65. },
  66. force: false,//是否必输入
  67. },
  68. {
  69. label: '选择日期',//标题
  70. type: 'datepicker', //表单类型 text,upload,picker,time
  71. id: 'input-sex-form', //表单id
  72. inputValue:(new Date()).toISOString().split('T')[0],
  73. placeholder: '选择日期',//设置文本框默认提示
  74. role: {
  75. type: 'reg',
  76. value: '',//正则表达式
  77. },
  78. force: false,//是否必输入
  79. },
  80. ]
  81. }
  82. Page({
  83. data: {
  84. motto: 'Hello World',
  85. userInfo: {},
  86. hasUserInfo: false,
  87. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  88. canIUseGetUserProfile: false,
  89. canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
  90. formLayout
  91. },
  92. // 事件处理函数
  93. bindViewTap() {
  94. wx.navigateTo({
  95. url: '../logs/logs'
  96. })
  97. },
  98. onLoad() {
  99. if (wx.getUserProfile) {
  100. this.setData({
  101. canIUseGetUserProfile: true
  102. })
  103. }
  104. },
  105. getUserProfile(e) {
  106. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  107. wx.getUserProfile({
  108. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  109. success: (res) => {
  110. console.log(res)
  111. this.setData({
  112. userInfo: res.userInfo,
  113. hasUserInfo: true
  114. })
  115. }
  116. })
  117. },
  118. getUserInfo(e) {
  119. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
  120. console.log(e)
  121. this.setData({
  122. userInfo: e.detail.userInfo,
  123. hasUserInfo: true
  124. })
  125. }
  126. })