index.vue 12 KB

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