edit.vue 7.8 KB

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