index.vue 12 KB

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