index.vue 12 KB

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