addClient.vue 7.7 KB

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