address_edit.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="address-edit">
  3. <view class="form bg-white">
  4. <u-field v-model="addressObj.contact" label="收货人" placeholder="请填写收货人姓名">
  5. </u-field>
  6. <u-field v-model="addressObj.telephone" label="联系方式" placeholder="请填写手机号码">
  7. </u-field>
  8. <view @click="showRegion = true">
  9. <u-field v-model="region" :disabled="true" label="所在地区" placeholder="请选择省、市、区" right-icon="arrow-right">
  10. </u-field>
  11. </view>
  12. <view>
  13. <u-field v-model="addressObj.address" type="textarea" label="详细地址" placeholder="请填写小区、街道、门牌号等信息"
  14. :field-style="{flex: 1, height: '200rpx'}" />
  15. </view>
  16. </view>
  17. <view class="m-t-10 m-b-10 bg-white p-20">
  18. <u-checkbox @click="changeDefault" v-model="addressObj.is_default" shape="circle">
  19. <text class="xs">设置为默认</text>
  20. </u-checkbox>
  21. </view>
  22. <button class="my-btn bg-primary white br60" @tap="formSubmit">完成</button>
  23. <u-select v-model="showRegion" mode="mutil-column-auto" @confirm="regionChange" :list="lists"></u-select>
  24. </view>
  25. </template>
  26. <script>
  27. // +----------------------------------------------------------------------
  28. // | likeshop开源商城系统
  29. // +----------------------------------------------------------------------
  30. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  31. // | gitee下载:https://gitee.com/likeshop_gitee
  32. // | github下载:https://github.com/likeshop-github
  33. // | 访问官网:https://www.likeshop.cn
  34. // | 访问社区:https://home.likeshop.cn
  35. // | 访问手册:http://doc.likeshop.cn
  36. // | 微信公众号:likeshop技术社区
  37. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  38. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  39. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  40. // | likeshop团队版权所有并拥有最终解释权
  41. // +----------------------------------------------------------------------
  42. // | author: likeshop.cn.team
  43. // +----------------------------------------------------------------------
  44. import {
  45. editAddress,
  46. getOneAddress,
  47. hasRegionCode,
  48. addAddress
  49. } from '@/api/user';
  50. import area from '@/utils/area'
  51. export default {
  52. data() {
  53. return {
  54. addressObj: {
  55. contact: '',
  56. telephone: '',
  57. province: '',
  58. city: '',
  59. district: '',
  60. address: '',
  61. is_default: false
  62. },
  63. region: '',
  64. addressId: '',
  65. defaultRegion: ['广东省', '广州市', '番禺区'],
  66. defaultRegionCode: '440113',
  67. showRegion: false,
  68. lists: []
  69. };
  70. },
  71. onLoad: function(options) {
  72. this.addressId = parseInt(options.id)
  73. if (options.id) {
  74. uni.setNavigationBarTitle({
  75. title: '编辑地址'
  76. });
  77. this.getOneAddressFun();
  78. } else {
  79. uni.setNavigationBarTitle({
  80. title: '添加地址'
  81. });
  82. this.getWxAddressFun();
  83. }
  84. this.$nextTick(() => {
  85. this.lists = area
  86. console.log(area);
  87. })
  88. },
  89. onUnload: function() {
  90. uni.removeStorageSync('wxAddress');
  91. },
  92. methods: {
  93. async formSubmit() {
  94. let {
  95. addressObj: {
  96. contact,
  97. telephone,
  98. province_id,
  99. city_id,
  100. district_id,
  101. is_default,
  102. address
  103. },
  104. addressId,
  105. region,
  106. } = this;
  107. if (!contact) return this.$toast({
  108. title: '请填写收货人姓名'
  109. });
  110. if (!telephone) return this.$toast({
  111. title: '请填写手机号码'
  112. });
  113. if (!region) return this.$toast({
  114. title: '请选择省、市、区'
  115. });
  116. if (!address) return this.$toast({
  117. title: '请填写小区、街道、门牌号等信息'
  118. });
  119. const params = {
  120. contact,
  121. telephone,
  122. province_id: parseInt(province_id),
  123. city_id: parseInt(city_id),
  124. district_id: parseInt(district_id),
  125. is_default: is_default ? 1 : 0,
  126. id: addressId,
  127. address
  128. }
  129. const {
  130. code,
  131. msg
  132. } = addressId ? await editAddress(params) : await addAddress(params)
  133. if (code == 1) {
  134. this.$toast({
  135. title: msg
  136. }, {
  137. tab: 3,
  138. url: 1
  139. });
  140. }
  141. },
  142. regionChange(region) {
  143. this.addressObj.province_id = region[0].value;
  144. this.addressObj.city_id = region[1].value;
  145. this.addressObj.district_id = region[2].value;
  146. this.region = region[0].label + " " + region[1].label + " " + region[2].label
  147. },
  148. getOneAddressFun() {
  149. getOneAddress(this.addressId).then(res => {
  150. if (res.code == 1) {
  151. let {
  152. city,
  153. province,
  154. district
  155. } = res.data;
  156. this.addressObj = res.data;
  157. this.region = `${province} ${city} ${district}`
  158. }
  159. });
  160. },
  161. getWxAddressFun() {
  162. let wxAddress = uni.getStorageSync('wxAddress');
  163. if (!wxAddress) return;
  164. wxAddress = JSON.parse(wxAddress)
  165. let {
  166. userName: contact,
  167. telNumber: telephone,
  168. provinceName: province,
  169. cityName: city,
  170. detailInfo: address
  171. } = wxAddress;
  172. let district = wxAddress.countryName || wxAddress.countyName
  173. hasRegionCode({
  174. province,
  175. city,
  176. district
  177. }).then(res => {
  178. if (res.code == 1) {
  179. if (res.data.province && res.data.city && res.data.district) {
  180. this.region = `${province} ${city} ${district}`;
  181. this.addressObj.province_id = res.data.province;
  182. this.addressObj.city_id = res.data.city;
  183. this.addressObj.district_id = res.data.district;
  184. }
  185. this.addressObj.contact = contact;
  186. this.addressObj.telephone = telephone
  187. this.addressObj.address = address
  188. }
  189. });
  190. }
  191. }
  192. };
  193. </script>
  194. <style lang="scss">
  195. .address-edit {
  196. padding-top: 10rpx;
  197. .my-btn {
  198. margin: 30rpx 26rpx;
  199. text-align: center;
  200. }
  201. }
  202. </style>