张延森 hace 3 años
padre
commit
9463724843

+ 22
- 0
src/components/Authorize/AuthProfile.jsx Ver fichero

@@ -0,0 +1,22 @@
1
+import Taro from '@tarojs/taro';
2
+import { useModel } from '@/store';
3
+
4
+// 需要引入 mp-dialog
5
+export default function (props) {
6
+  const { person, pets } = useModel('userData')
7
+
8
+  const show = !person.phone || !pets || !pets.length
9
+  const message = !person.phone ? '请先维护个人信息' : '请先维护狗狗信息'
10
+
11
+  const handleTap = () => {
12
+    if (!show) return;
13
+    const url = !person.phone ? '/pages/setUser/index' : '/pages/setDogInfo/index'
14
+    Taro.navigateTo({ url })
15
+  }
16
+
17
+  return (
18
+    <mp-dialog show={show} title='提示' maskClosable={false} buttons={[{ text: '确定' }]} onButtontap={handleTap} >
19
+      {message}
20
+    </mp-dialog>
21
+  )
22
+}

+ 13
- 0
src/components/Form/FieldLabel.jsx Ver fichero

@@ -0,0 +1,13 @@
1
+import Taro from '@tarojs/taro';
2
+import { View, Text } from '@tarojs/components'
3
+
4
+export default function (props) {
5
+  const { required } = props
6
+
7
+  return (
8
+    <View className='weui-label'>
9
+      {required && <Text style={{ color: 'red' }}>*</Text>}
10
+      {props.children}
11
+    </View>
12
+  )
13
+}

+ 7
- 5
src/components/Form/index.jsx Ver fichero

