addAddress.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. <u-modal width="450rpx" :show="show" showCancelButton :content='title' @cancel="show = false"
  10. @confirm="expurgate"></u-modal>
  11. </view>
  12. </template>
  13. <script>
  14. import {
  15. urbanArea
  16. } from '@/static/js/districtCode.js'
  17. export default {
  18. data() {
  19. return {
  20. show: false,
  21. title: '地址删除后无法恢复是否删除地址?',
  22. areaData: urbanArea(), //原始数据
  23. deleteState: false,
  24. list: [{
  25. field: 'name',
  26. label: '姓名',
  27. placeholder: '请输入姓名',
  28. type: 'input',
  29. required: true,
  30. }, {
  31. field: 'phone',
  32. label: '手机号码',
  33. placeholder: '请输入手机号码',
  34. type: 'input',
  35. required: true,
  36. genre: 'number',
  37. }, {
  38. field: 'region',
  39. label: '所在城市/地区',
  40. placeholder: '请选择所在城市/地区',
  41. type: 'cascader',
  42. required: true,
  43. }, {
  44. field: 'address',
  45. label: '详细地址',
  46. placeholder: '请输入详细地址',
  47. type: 'input',
  48. required: true,
  49. }, {
  50. field: 'isDefault',
  51. label: '设置默认地址',
  52. type: 'default',
  53. }],
  54. model: {
  55. name: '',
  56. phone: '',
  57. region: '',
  58. address: '',
  59. isDefault: false,
  60. },
  61. rules: {
  62. 'name': {
  63. type: 'string',
  64. required: true,
  65. message: '请输入姓名',
  66. trigger: ['blur', 'change']
  67. },
  68. 'phone': {
  69. type: 'string',
  70. required: true,
  71. message: '请输入手机号码',
  72. trigger: ['blur', 'change']
  73. },
  74. 'region': {
  75. type: 'string',
  76. required: true,
  77. message: '请选择所在城市/地区',
  78. trigger: ['blur', 'change']
  79. },
  80. 'address': {
  81. type: 'string',
  82. required: true,
  83. message: '请输入详细地址',
  84. trigger: ['blur', 'change']
  85. },
  86. },
  87. regionCode: [],
  88. addressId: '',
  89. }
  90. },
  91. onLoad(value) {
  92. // console.log(value, 24)
  93. if (value.revamp == 'true') {
  94. this.deleteState = true
  95. this.getListDetails(value.id)
  96. this.addressId = value.id
  97. } else {
  98. this.deleteState = false
  99. }
  100. },
  101. methods: {
  102. confirm(value) {
  103. this.regionCode = value.value
  104. this.model.city = this.regionCode[0].value
  105. this.model.area = this.regionCode[1].value
  106. let arr1 = []
  107. let arr2 = []
  108. this.regionCode.forEach(item => {
  109. arr1.push(item.value)
  110. arr2.push(item.label)
  111. })
  112. this.model.hr = arr1.join('/')
  113. this.model.region = arr2.join('/')
  114. },
  115. async clientele() {
  116. // console.log(this.model, 234)
  117. if (!this.deleteState) {
  118. // 添加
  119. const param = {
  120. ...this.model
  121. }
  122. delete param.region
  123. let arr = await this.longitudeLatitude(param)
  124. var propertyName = arr.location.split(",")
  125. param.lng = Number(propertyName[0])
  126. param.lat = Number(propertyName[1])
  127. this.$api.post('/api/applet/address', param).then(res => {
  128. if (res.code == 200) {
  129. uni.navigateBack({
  130. delta: 1
  131. });
  132. }
  133. })
  134. } else {
  135. // 修改
  136. const param = {
  137. id: Number(this.addressId),
  138. ...this.model
  139. }
  140. delete param.region
  141. delete param.hr
  142. let arr = await this.longitudeLatitude(param)
  143. var propertyName = arr.location.split(",")
  144. param.lng = Number(propertyName[0])
  145. param.lat = Number(propertyName[1])
  146. this.$api.put('/api/applet/address', param).then(res => {
  147. if (res.code == 200) {
  148. uni.navigateBack({
  149. delta: 1
  150. });
  151. }
  152. })
  153. }
  154. },
  155. // modal删除弹窗
  156. rightClick() {
  157. this.show = true
  158. },
  159. // 删除
  160. expurgate() {
  161. this.$api.delete('/api/applet/address', {
  162. id: Number(this.addressId),
  163. }).then(res => {
  164. if (res.code == 200) {
  165. uni.navigateBack({
  166. delta: 1
  167. });
  168. }
  169. })
  170. },
  171. getListDetails(id) {
  172. const arr = this.$cache.getCache('addressInfo')
  173. this.model.name = arr.name
  174. this.model.phone = arr.phone
  175. this.model.address = arr.address
  176. this.model.isDefault = arr.isDefault
  177. this.model.city = arr.city
  178. this.model.area = arr.area
  179. var city = this.cityScreening(arr.city, this.areaData)
  180. var area = this.cityScreening(arr.area, this.areaData)
  181. this.model.region = city + '/' + area
  182. },
  183. // 获取经纬度
  184. longitudeLatitude(value) {
  185. var cityArr = urbanArea()
  186. var city = this.cityScreening(value.city, cityArr)
  187. var area = this.cityScreening(value.area, cityArr)
  188. return new Promise((resolve, inject) => {
  189. uni.request({
  190. url: 'https://restapi.amap.com/v3/geocode/geo', // 你的API接口地址
  191. method: 'GET', // 请求方法,可选'GET','POST'等
  192. data: {
  193. key: 'fcddcc366fd5bd355e35f01b7cce82ef',
  194. address: city + area + value.address,
  195. },
  196. success: (res) => {
  197. resolve(res.data.geocodes[0])
  198. },
  199. fail: (err) => {
  200. this.confirmLoading = false
  201. // console.log('请求失败:', err); // 请求失败的回调函数
  202. }
  203. });
  204. })
  205. },
  206. // 城市筛选
  207. cityScreening(value, list) {
  208. function getChildById(parentArray, id) {
  209. for (let i = 0; i < parentArray.length; i++) {
  210. if (parentArray[i].value === id) { // 如果当前元素的ID与目标ID相等,则返回该元素
  211. return parentArray[i];
  212. } else if (parentArray[i].children && Array.isArray(parentArray[i]
  213. .children)) { // 判断当前元素是否有子节点且类型为数组
  214. const result = getChildById(parentArray[i].children, id); // 对子节点进行递归调用
  215. if (result !== null) { // 若子节点存在结果,则返回该结果
  216. return result;
  217. }
  218. }
  219. }
  220. return null; // 没有找到符合条件的元素时返回null
  221. }
  222. var name = getChildById(list, value)
  223. if (name != null) {
  224. return name.label
  225. }
  226. },
  227. }
  228. }
  229. </script>
  230. <style lang="scss" scoped>
  231. ::v-deep .u-modal__content__text {
  232. text-align: center;
  233. }
  234. .card_form {
  235. margin: 10rpx 40rpx;
  236. }
  237. </style>