[baozhangchao] 3 лет назад
Родитель
Сommit
598baf1a72

+ 2
- 0
config/dev.js Просмотреть файл

@@ -3,6 +3,8 @@ module.exports = {
3 3
     NODE_ENV: '"development"'
4 4
   },
5 5
   defineConstants: {
6
+    HOST: '"http://192.168.89.147:9002"'
7
+
6 8
   },
7 9
   mini: {},
8 10
   h5: {}

+ 16143
- 0
package-lock.json
Разница между файлами не показана из-за своего большого размера
Просмотреть файл


+ 4
- 3
package.json Просмотреть файл

@@ -37,11 +37,12 @@
37 37
   "dependencies": {
38 38
     "@babel/runtime": "^7.7.7",
39 39
     "@tarojs/components": "3.3.6",
40
+    "@tarojs/react": "3.3.6",
40 41
     "@tarojs/runtime": "3.3.6",
41 42
     "@tarojs/taro": "3.3.6",
42
-    "@tarojs/react": "3.3.6",
43
-    "react-dom": "^17.0.0",
44
-    "react": "^17.0.0"
43
+    "@zjxpcyc/react-tiny-store": "^1.0.0",
44
+    "react": "^17.0.0",
45
+    "react-dom": "^17.0.0"
45 46
   },
46 47
   "devDependencies": {
47 48
     "@types/webpack-env": "^1.13.6",

+ 7
- 1
src/app.config.js Просмотреть файл

@@ -2,11 +2,17 @@ export default {
2 2
   pages: [
3 3
     'pages/index/index',
4 4
     'pages/main/index',
5
+    'pages/NoticeInfo/index',
6
+    'pages/setUser/index',
7
+    'pages/setDogInfo/index',
5 8
   ],
6 9
   window: {
7 10
     backgroundTextStyle: 'light',
8 11
     navigationBarBackgroundColor: '#fff',
9 12
     navigationBarTitleText: 'WeChat',
10 13
     navigationBarTextStyle: 'black'
11
-  }
14
+  },
15
+  useExtendedLib: {
16
+    weui: true
17
+  },
12 18
 }

+ 29
- 5
src/app.js Просмотреть файл

@@ -1,19 +1,43 @@
1 1
 import { Component } from 'react'
2
+import Taro from '@tarojs/taro';
3
+
4
+import store, { StoreRoot, useModel } from './store'
5
+
2 6
 import './app.less'
3 7
 
4 8
 class App extends Component {
5 9
 
6
-  componentDidMount () {}
10
+  componentDidMount () { }
11
+
12
+  componentDidShow () { }
13
+
14
+  componentDidHide () { }
15
+
16
+  componentDidCatchError () { }
17
+  onLaunch (options) {
18
+    const { logins, setUserLocation } = store.getModel('userData').getState()
7 19
 
8
-  componentDidShow () {}
9 20
 
10
-  componentDidHide () {}
21
+    // 登录获取人员信息
11 22
 
12
-  componentDidCatchError () {}
23
+
24
+    Taro.login({
25
+      success: (res) => {
26
+        const { code } = res;
27
+        logins({ code })
28
+
29
+
30
+      }
31
+    })
32
+
33
+  }
13 34
 
14 35
   // this.props.children 是将要会渲染的页面
15 36
   render () {
16
-    return this.props.children
37
+    return <StoreRoot>
38
+      {this.props.children}
39
+    </StoreRoot>
40
+    // return this.props.children
17 41
   }
18 42
 }
19 43
 

Двоичные данные
src/assets/tipsLogo.png Просмотреть файл


+ 34
- 0
src/components/Banner/index.jsx Просмотреть файл

