form.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <template>
  2. <view class="app-container">
  3. <view class="row">
  4. <view class="title">收货人</view>
  5. <view class="right input"><input type="text" placeholder="请输入收货人" v-model="formData.name"></view>
  6. </view>
  7. <view class="row">
  8. <view class="title">手机号码</view>
  9. <view class="right input"><input type="number" placeholder="请输入手机号码" v-model="formData.phone"></view>
  10. </view>
  11. <view class="row">
  12. <view class="title">所在地区</view>
  13. <view class="right">
  14. <picker @change="onRegionChange" v-model="regionValue" mode="region">
  15. <view class="picker">
  16. <view v-if="regionValue.length">{{regionValue[0] + '/' +regionValue[1] + '/' +regionValue[2]}}
  17. </view>
  18. <view v-else style="color: #666666;">请选择省/市/区</view>
  19. <image src="@/static/icon/right.png"></image>
  20. </view>
  21. </picker>
  22. </view>
  23. </view>
  24. <view class="row">
  25. <view class="title">所在街道</view>
  26. <view class="right">
  27. <picker @change="onStreetChange" :value="streetValue" :range="streetList" range-key="name"
  28. :disabled="!regionValue[2]">
  29. <view class="picker">
  30. <view v-if="streetValue !== ''">{{streetList[streetValue].name}}</view>
  31. <view v-else style="color: #666666;">请选择街道</view>
  32. <image src="@/static/icon/right.png"></image>
  33. </view>
  34. </picker>
  35. </view>
  36. </view>
  37. <view class="row">
  38. <view class="title">收货地址</view>
  39. <view class="right textarea">
  40. <textarea placeholder="请输入收货地址" auto-height v-model="formData.address"></textarea>
  41. <view class="r" @tap="getLocation">
  42. <image src="@/static/icon/address2.png"></image>定位
  43. </view>
  44. </view>
  45. </view>
  46. <view class="row">
  47. <view class="title">门牌号</view>
  48. <view class="right input"><input type="text" placeholder="请输入门牌号" maxlength="15" v-model="formData.houseNo">
  49. </view>
  50. </view>
  51. <view class="row">
  52. <view class="title">设为默认地址</view>
  53. <view class="right default">
  54. <switch @change="switchChange" color="#FF3F42" v-model="formData.defaultAddr"
  55. :checked="formData.defaultAddr" style="transform:scale(0.7)" />
  56. </view>
  57. </view>
  58. <view class="button red" @tap="submitForm">保存</view>
  59. <view class="button white" v-if="editId" @tap="isDialog = true">删除</view>
  60. <modal-dialog showText="确定要删除该地址吗?" :isShowDialog="isDialog" @cancel="isDialog = false"
  61. @confirm="confirmDelete"></modal-dialog>
  62. </view>
  63. </template>
  64. <script>
  65. import {
  66. mapState
  67. } from 'vuex';
  68. import {
  69. getArea
  70. } from '@/utils/utils.js';
  71. import modalDialog from '@/components/modalDialog.vue';
  72. export default {
  73. components: {
  74. modalDialog
  75. },
  76. data() {
  77. return {
  78. editId: null, // 编辑的id
  79. isDialog: false, // 是否显示删除弹窗
  80. regionValue: [], // 省市区值
  81. streetList: [],
  82. streetValue: '',
  83. formData: { // 表单数据
  84. name: '',
  85. phone: '',
  86. province: '',
  87. city: '',
  88. area: '',
  89. street: '',
  90. address: '',
  91. houseNo: '',
  92. defaultAddr: false,
  93. },
  94. canClickSave: true, // 能否点击提交
  95. }
  96. },
  97. computed: {
  98. ...mapState(['userInfo', 'isLogin', 'userId'])
  99. },
  100. onLoad({
  101. id,
  102. addressData
  103. }) {
  104. this.editId = id;
  105. if (id) {
  106. uni.setNavigationBarTitle({
  107. title: '编辑收货地址'
  108. })
  109. this.getAddressData();
  110. }
  111. if (addressData) {
  112. addressData = JSON.parse(addressData);
  113. this.formData = {
  114. name: addressData.name,
  115. phone: addressData.phone,
  116. province: addressData.province,
  117. city: addressData.city,
  118. area: addressData.area,
  119. address: addressData.address,
  120. houseNo: addressData.houseNo,
  121. defaultAddr: addressData.defaultAddr,
  122. }
  123. this.regionValue = [addressData.province, addressData.city, addressData.area];
  124. this.getStreetList();
  125. }
  126. },
  127. methods: {
  128. // 获取地址信息
  129. getAddressData() {
  130. this.$axios({
  131. url: '/user/address/detail',
  132. method: 'get',
  133. params: {
  134. userAddressId: this.editId
  135. }
  136. }).then(res => {
  137. this.formData = {
  138. name: res.data.name,
  139. phone: res.data.phone,
  140. province: res.data.province,
  141. city: res.data.city,
  142. area: res.data.area,
  143. address: res.data.address,
  144. houseNo: res.data.houseNo,
  145. defaultAddr: res.data.defaultAddr,
  146. }
  147. this.regionValue = [res.data.province, res.data.city, res.data.area];
  148. this.getStreetList(res.data.street);
  149. })
  150. },
  151. // 地图选点
  152. getLocation() {
  153. let that = this;
  154. uni.chooseLocation({
  155. success: function(res) {
  156. let SSQ_value = getArea(res.address);
  157. if (!SSQ_value.Province) {
  158. SSQ_value.Province = SSQ_value.City;
  159. }
  160. that.formData.province = SSQ_value.Province;
  161. that.formData.city = SSQ_value.City;
  162. that.formData.area = SSQ_value.Country;
  163. let regionValue = [];
  164. regionValue.push(SSQ_value.Province);
  165. regionValue.push(SSQ_value.City);
  166. regionValue.push(SSQ_value.Country);
  167. that.regionValue = regionValue;
  168. that.formData.address = res.address.slice(res.address.indexOf(SSQ_value.Country) +
  169. SSQ_value.Country.length) + res.name;
  170. that.getStreetList();
  171. that.streetValue = '';
  172. that.formData.street = '';
  173. },
  174. fail: function(res) {
  175. uni.getSetting({
  176. success: function(res) {
  177. if (!res.authSetting['scope.userLocation']) {
  178. uni.showModal({
  179. title: '是否授权当前位置',
  180. content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用',
  181. success(tip) {
  182. if (tip.confirm) {
  183. uni.openSetting({
  184. success: function(data) {
  185. if (data.authSetting[
  186. "scope.userLocation"
  187. ] === true) {
  188. that.$successToast(
  189. '授权成功');
  190. setTimeout(() => {
  191. that
  192. .getLocation();
  193. }, 1000)
  194. }
  195. }
  196. })
  197. }
  198. }
  199. })
  200. }
  201. }
  202. })
  203. }
  204. });
  205. },
  206. // 切换省市区
  207. onRegionChange(e) {
  208. console.log(e.detail);
  209. this.regionValue = e.detail.value;
  210. this.formData.province = this.regionValue[0];
  211. this.formData.city = this.regionValue[1];
  212. this.formData.area = this.regionValue[2];
  213. this.getStreetList();
  214. this.streetValue = '';
  215. },
  216. // 获取街道列表
  217. getStreetList(street) {
  218. this.$axios({
  219. url: '/common/street',
  220. method: 'get',
  221. params: {
  222. province: this.regionValue[0],
  223. city: this.regionValue[1],
  224. area: this.regionValue[2],
  225. }
  226. }).then(res => {
  227. this.streetList = res.data;
  228. if (street) {
  229. let index = this.findElem(this.streetList, 'name', street);
  230. this.streetValue = index;
  231. this.formData.street = street;
  232. }
  233. })
  234. },
  235. // 切换街道
  236. onStreetChange(e) {
  237. console.log(e.detail.value);
  238. this.streetValue = e.detail.value;
  239. this.formData.street = this.streetList[this.streetValue].name;
  240. },
  241. findElem(array, attr, val) {
  242. for (var i = 0; i < array.length; i++) {
  243. if (array[i][attr] == val) {
  244. return i; //返回当前索引值
  245. }
  246. }
  247. return -1;
  248. },
  249. switchChange(e) {
  250. console.log(e.detail.value);
  251. this.formData.defaultAddr = e.detail.value;
  252. },
  253. // 验证数据
  254. vailateData() {
  255. if (!this.formData.name) {
  256. this.$toast('请填写姓名');
  257. return false;
  258. }
  259. if (!this.formData.phone) {
  260. this.$toast('请填写手机号码');
  261. return false;
  262. }
  263. if (!(/^1[3456789]\d{9}$/.test(this.formData.phone))) {
  264. this.$toast('请填写正确的手机号码');
  265. return false;
  266. }
  267. if (!this.formData.province) {
  268. this.$toast('请选择所在地区');
  269. return false;
  270. }
  271. if (!this.formData.street) {
  272. this.$toast('请选择所在街道');
  273. return false;
  274. }
  275. if (!this.formData.address) {
  276. this.$toast('请填写收货地址');
  277. return false;
  278. }
  279. return true;
  280. },
  281. // 提交表单
  282. submitForm() {
  283. if (!this.canClickSave) return false;
  284. this.canClickSave = false;
  285. setTimeout(() => {
  286. this.canClickSave = true
  287. }, 3000)
  288. if (!this.vailateData()) return;
  289. let params = this.formData;
  290. params.userId = this.userId;
  291. let url = '';
  292. if (this.editId) {
  293. params.userAddressId = this.editId;
  294. url = '/user/address/update';
  295. } else {
  296. url = '/user/address/save';
  297. }
  298. this.$axios({
  299. url: url,
  300. type: 'application/json',
  301. params,
  302. isLoading: 1,
  303. }).then(res => {
  304. this.$successToast(this.editId ? '编辑成功' : '添加成功');
  305. setTimeout(() => {
  306. uni.navigateBack({
  307. delta: 1
  308. })
  309. }, 1000)
  310. })
  311. },
  312. // 删除
  313. confirmDelete() {
  314. this.$axios({
  315. url: '/user/address/del',
  316. params: {
  317. userAddressId: this.editId
  318. },
  319. isLoading: 1,
  320. }).then(res => {
  321. this.isDialog = false;
  322. this.$successToast('删除成功');
  323. setTimeout(() => {
  324. uni.navigateBack({
  325. delta: 1
  326. })
  327. }, 1000)
  328. })
  329. }
  330. }
  331. }
  332. </script>
  333. <style lang="scss">
  334. .app-container {
  335. background: #F4F2F2;
  336. padding: 0 30rpx;
  337. padding-top: 20rpx;
  338. box-sizing: border-box;
  339. .row {
  340. background: #FFFFFF;
  341. margin-bottom: 20rpx;
  342. min-height: 88rpx;
  343. border-radius: 20rpx;
  344. padding: 0 20rpx;
  345. display: flex;
  346. justify-content: space-between;
  347. align-items: center;
  348. .title {
  349. font-size: 28rpx;
  350. color: #333333;
  351. }
  352. .right {
  353. width: 480rpx;
  354. input {
  355. font-size: 28rpx;
  356. }
  357. .picker {
  358. display: flex;
  359. align-items: center;
  360. justify-content: space-between;
  361. image {
  362. width: 16rpx;
  363. height: 28rpx;
  364. }
  365. }
  366. }
  367. .textarea {
  368. display: flex;
  369. justify-content: space-between;
  370. align-items: center;
  371. textarea {
  372. padding: 10rpx 0;
  373. }
  374. .r {
  375. display: flex;
  376. align-items: center;
  377. flex-shrink: 0;
  378. font-size: 24rpx;
  379. color: #666666;
  380. margin-left: 15rpx;
  381. image {
  382. width: 28rpx;
  383. height: 36rpx;
  384. margin-right: 16rpx;
  385. }
  386. }
  387. }
  388. .default {
  389. display: flex;
  390. justify-content: flex-end;
  391. switch {
  392. margin-right: -20rpx;
  393. }
  394. }
  395. }
  396. .button {
  397. width: 100%;
  398. height: 72rpx;
  399. border-radius: 72rpx;
  400. text-align: center;
  401. line-height: 72rpx;
  402. &.red {
  403. background: #FF3F42;
  404. color: #FFFFFF;
  405. margin-top: 120rpx;
  406. }
  407. &.white {
  408. background: #FFFFFF;
  409. color: #333333;
  410. margin-top: 20rpx;
  411. }
  412. }
  413. }
  414. </style>