addAddress.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <view class="address" :style="{ background:isAdd ? '#fff' : '#f5f5f5' }">
  3. <custom-nav :title="pageTitle"></custom-nav>
  4. <custom-toast ref='toast'></custom-toast>
  5. <view v-if="isAdd">
  6. <view class="form">
  7. <view class="form_item">
  8. <view class="label">
  9. <text>收货人</text>
  10. <text class="star">*</text>
  11. </view>
  12. <input v-model="adsInfo.username" type="text" placeholder="请填写真实姓名" />
  13. </view>
  14. <view class="form_item">
  15. <view class="label">
  16. <text>手机号</text>
  17. <text class="star">*</text>
  18. </view>
  19. <input type="text" placeholder="请填写手机号" v-model="adsInfo.mobile" maxlength="11">
  20. </view>
  21. <view class="form_item">
  22. <view class="label">
  23. <text>省市县区</text>
  24. <text class="star">*</text>
  25. </view>
  26. <view class="picker_box">
  27. <picker mode="region" @change="RegionChange" :value="region">
  28. <view class="picker" :style="{ color: region.length ? '#333' : '#999' }">
  29. <text>{{region.length==0?'请点击选择地址':region[0]+'-'+region[1]+'-'+region[2]}}</text>
  30. <text class="cuIcon-right"></text>
  31. </view>
  32. </picker>
  33. </view>
  34. </view>
  35. <view class="form_item">
  36. <view class="label">
  37. <text>详细地址</text>
  38. <text class="star">*</text>
  39. </view>
  40. <input type="text" placeholder="请填写详细地址" v-model="adsInfo.address">
  41. </view>
  42. </view>
  43. <view class="sub_btn" @click="saveAds">保存地址</view>
  44. </view>
  45. <view class="ads_box" v-else>
  46. <view class="box_item" v-if="adsInfo.username">
  47. <view>
  48. <text class="user_name">{{adsInfo.username}}</text>
  49. <text class="user_phone">{{adsInfo.mobile}}</text>
  50. </view>
  51. <view class="user_ads" v-if="adsInfo.province">
  52. {{adsInfo.province}}{{adsInfo.city}}{{adsInfo.area}}{{adsInfo.address}}
  53. </view>
  54. <view class="opera_box">
  55. <view @click="isAdd=true">编辑</view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import {
  63. showAddress,
  64. UpdateAddAddress
  65. } from '../../api.js' //添加地址接口
  66. export default {
  67. data() {
  68. return {
  69. pageTitle: '地址管理', //导航栏名字
  70. region: [],
  71. adsInfo: {
  72. username: '',
  73. mobile: '',
  74. province: '',
  75. city: '',
  76. area: '',
  77. address: ''
  78. },
  79. isAdd: false,
  80. isPoint: true, //防连点
  81. };
  82. },
  83. onShow() {
  84. //获取地址
  85. this.$ajax.get(showAddress).then(([, {
  86. data: res
  87. }]) => {
  88. this.$hideLoading()
  89. if (res.code === 200) {
  90. if (res.data && res.data != 'null') {
  91. const {
  92. province,
  93. city,
  94. area
  95. } = res.data
  96. this.adsInfo = res.data
  97. console.log(this.adsInfo,'info')
  98. this.region = [province, city, area]
  99. this.isAdd = false
  100. this.pageTitle = '地址管理'
  101. } else {
  102. this.isAdd = true
  103. this.pageTitle = '地址填写'
  104. console.log(this.adsInfo,'infoelse')
  105. }
  106. } else {
  107. this.$refs.toast.hover(res.msg || '获取地址失败')
  108. }
  109. })
  110. },
  111. methods: {
  112. //选择地址
  113. RegionChange(e) {
  114. this.region = e.detail.value
  115. },
  116. //保存地址
  117. saveAds() {
  118. let _this = this;
  119. console.log('提交')
  120. if (!this.isPoint) {
  121. uni.showModal({
  122. content: '请稍后重试~',
  123. showCancel: false
  124. });
  125. return
  126. }
  127. const {
  128. username,
  129. mobile,
  130. address
  131. } = _this.adsInfo
  132. if (!username.match(/^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/)) {
  133. this.$refs.toast.hover('真实姓名不符合要求')
  134. return
  135. }
  136. if (!mobile || mobile.length != 11) {
  137. this.$refs.toast.hover('手机号格式不正确')
  138. return
  139. }
  140. if (this.region.length === 0) {
  141. this.$refs.toast.hover('请选择地址')
  142. return
  143. }
  144. if (!address) {
  145. this.$refs.toast.hover('详细地址不能为空')
  146. return
  147. }
  148. this.isPoint = false;
  149. this.adsInfo.province = this.region[0]
  150. this.adsInfo.city = this.region[1]
  151. this.adsInfo.area = this.region[2]
  152. this.$ajax.post(UpdateAddAddress, this.adsInfo).then(([, {
  153. data: res
  154. }]) => {
  155. if (+res.code === 200) {
  156. this.$refs.toast.hover('添加成功')
  157. this.isAdd = false
  158. this.pageTitle = '地址管理'
  159. } else {
  160. this.$refs.toast.hover(res.msg || '添加失败')
  161. }
  162. }).catch(err => {
  163. this.isPoint = true;
  164. uni.hideLoading();
  165. })
  166. .finally(() => {
  167. uni.hideLoading();
  168. setTimeout(() => {
  169. this.isPoint = true;
  170. }, 2000);
  171. });
  172. },
  173. },
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. .address {
  178. // @include page();
  179. width: 100%;
  180. height: 100%;
  181. .form {
  182. width: 100%;
  183. margin-top: 30rpx;
  184. .form_item {
  185. width: 690rpx;
  186. margin: 0 auto;
  187. .star {
  188. color: #f00;
  189. margin-left: 10rpx;
  190. }
  191. .label {
  192. font-size: 36rpx;
  193. font-weight: bold;
  194. margin-top: 24rpx;
  195. }
  196. input {
  197. width: 100%;
  198. height: 88rpx;
  199. font-size: 34rpx;
  200. border-bottom: 2rpx solid #E9E9E9;
  201. color: #333;
  202. }
  203. .picker_box {
  204. display: flex;
  205. justify-content: space-between;
  206. align-items: center;
  207. border-bottom: 2rpx solid #E9E9E9;
  208. min-height: 88rpx;
  209. picker {
  210. width: 100%;
  211. .cuIcon-right {
  212. font-size: 45rpx;
  213. color: #999;
  214. }
  215. .picker {
  216. width:100%;
  217. display: flex;
  218. justify-content: space-between;
  219. align-items: center;
  220. font-size: 34rpx;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. .sub_btn {
  227. width: 570rpx;
  228. height: 88rpx;
  229. margin: 60rpx auto;
  230. background: linear-gradient(93deg, #F97C55 0%, #F44545 100%);
  231. opacity: 1;
  232. border-radius: 44rpx;
  233. color: #fff;
  234. font-size: 32rpx;
  235. text-align: center;
  236. line-height: 88rpx;
  237. }
  238. .ads_box {
  239. margin-top: 30rpx;
  240. .box_item {
  241. padding: 24rpx;
  242. box-sizing: border-box;
  243. background: #fff;
  244. width: 702rpx;
  245. background: #FFFFFF;
  246. border-radius: 16rpx;
  247. margin: 24rpx auto 0;
  248. .user_name {
  249. font-size: 34rpx;
  250. font-weight: bold;
  251. margin-right: 10rpx;
  252. }
  253. .user_phone {
  254. font-size: 32rpx;
  255. color: #999;
  256. }
  257. .user_ads {
  258. margin: 24rpx 0;
  259. font-size: 32rpx;
  260. color: #333;
  261. }
  262. .opera_box {
  263. width: 100%;
  264. display: flex;
  265. justify-content: flex-end;
  266. align-items: center;
  267. view {
  268. width: 120rpx;
  269. height: 60rpx;
  270. background: #F8F8F8;
  271. border-radius: 30rpx;
  272. opacity: 1;
  273. display: flex;
  274. align-items: center;
  275. justify-content: center;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. </style>