@@ -0,0 +1,34 @@
1
+import Taro from '@tarojs/taro';
2
+import { View, Swiper, SwiperItem, Image } from '@tarojs/components';
3
+import './style.less'
4
+
5
+export default (props) => {
6
+  const { banner } = props;
7
+  return (
8
+    <View>
9
+      <Swiper
10
+        className='swiper-container'
11
+        circular
12
+        indicatorDots
13
+        indicatorColor='#fff'
14
+        indicatorActiveColor='#FBD804'
15
+        autoplay
16
+      // vertical//纵向滑动  上下
17
+      >
18
+        {
19
+          banner && banner.map((item, index) => (
20
+            <SwiperItem key={index} >
21
+              <Image className='swiper-img' src={item.imgUrl} mode='widthFix'></Image>
22
+            </SwiperItem>
23
+          ))
24
+        }
25
+
26
+
27
+      </Swiper>
28
+    </View>
29
+  )
30
+}
31
+
32
+
33
+
34
+

+ 9
- 0
src/components/Banner/style.less Просмотреть файл

@@ -0,0 +1,9 @@
1
+.swiper-container {
2
+  width: 100vw;
3
+  height: 400px;
4
+
5
+  .swiper-img {
6
+    height: 100%;
7
+    width: 100%;
8
+  }
9
+}

+ 37
- 0
src/components/Notice/index.jsx Просмотреть файл

@@ -0,0 +1,37 @@
1
+import Taro from '@tarojs/taro';
2
+import { View, Text, Swiper, SwiperItem, Image } from '@tarojs/components';
3
+import tipsImg from '../../assets/tipsLogo.png'
4
+
5
+import './style.less'
6
+
7
+export default (props) => {
8
+  const { Notice, onChange } = props;
9
+
10
+  return (
11
+    <View className='Notice-box'>
12
+      <Image src={tipsImg} />
13
+
14
+      <Swiper
15
+        className='NoticeSwiper'
16
+        circular
17
+        autoplay
18
+        interval={2000}
19
+        vertical//纵向滑动  上下
20
+      >
21
+        {
22
+          Notice && Notice.map((item, index) => (
23
+            <SwiperItem key={index} onClick={() => { onChange(item.text) }} >
24
+              <View className='NoticeSwiper-Text'>{item.text}</View>
25
+            </SwiperItem>
26
+          ))
27
+        }
28
+
29
+
30
+      </Swiper>
31
+    </View>
32
+  )
33
+}
34
+
35
+
36
+
37
+

+ 25
- 0
src/components/Notice/style.less Просмотреть файл

@@ -0,0 +1,25 @@
1
+.Notice-box {
2
+  width: 100vw;
3
+  // height: 50px;
4
+  background-color: #fff5f6;
5
+  display: flex;
6
+  align-items: center;
7
+  padding: 15px 10px;
8
+  > Image {
9
+    width: 55px;
10
+    height: 55px;
11
+    margin-right: 15px;
12
+    color: #fbd804;
13
+  }
14
+  .NoticeSwiper {
15
+    width: 85%;
16
+    height: 40px;
17
+    .NoticeSwiper-Text {
18
+      font-size: 26px;
19
+      text-overflow: ellipsis;
20
+      overflow: hidden;
21
+      white-space: nowrap;
22
+      color: #666;
23
+    }
24
+  }
25
+}

+ 10
- 0
src/pages/NoticeInfo/index.config.js Просмотреть файл

@@ -0,0 +1,10 @@
1
+export default {
2
+  navigationBarTitleText: '公告详情',
3
+  disableScroll: true,
4
+  navigationBarBackgroundColor: "#FBD804",
5
+  navigationBarTextStyle: "black",
6
+  backgroundColor: "#FBD804",
7
+  usingComponents: {
8
+  }
9
+
10
+}

+ 18
- 0
src/pages/NoticeInfo/index.jsx Просмотреть файл

