addAddress.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. validator: (rule, value, callback) => {
  75. return uni.$u.test.mobile(value);
  76. },
  77. message: '手机号码不正确',
  78. trigger: ['change', 'blur'],
  79. }],
  80. 'region': {
  81. type: 'string',
  82. required: true,
  83. message: '请选择所在城市/地区',
  84. trigger: ['blur', 'change']
  85. },
  86. 'address': {
  87. type: 'string',
  88. required: true,
  89. message: '请输入详细地址',
  90. trigger: ['blur', 'change']
  91. },
  92. },
  93. regionCode: [],
  94. addressId: '',
  95. }
  96. },
  97. onLoad(value) {
  98. // console.log(value, 24)
  99. if (value.revamp == 'true') {
  100. this.deleteState = true
  101. this.getListDetails(value.id)
  102. this.addressId = value.id
  103. } else {
  104. this.deleteState = false
  105. }
  106. },
  107. methods: {
  108. confirm(value) {
  109. this.regionCode = value.value
  110. this.model.city = this.regionCode[0].value
  111. this.model.area = this.regionCode[1].value
  112. let arr1 = []
  113. let arr2 = []
  114. this.regionCode.forEach(item => {
  115. arr1.push(item.value)
  116. arr2.push(item.label)
  117. })
  118. this.model.hr = arr1.join('/')
  119. this.model.region = arr2.join('/')
  120. },
  121. async clientele() {
  122. // console.log(this.model, 234)
  123. if (!this.deleteState) {
  124. // 添加
  125. const param = {
  126. ...this.model
  127. }
  128. delete param.region
  129. let arr = await this.longitudeLatitude(param)
  130. var propertyName = arr.location.split(",")
  131. param.lng = Number(propertyName[0])
  132. param.lat = Number(propertyName[1])
  133. this.$api.post('/api/applet/address', param).then(res => {
  134. if (res.code == 200) {
  135. uni.navigateBack({
  136. delta: 1
  137. });
  138. }
  139. })
  140. } else {
  141. // 修改
  142. const param = {
  143. id: Number(this.addressId),
  144. ...this.model
  145. }
  146. delete param.region
  147. delete param.hr
  148. let arr = await this.longitudeLatitude(param)
  149. var propertyName = arr.location.split(",")
  150. param.lng = Number(propertyName[0])
  151. param.lat = Number(propertyName[1])
  152. this.$api.put('/api/applet/address', param).then(res => {
  153. if (res.code == 200) {
  154. uni.navigateBack({
  155. delta: 1
  156. });
  157. }
  158. })
  159. }
  160. },
  161. // modal删除弹窗
  162. rightClick() {
  163. this.show = true
  164. },
  165. // 删除
  166. expurgate() {
  167. this.$api.delete('/api/applet/address', {
  168. id: Number(this.addressId),
  169. }).then(res => {
  170. if (res.code == 200) {
  171. uni.navigateBack({
  172. delta: 1
  173. });
  174. }
  175. })
  176. },
  177. getListDetails(id) {
  178. const arr = this.$cache.getCache('addressInfo')
  179. this.model.name = arr.name
  180. this.model.phone = arr.phone
  181. this.model.address = arr.address
  182. this.model.isDefault = arr.isDefault
  183. this.model.city = arr.city
  184. this.model.area = arr.area
  185. var city = this.cityScreening(arr.city, this.areaData)
  186. var area = this.cityScreening(arr.area, this.areaData)
  187. this.model.region = city + '/' + area
  188. },
  189. // 获取经纬度
  190. longitudeLatitude(value) {
  191. var cityArr = urbanArea()
  192. var city = this.cityScreening(value.city, cityArr)
  193. var area = this.cityScreening(value.area, cityArr)
  194. return new Promise((resolve, inject) => {
  195. uni.request({
  196. url: 'https://restapi.amap.com/v3/geocode/geo', // 你的API接口地址
  197. method: 'GET', // 请求方法,可选'GET','POST'等
  198. data: {
  199. key: 'fcddcc366fd5bd355e35f01b7cce82ef',
  200. address: city + area + value.address,
  201. },
  202. success: (res) => {
  203. resolve(res.data.geocodes[0])
  204. },
  205. fail: (err) => {
  206. this.confirmLoading = false
  207. // console.log('请求失败:', err); // 请求失败的回调函数
  208. }
  209. });
  210. })
  211. },
  212. // 城市筛选
  213. cityScreening(value, list) {
  214. function getChildById(parentArray, id) {
  215. for (let i = 0; i < parentArray.length; i++) {
  216. if (parentArray[i].value === id) { // 如果当前元素的ID与目标ID相等,则返回该元素
  217. return parentArray[i];
  218. } else if (parentArray[i].children && Array.isArray(parentArray[i]
  219. .children)) { // 判断当前元素是否有子节点且类型为数组
  220. const result = getChildById(parentArray[i].children, id); // 对子节点进行递归调用
  221. if (result !== null) { // 若子节点存在结果,则返回该结果
  222. return result;
  223. }
  224. }
  225. }
  226. return null; // 没有找到符合条件的元素时返回null
  227. }
  228. var name = getChildById(list, value)
  229. if (name != null) {
  230. return name.label
  231. }
  232. },
  233. }
  234. }
  235. </script>
  236. <style lang="scss" scoped>
  237. ::v-deep .u-modal__content__text {
  238. text-align: center;
  239. }
  240. .card_form {
  241. margin: 10rpx 40rpx;
  242. }
  243. </style>