edit.vue 7.8 KB

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