edit.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. state.model = {
  101. ...state.model,
  102. ...e,
  103. };
  104. state.showRegion = false;
  105. };
  106. const getAreaData = () => {
  107. if (_.isEmpty(uni.getStorageSync('areaData'))) {
  108. sheep.$api.data.area().then((res) => {
  109. if (res.code === 0) {
  110. uni.setStorageSync('areaData', res.data);
  111. }
  112. });
  113. }
  114. };
  115. const onSave = async () => {
  116. const validate = await unref(addressFormRef)
  117. .validate()
  118. .catch((error) => {
  119. console.log('error: ', error);
  120. });
  121. if (!validate) return;
  122. let res = null;
  123. if (state.model.id) {
  124. res = await sheep.$api.user.address.update({
  125. id: state.model.id,
  126. areaId: state.model.district_id,
  127. defaultStatus: state.model.is_default,
  128. detailAddress: state.model.address,
  129. mobile: state.model.mobile,
  130. name: state.model.consignee
  131. });
  132. } else {
  133. res = await sheep.$api.user.address.create({
  134. areaId: state.model.district_id,
  135. defaultStatus: state.model.is_default,
  136. detailAddress: state.model.address,
  137. mobile: state.model.mobile,
  138. name: state.model.consignee
  139. });
  140. }
  141. if (res.code === 0) {
  142. sheep.$router.back();
  143. }
  144. };
  145. const onDelete = () => {
  146. uni.showModal({
  147. title: '提示',
  148. content: '确认删除此收货地址吗?',
  149. success: async function(res) {
  150. if (res.confirm) {
  151. const {
  152. code
  153. } = await sheep.$api.user.address.delete(state.model.id);
  154. if (code === 0) {
  155. sheep.$router.back();
  156. }
  157. }
  158. },
  159. });
  160. };
  161. onLoad(async (options) => {
  162. getAreaData();
  163. if (options.id) {
  164. let res = await sheep.$api.user.address.detail(options.id);
  165. if (res.code === 0) {
  166. state.model = {
  167. ...state.model,
  168. district_id: res.data.areaId,
  169. is_default: res.data.defaultStatus,
  170. address: res.data.detailAddress,
  171. mobile: res.data.mobile,
  172. consignee: res.data.name,
  173. id: res.data.id,
  174. province_name: res.data.areaName.split(' ')[0],
  175. city_name: res.data.areaName.split(' ')[1],
  176. district_name: res.data.areaName.split(' ')[2]
  177. };
  178. }
  179. }
  180. if (options.data) {
  181. let data = JSON.parse(options.data);
  182. console.log(data)
  183. state.model = {
  184. ...state.model,
  185. ...data,
  186. };
  187. }
  188. });
  189. </script>
  190. <style lang="scss" scoped>
  191. :deep() {
  192. .uni-forms-item__label .label-text {
  193. font-size: 28rpx !important;
  194. color: #333333 !important;
  195. line-height: normal !important;
  196. }
  197. .uni-easyinput__content-input {
  198. font-size: 28rpx !important;
  199. color: #333333 !important;
  200. line-height: normal !important;
  201. padding-left: 0 !important;
  202. }
  203. .uni-easyinput__content-textarea {
  204. font-size: 28rpx !important;
  205. color: #333333 !important;
  206. line-height: normal !important;
  207. margin-top: 8rpx !important;
  208. }
  209. .uni-icons {
  210. font-size: 40rpx !important;
  211. }
  212. .is-textarea-icon {
  213. margin-top: 22rpx;
  214. }
  215. .is-disabled {
  216. color: #333333;
  217. }
  218. }
  219. .default-box {
  220. width: 100%;
  221. box-sizing: border-box;
  222. height: 100rpx;
  223. .default-box-title {
  224. font-size: 28rpx;
  225. color: #333333;
  226. line-height: normal;
  227. }
  228. }
  229. .footer-box {
  230. .save-btn {
  231. width: 710rpx;
  232. height: 80rpx;
  233. border-radius: 40rpx;
  234. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  235. color: $white;
  236. }
  237. .cancel-btn {
  238. width: 710rpx;
  239. height: 80rpx;
  240. border-radius: 40rpx;
  241. background: var(--ui-BG);
  242. }
  243. }
  244. </style>