addAddress.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view>
  3. <u-navbar title="我的收货地址" :rightText="deleteState ? '删除' : ''" autoBack placeholder
  4. @rightClick="rightClick"></u-navbar>
  5. <view class="card_form">
  6. <x-form ref="personage" :list="list" :model="model" :rules="rules" btnTitle="保存地址" @confirm="confirm"
  7. @clientele="clientele"></x-form>
  8. </view>
  9. <x-modal :show="show" :title="title" @close="show = false"></x-modal>
  10. </view>
  11. </template>
  12. <script>
  13. import {
  14. urbanArea
  15. } from '@/static/js/districtCode.js'
  16. export default {
  17. data() {
  18. return {
  19. show: false,
  20. title: '地址删除后无法恢复是否删除地址?',
  21. areaData: urbanArea(), //原始数据
  22. deleteState: false,
  23. list: [{
  24. field: 'name',
  25. label: '姓名',
  26. placeholder: '请输入姓名',
  27. type: 'input',
  28. required: true,
  29. }, {
  30. field: 'phone',
  31. label: '手机号码',
  32. placeholder: '请输入手机号码',
  33. type: 'input',
  34. required: true,
  35. }, {
  36. field: 'region',
  37. label: '所在城市/地区',
  38. placeholder: '请选择所在城市/地区',
  39. type: 'cascader',
  40. required: true,
  41. }, {
  42. field: 'address',
  43. label: '详细地址',
  44. placeholder: '请输入详细地址',
  45. type: 'input',
  46. required: true,
  47. }, {
  48. field: 'isDefault',
  49. label: '设置默认地址',
  50. type: 'default',
  51. }],
  52. model: {
  53. name: '',
  54. phone: '',
  55. region: '',
  56. address: '',
  57. isDefault: false,
  58. },
  59. rules: {
  60. 'name': {
  61. type: 'string',
  62. required: true,
  63. message: '请输入姓名',
  64. trigger: ['blur', 'change']
  65. },
  66. 'phone': {
  67. type: 'string',
  68. required: true,
  69. message: '请输入手机号码',
  70. trigger: ['blur', 'change']
  71. },
  72. 'region': {
  73. type: 'string',
  74. required: true,
  75. message: '请选择所在城市/地区',
  76. trigger: ['blur', 'change']
  77. },
  78. 'address': {
  79. type: 'string',
  80. required: true,
  81. message: '请输入详细地址',
  82. trigger: ['blur', 'change']
  83. },
  84. },
  85. regionCode: [],
  86. addressId: '',
  87. }
  88. },
  89. onLoad(value) {
  90. // console.log(value, 24)
  91. if (value.revamp == 'true') {
  92. this.deleteState = true
  93. this.getListDetails(value.id)
  94. this.addressId = value.id
  95. } else {
  96. this.deleteState = false
  97. }
  98. },
  99. methods: {
  100. confirm(value) {
  101. this.regionCode = value.value
  102. this.model.city = this.regionCode[0].value
  103. this.model.area = this.regionCode[1].value
  104. let arr1 = []
  105. let arr2 = []
  106. this.regionCode.forEach(item => {
  107. arr1.push(item.value)
  108. arr2.push(item.label)
  109. })
  110. this.model.hr = arr1.join('/')
  111. this.model.region = arr2.join('/')
  112. },
  113. clientele() {
  114. // console.log(this.model, 234)
  115. if (!this.deleteState) {
  116. // 添加
  117. const param = {
  118. ...this.model
  119. }
  120. delete param.region
  121. this.$api.post('/api/applet/address', param).then(res => {
  122. if (res.code == 200) {
  123. uni.navigateBack({
  124. delta: 1
  125. });
  126. }
  127. })
  128. } else {
  129. // 修改
  130. const param = {
  131. ...this.model
  132. }
  133. delete param.region
  134. delete param.hr
  135. console.log(param, 25)
  136. this.$api.put('/api/applet/address', param).then(res => {
  137. if (res.code == 200) {
  138. uni.navigateBack({
  139. delta: 1
  140. });
  141. }
  142. })
  143. }
  144. },
  145. // 删除
  146. rightClick() {
  147. this.show = true
  148. return
  149. this.$api.delete('/api/applet/address', {
  150. id: this.addressId,
  151. }).then(res => {
  152. if (res.code == 200) {
  153. uni.navigateBack({
  154. delta: 1
  155. });
  156. }
  157. })
  158. },
  159. getListDetails(id) {
  160. const arr = this.$cache.getCache('addressInfo')
  161. console.log(arr, 25)
  162. this.model.name = arr.name
  163. this.model.phone = arr.phone
  164. this.model.address = arr.address
  165. this.model.isDefault = arr.isDefault
  166. this.model.city = arr.city
  167. this.model.area = arr.area
  168. var city = this.cityScreening(arr.city, this.areaData)
  169. var area = this.cityScreening(arr.area, this.areaData)
  170. this.model.region = city + '/' + area
  171. },
  172. // 城市筛选
  173. cityScreening(value, list) {
  174. function getChildById(parentArray, id) {
  175. for (let i = 0; i < parentArray.length; i++) {
  176. if (parentArray[i].value === id) { // 如果当前元素的ID与目标ID相等,则返回该元素
  177. return parentArray[i];
  178. } else if (parentArray[i].children && Array.isArray(parentArray[i]
  179. .children)) { // 判断当前元素是否有子节点且类型为数组
  180. const result = getChildById(parentArray[i].children, id); // 对子节点进行递归调用
  181. if (result !== null) { // 若子节点存在结果,则返回该结果
  182. return result;
  183. }
  184. }
  185. }
  186. return null; // 没有找到符合条件的元素时返回null
  187. }
  188. var name = getChildById(list, value)
  189. if (name != null) {
  190. return name.label
  191. }
  192. },
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .card_form {
  198. margin: 10rpx 40rpx;
  199. }
  200. </style>