addClient.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <!-- 添加新地址 -->
  3. <view :style="colorStyle">
  4. <form @submit="formSubmit">
  5. <view class='addAddress'>
  6. <view class="input-wrapper">
  7. <view class='list'>
  8. <view class='item acea-row row-between-wrapper'>
  9. <view class='name'>姓名</view>
  10. <input type='text' placeholder='请输入姓名' name='real_name' :value="userAddress.real_name" placeholder-class='placeholder'></input>
  11. </view>
  12. <view class='item acea-row row-between-wrapper'>
  13. <view class='name'>联系电话</view>
  14. <input type='number' maxlength="11" placeholder='请输入联系电话' name="phone" :value='userAddress.phone' placeholder-class='placeholder' pattern="\d*"></input>
  15. </view>
  16. <view class='item acea-row row-between-wrapper'>
  17. <view class='name'>所在地区</view>
  18. <view class="address acea-row row-between-wrapper">
  19. <view class="addressCon acea-row" @click="changeRegion">
  20. <text class="picker color-add" v-if="!addressInfo.length">请选择地址</text>
  21. <view v-else>
  22. <text class="picker">{{addressText}}</text>
  23. </view>
  24. </view>
  25. <view class="location1" @click="chooseLocation">
  26. <text class="iconfont icon-ic_location1 text-primary"></text>定位
  27. </view>
  28. </view>
  29. </view>
  30. <view class='item acea-row row-between-wrapper'>
  31. <view class='name'>详细地址</view>
  32. <view class="address">
  33. <input type='text' placeholder='请填写具体地址' name='detail' placeholder-class='placeholder' :value='userAddress.detail' class="detail"></input>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <button class='keepBnt' form-type="submit">立即保存</button>
  39. </view>
  40. </form>
  41. <areaWindow ref="areaWindow" :display="display" :address="addressInfo" @submit="OnChangeAddress" @changeClose="changeClose"></areaWindow>
  42. </view>
  43. </template>
  44. <script>
  45. import {getGeocoder,getCityList} from '@/api/user.js';
  46. import {mapGetters} from "vuex";
  47. import colors from '@/mixins/color.js';
  48. import areaWindow from '@/components/areaWindow';
  49. import AddressParse from '../components/zh-address-parse.min.js'
  50. export default {
  51. components: {
  52. areaWindow,
  53. },
  54. mixins: [colors],
  55. data() {
  56. return {
  57. id: 0, //地址id
  58. userAddress: {}, //地址详情
  59. district: [],
  60. display: false,
  61. addressInfo: [],
  62. addressVal: '',
  63. latitude: '',
  64. longitude: '',
  65. city_id: 0,
  66. addressValue: "",
  67. };
  68. },
  69. computed: {
  70. ...mapGetters(['isLogin']),
  71. addressText() {
  72. return this.addressInfo.map(v => v.label).join('/');
  73. }
  74. },
  75. onLoad(options) {
  76. },
  77. methods: {
  78. changeRegion() {
  79. this.display = true;
  80. },
  81. OnChangeAddress(address) {
  82. this.latitude = ''
  83. this.longitude = ''
  84. this.addressInfo = address;
  85. },
  86. // 关闭地址弹窗;
  87. changeClose: function() {
  88. this.display = false;
  89. },
  90. // 获取选中位置
  91. chooseLocation: function() {
  92. let self = this;
  93. uni.chooseLocation({
  94. success: (res) => {
  95. let latitude, longitude;
  96. latitude = res.latitude.toString();
  97. longitude = res.longitude.toString();
  98. this.latitude = res.latitude
  99. this.longitude = res.longitude
  100. getGeocoder({
  101. lat: latitude,
  102. long: longitude
  103. }).then(res => {
  104. const data = res.data;
  105. getCityList(data.address_component.province + '/' + data.address_component.city + '/' + data.address_component.district + '/' + (!data
  106. .address_reference.town ? '' : data.address_reference.town.title)).then(res => {
  107. self.addressInfo = res.data;
  108. self.userAddress.detail = data.formatted_addresses.recommend;
  109. }).catch(err => {
  110. self.$util.Tips({
  111. title: err
  112. });
  113. });
  114. })
  115. },
  116. fail: (err) => {
  117. console.log(err)
  118. }
  119. })
  120. },
  121. /**
  122. * 提交用户添加地址
  123. *
  124. */
  125. formSubmit: function(e) {
  126. let that = this,
  127. value = e.detail.value;
  128. if (!value.real_name) return that.$util.Tips({
  129. title: '请填写收货人姓名'
  130. });
  131. if (!value.phone) return that.$util.Tips({
  132. title: '请填写联系电话'
  133. });
  134. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  135. title: '请输入正确的手机号码'
  136. });
  137. if (!that.addressInfo.length) return that.$util.Tips({
  138. title: '请选择所在地区'
  139. });
  140. if (that.addressInfo.length < 3) return that.$util.Tips({
  141. title: '请补全所在地区信息'
  142. });
  143. if (!value.detail) return that.$util.Tips({
  144. title: '请填写详细地址'
  145. });
  146. value.id = 0;
  147. let regionArray = that.addressInfo;
  148. value.address = {
  149. province: regionArray[0].label,
  150. city: regionArray[1].label,
  151. district: regionArray[2].label,
  152. street: regionArray[3] ? regionArray[3].label : '',
  153. city_id: regionArray[regionArray.length - 1].id ? regionArray[regionArray.length - 1].id : that.city_id,
  154. };
  155. uni.$emit("refresh", value)
  156. uni.navigateBack()
  157. },
  158. identify() {
  159. const options = {
  160. type: 0, // 哪种方式解析,0:正则,1:树查找
  161. textFilter: [], // 预清洗的字段
  162. nameMaxLength: 4, // 查找最大的中文名字长度
  163. }
  164. const parseResult = AddressParse(this.addressValue.trim(), options)
  165. // type参数0表示使用正则解析,1表示采用树查找, textFilter地址预清洗过滤字段。
  166. if (this.addressValue.trim()) {
  167. getCityList(parseResult.province + '/' + parseResult.city + '/' + parseResult.area).then(res => {
  168. this.addressInfo = res.data;
  169. this.userAddress.phone = parseResult.phone;
  170. this.userAddress.real_name = parseResult.name;
  171. this.userAddress.detail = parseResult.detail;
  172. }).catch(err => {
  173. return this.$util.Tips({
  174. title: err
  175. });
  176. })
  177. }
  178. }
  179. }
  180. }
  181. </script>
  182. <style scoped lang="scss">
  183. .color-add {
  184. color: #cdcdcd;
  185. }
  186. .location1 {
  187. .iconfont {
  188. margin-right: 8rpx;
  189. }
  190. }
  191. .text-primary {
  192. color: $primary-admin;
  193. }
  194. .addAddress {
  195. padding: 24rpx 20rpx;
  196. }
  197. .addAddress .input-wrapper {
  198. margin-bottom: 20rpx;
  199. }
  200. .addAddress .list {
  201. padding: 8rpx 0;
  202. border-radius: 16rpx;
  203. background-color: #fff;
  204. }
  205. .addAddress .list .item {
  206. padding: 32rpx 24rpx;
  207. position: relative;
  208. }
  209. .addAddress .list .item .detail {
  210. width: 368rpx;
  211. }
  212. .addAddress .list .item .location {
  213. position: absolute;
  214. right: 46rpx;
  215. top: 50%;
  216. margin-top: -40rpx !important;
  217. font-size: 24rpx;
  218. text-align: center;
  219. }
  220. .addAddress .list .item .icon-dizhi {
  221. font-size: 36rpx !important;
  222. }
  223. .addAddress .list .item .name {
  224. width: 195rpx;
  225. font-size: 30rpx;
  226. color: #333;
  227. }
  228. .addAddress .list .item .address {
  229. // width: 412rpx;
  230. flex: 1;
  231. // margin-left: 20rpx;
  232. }
  233. .addAddress .list .item .address .addressCon {
  234. width: 360rpx;
  235. }
  236. .addAddress .list .item .address .addressCon .tip {
  237. font-size: 21rpx;
  238. margin-top: 4rpx;
  239. }
  240. .addAddress .list .item input {
  241. flex: 1;
  242. font-size: 30rpx;
  243. }
  244. .placeholder {
  245. color: #ccc;
  246. }
  247. .addAddress .list .item .picker {
  248. width: 430rpx;
  249. font-size: 30rpx;
  250. }
  251. .addAddress .list .item .iconfont {
  252. font-size: 30rpx;
  253. margin-top: 4rpx;
  254. }
  255. .addAddress .keepBnt {
  256. position: fixed;
  257. right: 20rpx;
  258. bottom: 40rpx;
  259. left: 20rpx;
  260. height: 80rpx;
  261. border-radius: 40rpx;
  262. text-align: center;
  263. line-height: 80rpx;
  264. font-size: 28rpx;
  265. color: #fff;
  266. background-color: $primary-admin;
  267. }
  268. </style>