@@ -8,21 +8,23 @@ export default function (props) {
8 8
   const { onSubmit, onError, rules, ...leftProps } = props
9 9
 
10 10
   const handleSubmit = (e) => {
11
+    const formData = Object.assign({}, e.detail.value)
12
+    console.log('--表单内容--', formData)
11 13
     if (!rules) {
12 14
       if (onSubmit) {
13
-        onSubmit(e.detail.value)
15
+        onSubmit(formData)
14 16
       }
15 17
       return;
16 18
     }
17 19
 
18 20
     const validator = new Schema(rules);
19
-    validator.validate(e.detail.value).then(() => {
21
+    validator.validate(formData).then(() => {
20 22
       if (onSubmit) {
21
-        onSubmit(e.detail.value)
23
+        onSubmit(formData)
22 24
       }
23
-    }).catch((...args) => {
25
+    }).catch(({ errors }) => {
24 26
       if (onError) {
25
-        onError(...args)
27
+        onError(errors)
26 28
       }
27 29
     })
28 30
   }

+ 1
- 1
src/components/s-picker/index.js Ver fichero

@@ -8,7 +8,7 @@ Component({
8 8
       type: String
9 9
     },
10 10
     value: {
11
-      type: undefined,
11
+      type: null,
12 12
       observer (val) {
13 13
         if (val === undefined || val === null) {
14 14
           if (this.data.index !== undefined) {

+ 47
- 0
src/components/uploader/index.js Ver fichero

@@ -0,0 +1,47 @@
1
+Component({
2
+  behaviors: ['wx://form-field'],
3
+  options: {
4
+    addGlobalClass: true,
5
+  },
6
+  properties: {
7
+    name: {
8
+      type: String
9
+    },
10
+    value: {
11
+      type: null,
12
+    },
13
+  },
14
+  methods: {
15
+    handleValue (value) {
16
+      this.setData({ value })
17
+    },
18
+    handleUpload() {
19
+      const next = this.handleValue.bind(this)
20
+      const emit = this.triggerEvent.bind(this)
21
+
22
+      // eslint-disable-next-line no-undef
23
+      wx.chooseImage({
24
+        count: 1, // 默认9
25
+        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
26
+        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有,在H5浏览器端支持使用 `user` 和 `environment`分别指定为前后摄像头
27
+        success: function (res) {
28
+          const file = res.tempFiles[0];
29
+          emit('file', { file, next })
30
+        },
31
+        fail (err) {
32
+          console.error('----上传失败----', err)
33
+          if (err.errMsg === 'chooseImage:fail cancel') {
34
+            return;
35
+          }
36
+
37
+          // eslint-disable-next-line no-undef
38
+          wx.showToast({
39
+            title: err.errMsg,
40
+            icon: 'error',
41
+            duration: 1000
42
+          })
43
+        }
44
+      })
45
+    }
46
+  }
47
+})

+ 3
- 0
src/components/uploader/index.json Ver fichero

@@ -0,0 +1,3 @@
1
+{
2
+  "component": true
3
+}

+ 4
- 0
src/components/uploader/index.wxml Ver fichero

@@ -0,0 +1,4 @@
1
+<view>
2
+  <view class="uploader-icon" wx:if="{{!value}}" bind:tap="handleUpload">+</view>
3
+  <image class="uploader-thumb" wx:else src="{{value}}" mode="aspectFit" bind:tap="handleUpload" />
4
+</view>

+ 15
- 0
src/components/uploader/index.wxss Ver fichero

@@ -0,0 +1,15 @@
1
+
2
+.uploader-icon {
3
+  width: 96rpx;
4
+  height: 96rpx;
5
+  border: 1px dashed #858585;
6
+  line-height: 96rpx;
7
+  font-size: 48rpx;
8
+  color: #333;
9
+  text-align: center;
10
+}
11
+
12
+.uploader-thumb {
13
+  width: 128rpx;
14
+  height: 96rpx;
15
+}

+ 2
- 16
src/pages/setDogLicense/index.jsx Ver fichero

@@ -2,6 +2,7 @@ import { View, Button, Text, RadioGroup, Picker, Image, Input, Radio, Label, Scr
2 2
 import Taro, { useDidShow } from '@tarojs/taro';
3 3
 import { useState, useEffect } from 'react';
4 4
 import Form from '@/components/Form';
5
+import AuthProfile from '@/components/Authorize/AuthProfile';
5 6
 
6 7
 import { addDogList, addlication, getDogCardList, getDogInfo } from '../../services/dogAPI';
7 8
 import { useModel } from '../../store';
@@ -85,17 +86,6 @@ export default (props) => {
85 86
   }
86 87
 
87 88
   const buttons = [{ text: '确定' }]
88
-  // const dogSex = [
89
-  //   {
90
-  //     value: 1,
91
-  //     text: '雄',
92
-  //   },
93
-  //   {
94
-  //     value: 2,
95
-  //     text: '雌',
96
-  //   },
97
-
98
-  // ]
99 89
 
100 90
   const modeList = [
101 91
     {
@@ -176,11 +166,7 @@ export default (props) => {
176 166
 
177 167
   return (
178 168
     <ScrollView scrollY style={{ height: '100vh' }} >
179
-      <mp-dialog show={showDialog} title='提示' maskClosable={false} buttons={buttons} onButtonTap={handleDialog} >
180
-        <View>
181
-          请先添加本人信息或狗狗信息
182
-        </View>
183
-      </mp-dialog>
169
+      <AuthProfile />
184 170
       <View class='page' >
185 171
         <Form onSubmit={formSubmit} >
186 172
           <mp-form  >

+ 3
- 1
src/pages/setUser/index.config.js Ver fichero

@@ -9,7 +9,9 @@ export default {
9 9
     "mp-cell": "weui-miniprogram/cell/cell",
10 10
     "mp-form-page": "weui-miniprogram/form-page/form-page",
11 11
     "mp-form": "weui-miniprogram/form/form",
12
-    "s-picker": "../../components/s-picker/index"
12
+    "mp-toptips": "weui-miniprogram/toptips/toptips",
13
+    "s-picker": "../../components/s-picker/index",
14
+    "uploader": "../../components/uploader/index"
13 15
   }
14 16
 
15 17
 }

+ 40
- 26
src/pages/setUser/index.jsx Ver fichero

@@ -1,21 +1,31 @@
1
-import { View, Button, Text, Image, Switch, Input, Radio, Label, ScrollView } from '@tarojs/components'
1
+import { View, Button, Text, Image, Slot, Input, ScrollView } from '@tarojs/components'
2 2
 import Taro from '@tarojs/taro';
3 3
 import { useEffect, useState } from 'react';
4 4
 import { setPersons } from '@/services/dogAPI';
5 5
 import { useModel } from '@/store';
6 6
 import { uploadFile } from '@/utils/request';
7 7
 import Form from '@/components/Form';
8
+import FieldLabel from '@/components/Form/FieldLabel';
8 9
 
9 10
 import './style.less'
10 11
 
12
+const rules = {
13
+  nickName: [{ required: true, message: '请填写姓名' }],
14
+  sex: [{ type: 'number', required: true, message: '请选择性别' }],
15
+  phone: [{ required: true, message: '请填写手机号' }, { len: 11, message: '请输入11位手机号' }],
16
+  idCard: [{ required: true, message: '请填写身份证号码' }, { len: 18, message: '请输入18位身份证号码' }],
17
+  cardImg1: [{ required: true, message: '请上传身份证照片' }],
18
+}
19
+
11 20
 export default (props) => {
21
+  const [showTopTips, setShowTopTips] = useState(false)
22
+  const [errorMsg, setErrorMsg] = useState()
12 23
   const { setPerson, person, setDogs } = useModel('userData')
13 24
   // const [imgUrl, setImgUrl] = useState()
14 25
   const [userInfo, setUserInfo] = useState({ cardImg1: null, sex: '请选择性别' })
15 26
   useEffect(() => {
16 27
     if (person) {
17 28
       setUserInfo(person)
18
-
19 29
     }
20 30
   }, [person])
21 31
 
@@ -31,10 +41,6 @@ export default (props) => {
31 41
 
32 42
   ]
33 43
 
34
-  const addImage = () => {
35
-
36
-
37
-  }
38 44
   const formSubmit = e => {
39 45
     const formData = e
40 46
     console.log(e)
@@ -55,20 +61,25 @@ export default (props) => {
55 61
     })
56 62
   }
57 63
 
64
+  const handleError = (errors) => {
65
+    setErrorMsg(errors[0].message)
66
+    setShowTopTips(true)
67
+  }
58 68
 
69
+  const handleFile = (e) => {
70
+    const { file, next } = e.detail
71
+    uploadFile(file.path).then(next)
72
+  }
59 73
 
60 74
   const handleSetUser = (type, value) => {
61 75
     // console.log(e)
62
-    if (type == 'sex') {
63
-      setUserInfo({ ...userInfo, sex: userSex[value] })
64
-      console.log("🚀 ~ file: index.jsx ~ line 38 ~ handleSetUser ~ value", value)
65
-
66
-    } else {
76
+    if (type == 'img') {
67 77
       Taro.chooseImage({
68 78
         count: 1, // 默认9
69 79
         sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
70 80
         sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有,在H5浏览器端支持使用 `user` 和 `environment`分别指定为前后摄像头
71 81
         success: function (res) {
82
+          console.log('-------chooseImage----->', res)
72 83
           // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
73 84
           var tempFilePaths = res.tempFilePaths
74 85
           uploadFile(tempFilePaths[0]).then((res2) => {
@@ -89,36 +100,39 @@ export default (props) => {
89 100
   }
90 101
 
91 102
   return (
92
-    <ScrollView scrollY style={{
93
-      height: '100vh'
94
-    }}
95
-    >
103
+    <ScrollView scrollY style={{ height: '100vh' }}>
104
+      <mp-toptips msg={errorMsg} type='error' show={showTopTips} onHide={() => setShowTopTips(false)}></mp-toptips>
96 105
       <View class='page' >
97
-        <Form onSubmit={formSubmit}>
106
+        <Form rules={rules} onSubmit={formSubmit} onError={handleError}>
98 107
           <mp-form>
99
-            <mp-cells title=' '>
100
-              <mp-cell title='姓名' extClass=''>
108
+            <mp-cells>
109
+              <mp-cell>
110
+                <Slot name='title'><FieldLabel required>姓名</FieldLabel></Slot>
101 111
                 <Input name='nickName' placeholder='请输入姓名' value={person?.nickName} />
102 112
               </mp-cell>
103
-              <mp-cell title='性别' extClass=''>
104
-                {/* <Input name='sex' placeholder='请输入卡号' /> */}
105
-                <s-picker name='sex' rangeKey='text' rangeValue='value' range={userSex} value={person?.sex} onChange={(e) => handleSetUser('sex', e.detail.data.value)} />
113
+              <mp-cell>
114
+                <Slot name='title'><FieldLabel required>性别</FieldLabel></Slot>
115
+                <s-picker name='sex' rangeKey='text' rangeValue='value' range={userSex} value={person?.sex} />
106 116
               </mp-cell>
107
-              <mp-cell title='手机号' extClass=''>
117
+              <mp-cell>
118
+                <Slot name='title'><FieldLabel required>手机号</FieldLabel></Slot>
108 119
                 <Input name='phone' type='number' placeholder='请输入手机号' maxlength={11} value={person?.phone} />
109 120
               </mp-cell>
110
-              <mp-cell title='身份证号' extClass=''>
121
+              <mp-cell>
122
+                <Slot name='title'><FieldLabel required>身份证号</FieldLabel></Slot>
111 123
                 <Input name='idCard' placeholder='请输入身份证号' type='idcard' maxlength={18} value={person?.idCard} />
112 124
               </mp-cell>
113
-              <mp-cell title='身份证图片' extClass=''>
114
-                {
125
+              <mp-cell>
126
+                <Slot name='title'><FieldLabel required>身份证图片</FieldLabel></Slot>
127
+                <uploader name='cardImg1' value={person?.cardImg1} onFile={handleFile} />
128
+                {/* {
115 129
                   !userInfo.cardImg1 ?
116 130
                     <View className='addImage' onClick={(e) => handleSetUser('img', e.detail.value)} >
117 131
                       <Text>+</Text>
118 132
                     </View>
119 133
                     :
120 134
                     <Image className='userImg1' name='cardImg1' src={userInfo.cardImg1} onClick={(e) => handleSetUser('img', e.detail.value)} />
121
-                }
135
+                } */}
122 136
               </mp-cell>
123 137
             </mp-cells>
124 138
           </mp-form>