index.vue 14 KB

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