addAddress.vue 6.3 KB

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