edit.vue 8.4 KB

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