addAddress.vue 7.7 KB

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