userAddress.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. that.$set(that, 'userAddress', res.data);
  235. that.$set(that, 'region', region);
  236. that.cityId = res.data.city_id
  237. });
  238. },
  239. // 导入共享地址(小程序)
  240. getWxAddress() {
  241. let that = this;
  242. uni.authorize({
  243. scope: 'scope.address',
  244. success: function(res) {
  245. uni.chooseAddress({
  246. success: function(res) {
  247. let addressP = {};
  248. addressP.province = res.provinceName;
  249. addressP.city = res.cityName;
  250. addressP.district = res.countyName;
  251. editAddress({
  252. address: addressP,
  253. is_default: 1,
  254. real_name: res.userName,
  255. post_code: res.postalCode,
  256. phone: res.telNumber,
  257. detail: res.detailInfo,
  258. id: 0,
  259. type: 1,
  260. }).then(res => {
  261. setTimeout(function() {
  262. if (that.cartId) {
  263. let cartId = that.cartId;
  264. let pinkId = that.pinkId;
  265. let couponId = that.couponId;
  266. that.cartId = '';
  267. that.pinkId = '';
  268. that.couponId = '';
  269. uni.navigateTo({
  270. url: '/pages/goods/lottery/grids/index?addressId=' +
  271. that.id ? that
  272. .id : res.data
  273. .id
  274. });
  275. } else {
  276. uni.navigateBack({
  277. delta: 1
  278. });
  279. }
  280. }, 1000);
  281. return that.$util.Tips({
  282. title: that.$t(`添加成功`),
  283. icon: 'success'
  284. });
  285. }).catch(err => {
  286. return that.$util.Tips({
  287. title: err
  288. });
  289. });
  290. },
  291. fail: function(res) {
  292. if (res.errMsg == 'chooseAddress:cancel') return that.$util
  293. .Tips({
  294. title: that.$t(`取消`)
  295. });
  296. },
  297. })
  298. },
  299. fail: function(res) {
  300. uni.showModal({
  301. title: that.$t(`您已拒绝导入微信地址权限`),
  302. content: that.$t(`是否进入权限管理,调整授权?`),
  303. success(res) {
  304. if (res.confirm) {
  305. uni.openSetting({
  306. success: function(res) {}
  307. });
  308. } else if (res.cancel) {
  309. return that.$util.Tips({
  310. title: that.$t(`已取消`)
  311. });
  312. }
  313. }
  314. })
  315. },
  316. })
  317. },
  318. // 导入共享地址(微信);
  319. getAddress() {
  320. let that = this;
  321. that.$wechat.openAddress().then(userInfo => {
  322. editAddress({
  323. id: this.id,
  324. real_name: userInfo.userName,
  325. phone: userInfo.telNumber,
  326. address: {
  327. province: userInfo.provinceName,
  328. city: userInfo.cityName,
  329. district: userInfo.countryName
  330. },
  331. detail: userInfo.detailInfo,
  332. is_default: 1,
  333. post_code: userInfo.postalCode,
  334. type: 1,
  335. })
  336. .then(() => {
  337. setTimeout(() => {
  338. uni.navigateTo({
  339. url: '/pages/goods/lottery/grids/index?addressId=' +
  340. that.id ? that.id : res.data.id
  341. })
  342. }, 1000);
  343. // close();
  344. that.$util.Tips({
  345. title: that.$t(`添加成功`),
  346. icon: 'success'
  347. });
  348. })
  349. .catch(err => {
  350. return that.$util.Tips({
  351. title: err || that.$t(`添加失败`)
  352. });
  353. });
  354. }).catch(err => {});
  355. },
  356. /**
  357. * 提交用户添加地址
  358. *
  359. */
  360. formSubmit(e) {
  361. let that = this,
  362. value = e.detail.value;
  363. if (!value.name) return that.$util.Tips({
  364. title: that.$t(`请填写收货人姓名`)
  365. });
  366. if (!value.phone) return that.$util.Tips({
  367. title: that.$t(`请输入手机号`)
  368. });
  369. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  370. title: that.$t(`请输入正确的手机号码`)
  371. });
  372. if (that.region[0] == '省') return that.$util.Tips({
  373. title: that.$t(`请选择所在地区`)
  374. });
  375. if (!value.detail) return that.$util.Tips({
  376. title: that.$t(`请填写详细地址`)
  377. });
  378. let regionArray = that.region;
  379. value.address = {
  380. province: regionArray[0],
  381. city: regionArray[1],
  382. district: regionArray[2],
  383. city_id: that.cityId,
  384. };
  385. this.$emit('getAddress', value)
  386. },
  387. //隐藏弹窗
  388. posterImageClose() {
  389. this.$emit("close", false)
  390. },
  391. }
  392. }
  393. </script>
  394. <style lang="scss" scoped>
  395. .aleart {
  396. width: 500rpx;
  397. // height: 714rpx;
  398. position: fixed;
  399. left: 50%;
  400. transform: translateX(-50%);
  401. z-index: 999;
  402. top: 50%;
  403. margin-top: -357rpx;
  404. border-radius: 12rpx;
  405. .title {
  406. padding: 0;
  407. margin: 0;
  408. height: 110rpx;
  409. .title-img {
  410. width: 100%;
  411. height: 100%;
  412. }
  413. }
  414. .aleart-body {
  415. display: flex;
  416. align-items: center;
  417. justify-content: center;
  418. flex-direction: column;
  419. background-color: #FDF7E7;
  420. .form-data {
  421. width: 100%;
  422. }
  423. .goods-img {
  424. width: 150rpx;
  425. height: 150rpx;
  426. }
  427. .msg {
  428. font-size: 30rpx;
  429. color: #282828;
  430. }
  431. }
  432. .close {
  433. width: 46rpx;
  434. height: 75rpx;
  435. position: fixed;
  436. right: 20rpx;
  437. top: -73rpx;
  438. display: block;
  439. }
  440. }
  441. .addAddress {
  442. width: 100%;
  443. padding: 30rpx 30rpx;
  444. }
  445. .addAddress .list {
  446. background-color: #FDF7E7;
  447. }
  448. .addAddress .list .item {
  449. border: 1rpx solid #A05644;
  450. margin-bottom: 24rpx;
  451. padding: 10rpx;
  452. border-radius: 6rpx;
  453. }
  454. .addAddress .list .item .name {
  455. width: 195rpx;
  456. font-size: 30rpx;
  457. color: #333;
  458. }
  459. .addAddress .list .item .address {
  460. // width: 412rpx;
  461. flex: 1;
  462. margin-left: 20rpx;
  463. }
  464. .addAddress .list .item input {
  465. // width: 475rpx;
  466. font-size: 30rpx;
  467. }
  468. .addAddress .list .item .placeholder {
  469. color: #ccc;
  470. }
  471. .addAddress .list .item picker {
  472. // width: 475rpx;
  473. }
  474. .addAddress .list .item picker .picker {
  475. // width: 410rpx;
  476. font-size: 30rpx;
  477. }
  478. .addAddress .list .item picker .iconfont {
  479. font-size: 43rpx;
  480. }
  481. .addAddress .default {
  482. padding: 0 30rpx;
  483. height: 90rpx;
  484. background-color: #fff;
  485. margin-top: 23rpx;
  486. }
  487. .addAddress .default checkbox {
  488. margin-right: 15rpx;
  489. }
  490. .addAddress .keepBnt {
  491. width: 100%;
  492. height: 86rpx;
  493. border-radius: 6rpx;
  494. text-align: center;
  495. line-height: 86rpx;
  496. font-size: 32rpx;
  497. color: #452015;
  498. font-weight: bold;
  499. background: #EEBE6B;
  500. box-shadow: 0px 5px 9px 0px rgba(220, 166, 72, 0.24);
  501. }
  502. .addAddress .wechatAddress {
  503. height: 86rpx;
  504. border-radius: 6rpx;
  505. text-align: center;
  506. line-height: 86rpx;
  507. margin: 0 auto;
  508. font-size: 32rpx;
  509. color: #fe960f;
  510. border: 1px solid #fe960f;
  511. }
  512. </style>