userAddress.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <template>
  2. <uni-popup ref="popup" type="bottom">
  3. <view class="container">
  4. <div class="tit">
  5. <text class="close">关闭</text>
  6. </div>
  7. <form @submit="formSubmit" v-if="isadd">
  8. <view class='addAddress'>
  9. <view class="pad30">
  10. <view class='default acea-row row-middle borderRadius15'>
  11. <uni-easyinput type="textarea" v-model="addressValue" placeholder="粘贴地址信息,自动拆分姓名、电话和地址" @blur="identify()" />
  12. </view>
  13. </view>
  14. <view class="pad30 mt-22">
  15. <view class='list borderRadius15'>
  16. <view class='item acea-row row-between-wrapper'>
  17. <view class='name'>姓名</view>
  18. <input type='text' placeholder='请输入姓名' name='real_name' v-model="userAddress.real_name"
  19. placeholder-class='placeholder' />
  20. </view>
  21. <view class='item acea-row row-between-wrapper'>
  22. <view class='name'>联系电话</view>
  23. <input type='number' placeholder='请输入联系电话' name="phone" v-model='userAddress.phone'
  24. placeholder-class='placeholder' pattern="\d*" />
  25. </view>
  26. <view class='item acea-row row-between-wrapper'>
  27. <view class='name'>所在地区</view>
  28. <view class="address acea-row row-between">
  29. <input type='text' placeholder='请填写具体地址' name='region' placeholder-class='placeholder'
  30. v-model='userAddress.region' class="detail" />
  31. </view>
  32. </view>
  33. <view class='item acea-row row-between-wrapper'>
  34. <view class='name'>详细地址</view>
  35. <view class="address">
  36. <input type='text' placeholder='请填写具体地址' name='detail' placeholder-class='placeholder'
  37. v-model='userAddress.detail' class="detail" />
  38. </view>
  39. </view>
  40. <view class='item acea-row row-between-wrapper'>
  41. <view class='name'>详细地址</view>
  42. <view class="address">
  43. <uni-data-checkbox v-model="is_default" :localdata="range"></uni-data-checkbox>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <button class='keepBnt bg-color' form-type="submit">立即保存</button>
  49. </view>
  50. </form>
  51. <div class="editaddress" v-else>
  52. <view class="addbar" @click="isadd = true">
  53. <text class="iconfont sp">&#xe760;</text>
  54. <text class="sp">添加地址</text>
  55. </view>
  56. <uni-swipe-action class="swipeAction">
  57. <uni-swipe-action-item class="swipeAction_item" v-for="(i,s) in addressList" :key="s">
  58. <view class="addressLi clearfix" @click="onSelect(i,s)">
  59. <view class="addressLi_l">
  60. <text v-if="i.status != 1" class="iconfont">&#xe623;</text>
  61. <text v-else class="iconfont pitchOn">&#xe624;</text>
  62. </view>
  63. <view class="addressLi_r">
  64. <view class="user">
  65. <text class="name">{{i.name}}</text>
  66. <text>{{i.mobile}}</text>
  67. </view>
  68. <view class="address">{{i.address}}</view>
  69. </view>
  70. </view>
  71. <template v-slot:right>
  72. <view class="btns clearfix">
  73. <view class="btn edit"><text class="iconfont">&#xe600;</text></view>
  74. <view class="btn del"><text class="iconfont">&#xe601;</text></view>
  75. </view>
  76. </template>
  77. </uni-swipe-action-item>
  78. </uni-swipe-action>
  79. </div>
  80. </view>
  81. </uni-popup>
  82. </template>
  83. <script>
  84. import { get, post, u_post } from "@/request/api.js";
  85. import AddressParse from './zh-address-parse.min.js'
  86. let app = getApp();
  87. var appEv = app.$vm.$options;
  88. export default {
  89. data() {
  90. return {
  91. userAddress: {
  92. real_name:"",
  93. phone:"",
  94. region:"",
  95. detail:"",
  96. }, //地址详情
  97. addressValue:"",
  98. is_default: 0, //是否设为默认地址
  99. range: [{"value": 0,"text": "否" },{"value": 1,"text": "是"}],
  100. isadd: false,
  101. addressList: [],
  102. }
  103. },
  104. components:{},
  105. created () {
  106. this.loadAddress();
  107. },
  108. methods: {
  109. identify(){
  110. const options = {
  111. type: 0, // 哪种方式解析,0:正则,1:树查找
  112. textFilter: [], // 预清洗的字段
  113. nameMaxLength: 4, // 查找最大的中文名字长度
  114. }
  115. // type参数0表示使用正则解析,1表示采用树查找, textFilter地址预清洗过滤字段。
  116. if(!!this.addressValue){
  117. const parseResult = AddressParse(this.addressValue, options)
  118. // console.log(parseResult);
  119. this.userAddress.real_name = parseResult.name;
  120. this.userAddress.phone = parseResult.phone;
  121. this.userAddress.detail = parseResult.detail;
  122. this.userAddress.region = parseResult.province + '/' + parseResult.city + '/' + parseResult.area;
  123. }
  124. },
  125. formSubmit(){
  126. post("user/addAddress",{
  127. address: this.userAddress.region + this.userAddress.detail,
  128. mobile: this.userAddress.phone,
  129. name: this.userAddress.real_name,
  130. is_default: this.is_default
  131. }).then(res => {
  132. if (res.code === 0) {
  133. appEv.errTips(res.msg);
  134. setTimeout(() => {
  135. // uni.navigateBack();
  136. this.$emit('addressConfirm');
  137. this.close();
  138. }, 1500);
  139. }
  140. })
  141. },
  142. // 获取用户地址
  143. loadAddress(){
  144. post("user/addressList").then(res => {
  145. if(res.code === 0){
  146. this.addressList = res.data.data;
  147. }
  148. })
  149. },
  150. onSelect(i,s){
  151. this.addressList.forEach(e => {
  152. e.status = "0";
  153. });
  154. this.addressList[s].status = "1";
  155. this.editAddress();
  156. },
  157. editAddress(){
  158. this.addressList.forEach((e,s) => {
  159. post("user/editAddress",{
  160. ...e, is_default: e.status
  161. }).then(res=>{
  162. if(this.addressList.length == s+1){
  163. this.$emit('addressConfirm');
  164. this.close();
  165. }
  166. })
  167. });
  168. },
  169. open(type) {
  170. this.$refs.popup.open('bottom')
  171. if(type == "add") this.isadd = true
  172. },
  173. close() {
  174. this.$refs.popup.close()
  175. }
  176. },
  177. }
  178. </script>
  179. <style lang="scss" scoped>
  180. .container{
  181. background-color: #f5f5f5;
  182. height: 70vh;
  183. // padding:30rpx 0;
  184. border-radius: 30rpx 30rpx 0 0;
  185. }
  186. .pad30 {
  187. padding: 0 30rpx
  188. }
  189. .p_center {
  190. text-align: center;
  191. }
  192. .acea-row {
  193. display: flex;
  194. }
  195. .row-middle {
  196. align-items: center
  197. }
  198. .row-between-wrapper {
  199. align-items: center;
  200. justify-content: space-between
  201. }
  202. .borderRadius15 {
  203. border-radius: 15rpx !important;
  204. }
  205. .fontcolor{
  206. color: var(--view-theme);
  207. }
  208. .addAddress .list {
  209. background-color: #fff;
  210. }
  211. .addAddress .list .item {
  212. padding: 30rpx;
  213. border-top: 1rpx solid #eee;
  214. position: relative;
  215. }
  216. .addAddress .list .item .detail{
  217. width: 368rpx;
  218. }
  219. .addAddress .list .item .location{
  220. position: absolute;
  221. right: 46rpx;
  222. top: 50%;
  223. margin-top: -40rpx!important;
  224. font-size: 24rpx;
  225. text-align: center;
  226. }
  227. .addAddress .list .item .icon-dizhi{
  228. font-size: 36rpx!important;
  229. }
  230. .addAddress .list .item .name {
  231. width: 195rpx;
  232. font-size: 30rpx;
  233. color: #333;
  234. }
  235. .addAddress .list .item .address {
  236. flex: 1;
  237. }
  238. .addAddress .list .item .address .addressCon{
  239. width: 360rpx;
  240. }
  241. .addAddress .list .item .address .addressCon .tip{
  242. font-size: 21rpx;
  243. margin-top: 4rpx;
  244. }
  245. .addAddress .list .item input {
  246. /* // width: 475rpx; */
  247. flex: 1;
  248. font-size: 30rpx;
  249. }
  250. .placeholder {
  251. color: #ccc;
  252. }
  253. .addAddress .list .item .picker {
  254. width: 430rpx;
  255. font-size: 30rpx;
  256. }
  257. .addAddress .list .item .iconfont {
  258. font-size: 30rpx;
  259. margin-top: 4rpx;
  260. }
  261. .addAddress .default {
  262. padding: 30rpx;
  263. /* height: 90rpx; */
  264. background-color: #fff;
  265. }
  266. .addAddress .default checkbox {
  267. margin-right: 15rpx;
  268. }
  269. .addAddress .keepBnt {
  270. width: 690rpx;
  271. height: 86rpx;
  272. border-radius: 50rpx;
  273. text-align: center;
  274. line-height: 86rpx;
  275. margin: 50rpx auto;
  276. font-size: 32rpx;
  277. color: #fff;
  278. }
  279. .mt-22{
  280. margin-top: 22rpx;
  281. }
  282. .bg-color {
  283. background-color: #2DB389;
  284. }
  285. .addbar{
  286. background: #fff;
  287. text-align: center;
  288. margin-bottom: 50rpx;
  289. padding: 36rpx 0;
  290. .iconfont{
  291. font-size: 46rpx;
  292. margin-right: 20rpx;
  293. }
  294. .sp{
  295. vertical-align: middle;
  296. }
  297. }
  298. .swipeAction{
  299. // padding: 20rpx 30rpx;
  300. font-size: 28rpx;
  301. .addressLi{
  302. .addressLi_l{
  303. width: 80rpx;
  304. height: 80rpx;
  305. display: flex;
  306. // justify-content: center;
  307. align-items: center;
  308. .iconfont{
  309. color: #666;
  310. font-size: 38rpx;
  311. }
  312. .pitchOn{
  313. color: #1989fa;
  314. }
  315. }
  316. .addressLi_r{
  317. width: calc(100% - 80rpx);
  318. }
  319. .addressLi_l,.addressLi_r{
  320. float: left;
  321. }
  322. .user{
  323. margin-bottom: 10rpx;
  324. }
  325. .name{
  326. // font-weight: bold;
  327. font-size: 32rpx;
  328. margin-right: 10rpx;
  329. }
  330. .address{
  331. font-size: 24rpx;
  332. color: #999;
  333. }
  334. }
  335. .swipeAction_item{
  336. background: #fff;
  337. margin-bottom: 20rpx;
  338. }
  339. .btns{
  340. .btn{
  341. float: left;
  342. display:flex;
  343. justify-content : center;
  344. align-items : center;
  345. height: 100%;
  346. padding: 0 30rpx;
  347. color: #fff;
  348. font-size: 36rpx;
  349. }
  350. .edit{
  351. background: rgba(25,137,250,0.5);
  352. }
  353. .del{
  354. background: rgba(238,10,36,0.5);
  355. }
  356. }
  357. }
  358. ::v-deep .uni-swipe_box{
  359. padding: 20rpx 30rpx;
  360. }
  361. .tit{
  362. padding: 10rpx 0;
  363. .close{
  364. float: right;
  365. }
  366. }
  367. </style>