edit.vue 7.0 KB

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