add.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <view class="address-new">
  3. <pub-loading-view :loaded="loaded" height="100%">
  4. <view class="address-new-section" v-if="formData">
  5. <uni-forms ref="formRef" :modelValue="formData" :rules="formRules" label-position="top" label-width="200px">
  6. <view class="address-new-title">联系人信息</view>
  7. <uni-forms-item label="收件人姓名" name="shrxm">
  8. <uni-easyinput class="pub-input" :inputBorder="false" trim="all" v-model="formData.shrxm"
  9. placeholder="请输入"></uni-easyinput>
  10. </uni-forms-item>
  11. <uni-forms-item label="联系电话" name="shrsjh">
  12. <uni-easyinput class="pub-input" :inputBorder="false" trim="all" v-model="formData.shrsjh"
  13. placeholder="请输入"></uni-easyinput>
  14. </uni-forms-item>
  15. <uni-forms-item label="省市区" name="province">
  16. <view class="form-address" @click="pickerAddressRef?.openPop">
  17. <!-- todo 没值显示请选择,有值显示 省 市 区,样式用checked -->
  18. <view class="form-address-input" :class="popupText?'checked':''">
  19. {{ popupText ? popupText : '请选择'}}
  20. </view>
  21. <view><text class="iconfont checkbox">&#xe656;</text></view>
  22. </view>
  23. <!-- 地址级联选中的字体要比不选中的要大,用picker没法实现 -->
  24. <PickerAddress ref="pickerAddressRef" :province="formData.province" :city="formData.city"
  25. :area="formData.area" @change="onChangeArea" />
  26. </uni-forms-item>
  27. <uni-forms-item label="详细地址(例如**街**号)" name="shrdzxxdz">
  28. <uni-easyinput class="pub-input" :inputBorder="false" trim="all" v-model="formData.shrdzxxdz"
  29. placeholder="请输入"></uni-easyinput>
  30. </uni-forms-item>
  31. </uni-forms>
  32. <view class="address-new-default">
  33. <view class="row" @click="onDefault">
  34. <text class="iconfont checkbox is-active" v-if="formData.isDefaulted">&#xe63b;</text>
  35. <text class="iconfont checkbox" v-else>&#xe8bb;</text>
  36. 默认收件地址
  37. </view>
  38. <view @click="onClear">清空</view>
  39. </view>
  40. </view>
  41. </pub-loading-view>
  42. <view class="pub-button is-bg large" @click="onConfirm">确定</view>
  43. </view>
  44. </template>
  45. <script lang="ts" setup>
  46. import { ref, PropType, onMounted, computed, reactive } from 'vue'
  47. import { aesEncrypt, aesDecrypt } from "@/lib/encryption";
  48. import rest from '@/stores/rest'
  49. import dlg from '@/lib/dlg.ts'
  50. import * as link from '@/lib/link'
  51. import { onLoad } from "@dcloudio/uni-app";
  52. import PickerAddress from './PickerAddress.vue'
  53. import { ConsigneeInfoVO } from '../../lib/type';
  54. const formRules = reactive({
  55. shrxm: {
  56. rules: [{
  57. required: true,
  58. errorMessage: '姓名不能为空'
  59. }]
  60. },
  61. shrsjh: {
  62. rules: [{
  63. required: true,
  64. errorMessage: '请填写手机号码',
  65. }, {
  66. validateFunction: function (rule, value, data, callback) {
  67. let iphoneReg = (
  68. /^(13[0-9]|14[1579]|15[0-3,5-9]|16[6]|17[0123456789]|18[0-9]|19[0-9])\d{8}$/
  69. ); //手机号码
  70. if (!iphoneReg.test(value)) {
  71. callback('手机号码格式不正确,请重新填写')
  72. }
  73. }
  74. }]
  75. },
  76. province: {
  77. rules: [{
  78. required: true,
  79. errorMessage: '省市区不能为空'
  80. }]
  81. },
  82. shrdzxxdz: {
  83. rules: [{
  84. required: true,
  85. errorMessage: '详细地址不能为空'
  86. }]
  87. }
  88. })
  89. const pickerAddressRef = ref()
  90. const uniType = ref('add')
  91. const formRef = ref()
  92. const popupText = ref()
  93. const loaded = ref(false)
  94. const formData = ref({
  95. shrxm: '',
  96. shrsjh: '',
  97. province: '',
  98. city: '',
  99. area: '',
  100. shrdzxxdz: '',
  101. shrxxid: null,
  102. hzid: '',
  103. isDefaulted: false
  104. })
  105. // 默认地址
  106. const onDefault = () => {
  107. formData.value.isDefaulted = !formData.value.isDefaulted
  108. }
  109. const onChangeArea = (e) => {
  110. formData.value.province = e.province
  111. formData.value.city = e.city
  112. formData.value.area = e.area
  113. popupText.value = e.province + ' ' + e.city + ' ' + e.area
  114. }
  115. // 清除
  116. const onClear = () => {
  117. popupText.value = null
  118. formData.value = {
  119. shrxm: '',
  120. shrsjh: '',
  121. province: '',
  122. city: '',
  123. area: '',
  124. shrdzxxdz: '',
  125. shrxxid: null,
  126. hzid: '',
  127. isDefaulted: false
  128. }
  129. }
  130. const onConfirm = async () => {
  131. // 校验表单
  132. formRef.value.validate().then(async res => {
  133. // 提交请求
  134. try {
  135. if (uniType.value == 'add') {
  136. await rest.post('/default/consignee-info/create', formData.value)
  137. dlg.success('新增地址管理成功!')
  138. } else if (uniType.value == 'edit') {
  139. await rest.put('/default/consignee-info/update', formData.value)
  140. dlg.success('修改地址管理成功!')
  141. }
  142. link.back()
  143. } catch (e) {
  144. dlg.error(e)
  145. }
  146. }).catch(err => {
  147. console.log('表单错误信息:', err);
  148. })
  149. }
  150. const getInfo = async (shrxxid:string)=> {
  151. try {
  152. let res = await rest.get('/default/consignee-info/get', {id:shrxxid}) as ConsigneeInfoVO
  153. formData.value.shrxm = res.shrxm
  154. formData.value.shrsjh = res.shrsjh
  155. formData.value.province = res.province
  156. formData.value.city = res.city
  157. formData.value.area = res.area
  158. formData.value.shrdzxxdz = res.shrdzxxdz
  159. formData.value.hzid = res.hzid
  160. formData.value.shrxxid = res.shrxxid
  161. popupText.value = res.province + res.city + res.area
  162. console.log("内容",formData.value)
  163. } catch (e) {
  164. dlg.error(e)
  165. }
  166. }
  167. onLoad((data) => {
  168. formData.value.hzid = aesDecrypt(data.hzid)
  169. if (data.shrxxid){
  170. let shrxxid = aesDecrypt(data.shrxxid)
  171. let defaulted = aesDecrypt(data.defaulted)
  172. console.log("内容",defaulted)
  173. uni.setNavigationBarTitle({
  174. title: '编辑地址'
  175. })
  176. uniType.value = 'edit'
  177. formData.value.isDefaulted = defaulted == "true" ? true : false
  178. getInfo(shrxxid)
  179. }
  180. })
  181. </script>
  182. <style lang="scss" scoped>
  183. .address-new {
  184. padding: $uni-spacing-col-s3 $page-row-spacing 0;
  185. box-sizing: border-box;
  186. display: flex;
  187. flex-direction: column;
  188. height: 100%;
  189. // position: relative;
  190. :deep(.pub-loading) {
  191. flex: 1;
  192. }
  193. .address-new-section {
  194. background: #fff;
  195. border-radius: 16rpx;
  196. padding: $uni-spacing-col-s4 $page-row-spacing $uni-spacing-col-s3;
  197. .address-new-title {
  198. font-size: $uni-font-size-lg;
  199. line-height: $uni-line-height-lg;
  200. font-weight: 800;
  201. margin-bottom: 24rpx;
  202. }
  203. .form-address {
  204. @include flex-between;
  205. border-bottom: 1px solid $uni-border-color;
  206. // height: 70rpx;
  207. padding: 8rpx 0 24rpx;
  208. // todo 模拟校验出错
  209. font-size: $uni-font-size-lg;
  210. line-height: $uni-line-height-lg;
  211. color: $uni-text-color-light;
  212. .form-address-input.checked {
  213. color: $uni-text-color;
  214. }
  215. }
  216. :deep(.uni-forms-item__label) {
  217. color: $uni-text-color-grey;
  218. padding: 0rpx 0 0rpx 0; //12,4
  219. font-size: $uni-font-size-base;
  220. line-height: $uni-line-height-base;
  221. height: auto;
  222. }
  223. :deep(.uni-easyinput__content) {
  224. border-bottom: 1px solid $uni-border-color;
  225. .uni-easyinput__content-input {
  226. padding-left: 0 !important;
  227. height: 70rpx;
  228. .uni-input-wrapper {
  229. .uni-input-placeholder {
  230. font-size: $uni-font-size-lg;
  231. line-height: $uni-line-height-lg;
  232. color: $uni-text-color-light;
  233. }
  234. .uni-input-input {
  235. font-size: $uni-font-size-lg;
  236. line-height: $uni-line-height-lg;
  237. color: $uni-text-color;
  238. }
  239. }
  240. }
  241. }
  242. .address-new-default {
  243. // border-top: 1px solid $uni-border-color;
  244. // margin-top: 24rpx;
  245. // padding-top: 16rpx;
  246. @include flex-between;
  247. font-size: $uni-font-size-sm;
  248. line-height: $uni-line-height-sm;
  249. .row {
  250. @include flex;
  251. }
  252. .iconfont {
  253. font-size: 36rpx;
  254. margin-right: 8rpx;
  255. &.checkbox {
  256. margin-right: 16rpx;
  257. color: $uni-text-color-disable;
  258. }
  259. &.is-active {
  260. color: $uni-color-primary;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. </style>