edit.vue 6.7 KB

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