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. const city_res = require('@/mock/json/city_res.json')
  49. import {
  50. editAddress,
  51. getAddressDetail
  52. } from '@/api/user.js';
  53. import {
  54. getCity
  55. } from '@/api/api.js';
  56. import {
  57. toLogin
  58. } from '@/libs/login.js';
  59. import {
  60. mapGetters
  61. } from "vuex";
  62. // #ifdef MP
  63. import authorize from '@/components/Authorize';
  64. // #endif
  65. import home from '@/components/home';
  66. export default {
  67. components: {
  68. // #ifdef MP
  69. authorize,
  70. // #endif
  71. home,
  72. },
  73. props: {
  74. aleartStatus: {
  75. type: Boolean,
  76. default: false
  77. }
  78. },
  79. data() {
  80. return {
  81. regionDval: ['浙江省', '杭州市', '滨江区'],
  82. id: 0, //地址id
  83. userAddress: {
  84. is_default: false
  85. }, //地址详情
  86. region: [this.$t(`省`), this.$t(`市`), this.$t(`区`)],
  87. valueRegion: [0, 0, 0],
  88. isAuto: false, //没有授权的不会自动授权
  89. isShowAuth: false, //是否隐藏授权
  90. district: [],
  91. multiArray: [],
  92. multiIndex: [0, 0, 0],
  93. cityId: 0,
  94. defaultRegion: ['广东省', '广州市', '番禺区'],
  95. defaultRegionCode: '110101',
  96. };
  97. },
  98. computed: mapGetters(['isLogin']),
  99. watch: {
  100. isLogin: {
  101. handler: function(newV, oldV) {
  102. if (newV) {
  103. this.getUserAddress();
  104. }
  105. },
  106. deep: true
  107. }
  108. },
  109. created(options) {
  110. if (this.isLogin) {
  111. this.getCityList();
  112. this.getUserAddress();
  113. } else {
  114. toLogin();
  115. }
  116. },
  117. methods: {
  118. // #ifdef APP-PLUS
  119. // 获取选择的地区
  120. handleGetRegion(region) {
  121. this.region = region
  122. },
  123. // #endif
  124. // 获取地址数据
  125. getCityList() {
  126. let that = this;
  127. // getCity().then(res => {
  128. this.district = city_res
  129. that.initialize();
  130. // })
  131. },
  132. // 处理地址数据
  133. initialize() {
  134. let that = this,
  135. province = [],
  136. city = [],
  137. area = [];
  138. if (that.district.length) {
  139. let cityChildren = that.district[0].c || [];
  140. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  141. that.district.forEach(function(item) {
  142. province.push(item.n);
  143. });
  144. cityChildren.forEach(function(item) {
  145. city.push(item.n);
  146. });
  147. areaChildren.forEach(function(item) {
  148. area.push(item.n);
  149. });
  150. this.multiArray = [province, city, area]
  151. }
  152. },
  153. bindRegionChange(e) {
  154. let multiIndex = this.multiIndex,
  155. province = this.district[multiIndex[0]] || {
  156. c: []
  157. },
  158. city = province.c[multiIndex[1]] || {
  159. v: 0
  160. },
  161. multiArray = this.multiArray,
  162. value = e.detail.value;
  163. this.region = [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]]
  164. // this.$set(this.region,0,multiArray[0][value[0]]);
  165. // this.$set(this.region,1,multiArray[1][value[1]]);
  166. // this.$set(this.region,2,multiArray[2][value[2]]);
  167. this.cityId = city.v
  168. this.valueRegion = [0, 0, 0]
  169. this.initialize();
  170. },
  171. bindMultiPickerColumnChange(e) {
  172. let that = this,
  173. column = e.detail.column,
  174. value = e.detail.value,
  175. currentCity = this.district[value] || {
  176. c: []
  177. },
  178. multiArray = that.multiArray,
  179. multiIndex = that.multiIndex;
  180. multiIndex[column] = value;
  181. switch (column) {
  182. case 0:
  183. let areaList = currentCity.c[0] || {
  184. c: []
  185. };
  186. multiArray[1] = currentCity.c.map((item) => {
  187. return item.n;
  188. });
  189. multiArray[2] = areaList.c.map((item) => {
  190. return item.n;
  191. });
  192. break;
  193. case 1:
  194. let cityList = that.district[multiIndex[0]].c[multiIndex[1]].c || [];
  195. multiArray[2] = cityList.map((item) => {
  196. return item.n;
  197. });
  198. break;
  199. case 2:
  200. break;
  201. }
  202. // #ifdef MP || APP-PLUS
  203. this.$set(this.multiArray, 0, multiArray[0]);
  204. this.$set(this.multiArray, 1, multiArray[1]);
  205. this.$set(this.multiArray, 2, multiArray[2]);
  206. // #endif
  207. // #ifdef H5
  208. this.multiArray = multiArray;
  209. // #endif
  210. this.multiIndex = multiIndex
  211. // this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  212. },
  213. // 授权回调
  214. onLoadFun() {
  215. this.getUserAddress();
  216. },
  217. // 授权关闭
  218. authColse(e) {
  219. this.isShowAuth = e
  220. },
  221. toggleTab(str) {
  222. this.$refs[str].show();
  223. },
  224. // bindRegionChange: function(e) {
  225. // this.$set(this, 'region', e.detail.value);
  226. // },
  227. onConfirm(val) {
  228. this.region = val.checkArr[0] + '-' + val.checkArr[1] + '-' + val.checkArr[2];
  229. },
  230. getUserAddress() {
  231. if (!this.id) return false;
  232. let that = this;
  233. getAddressDetail(this.id).then(res => {
  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] == that.$t(`省`)) 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>