@@ -0,0 +1,18 @@
1
+import { View, Text, Image } from '@tarojs/components'
2
+import Taro from '@tarojs/taro';
3
+
4
+import './style.less'
5
+
6
+export default (props) => {
7
+
8
+
9
+  return (
10
+    <View className='index-UserQRcode'>
11
+      我是公告嗷嗷嗷嗷嗷
12
+    </View>
13
+  )
14
+}
15
+
16
+
17
+
18
+

+ 0
- 0
src/pages/NoticeInfo/style.less Просмотреть файл


+ 22
- 2
src/pages/index/index.jsx Просмотреть файл

@@ -1,13 +1,33 @@
1 1
 import { View, Text, Image } from '@tarojs/components'
2
-import Taro from '@tarojs/taro';
3
-
2
+import Taro from '@tarojs/taro'
3
+import Banner from '../../components/Banner'
4
+import Notice from '../../components/Notice'
4 5
 import './style.less'
5 6
 
6 7
 export default (props) => {
7 8
 
9
+  const banner = [
10
+    { imgUrl: 'https://tse3-mm.cn.bing.net/th/id/OIP-C.yd6uU_V7fqswON8cwWUfewHaE7?w=300&h=200&c=7&r=0&o=5&pid=1.7' },
11
+    { imgUrl: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.A31N2pd7ApUojqrR6x41bwHaFj?pid=ImgDet&rs=1' },
12
+    { imgUrl: 'https://tse1-mm.cn.bing.net/th/id/R-C.413cf61f5c976cdaddafb30e3aeeb547?rik=AJJ41q9z0C1Anw&riu=http%3a%2f%2ffunpicimg.loveinhere.com%2f1008%2f1%2f4770.jpg&ehk=bP4qnBFW9Ha6lXMKKtVhAnkL2hTHv15pjb5oQpAhF6o%3d&risl=&pid=ImgRaw&r=0' },
13
+    { imgUrl: 'https://img.zcool.cn/community/01c62e565d10e76ac7253403cbde61.jpg@1280w_1l_2o_100sh.jpg' }
14
+  ]
15
+  const NoticeList = [
16
+    { text: '我是官方系统通知Notice' },
17
+    { text: '我是第二条通知奥奥奥奥奥奥' },
18
+    { text: '我是第3165656565' },
19
+    { text: '我是第四条通知啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊' }
20
+  ]
21
+  const onChangeGoNotice = (e) => {
22
+    Taro.navigateTo({
23
+      url: '/pages/setDogInfo/index'
24
+    })
25
+  }
8 26
 
9 27
   return (
10 28
     <View className='index-UserQRcode'>
29
+      <Banner banner={banner} />
30
+      <Notice Notice={NoticeList} onChange={onChangeGoNotice} />
11 31
     </View>
12 32
   )
13 33
 }

+ 1
- 0
src/pages/main/index.jsx Просмотреть файл

@@ -8,6 +8,7 @@ export default (props) => {
8 8
 
9 9
   return (
10 10
     <View className='index-UserQRcode'>
11
+
11 12
     </View>
12 13
   )
13 14
 }

+ 14
- 0
src/pages/setDogInfo/index.config.js Просмотреть файл

@@ -0,0 +1,14 @@
1
+export default {
2
+  navigationBarTitleText: '狗狗信息',
3
+  disableScroll: true,
4
+  navigationBarBackgroundColor: "#FBD804",
5
+  navigationBarTextStyle: "black",
6
+  backgroundColor: "#FBD804",
7
+  usingComponents: {
8
+    "mp-cells": "weui-miniprogram/cells/cells",
9
+    "mp-cell": "weui-miniprogram/cell/cell",
10
+    "mp-form-page": "weui-miniprogram/form-page/form-page",
11
+    "mp-form": "weui-miniprogram/form/form"
12
+  }
13
+
14
+}

+ 206
- 0
src/pages/setDogInfo/index.jsx Просмотреть файл

@@ -0,0 +1,206 @@
1
+import { View, Button, Form, Text, RadioGroup, Picker, Image, Input, Radio, Label, ScrollView } from '@tarojs/components'
2
+import Taro from '@tarojs/taro';
3
+import { useState } from 'react';
4
+
5
+import './style.less'
6
+
7
+export default (props) => {
8
+  const [dogDate, setDogDate] = useState({ petBirthday: '请选择生日', petSex: '请选择性别', petType: '请选择犬种' })
9
+  const [imgUrl, setImgUrl] = useState({ img1: null, immunization: null })
10
+
11
+
12
+  const onDateChange = (type, value) => {
13
+    switch (type) {
14
+      case 'petSex':
15
+        setDogDate({ ...dogDate, petSex: selector[value] })
16
+        break;
17
+      case 'petBirthday':
18
+        setDogDate({ ...dogDate, petBirthday: value })
19
+        break;
20
+      case 'petType':
21
+        setDogDate({ ...dogDate, petType: dogType[value].text })
22
+        break;
23
+      default:
24
+        break;
25
+    }
26
+
27
+  }
28
+  const selector = ['公', '母']
29
+  const dogType = [
30
+    {
31
+      value: 0,
32
+      text: '金毛',
33
+    },
34
+    {
35
+      value: 1,
36
+      text: '拉布拉多',
37
+    },
38
+
39
+  ]
40
+
41
+  const modeList = [
42
+    {
43
+      value: 0,
44
+      text: '邮寄',
45
+    },
46
+    {
47
+      value: 1,
48
+      text: '自取',
49
+    },
50
+
51
+  ]
52
+
53
+  const addImage = (e) => {
54
+    console.log("🚀 ~ file: index.jsx ~ line 94 ~ addImage ~ e", e)
55
+
56
+    if (e == 'img1') {
57
+      Taro.chooseImage({
58
+        count: 1, // 默认9
59
+        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
60
+        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有,在H5浏览器端支持使用 `user` 和 `environment`分别指定为前后摄像头
61
+        success: function (res) {
62
+          // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
63
+          var tempFilePaths = res.tempFilePaths
64
+          setImgUrl({ ...imgUrl, img1: tempFilePaths[0] })
65
+        },
66
+        fail () {
67
+          Taro.showToast({
68
+            title: '上传失败请重试',
69
+            icon: 'error',
70
+            duration: 1000
71
+          })
72
+        }
73
+      })
74
+
75
+    } else {
76
+      Taro.chooseImage({
77
+        count: 1, // 默认9
78
+        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
79
+        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有,在H5浏览器端支持使用 `user` 和 `environment`分别指定为前后摄像头
80
+        success: function (res) {
81
+          // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
82
+          var tempFilePaths = res.tempFilePaths
83
+          setImgUrl({ ...imgUrl, immunization: tempFilePaths[0] })
84
+        },
85
+        fail () {
86
+          Taro.showToast({
87
+            title: '上传失败请重试',
88
+            icon: 'error',
89
+            duration: 1000
90
+          })
91
+        }
92
+      })
93
+    }
94
+
95
+    console.log("🚀 ~ file: index.jsx ~ line 61 ~ addImage ~ tempFilePaths", imgUrl)
96
+
97
+
98
+  }
99
+  const formSubmit = e => {
100
+    console.log(e)
101
+  }
102
+
103
+  const formReset = e => {
104
+    console.log(e)
105
+  }
106
+  const rules = [
107
+    {
108
+      name: 'name',
109
+      rules: { required: true, message: '请填写收货人姓名' },
110
+    },
111
+    {
112
+      name: 'telNumber',
113
+      rules: [{ required: true, message: '请填写收货人电话' }, { mobile: true, message: '电话格式不对' }]
114
+    },
115
+  ]
116
+  return (
117
+    <ScrollView scrollY style={{
118
+      height: '100vh'
119
+    }}
120
+    >
121
+      <View class='page' >
122
+        <Form onSubmit={formSubmit} onReset={formReset} >
123
+          <mp-form rules={rules} >
124
+            <mp-cells title=''>
125
+              <mp-cell title='宠物名称' extClass=''>
126
+                <Input name='petName' placeholder='请输入宠养区域' />
127
+              </mp-cell>
128
+              <mp-cell title='宠物性别' extClass=''>
129
+                <Picker name='petSex' mode='selector' range={selector} onChange={(e) => { onDateChange('petSex', e.detail.value) }}>
130
+                  <View className='picker'>
131
+                    {dogDate.petSex}
132
+                  </View>
133
+                </Picker>
134
+              </mp-cell>
135
+              <mp-cell title='犬种' extClass=''>
136
+                <Picker name='petType' mode='selector' range={dogType} rangeKey='text' onChange={(e) => { onDateChange('petType', e.detail.value) }}>
137
+                  <View className='picker'>
138
+                    {dogDate.petType}
139
+                  </View>
140
+                </Picker>
141
+              </mp-cell>
142
+              <mp-cell title='生日' extClass=''>
143
+                <Picker name='petBirthday' mode='date' onChange={(e) => { onDateChange('petBirthday', e.detail.value) }}>
144
+                  <View className='picker'>
145
+                    {dogDate.petBirthday}
146
+                  </View>
147
+                </Picker>
148
+              </mp-cell>
149
+              <mp-cell title='毛色' extClass=''>
150
+                <Input name='petColor' placeholder='请输入宠养区域' />
151
+              </mp-cell>
152
+              <mp-cell title='宠物照片' extClass=''>
153
+                {
154
+                  !imgUrl.img1 ?
155
+                    <View className='addImage' onClick={() => addImage('img1')} >
156
+                      <Text>+</Text>
157
+                    </View>
158
+                    :
159
+                    <Image className='dogImg1' name='img1' src={imgUrl.img1} onClick={() => addImage('img1')} />
160
+                }
161
+
162
+              </mp-cell>
163
+              <mp-cell title='免疫证明' extClass=''>
164
+                {
165
+                  !imgUrl.immunization ?
166
+                    <View className='addImage' onClick={() => addImage('immunization')} >
167
+                      <Text>+</Text>
168
+                    </View>
169
+                    :
170
+                    <Image className='dogImg1' name='immunization' src={imgUrl.immunization} onClick={() => addImage('immunization')} />
171
+                }
172
+                {/* <Input name='immunization' placeholder='请输入身份证号' type='idcard' /> */}
173
+              </mp-cell>
174
+              <mp-cell title='详细地址' extClass=''>
175
+                <Input name='address' placeholder='请输入详细地址' />
176
+              </mp-cell>
177
+              <mp-cell title='申领方式' extClass=''>
178
+                <View className='radio-list'>
179
+                  <RadioGroup name='applyMethod'>
180
+                    {modeList.map((item, i) => {
181
+                      return (
182
+                        <Label className='radio-list__label' for={i} key={i}>
183
+                          <Radio color='#FCD905' className='radio-list__radio' value={item.value} >{item.text}</Radio>
184
+                        </Label>
185
+                      )
186
+                    })}
187
+                  </RadioGroup>
188
+                </View>
189
+              </mp-cell>
190
+
191
+
192
+
193
+            </mp-cells>
194
+          </mp-form>
195
+          <View slot='button'>
196
+            <Button class='weui-btn' type='primary' loading={false} formType='submit'>确定</Button>
197
+          </View>
198
+        </Form>
199
+      </View>
200
+    </ScrollView >
201
+  )
202
+}
203
+
204
+
205
+
206
+

+ 22
- 0
src/pages/setDogInfo/style.less Просмотреть файл

@@ -0,0 +1,22 @@
1
+.addImage {
2
+  width: 100px;
3
+  height: 100px;
4
+  border: 1px dashed #666;
5
+  display: flex;
6
+  align-items: center;
7
+  > Text {
8
+    text-align: center;
9
+    margin: 0 auto;
10
+    font-size: 60px;
11
+    font-weight: bold;
12
+  }
13
+}
14
+.dogImg1 {
15
+  width: 200px;
16
+  height: 150px;
17
+}
18
+.radio-list__radio {
19
+  width: 140px;
20
+  font-size: 35px;
21
+  transform: scale(0.9);
22
+}

+ 14
- 0
src/pages/setUser/index.config.js Просмотреть файл

@@ -0,0 +1,14 @@
1
+export default {
2
+  navigationBarTitleText: '犬主信息',
3
+  disableScroll: true,
4
+  navigationBarBackgroundColor: "#FBD804",
5
+  navigationBarTextStyle: "black",
6
+  backgroundColor: "#FBD804",
7
+  usingComponents: {
8
+    "mp-cells": "weui-miniprogram/cells/cells",
9
+    "mp-cell": "weui-miniprogram/cell/cell",
10
+    "mp-form-page": "weui-miniprogram/form-page/form-page",
11
+    "mp-form": "weui-miniprogram/form/form"
12
+  }
13
+
14
+}

+ 93
- 0
src/pages/setUser/index.jsx Просмотреть файл

@@ -0,0 +1,93 @@
1
+import { View, Button, Form, Text, Image, Picker, Switch, Input, Radio, Label, ScrollView } from '@tarojs/components'
2
+import Taro from '@tarojs/taro';
3
+import { useState } from 'react';
4
+
5
+import './style.less'
6
+
7
+export default (props) => {
8
+  const [imgUrl, setImgUrl] = useState()
9
+
10
+  const addImage = () => {
11
+    Taro.chooseImage({
12
+      count: 1, // 默认9
13
+      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
14
+      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有,在H5浏览器端支持使用 `user` 和 `environment`分别指定为前后摄像头
15
+      success: function (res) {
16
+        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
17
+        var tempFilePaths = res.tempFilePaths
18
+        setImgUrl(tempFilePaths)
19
+      },
20
+      fail () {
21
+        Taro.showToast({
22
+          title: '上传失败请重试',
23
+          icon: 'error',
24
+          duration: 1000
25
+        })
26
+      }
27
+    })
28
+
29
+  }
30
+  const formSubmit = e => {
31
+    console.log(e)
32
+  }
33
+
34
+  const formReset = e => {
35
+    console.log(e)
36
+  }
37
+  const rules = [
38
+    {
39
+      name: 'name',
40
+      rules: { required: true, message: '请填写收货人姓名' },
41
+    },
42
+    {
43
+      name: 'telNumber',
44
+      rules: [{ required: true, message: '请填写收货人电话' }, { mobile: true, message: '电话格式不对' }]
45
+    },
46
+  ]
47
+  return (
48
+    <ScrollView scrollY style={{
49
+      height: '100vh'
50
+    }}
51
+    >
52
+      <View class='page' >
53
+        <Form onSubmit={formSubmit} onReset={formReset} >
54
+
55
+          <mp-form rules={rules} >
56
+            <mp-cells title='我是提示信息'>
57
+              <mp-cell title='姓名' extClass=''>
58
+                <Input name='nickName' placeholder='请输入卡号' />
59
+              </mp-cell>
60
+              <mp-cell title='性别' extClass=''>
61
+                <Input name='sex' placeholder='请输入卡号' />
62
+              </mp-cell>
63
+              <mp-cell title='手机号' extClass=''>
64
+                <Input name='phone' type='number' placeholder='请输入宠养区域' />
65
+              </mp-cell>
66
+              <mp-cell title='身份证号' extClass=''>
67
+                <Input name='idCard' placeholder='请输入身份证号' type='idcard' />
68
+              </mp-cell>
69
+              <mp-cell title='身份证图片' extClass=''>
70
+                {
71
+                  !imgUrl ?
72
+                    <View className='addImage' onClick={addImage} >
73
+                      <Text>+</Text>
74
+                    </View>
75
+                    :
76
+                    <Image className='userImg1' name='cardImg1' src={imgUrl} onClick={addImage} />
77
+                }
78
+              </mp-cell>
79
+
80
+            </mp-cells>
81
+          </mp-form>
82
+          <View slot='button'>
83
+            <Button type='primary' loading={false} formType='submit'>确定 Loading</Button>
84
+          </View>
85
+        </Form>
86
+      </View>
87
+    </ScrollView>
88
+  )
89
+}
90
+
91
+
92
+
93
+

+ 20
- 0
src/pages/setUser/style.less Просмотреть файл

@@ -0,0 +1,20 @@
1
+.sizeStyle {
2
+  font-size: 20px;
3
+}
4
+.addImage {
5
+  width: 100px;
6
+  height: 100px;
7
+  border: 1px dashed #666;
8
+  display: flex;
9
+  align-items: center;
10
+  > Text {
11
+    text-align: center;
12
+    margin: 0 auto;
13
+    font-size: 60px;
14
+    font-weight: bold;
15
+  }
16
+}
17
+.userImg1 {
18
+  width: 200px;
19
+  height: 150px;
20
+}

+ 11
- 0
src/services/dogAPI.js Просмотреть файл

@@ -0,0 +1,11 @@
1
+
2
+import request from '../utils/request'
3
+// utils/request
4
+
5
+
6
+/**
7
+ * 登陆
8
+ * @param {*} data
9
+ * @returns
10
+ */
11
+export const login = (data) => request('/login', { data, method: 'POST' })

+ 16
- 0
src/store/index.js Просмотреть файл

@@ -0,0 +1,16 @@
1
+import { createStore, createStoreRoot, createModelHook } from '@zjxpcyc/react-tiny-store'
2
+import userData from './userData'
3
+
4
+const store = createStore()
5
+const models = { userData, }
6
+
7
+
8
+
9
+const useModel = createModelHook(store)
10
+const StoreRoot = createStoreRoot(store, models)
11
+
12
+export default store
13
+export {
14
+  useModel,
15
+  StoreRoot,
16
+}

+ 39
- 0
src/store/userData.js Просмотреть файл

@@ -0,0 +1,39 @@
1
+import { useState } from "react"
2
+import Taro from "@tarojs/taro"
3
+
4
+import { login } from "../services/dogAPI"
5
+
6
+
7
+export default () => {
8
+  const [user, setUser] = useState()
9
+  const [person, setPerson] = useState()
10
+  const [sessionKey, setSessionKey] = useState()
11
+
12
+  const logins = (params) => {
13
+    console.log('login');
14
+
15
+    login(params).then((res) => {
16
+
17
+      const { person: taPerson, sessionKey: skey, user: taUser } = res;
18
+      // taPerson.phone = null
19
+
20
+      setPerson(taPerson);
21
+      setUser(taUser);
22
+      setSessionKey(skey);
23
+    })
24
+  }
25
+
26
+  return {
27
+    user,
28
+    setUser,
29
+
30
+    person,
31
+    setPerson,
32
+
33
+    sessionKey,
34
+    setSessionKey,
35
+
36
+
37
+    logins,
38
+  }
39
+}

+ 23
- 0
src/utils/codeSegment.js Просмотреть файл

@@ -0,0 +1,23 @@
1
+
2
+
3
+/**
4
+ * object 转 queryStr
5
+ * @param {*} params 
6
+ * @returns 
7
+ */
8
+export function getQueryString (params) {
9
+  if (!params) return;
10
+
11
+  const keys = Object.keys(params)
12
+  const arr = keys.map((key) => {
13
+    const val = params[key]
14
+    if (val === null || val === undefined) return false;
15
+
16
+    return `${key}=${encodeURIComponent(val)}`
17
+  })
18
+
19
+  return arr.filter(Boolean).join('&')
20
+}
21
+
22
+
23
+

+ 65
- 0
src/utils/request.js Просмотреть файл

@@ -0,0 +1,65 @@
1
+import Taro from '@tarojs/taro'
2
+import { getQueryString } from './codeSegment.js'
3
+
4
+let appid
5
+
6
+export default (url, options) => {
7
+  const { params, skipError, header, ...leftOptions } = options || {}
8
+  const queryStr = getQueryString(params)
9
+
10
+  if (!appid) {
11
+    const { miniProgram } = Taro.getAccountInfoSync()
12
+    appid = miniProgram.appId
13
+  }
14
+
15
+  const urlWithParams = queryStr ? `${url}?${queryStr}` : url;
16
+  // eslint-disable-next-line no-undef
17
+  const nwUrl = `${HOST}/api/wx/${appid}${urlWithParams}`
18
+
19
+  const authToken = Taro.getStorageSync('token')
20
+  const tokenHeader = authToken ? { Authorization: authToken } : {}
21
+  const nwHeader = {
22
+    ...(header || {}),
23
+    ...tokenHeader,
24
+  }
25
+
26
+  return new Promise((resolve, reject) => {
27
+    Taro.request({
28
+      ...leftOptions,
29
+      url: nwUrl,
30
+      header: nwHeader,
31
+      success: (res) => {
32
+        const { code, message, data, token } = res.data
33
+
34
+        if (token || data?.token) {
35
+          Taro.setStorage({ key: 'token', data: token || data?.token })
36
+        }
37
+
38
+        if (code === 1000) {
39
+          resolve(data)
40
+
41
+        } else {
42
+          if (!skipError) {
43
+            Taro.showToast({
44
+              title: message || '网络异常',
45
+              icon: 'none',
46
+            })
47
+          }
48
+          reject(message?.indexOf('java') > -1 ? '系统内部错误' : message)
49
+        }
50
+      },
51
+
52
+      fail: (err) => {
53
+        console.error(err)
54
+        const message = err.message || err.errMsg || err
55
+        if (!skipError) {
56
+          Taro.showToast({
57
+            title: message,
58
+            icon: 'none',
59
+          })
60
+        }
61
+        reject(message)
62
+      }
63
+    })
64
+  })
65
+}

+ 8
- 1
yarn.lock Просмотреть файл

@@ -1951,6 +1951,13 @@
1951 1951
   resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
1952 1952
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
1953 1953
 
1954
+"@zjxpcyc/react-tiny-store@^1.0.0":
1955
+  version "1.0.0"
1956
+  resolved "https://registry.yarnpkg.com/@zjxpcyc/react-tiny-store/-/react-tiny-store-1.0.0.tgz#316a2a0336273c1c014bf2f55a513583770c7181"
1957
+  integrity sha512-ykPq4Tlfv4dr8HHSsBHBzcms8R3dld80FaZruMwxhIUqTQ+ypIDvQYqpajnnvS9sOPDZNqWyd9+mRiyKcADf9g==
1958
+  dependencies:
1959
+    fast-deep-equal "^3.1.3"
1960
+
1954 1961
 abab@^2.0.0:
1955 1962
   version "2.0.6"
1956 1963
   resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.6.tgz#41b80f2c871d19686216b82309231cfd3cb3d291"
@@ -4625,7 +4632,7 @@ extsprintf@^1.2.0:
4625 4632
   resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.1.tgz#8d172c064867f235c0c84a596806d279bf4bcc07"
4626 4633
   integrity sha512-Wrk35e8ydCKDj/ArClo1VrPVmN8zph5V4AtHwIuHhvMXsKf73UT3BOD+azBIW+3wOJ4FhEH7zyaJCFvChjYvMA==
4627 4634
 
4628
-fast-deep-equal@^3.1.1:
4635
+fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
4629 4636
   version "3.1.3"
4630 4637
   resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
4631 4638
   integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==