userAddress.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <template>
  2. <view class="aleart" v-if="aleartStatus">
  3. <image src="../../../../static/images/poster-close.png" class="close" @click="posterImageClose"></image>
  4. <view class="title">
  5. <!-- <image class="title-img" src="../../static/address-aleart-header.png" mode=""></image> -->
  6. </view>
  7. <view class="aleart-body">
  8. <form @submit="formSubmit" class="form-data">
  9. <view class='addAddress'>
  10. <view class='list'>
  11. <view class='item acea-row row-between-wrapper'>
  12. <input type='text' :placeholder='$t(`请输入姓名`)' name='name' :value="userAddress.name"
  13. placeholder-class='placeholder'></input>
  14. </view>
  15. <view class='item acea-row row-between-wrapper'>
  16. <input type='number' :placeholder='$t(`请输入手机号`)' name="phone" :value='userAddress.phone'
  17. placeholder-class='placeholder' pattern="\d*"></input>
  18. </view>
  19. <view class='item acea-row row-between-wrapper'>
  20. <view class="address">
  21. <picker mode="multiSelector" @change="bindRegionChange"
  22. @columnchange="bindMultiPickerColumnChange" :value="valueRegion"
  23. :range="multiArray">
  24. <view class='acea-row'>
  25. <view class="picker">{{region[0]}},{{region[1]}},{{region[2]}}</view>
  26. </view>
  27. </picker>
  28. </view>
  29. </view>
  30. <view class='item acea-row row-between-wrapper'>
  31. <input type='text' :placeholder='$t(`请填写具体地址`)' name='detail' placeholder-class='placeholder'
  32. :value='userAddress.detail'></input>
  33. </view>
  34. </view>
  35. <button class='keepBnt' form-type="submit">{{$t(`提交`)}}</button>
  36. <!-- #ifdef MP -->
  37. <!-- <view class="wechatAddress" v-if="!id" @click="getWxAddress">导入微信地址</view> -->
  38. <!-- #endif -->
  39. <!-- #ifdef H5 -->
  40. <!-- <view class="wechatAddress" v-if="this.$wechat.isWeixin() && !id" @click="getAddress">导入微信地址</view> -->
  41. <!-- #endif -->
  42. </view>
  43. </form>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import {
  49. editAddress,
  50. getAddressDetail
  51. } from '@/api/user.js';
  52. import {
  53. getCity
  54. } from '@/api/api.js';
  55. import {
  56. toLogin
  57. } from '@/libs/login.js';
  58. import {
  59. mapGetters
  60. } from "vuex";
  61. // #ifdef MP
  62. import authorize from '@/components/Authorize';
  63. // #endif
  64. import home from '@/components/home';
  65. export default {
  66. components: {
  67. // #ifdef MP
  68. authorize,
  69. // #endif
  70. home,
  71. },
  72. props: {
  73. aleartStatus: {
  74. type: Boolean,
  75. default: false
  76. }
  77. },
  78. data() {
  79. return {
  80. regionDval: ['浙江省', '杭州市', '滨江区'],
  81. id: 0, //地址id
  82. userAddress: {
  83. is_default: false
  84. }, //地址详情
  85. region: [this.$t(`省`), this.$t(`市`), this.$t(`区`)],
  86. valueRegion: [0, 0, 0],
  87. isAuto: false, //没有授权的不会自动授权
  88. isShowAuth: false, //是否隐藏授权
  89. district: [],
  90. multiArray: [],
  91. multiIndex: [0, 0, 0],
  92. cityId: 0,
  93. defaultRegion: ['广东省', '广州市', '番禺区'],
  94. defaultRegionCode: '110101',
  95. };
  96. },
  97. computed: mapGetters(['isLogin']),
  98. watch: {
  99. isLogin: {
  100. handler: function(newV, oldV) {
  101. if (newV) {
  102. this.getUserAddress();
  103. }
  104. },
  105. deep: true
  106. }
  107. },
  108. created(options) {
  109. if (this.isLogin) {
  110. this.getCityList();
  111. this.getUserAddress();
  112. } else {
  113. toLogin();
  114. }
  115. },
  116. methods: {
  117. // #ifdef APP-PLUS
  118. // 获取选择的地区
  119. handleGetRegion(region) {
  120. this.region = region
  121. },
  122. // #endif
  123. // 获取地址数据
  124. getCityList() {
  125. let that = this;
  126. getCity().then(res => {
  127. this.district = res.data
  128. that.initialize();
  129. })
  130. },
  131. // 处理地址数据
  132. initialize() {
  133. let that = this,
  134. province = [],
  135. city = [],
  136. area = [];
  137. if (that.district.length) {
  138. let cityChildren = that.district[0].c || [];
  139. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  140. that.district.forEach(function(item) {
  141. province.push(item.n);
  142. });
  143. cityChildren.forEach(function(item) {
  144. city.push(item.n);
  145. });
  146. areaChildren.forEach(function(item) {
  147. area.push(item.n);
  148. });
  149. this.multiArray = [province, city, area]
  150. }
  151. },
  152. bindRegionChange(e) {
  153. let multiIndex = this.multiIndex,
  154. province = this.district[multiIndex[0]] || {
  155. c: []
  156. },
  157. city = province.c[multiIndex[1]] || {
  158. v: 0
  159. },
  160. multiArray = this.multiArray,
  161. value = e.detail.value;
  162. this.region = [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]]
  163. // this.$set(this.region,0,multiArray[0][value[0]]);
  164. // this.$set(this.region,1,multiArray[1][value[1]]);
  165. // this.$set(this.region,2,multiArray[2][value[2]]);
  166. this.cityId = city.v
  167. this.valueRegion = [0, 0, 0]
  168. this.initialize();
  169. },
  170. bindMultiPickerColumnChange(e) {
  171. let that = this,
  172. column = e.detail.column,
  173. value = e.detail.value,
  174. currentCity = this.district[value] || {
  175. c: []
  176. },
  177. multiArray = that.multiArray,
  178. multiIndex = that.multiIndex;
  179. multiIndex[column] = value;
  180. switch (column) {
  181. case 0:
  182. let areaList = currentCity.c[0] || {
  183. c: []
  184. };
  185. multiArray[1] = currentCity.c.map((item) => {
  186. return item.n;
  187. });
  188. multiArray[2] = areaList.c.map((item) => {
  189. return item.n;
  190. });
  191. break;
  192. case 1:
  193. let cityList = that.district[multiIndex[0]].c[multiIndex[1]].c || [];
  194. multiArray[2] = cityList.map((item) => {
  195. return item.n;
  196. });
  197. break;
  198. case 2:
  199. break;
  200. }
  201. // #ifdef MP || APP-PLUS
  202. this.$set(this.multiArray, 0, multiArray[0]);
  203. this.$set(this.multiArray, 1, multiArray[1]);
  204. this.$set(this.multiArray, 2, multiArray[2]);
  205. // #endif
  206. // #ifdef H5
  207. this.multiArray = multiArray;
  208. // #endif
  209. this.multiIndex = multiIndex
  210. // this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  211. },
  212. // 授权回调
  213. onLoadFun() {
  214. this.getUserAddress();
  215. },
  216. // 授权关闭
  217. authColse(e) {
  218. this.isShowAuth = e
  219. },
  220. toggleTab(str) {
  221. this.$refs[str].show();
  222. },
  223. // bindRegionChange: function(e) {
  224. // this.$set(this, 'region', e.detail.value);
  225. // },
  226. onConfirm(val) {
  227. this.region = val.checkArr[0] + '-' + val.checkArr[1] + '-' + val.checkArr[2];
  228. },
  229. getUserAddress() {
  230. if (!this.id) return false;
  231. let that = this;
  232. getAddressDetail(this.id).then(res => {
  233. // let region = [res.data.province, res.data.city, res.data.district];
  234. let region = [res.data.province, res.data.city, res.data.district];
  235. that.$set(that, 'userAddress', res.data);
  236. that.$set(that, 'region', region);
  237. that.cityId = res.data.city_id
  238. });
  239. },
  240. // 导入共享地址(小程序)
  241. getWxAddress() {
  242. let that = this;
  243. uni.authorize({
  244. scope: 'scope.address',
  245. success: function(res) {
  246. uni.chooseAddress({
  247. success: function(res) {
  248. let addressP = {};
  249. addressP.province = res.provinceName;
  250. addressP.city = res.cityName;
  251. addressP.district = res.countyName;
  252. editAddress({
  253. address: addressP,
  254. is_default: 1,
  255. real_name: res.userName,
  256. post_code: res.postalCode,
  257. phone: res.telNumber,
  258. detail: res.detailInfo,
  259. id: 0,
  260. type: 1,
  261. }).then(res => {
  262. setTimeout(function() {
  263. if (that.cartId) {
  264. let cartId = that.cartId;
  265. let pinkId = that.pinkId;
  266. let couponId = that.couponId;
  267. that.cartId = '';
  268. that.pinkId = '';
  269. that.couponId = '';
  270. uni.navigateTo({
  271. url: '/pages/goods/lottery/grids/index?addressId=' +
  272. that.id ? that
  273. .id : res.data
  274. .id
  275. });
  276. } else {
  277. uni.navigateBack({
  278. delta: 1
  279. });
  280. }
  281. }, 1000);
  282. return that.$util.Tips({
  283. title: that.$t(`添加成功`),
  284. icon: 'success'
  285. });
  286. }).catch(err => {
  287. return that.$util.Tips({
  288. title: err
  289. });
  290. });
  291. },
  292. fail: function(res) {
  293. if (res.errMsg == 'chooseAddress:cancel') return that.$util
  294. .Tips({
  295. title: that.$t(`取消`)
  296. });
  297. },
  298. })
  299. },
  300. fail: function(res) {
  301. uni.showModal({
  302. title: that.$t(`您已拒绝导入微信地址权限`),
  303. content: that.$t(`是否进入权限管理,调整授权?`),
  304. success(res) {
  305. if (res.confirm) {
  306. uni.openSetting({
  307. success: function(res) {}
  308. });
  309. } else if (res.cancel) {
  310. return that.$util.Tips({
  311. title: that.$t(`已取消`)
  312. });
  313. }
  314. }
  315. })
  316. },
  317. })
  318. },
  319. // 导入共享地址(微信);
  320. getAddress() {
  321. let that = this;
  322. that.$wechat.openAddress().then(userInfo => {
  323. editAddress({
  324. id: this.id,
  325. real_name: userInfo.userName,
  326. phone: userInfo.telNumber,
  327. address: {
  328. province: userInfo.provinceName,
  329. city: userInfo.cityName,
  330. district: userInfo.countryName
  331. },
  332. detail: userInfo.detailInfo,
  333. is_default: 1,
  334. post_code: userInfo.postalCode,
  335. type: 1,
  336. })
  337. .then(() => {
  338. setTimeout(() => {
  339. uni.navigateTo({
  340. url: '/pages/goods/lottery/grids/index?addressId=' +
  341. that.id ? that.id : res.data.id
  342. })
  343. }, 1000);
  344. // close();
  345. that.$util.Tips({
  346. title: that.$t(`添加成功`),
  347. icon: 'success'
  348. });
  349. })
  350. .catch(err => {
  351. return that.$util.Tips({
  352. title: err || that.$t(`添加失败`)
  353. });
  354. });
  355. }).catch(err => {});
  356. },
  357. /**
  358. * 提交用户添加地址
  359. *
  360. */
  361. formSubmit(e) {
  362. let that = this,
  363. value = e.detail.value;
  364. if (!value.name) return that.$util.Tips({
  365. title: that.$t(`请填写收货人姓名`)
  366. });
  367. if (!value.phone) return that.$util.Tips({
  368. title: that.$t(`请输入手机号`)
  369. });
  370. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  371. title: that.$t(`请输入正确的手机号码`)
  372. });
  373. if (that.region[0] == '省') return that.$util.Tips({
  374. title: that.$t(`请选择所在地区`)
  375. });
  376. if (!value.detail) return that.$util.Tips({
  377. title: that.$t(`请填写详细地址`)
  378. });
  379. let regionArray = that.region;
  380. value.address = {
  381. province: regionArray[0],
  382. city: regionArray[1],
  383. district: regionArray[2],
  384. city_id: that.cityId,
  385. };
  386. this.$emit('getAddress', value)
  387. },
  388. //隐藏弹窗
  389. posterImageClose() {
  390. this.$emit("close", false)
  391. },
  392. }
  393. }
  394. </script>
  395. <style lang="scss" scoped>
  396. .aleart {
  397. width: 500rpx;
  398. // height: 714rpx;
  399. position: fixed;
  400. left: 50%;
  401. transform: translateX(-50%);
  402. z-index: 999;
  403. top: 50%;
  404. margin-top: -357rpx;
  405. border-radius: 12rpx;
  406. .title {
  407. padding: 0;
  408. margin: 0;
  409. height: 110rpx;
  410. .title-img {
  411. width: 100%;
  412. height: 100%;
  413. }
  414. }
  415. .aleart-body {
  416. display: flex;
  417. align-items: center;
  418. justify-content: center;
  419. flex-direction: column;
  420. background-color: #FDF7E7;
  421. .form-data {
  422. width: 100%;
  423. }
  424. .goods-img {
  425. width: 150rpx;
  426. height: 150rpx;
  427. }
  428. .msg {
  429. font-size: 30rpx;
  430. color: #282828;
  431. }
  432. }
  433. .close {
  434. width: 46rpx;
  435. height: 75rpx;
  436. position: fixed;
  437. right: 20rpx;
  438. top: -73rpx;
  439. display: block;
  440. }
  441. }
  442. .addAddress {
  443. width: 100%;
  444. padding: 30rpx 30rpx;
  445. }
  446. .addAddress .list {
  447. background-color: #FDF7E7;
  448. }
  449. .addAddress .list .item {
  450. border: 1rpx solid #A05644;
  451. margin-bottom: 24rpx;
  452. padding: 10rpx;
  453. border-radius: 6rpx;
  454. }
  455. .addAddress .list .item .name {
  456. width: 195rpx;
  457. font-size: 30rpx;
  458. color: #333;
  459. }
  460. .addAddress .list .item .address {
  461. // width: 412rpx;
  462. flex: 1;
  463. margin-left: 20rpx;
  464. }
  465. .addAddress .list .item input {
  466. // width: 475rpx;
  467. font-size: 30rpx;
  468. }
  469. .addAddress .list .item .placeholder {
  470. color: #ccc;
  471. }
  472. .addAddress .list .item picker {
  473. // width: 475rpx;
  474. }
  475. .addAddress .list .item picker .picker {
  476. // width: 410rpx;
  477. font-size: 30rpx;
  478. }
  479. .addAddress .list .item picker .iconfont {
  480. font-size: 43rpx;
  481. }
  482. .addAddress .default {
  483. padding: 0 30rpx;
  484. height: 90rpx;
  485. background-color: #fff;
  486. margin-top: 23rpx;
  487. }
  488. .addAddress .default checkbox {
  489. margin-right: 15rpx;
  490. }
  491. .addAddress .keepBnt {
  492. width: 100%;
  493. height: 86rpx;
  494. border-radius: 6rpx;
  495. text-align: center;
  496. line-height: 86rpx;
  497. font-size: 32rpx;
  498. color: #452015;
  499. font-weight: bold;
  500. background: #EEBE6B;
  501. box-shadow: 0px 5px 9px 0px rgba(220, 166, 72, 0.24);
  502. }
  503. .addAddress .wechatAddress {
  504. height: 86rpx;
  505. border-radius: 6rpx;
  506. text-align: center;
  507. line-height: 86rpx;
  508. margin: 0 auto;
  509. font-size: 32rpx;
  510. color: #fe960f;
  511. border: 1px solid #fe960f;
  512. }
  513. </style>