edit.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <!-- 收货地址的新增/编辑 -->
  2. <template>
  3. <s-layout :title="state.model.id ? '编辑地址' : '新增地址'">
  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="收货人" 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.mobile" 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="areaName" label="省市区" @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="请选择省市区">
  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="详细地址" :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="请输入详细地址" 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"> 设为默认地址 </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">保存</button>
  42. </view>
  43. <button v-if="state.model.id" class="ss-reset-button cancel-btn" @tap="onDelete">
  44. 删除
  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. const addressFormRef = ref(null);
  70. const state = reactive({
  71. showRegion: false,
  72. model: {
  73. name: '',
  74. mobile: '',
  75. detailAddress: '',
  76. defaultStatus: false,
  77. areaName: '',
  78. },
  79. rules: {},
  80. });
  81. const rules = {
  82. name: {
  83. rules: [{
  84. required: true,
  85. errorMessage: '请输入收货人姓名',
  86. }, ],
  87. },
  88. mobile,
  89. detailAddress: {
  90. rules: [{
  91. required: true,
  92. errorMessage: '请输入详细地址',
  93. }]
  94. },
  95. areaName: {
  96. rules: [{
  97. required: true,
  98. errorMessage: '请选择您的位置'
  99. }]
  100. },
  101. };
  102. // 确认选择地区
  103. const onRegionConfirm = (e) => {
  104. state.model.areaName = `${e.province_name} ${e.city_name} ${e.district_name}`
  105. state.model.areaId = e.district_id;
  106. state.showRegion = false;
  107. };
  108. // 获得地区数据
  109. const getAreaData = () => {
  110. if (_.isEmpty(uni.getStorageSync('areaData'))) {
  111. AreaApi.getAreaTree().then((res) => {
  112. if (res.code === 0) {
  113. uni.setStorageSync('areaData', res.data);
  114. }
  115. });
  116. }
  117. };
  118. // 保存收货地址
  119. const onSave = async () => {
  120. // 参数校验
  121. const validate = await unref(addressFormRef)
  122. .validate()
  123. .catch((error) => {
  124. console.log('error: ', error);
  125. });
  126. if (!validate) {
  127. return;
  128. }
  129. // 提交请求
  130. const formData = {
  131. ...state.model
  132. }
  133. const {
  134. code
  135. } = state.model.id > 0 ? await AddressApi.updateAddress(formData) :
  136. await AddressApi.createAddress(formData);
  137. if (code === 0) {
  138. sheep.$router.back();
  139. }
  140. };
  141. // 删除收货地址
  142. const onDelete = () => {
  143. uni.showModal({
  144. title: '提示',
  145. content: '确认删除此收货地址吗?',
  146. success: async function(res) {
  147. if (!res.confirm) {
  148. return;
  149. }
  150. const {
  151. code
  152. } = await AddressApi.deleteAddress(state.model.id);
  153. if (code === 0) {
  154. sheep.$router.back();
  155. }
  156. },
  157. });
  158. };
  159. onLoad(async (options) => {
  160. // 获得地区数据
  161. getAreaData();
  162. // 情况一:基于 id 获得收件地址
  163. if (options.id) {
  164. let {
  165. code,
  166. data
  167. } = await AddressApi.getAddress(options.id);
  168. if (code !== 0) {
  169. return;
  170. }
  171. state.model = data;
  172. }
  173. // 情况二:微信导入 TODO 芋艿:待接入
  174. if (options.data) {
  175. let data = JSON.parse(options.data);
  176. $helper.toast(data);
  177. if (options.data) {
  178. let data = JSON.parse(options.data);
  179. state.model = {
  180. ...state.model,
  181. ...data,
  182. };
  183. }
  184. // const areaData = uni.getStorageSync('areaData');
  185. // let provinceArr = areaData.filter(item => item.name == data.province_name);
  186. // data.province_id = provinceArr[0].id;
  187. // let provinceArr2 = provinceArr[0].children.filter(item => item.name == data.city_name);
  188. // data.city_id = provinceArr2[0].id;
  189. // let provinceArr3 = provinceArr2[0].children.filter(item => item.name == data.district_name);
  190. // data.district_id = provinceArr3[0].id;
  191. // state.model = {
  192. // ...state.model,
  193. // ...data,
  194. // };
  195. }
  196. });
  197. </script>
  198. <style lang="scss" scoped>
  199. :deep() {
  200. .uni-forms-item__label .label-text {
  201. font-size: 28rpx !important;
  202. color: #333333 !important;
  203. line-height: normal !important;
  204. }
  205. .uni-easyinput__content-input {
  206. font-size: 28rpx !important;
  207. color: #333333 !important;
  208. line-height: normal !important;
  209. padding-left: 0 !important;
  210. }
  211. .uni-easyinput__content-textarea {
  212. font-size: 28rpx !important;
  213. color: #333333 !important;
  214. line-height: normal !important;
  215. margin-top: 8rpx !important;
  216. }
  217. .uni-icons {
  218. font-size: 40rpx !important;
  219. }
  220. .is-textarea-icon {
  221. margin-top: 22rpx;
  222. }
  223. .is-disabled {
  224. color: #333333;
  225. }
  226. }
  227. .default-box {
  228. width: 100%;
  229. box-sizing: border-box;
  230. height: 100rpx;
  231. .default-box-title {
  232. font-size: 28rpx;
  233. color: #333333;
  234. line-height: normal;
  235. }
  236. }
  237. .footer-box {
  238. .save-btn {
  239. width: 710rpx;
  240. height: 80rpx;
  241. border-radius: 40rpx;
  242. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  243. color: $white;
  244. }
  245. .cancel-btn {
  246. width: 710rpx;
  247. height: 80rpx;
  248. border-radius: 40rpx;
  249. background: var(--ui-BG);
  250. }
  251. }
  252. </style>