edit.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <!-- 收货地址的新增/编辑 -->
  2. <template>
  3. <!-- {{ru}} -->
  4. <s-layout :title="state.model.id ? t('address.edit_virtual_address') : t('address.add_virtual_address')">
  5. <uni-forms ref="addressFormRef" v-model="state.model" :rules="rules" validateTrigger="bind" labelWidth="160"
  6. labelAlign="left" border :labelStyle="{ fontWeight: 'bold' }">
  7. <view class="bg-white form-box ss-p-x-30">
  8. <uni-forms-item name="name" :label="t('address.recipient')" class="form-item">
  9. <uni-easyinput v-model="state.model.name" :placeholder="t('address.enter_recipient_name')" :inputBorder="false"
  10. placeholderStyle="color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal" :maxlength='10'/>
  11. </uni-forms-item>
  12. <uni-forms-item name="mobile" :label="t('account.phone_number')" class="form-item">
  13. <uni-easyinput v-model="state.model.mobile" type="number" :placeholder="t('account.enter_phone_number')" :inputBorder="false"
  14. placeholderStyle="color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal">
  15. </uni-easyinput>
  16. </uni-forms-item>
  17. <uni-forms-item name="detailAddress" :label="t('address.detailed_address')" :formItemStyle="{ alignItems: 'flex-start' }"
  18. :labelStyle="{ lineHeight: '5em' }" class="textarea-item">
  19. <uni-easyinput :inputBorder="false" type="textarea" v-model="state.model.detailAddress"
  20. placeholderStyle="color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal"
  21. :placeholder="t('address.enter_detailed_address')" clearable />
  22. </uni-forms-item>
  23. </view>
  24. <view class="ss-m-y-20 bg-white ss-p-x-30 ss-flex ss-row-between ss-col-center default-box">
  25. <view class="default-box-title"> {{ t('address.set_as_default_virtual_address') }} </view>
  26. <su-switch style="transform: scale(0.8)" v-model="state.model.defaultStatus" />
  27. </view>
  28. </uni-forms>
  29. <su-fixed bottom :opacity="false" bg="" placeholder :noFixed="false" :index="10">
  30. <view class="footer-box ss-flex-col ss-row-between ss-p-20">
  31. <view class="ss-m-b-20">
  32. <button class="ss-reset-button save-btn ui-Shadow-Main" @tap="onSave">{{ t('common.save') }}</button>
  33. </view>
  34. <button v-if="state.model.id" class="ss-reset-button cancel-btn" @tap="onDelete">
  35. {{ t('common.delete') }}
  36. </button>
  37. </view>
  38. </su-fixed>
  39. <!-- 省市区弹窗 -->
  40. <su-region-picker :show="state.showRegion" @cancel="state.showRegion = false" @confirm="onRegionConfirm" />
  41. </s-layout>
  42. </template>
  43. <script setup>
  44. import {
  45. ref,
  46. reactive,
  47. unref
  48. } from 'vue';
  49. import sheep from '@/sheep';
  50. import {
  51. onLoad
  52. } from '@dcloudio/uni-app';
  53. import _ from 'lodash';
  54. import {
  55. mobile
  56. } from '@/sheep/validate/form';
  57. import AreaApi from '@/sheep/api/system/area';
  58. import AddressApi from '@/sheep/api/member/address';
  59. import $helper from '@/sheep/helper';
  60. import { t } from '@/locale'
  61. const addressFormRef = ref(null);
  62. const state = reactive({
  63. showRegion: false,
  64. model: {
  65. name: '',
  66. mobile: '',
  67. detailAddress: '',
  68. defaultStatus: false,
  69. areaName: '',
  70. addressType:2
  71. },
  72. rules: {},
  73. });
  74. // let ru = ref({})
  75. const rules = {
  76. name: {
  77. rules: [{
  78. required: true,
  79. errorMessage: t('address.enter_recipient_name'),
  80. }, ],
  81. },
  82. mobile,
  83. detailAddress: {
  84. rules: [{
  85. required: true,
  86. errorMessage: t('address.enter_detailed_address'),
  87. }]
  88. },
  89. };
  90. // 确认选择地区
  91. const onRegionConfirm = (e) => {
  92. state.model.areaName = `${e.province_name} ${e.city_name} ${e.district_name}`
  93. state.model.areaId = e.district_id;
  94. state.showRegion = false;
  95. };
  96. // 获得地区数据
  97. const getAreaData = () => {
  98. if (_.isEmpty(uni.getStorageSync('areaData'))) {
  99. AreaApi.getAreaTree().then((res) => {
  100. if (res.code === 0) {
  101. uni.setStorageSync('areaData', res.data);
  102. }
  103. });
  104. }
  105. };
  106. // 保存收货地址
  107. const onSave = async () => {
  108. console.log(state)
  109. // 参数校验
  110. const validate = await unref(addressFormRef)
  111. .validate()
  112. .catch((error) => {
  113. console.log('error: ', error);
  114. });
  115. if (!validate) {
  116. return;
  117. }
  118. // 提交请求
  119. const formData = {
  120. ...state.model
  121. }
  122. const {
  123. code
  124. } = state.model.id > 0 ? await AddressApi.updateAddress(formData) :
  125. await AddressApi.createAddress(formData);
  126. if (code === 0) {
  127. sheep.$router.back();
  128. }
  129. };
  130. // 删除收货地址
  131. const onDelete = () => {
  132. uni.showModal({
  133. title: t('setting.prompt'),
  134. content: t('address.confirm_delete_address'),
  135. success: async function(res) {
  136. if (!res.confirm) {
  137. return;
  138. }
  139. const {
  140. code
  141. } = await AddressApi.deleteAddress( state.model.id , 2);
  142. if (code === 0) {
  143. sheep.$router.back();
  144. }
  145. },
  146. });
  147. };
  148. onLoad(async (options) => {
  149. // 获得地区数据
  150. getAreaData();
  151. // 情况一:基于 id 获得收件地址
  152. if (options.id) {
  153. let {
  154. code,
  155. data
  156. } = await AddressApi.getAddress(options.id, 2);
  157. if (code !== 0) {
  158. return;
  159. }
  160. // console.log(data)
  161. state.model = data;
  162. }
  163. // 情况二:微信导入 TODO 非繁人:待接入
  164. if (options.data) {
  165. let data = JSON.parse(options.data);
  166. // ru.value = data
  167. const areaData = uni.getStorageSync('areaData');
  168. let provinceArr = areaData.filter(item => item.name == data.province_name);
  169. data.province_id = provinceArr[0].id;
  170. let provinceArr2 = provinceArr[0].children.filter(item => item.name == data.city_name);
  171. data.city_id = provinceArr2[0].id;
  172. let provinceArr3 = provinceArr2[0].children.filter(item => item.name == data.district_name);
  173. data.district_id = provinceArr3[0].id;
  174. state.model = {
  175. name: data.consignee,
  176. mobile: data.mobile,
  177. detailAddress: data.address,
  178. defaultStatus: data.is_default,
  179. areaName: data.province_name + ' ' + data.city_name + ' ' + data.district_name,
  180. areaId: data.district_id
  181. }
  182. // state.model = {
  183. // ...state.model,
  184. // ...data,
  185. // };
  186. }
  187. });
  188. </script>
  189. <style lang="scss" scoped>
  190. :deep() {
  191. .uni-forms-item__label .label-text {
  192. font-size: 28rpx !important;
  193. color: #333333 !important;
  194. line-height: normal !important;
  195. }
  196. .uni-easyinput__content-input {
  197. font-size: 28rpx !important;
  198. color: #333333 !important;
  199. line-height: normal !important;
  200. padding-left: 0 !important;
  201. }
  202. .uni-easyinput__content-textarea {
  203. font-size: 28rpx !important;
  204. color: #333333 !important;
  205. line-height: normal !important;
  206. margin-top: 8rpx !important;
  207. }
  208. .uni-icons {
  209. font-size: 40rpx !important;
  210. }
  211. .is-textarea-icon {
  212. margin-top: 22rpx;
  213. }
  214. .is-disabled {
  215. color: #333333;
  216. }
  217. }
  218. .default-box {
  219. width: 100%;
  220. box-sizing: border-box;
  221. height: 100rpx;
  222. .default-box-title {
  223. font-size: 28rpx;
  224. color: #333333;
  225. line-height: normal;
  226. }
  227. }
  228. .footer-box {
  229. .save-btn {
  230. width: 710rpx;
  231. height: 80rpx;
  232. border-radius: 40rpx;
  233. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  234. color: $white;
  235. }
  236. .cancel-btn {
  237. width: 710rpx;
  238. height: 80rpx;
  239. border-radius: 40rpx;
  240. background: var(--ui-BG);
  241. }
  242. }
  243. </style>