edit.vue 7.1 KB

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