index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <view>
  3. <!-- #ifdef APP-->
  4. <view class="status"></view>
  5. <!-- #endif -->
  6. <form @submit="formSubmit" report-submit="true">
  7. <view class="addAddress pad30">
  8. <view class="list borRadius14">
  9. <view class="item acea-row row-between-wrapper" style="border: none">
  10. <view class="name">姓名</view>
  11. <input
  12. type="text"
  13. placeholder="请输入姓名"
  14. placeholder-style="color:#ccc;"
  15. name="realName"
  16. :value="userAddress.realName"
  17. placeholder-class="placeholder"
  18. />
  19. </view>
  20. <view class="item acea-row row-between-wrapper">
  21. <view class="name">联系电话</view>
  22. <input
  23. type="number"
  24. placeholder="请输入联系电话"
  25. placeholder-style="color:#ccc;"
  26. name="phone"
  27. :value="userAddress.phone"
  28. placeholder-class="placeholder"
  29. maxlength="11"
  30. />
  31. </view>
  32. <view class="item acea-row row-between-wrapper relative">
  33. <view class="name">所在地区</view>
  34. <view class="address">
  35. <picker
  36. mode="multiSelector"
  37. @change="bindRegionChange"
  38. @columnchange="bindMultiPickerColumnChange"
  39. :value="valueRegion"
  40. :range="multiArray"
  41. >
  42. <view class="acea-row">
  43. <view class="picker line1"
  44. >{{ region[0] }},{{ region[1] }},{{ region[2] }}</view
  45. >
  46. <view class="iconfont icon-xiangyou abs_right"></view>
  47. </view>
  48. </picker>
  49. </view>
  50. </view>
  51. <view class="item acea-row row-between-wrapper relative">
  52. <view class="name">详细地址</view>
  53. <input
  54. type="text"
  55. placeholder="请填写具体地址"
  56. placeholder-style="color:#ccc;"
  57. name="detail"
  58. placeholder-class="placeholder"
  59. v-model="userAddress.detail"
  60. maxlength="100"
  61. />
  62. <view
  63. class="iconfont icon-dizhi font-color abs_right"
  64. @tap="chooseLocation"
  65. ></view>
  66. </view>
  67. </view>
  68. <view class="default acea-row row-middle borRadius14">
  69. <up-checkbox-group @change="ChangeIsDefault" shape="circle" class="check-box">
  70. <up-checkbox :checked="userAddress.isDefault" activeColor="#F8C008" />设置为默认地址
  71. </up-checkbox-group>
  72. </view>
  73. <button class="keepBnt bg-color" form-type="submit">立即保存</button>
  74. <!-- #ifdef MP -->
  75. <view class="wechatAddress" v-if="!id" @click="getWxAddress"
  76. >导入微信地址</view
  77. >
  78. <!-- #endif -->
  79. <!-- #ifdef H5 -->
  80. <view
  81. class="wechatAddress"
  82. v-if="wechat.isWeixin() && !id"
  83. @click="getAddress"
  84. >导入微信地址</view
  85. >
  86. <!-- #endif -->
  87. </view>
  88. </form>
  89. <!-- #ifdef MP -->
  90. <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
  91. <!-- #endif -->
  92. <!-- <home></home> -->
  93. </view>
  94. </template>
  95. <script setup>
  96. import { onLoad } from "@dcloudio/uni-app";
  97. import { ref, reactive, watch } from "vue";
  98. import { useAppStore } from "@/stores/app.js";
  99. import { editAddress, getAddressDetail } from "@/api/user.js";
  100. import { getCity } from "@/api/api.js";
  101. import { toLogin } from "@/libs/login.js";
  102. import { useToast } from "@/hooks/useToast.js";
  103. import wechat from "@/libs/wechat.js";
  104. import Cache from "@/utils/cache";
  105. // #ifdef MP
  106. // import authorize from '@/components/Authorize';
  107. // #endif
  108. // import home from "@/components/home";
  109. const { Toast } = useToast();
  110. const appStore = useAppStore();
  111. const regionDval = ["浙江省", "杭州市", "滨江区"];
  112. const cartId = ref("");
  113. const pinkId = ref(0);
  114. const couponId = ref(0);
  115. const id = ref(0);
  116. const userAddress = reactive({
  117. isDefault: false,
  118. realName: "",
  119. phone: "",
  120. detail: "",
  121. });
  122. const region = ref(["省", "市", "区"]);
  123. const valueRegion = ref([0, 0, 0]);
  124. const isAuto = ref(false);
  125. const isShowAuth = ref(false);
  126. const district = ref([]);
  127. const multiArray = ref([[], [], []]);
  128. const multiIndex = ref([0, 0, 0]);
  129. const cityId = ref(0);
  130. const defaultRegion = ["广东省", "广州市", "番禺区"];
  131. const defaultRegionCode = "440113";
  132. const bargain = ref(false);
  133. const combination = ref(false);
  134. const secKill = ref(false);
  135. const preOrderNo = ref(0);
  136. watch(
  137. () => appStore.isLogin,
  138. (newV) => {
  139. if (newV) {
  140. getUserAddress();
  141. getCityList();
  142. }
  143. }
  144. );
  145. onLoad((options) => {
  146. if (appStore.isLogin) {
  147. preOrderNo.value = options.preOrderNo || 0;
  148. id.value = options.id || 0;
  149. uni.setNavigationBarTitle({
  150. title: options.id ? "修改地址" : "添加地址",
  151. });
  152. getUserAddress();
  153. if (Cache.has("cityList")) {
  154. district.value = Cache.getItem("cityList");
  155. initialize();
  156. } else {
  157. getCityList();
  158. }
  159. } else {
  160. toLogin();
  161. }
  162. });
  163. function getCityList() {
  164. getCity().then((res) => {
  165. district.value = res.data;
  166. let oneDay = 24 * 3600 * 1000;
  167. Cache.setItem({ name: "cityList", value: res.data, expires: oneDay * 7 }); //设置七天过期时间
  168. initialize();
  169. });
  170. }
  171. function initialize() {
  172. if (district.value.length) {
  173. let province = [],
  174. city = [],
  175. area = [];
  176. let cityChildren = district.value[0].child || [];
  177. let areaChildren = cityChildren.length ? cityChildren[0].child || [] : [];
  178. district.value.forEach((item) => province.push(item.name));
  179. cityChildren.forEach((item) => city.push(item.name));
  180. areaChildren.forEach((item) => area.push(item.name));
  181. multiArray.value = [province, city, area];
  182. }
  183. }
  184. function bindRegionChange(e) {
  185. const mi = multiIndex.value;
  186. const province = district.value[mi[0]] || { child: [] };
  187. const city = province.child[mi[1]] || { cityId: 0 };
  188. const ma = multiArray.value;
  189. const value = e.detail.value;
  190. region.value = [ma[0][value[0]], ma[1][value[1]], ma[2][value[2]]];
  191. cityId.value = city.cityId;
  192. valueRegion.value = [0, 0, 0];
  193. initialize();
  194. }
  195. function bindMultiPickerColumnChange(e) {
  196. const column = e.detail.column;
  197. const value = e.detail.value;
  198. const ma = multiArray.value;
  199. const mi = multiIndex.value;
  200. mi[column] = value;
  201. switch (column) {
  202. case 0:
  203. const currentCity = district.value[value] || { child: [] };
  204. const areaList = currentCity.child[0] || { child: [] };
  205. ma[1] = currentCity.child.map((item) => item.name);
  206. ma[2] = areaList.child.map((item) => item.name);
  207. break;
  208. case 1:
  209. const cityList = district.value[mi[0]].child[mi[1]].child || [];
  210. ma[2] = cityList.map((item) => item.name);
  211. break;
  212. case 2:
  213. break;
  214. }
  215. multiArray.value = [...ma];
  216. multiIndex.value = [...mi];
  217. }
  218. function getUserAddress() {
  219. if (!id.value) return;
  220. getAddressDetail(id.value).then((res) => {
  221. const regionArr = [res.data.province, res.data.city, res.data.district];
  222. Object.assign(userAddress, res.data);
  223. region.value = regionArr;
  224. cityId.value = res.data.cityId;
  225. });
  226. }
  227. function chooseLocation() {
  228. uni.chooseLocation({
  229. success: (res) => {
  230. userAddress.detail = res.address.replace(
  231. /.+?(省|市|自治区|自治州|县|区)/g,
  232. ""
  233. );
  234. },
  235. });
  236. }
  237. // 导入微信地址(小程序)
  238. function getWxAddress() {
  239. uni.authorize({
  240. scope: "scope.address",
  241. success: function () {
  242. uni.chooseAddress({
  243. success: function (res) {
  244. let addressP = {
  245. province: res.provinceName,
  246. city: res.cityName,
  247. district: res.countyName,
  248. cityId: 0,
  249. };
  250. editAddress({
  251. address: addressP,
  252. isDefault: 1,
  253. realName: res.userName,
  254. postCode: res.postalCode,
  255. phone: res.telNumber,
  256. detail: res.detailInfo,
  257. id: 0,
  258. })
  259. .then((res2) => {
  260. setTimeout(() => {
  261. if (cartId.value) {
  262. let url = `/pages/users/order_confirm/index?cartId=${
  263. cartId.value
  264. }&addressId=${id.value ? id.value : res2.data.id}&pinkId=${
  265. pinkId.value
  266. }&couponId=${couponId.value}&secKill=${
  267. secKill.value
  268. }&combination=${combination.value}&bargain=${bargain.value}`;
  269. cartId.value = "";
  270. pinkId.value = "";
  271. couponId.value = "";
  272. uni.navigateTo({ url });
  273. } else {
  274. uni.navigateBack({ delta: 1 });
  275. }
  276. }, 1000);
  277. Toast({ title: "添加成功", icon: "success" });
  278. })
  279. .catch((err) => {
  280. Toast({ title: err });
  281. });
  282. },
  283. fail: function (res) {
  284. if (res.errMsg == "chooseAddress:cancel")
  285. return Toast({ title: "取消选择" });
  286. },
  287. });
  288. },
  289. fail: function () {
  290. uni.showModal({
  291. title: "您已拒绝导入微信地址权限",
  292. content: "是否进入权限管理,调整授权?",
  293. success(res) {
  294. if (res.confirm) {
  295. uni.openSetting({});
  296. } else if (res.cancel) {
  297. Toast({ title: "已取消!" });
  298. }
  299. },
  300. });
  301. },
  302. });
  303. }
  304. // 导入共享地址(微信);
  305. function getAddress() {
  306. wechat
  307. .openAddress()
  308. .then((userInfo) => {
  309. editAddress({
  310. id: id.value,
  311. realName: userInfo.userName,
  312. phone: userInfo.telNumber,
  313. address: {
  314. province: userInfo.provinceName,
  315. city: userInfo.cityName,
  316. district: userInfo.countryName,
  317. cityId: 0,
  318. },
  319. detail: userInfo.detailInfo,
  320. isDefault: 1,
  321. postCode: userInfo.postalCode,
  322. })
  323. .then((res2) => {
  324. setTimeout(() => {
  325. if (cartId.value) {
  326. let url = `/pages/users/order_confirm/index?cartId=${
  327. cartId.value
  328. }&addressId=${id.value ? id.value : res2.data.id}&pinkId=${
  329. pinkId.value
  330. }&couponId=${couponId.value}&secKill=${
  331. secKill.value
  332. }&combination=${combination.value}&bargain=${bargain.value}`;
  333. cartId.value = "";
  334. pinkId.value = "";
  335. couponId.value = "";
  336. uni.navigateTo({ url });
  337. } else {
  338. uni.navigateTo({ url: "/pages/users/user_address_list/index" });
  339. }
  340. }, 1000);
  341. Toast({ title: "添加成功", icon: "success" });
  342. })
  343. .catch((err) => {
  344. Toast({ title: err || "添加失败" });
  345. });
  346. })
  347. .catch((err) => {
  348. console.log(err);
  349. });
  350. }
  351. function formSubmit(e) {
  352. const value = e.detail.value;
  353. if (!value.realName) return Toast({ title: "请填写收货人姓名" });
  354. if (!value.phone) return Toast({ title: "请填写联系电话" });
  355. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone))
  356. return Toast({ title: "请输入正确的手机号码" });
  357. if (region.value == "省-市-区") return Toast({ title: "请选择所在地区" });
  358. if (!value.detail) return Toast({ title: "请填写详细地址" });
  359. value.id = id.value;
  360. let regionArray = region.value;
  361. value.address = {
  362. province: regionArray[0],
  363. city: regionArray[1],
  364. district: regionArray[2],
  365. cityId: cityId.value,
  366. };
  367. value.isDefault = userAddress.isDefault;
  368. uni.showLoading({
  369. title: "保存中",
  370. mask: true,
  371. });
  372. editAddress(value)
  373. .then((res2) => {
  374. Toast({
  375. title: id.value ? "修改成功" : "添加成功",
  376. icon: "success",
  377. });
  378. setTimeout(() => {
  379. if (preOrderNo.value > 0) {
  380. uni.redirectTo({
  381. url: `/pages/users/order_confirm/index?preOrderNo=${
  382. preOrderNo.value
  383. }&addressId=${id.value ? id.value : res2.data.id}`,
  384. });
  385. } else {
  386. // #ifdef H5
  387. history.back();
  388. // #endif
  389. // #ifndef H5
  390. uni.navigateBack({ delta: 1 });
  391. // #endif
  392. }
  393. }, 1000);
  394. })
  395. .catch((err) => {
  396. Toast({ title: err });
  397. });
  398. }
  399. function ChangeIsDefault() {
  400. userAddress.isDefault = !userAddress.isDefault;
  401. }
  402. </script>
  403. <style scoped lang="scss">
  404. .addAddress {
  405. padding-top: 20rpx;
  406. }
  407. .addAddress .list {
  408. background-color: #fff;
  409. padding: 0 24rpx;
  410. }
  411. .addAddress .list .item {
  412. border-top: 1rpx solid #eee;
  413. height: 90rpx;
  414. line-height: 90rpx;
  415. }
  416. .addAddress .list .item .name {
  417. // width: 195rpx;
  418. font-size: 30rpx;
  419. color: #333;
  420. }
  421. .addAddress .list .item .address {
  422. flex: 1;
  423. margin-left: 50rpx;
  424. }
  425. .addAddress .list .item input {
  426. width: 475rpx;
  427. font-size: 30rpx;
  428. font-weight: 400;
  429. }
  430. .addAddress .list .item .placeholder {
  431. color: #ccc;
  432. }
  433. .addAddress .list .item picker .picker {
  434. width: 410rpx;
  435. font-size: 30rpx;
  436. }
  437. .addAddress .default {
  438. padding: 0 30rpx;
  439. height: 90rpx;
  440. background-color: #fff;
  441. margin-top: 23rpx;
  442. }
  443. .addAddress .default checkbox {
  444. margin-right: 15rpx;
  445. }
  446. .addAddress .keepBnt {
  447. width: 690rpx;
  448. height: 86rpx;
  449. border-radius: 50rpx;
  450. text-align: center;
  451. line-height: 86rpx;
  452. margin: 80rpx auto 24rpx auto;
  453. font-size: 32rpx;
  454. color: #fff;
  455. }
  456. .addAddress .wechatAddress {
  457. width: 690rpx;
  458. height: 86rpx;
  459. border-radius: 50rpx;
  460. text-align: center;
  461. line-height: 86rpx;
  462. margin: 0 auto;
  463. font-size: 32rpx;
  464. color: #F8C008;
  465. border: 1px solid #F8C008;
  466. }
  467. .relative {
  468. position: relative;
  469. }
  470. .icon-dizhi {
  471. font-size: 44rpx;
  472. z-index: 100;
  473. }
  474. .abs_right {
  475. position: absolute;
  476. right: 0;
  477. }
  478. .status {
  479. display: flex;
  480. width: 750rpx;
  481. // background-color: #E93323;
  482. height: var(--status-bar-height);
  483. }
  484. :deep(.u-checkbox-group){
  485. align-items: center;
  486. }
  487. </style>