address-operation.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import OSticky from '@/components/o-sticky'
  2. import { Button, Cell, CellGroup, Field, Picker, Popup, showToast, Switch } from 'vant'
  3. import { defineComponent, onMounted, reactive } from 'vue'
  4. import request from '../request-music'
  5. import styles from './index.module.less'
  6. import { areas } from '@/helpers/area'
  7. import { verifiyNumberInteger } from '@/helpers/toolsValidate'
  8. import { router } from '@/router/routes-common'
  9. import { useRoute, useRouter } from 'vue-router'
  10. export default defineComponent({
  11. name: 'address-operation',
  12. setup() {
  13. const route = useRoute()
  14. const router = useRouter()
  15. const state = reactive({
  16. isClick: false,
  17. showPicker: false,
  18. defaultStatus: false,
  19. phoneNumber: null,
  20. province: null,
  21. city: null,
  22. region: null,
  23. pcrStr: '',
  24. name: null,
  25. detailAddress: null
  26. })
  27. const onFormatterInt = (val: any) => {
  28. if (val && val >= 1) {
  29. return verifiyNumberInteger(val)
  30. } else {
  31. return ''
  32. }
  33. }
  34. const onSubmit = async () => {
  35. try {
  36. if (!state.name) {
  37. showToast('请选择收货人')
  38. return
  39. }
  40. if (!state.phoneNumber && !/^1(3|4|5|6|7|8|9)\d{9}$/.test(state.phoneNumber as any)) {
  41. showToast('手机号输入有误')
  42. return
  43. }
  44. if (!state.pcrStr) {
  45. showToast('请选择所在地区')
  46. return
  47. }
  48. if (!state.detailAddress) {
  49. showToast('请输入详细地址')
  50. return
  51. }
  52. state.isClick = true
  53. const params: any = {
  54. name: state.name,
  55. phoneNumber: state.phoneNumber,
  56. province: state.province,
  57. city: state.city,
  58. region: state.region,
  59. detailAddress: state.detailAddress,
  60. defaultStatus: state.defaultStatus
  61. }
  62. if (route.query.id) {
  63. await request.post('/api-student/userReceiveAddress/update', {
  64. data: {
  65. id: route.query.id,
  66. ...params
  67. }
  68. })
  69. setTimeout(() => {
  70. showToast('修改成功')
  71. }, 100)
  72. } else {
  73. await request.post('/api-student/userReceiveAddress/save', {
  74. data: {
  75. ...params
  76. }
  77. })
  78. setTimeout(() => {
  79. showToast('添加成功')
  80. }, 100)
  81. }
  82. setTimeout(() => {
  83. state.isClick = false
  84. router.back()
  85. }, 1100)
  86. } catch (e: any) {
  87. //
  88. state.isClick = false
  89. console.log(e)
  90. }
  91. }
  92. const getDetails = async () => {
  93. try {
  94. const { data } = await request.get(
  95. '/api-student/userReceiveAddress/detail/' + route.query.id
  96. )
  97. state.name = data.name
  98. state.phoneNumber = data.phoneNumber
  99. state.province = data.province
  100. state.city = data.city
  101. state.region = data.region ? data.region : ''
  102. state.pcrStr = (data.provinceName || '') + (data.cityName || '') + (data.regionName || '')
  103. state.detailAddress = data.detailAddress
  104. state.defaultStatus = data.defaultStatus
  105. } catch {
  106. //
  107. }
  108. }
  109. onMounted(() => {
  110. // 判断是否有编号,有则为修改
  111. if (route.query.id) {
  112. getDetails()
  113. }
  114. })
  115. return () => (
  116. <div class={styles.operation}>
  117. <CellGroup inset class={styles.form}>
  118. <Field label="收货人" placeholder="请输入收货人姓名" v-model={state.name} />
  119. <Field
  120. label="手机号"
  121. placeholder="请输入收货人手机号"
  122. v-model={state.phoneNumber}
  123. maxlength={11}
  124. type="tel"
  125. formatter={onFormatterInt}
  126. />
  127. <Field
  128. label="所在地区"
  129. placeholder="省、市、区、街道"
  130. readonly
  131. isLink
  132. modelValue={state.pcrStr}
  133. onClick={() => {
  134. state.showPicker = true
  135. }}
  136. />
  137. <Field
  138. label="详细地址"
  139. placeholder="小区楼栋/乡村名称"
  140. type="textarea"
  141. rows={3}
  142. v-model={state.detailAddress}
  143. maxlength={100}
  144. />
  145. </CellGroup>
  146. <CellGroup inset style={{ marginTop: '12px' }}>
  147. <Cell title="设置为默认地址" center class={styles.default}>
  148. {{ value: () => <Switch v-model={state.defaultStatus} size="23px" /> }}
  149. </Cell>
  150. </CellGroup>
  151. <OSticky position="bottom">
  152. <div class="btnGroup">
  153. <Button type="primary" block round onClick={onSubmit} disabled={state.isClick}>
  154. 确认
  155. </Button>
  156. </div>
  157. </OSticky>
  158. <Popup v-model:show={state.showPicker} position="bottom" round>
  159. <Picker
  160. showToolbar
  161. columns={areas}
  162. columnsFieldNames={{ text: 'name', value: 'code', children: 'areas' }}
  163. onCancel={() => (state.showPicker = false)}
  164. onConfirm={(val: any) => {
  165. console.log(val, 'val')
  166. const selectedOptions = val.selectedOptions || []
  167. // 因为有的只
  168. state.pcrStr = ''
  169. if (selectedOptions.length === 2) {
  170. selectedOptions.forEach((item: any, index: number) => {
  171. state.pcrStr += item.name
  172. if (index === 0) {
  173. state.province = item.code
  174. } else if (index === 1) {
  175. state.city = item.code
  176. }
  177. })
  178. } else {
  179. selectedOptions.forEach((item: any, index: number) => {
  180. state.pcrStr += item.name
  181. if (index === 0) {
  182. state.province = item.code
  183. } else if (index === 1) {
  184. state.city = item.code
  185. } else if (index === 2) {
  186. state.region = item.code
  187. }
  188. })
  189. }
  190. state.showPicker = false
  191. }}
  192. />
  193. </Popup>
  194. </div>
  195. )
  196. }
  197. })