index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <template>
  2. <view>
  3. <view class="addr_list">
  4. <view class="addr_item" v-for="(item, index) in addrList" :key="index">
  5. <view class="contact_user" style="display: flex; justify-content: space-between; width: 100%">
  6. <view style="display: flex">
  7. <text class="contact_name">{{ item.contact_name }}</text>
  8. <text class="contact_phone">{{ item.contact_phone }}</text>
  9. </view>
  10. <view class="contact_shop text-ellipsis" style="width: 250rpx; text-align: end">
  11. <text v-if="item.shop_type">({{ $CONSTANTS.SHOP_TYPES[item.shop_type] }})&nbsp;</text>
  12. {{ item.contact_shop }}
  13. </view>
  14. </view>
  15. <view class="contact_addr">{{ item.contact_province }} {{ item.contact_city }} {{ item.contact_area }} {{ item.contact_addr }}</view>
  16. <view class="contact_handler">
  17. <view class="radio_label" @click="setDefault(index)">
  18. <image class="radio_icon" :src="item.is_default ? '../../static/icon/radioed.png' : '../../static/icon/radio.png'"></image>
  19. <text :class="item.is_default ? 'radioed_text' : ''">{{ item.is_default ? "已默认" : "设为默认" }}</text>
  20. </view>
  21. <view class="addr_right_btn" @click="removeAddr(index)">删除</view>
  22. <view class="addr_right_btn" @click="openForm(item)">编辑</view>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="create_box">
  27. <button class="create_addr" @click="openForm()">新建收货地址</button>
  28. </view>
  29. <Empty v-if="!addrList.length" text="----- 还没有地址啦 -----" />
  30. <view class="to_bottom" v-if="isLast && addrList.length">-----到底啦-----</view>
  31. <uni-popup ref="addrForm" type="bottom" class="popup" background-color="#FFFFFF" @change="popupChange">
  32. <view class="contact_addr_form">
  33. <!-- 地区代表表单 -->
  34. <form class="info_form">
  35. <view class="form_group">
  36. <view class="group_title">收货人:</view>
  37. <view class="group_box">
  38. <input type="text" class="form_ctrl" placeholder="请输入收件人" maxlength="20" required="" v-model="addrRequest.contact_name" />
  39. </view>
  40. </view>
  41. <view class="form_group">
  42. <view class="group_title">手机号:</view>
  43. <view class="group_box">
  44. <input type="text" class="form_ctrl" placeholder="请输入手机号" maxlength="11" required="" v-model="addrRequest.contact_phone" />
  45. </view>
  46. </view>
  47. <view class="form_group">
  48. <view class="group_title">店铺名称:</view>
  49. <view class="group_box">
  50. <uni-combox v-model="addrRequest.contact_shop" :candidates="searchResults" @input="onSearchInput" @change="onSearchChange" placeholder="请输入营业执照全称"> </uni-combox>
  51. </view>
  52. </view>
  53. <view class="form_group">
  54. <view class="group_title">终端类型:</view>
  55. <view class="group_box">
  56. <view class="terminalType_radio">
  57. <view v-for="item in terminalType" :key="item.key">
  58. <view style="display: flex; align-items: center" @click="handleRadioChange(item.key)">
  59. <image class="radio_icon" :src="item.key === addrRequest.shop_type ? '../../static/icon/radioed.png' : '../../static/icon/radio.png'"></image>
  60. &nbsp;<text style="font-size: 26rpx">{{ item.label }}</text>
  61. </view>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. <view class="form_group">
  67. <view class="group_title">所在地区:</view>
  68. <view class="group_box">
  69. <picker mode="multiSelector" class="form_ctrl" :value="cityValue" :range="cityArray" @columnchange="cityChange" @change="cityFinish">
  70. <view class="area_text">
  71. <text v-if="addrRequest.contact_province">{{ addrRequest.contact_province }}</text>
  72. <text v-if="addrRequest.contact_city">/{{ addrRequest.contact_city }}</text>
  73. <text v-if="addrRequest.contact_area">/{{ addrRequest.contact_area }}</text>
  74. </view>
  75. </picker>
  76. </view>
  77. </view>
  78. <view class="form_group">
  79. <view class="group_title">详细地址:</view>
  80. <view class="group_box">
  81. <input type="text" class="form_ctrl" placeholder="请输入详细地址:街道/小区/门牌号" maxlength="64" required="" v-model="addrRequest.contact_addr" />
  82. </view>
  83. </view>
  84. <view class="form_group">
  85. <view class="group_title">设为默认</view>
  86. <view class="group_box">
  87. <switch color="#E03519" style="transform: scale(0.6); float: right" @change="defaultSwitch" :checked="addrRequest.is_default ? true : false" />
  88. </view>
  89. </view>
  90. <button class="submit_btn" @click="saveAddr">保存地址</button>
  91. </form>
  92. </view>
  93. </uni-popup>
  94. </view>
  95. </template>
  96. <script>
  97. import { getProvinces, getMyCity, getAreas, getProvincesIndex, getCityIndex, getAreaIndex } from "../../utils/city";
  98. import Empty from "@/components/Empty/Empty.vue";
  99. // 防抖函数
  100. function debounce(func, wait) {
  101. let timeout;
  102. return function (...args) {
  103. clearTimeout(timeout);
  104. timeout = setTimeout(() => func.apply(this, args), wait);
  105. };
  106. }
  107. export default {
  108. components: { Empty },
  109. data() {
  110. return {
  111. // 产品列表
  112. addrList: [],
  113. // 请求参数
  114. requestParam: {
  115. page: 1,
  116. status: 0,
  117. },
  118. // 保存地址
  119. addrRequest: {
  120. id: 0,
  121. contact_name: "",
  122. contact_phone: "",
  123. contact_province: "",
  124. contact_city: "",
  125. contact_area: "",
  126. contact_addr: "",
  127. contact_shop: "",
  128. is_default: 0,
  129. shop_type: 1,
  130. },
  131. // 是否最后一页
  132. isLast: false,
  133. // 是否请求中
  134. isReqing: false,
  135. // 所在地区
  136. cityArray: [[], [], []],
  137. cityValue: [0, 0, 0],
  138. // 自动弹出
  139. autoShowForm: false,
  140. // 异步通知
  141. AddrEmit: false,
  142. //终端类型
  143. terminalType: [
  144. { key: 1, label: "单店" },
  145. { key: 2, label: "连锁" },
  146. { key: 3, label: "诊所、社康等" },
  147. ],
  148. //获取的默认地址
  149. defaultAddr: {},
  150. //远程搜索返回的数据
  151. searchResults: [],
  152. };
  153. },
  154. onLoad(param) {
  155. this.AddrEmit = param.notify == "addr" ? true : false;
  156. this.autoShowForm = param.type == "create" ? true : false;
  157. // 获取列表
  158. this.getList();
  159. this.get_remark_addr();
  160. },
  161. onReady() {
  162. if (this.autoShowForm) {
  163. var that = this;
  164. // 500毫秒后自动弹出
  165. setTimeout(function () {
  166. that.openForm();
  167. }, 500);
  168. }
  169. },
  170. onShow() {
  171. // 替换对应的数据
  172. this.cityArray.splice(0, 1, getProvinces());
  173. this.cityArray.splice(1, 1, getMyCity(this.cityValue[0]));
  174. this.cityArray.splice(2, 1, getAreas(this.cityValue[0], this.cityValue[1]));
  175. // 默认地址
  176. this.addrRequest.contact_province = this.cityArray[0][this.cityValue[0]];
  177. this.addrRequest.contact_city = this.cityArray[1][this.cityValue[1]];
  178. this.addrRequest.contact_area = this.cityArray[2][this.cityValue[2]];
  179. },
  180. methods: {
  181. onSearchInput: debounce(function (inputVal, isSelect) {
  182. console.log("搜索关键字:", this.addrRequest.contact_shop);
  183. console.log("是否选择:", isSelect);
  184. if (isSelect) return;
  185. if (this.addrRequest.contact_shop.trim() === "" || this.addrRequest.contact_shop.length < 2) {
  186. this.searchResults = [];
  187. return;
  188. }
  189. this.$http.request("api/custom_addr/get_guess_addr", { contact_shop: this.addrRequest.contact_shop }).then((callback) => {
  190. // 获取成功
  191. if (callback.code == "success") {
  192. this.searchResults = callback.data.map((item, index) => ({
  193. text: item.contact_shop,
  194. value: index,
  195. ...item,
  196. }));
  197. }
  198. });
  199. }, 300),
  200. onSearchChange(event) {
  201. if (event.shop_type !== 0) this.addrRequest.shop_type = event.shop_type;
  202. this.addrRequest.contact_addr = event.contact_addr;
  203. const { contact_province, contact_city, contact_area } = event;
  204. const provinces = getProvincesIndex(contact_province);
  205. const city = getCityIndex(contact_city, provinces.city, provinces.index);
  206. const area = getAreaIndex(contact_area, city.area);
  207. this.cityValue = [provinces.index, city.index, area.index];
  208. this.cityArray.splice(1, 1, provinces.city);
  209. this.cityArray.splice(2, 1, city.area);
  210. this.addrRequest.contact_province = contact_province;
  211. this.addrRequest.contact_area = contact_area;
  212. this.addrRequest.contact_city = contact_city;
  213. },
  214. //获取get_remark_addr
  215. get_remark_addr() {
  216. this.$http.request("api/custom_addr/get_remark_addr").then((callback) => {
  217. // 获取成功
  218. if (callback.code == "success") {
  219. // 通知地址变更
  220. this.defaultAddr = callback.data;
  221. }
  222. });
  223. },
  224. // 获取列表
  225. getList() {
  226. // 登录提示
  227. if (!this.$checkAccess.alterLogin()) return;
  228. // 判断数据
  229. this.$http.request("api/custom_addr/get_list").then((callback) => {
  230. // 获取成功
  231. if (callback.code == "success") {
  232. this.addrList = callback.data;
  233. // 通知地址变更
  234. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  235. }
  236. });
  237. },
  238. // 打开弹窗
  239. openForm(item) {
  240. if (!item) {
  241. this.addrRequest.id = 0;
  242. this.addrRequest.contact_name = this.defaultAddr.contact_name;
  243. this.addrRequest.contact_phone = this.defaultAddr.contact_phone;
  244. this.addrRequest.contact_shop = this.defaultAddr.contact_shop;
  245. this.addrRequest.contact_addr = this.defaultAddr.contact_addr;
  246. this.addrRequest.is_default = 0;
  247. this.addrRequest.shop_type = this.defaultAddr.shop_type == "" ? 1 : this.defaultAddr.shop_type;
  248. const { contact_province, contact_city, contact_area } = this.defaultAddr;
  249. if (contact_province == "") {
  250. this.cityValue = [0, 0, 0];
  251. this.addrRequest.contact_province = "请选择";
  252. this.addrRequest.contact_city = "请选择";
  253. this.addrRequest.contact_area = "请选择";
  254. } else {
  255. const provinces = getProvincesIndex(contact_province);
  256. const city = getCityIndex(contact_city, provinces.city, provinces.index);
  257. const area = getAreaIndex(contact_area, city.area);
  258. this.cityValue = [provinces.index, city.index, area.index];
  259. this.cityArray.splice(1, 1, provinces.city);
  260. this.cityArray.splice(2, 1, city.area);
  261. this.addrRequest.contact_province = contact_province;
  262. this.addrRequest.contact_area = contact_area;
  263. this.addrRequest.contact_city = contact_city;
  264. if (contact_area == "") this.addrRequest.contact_area = "请选择";
  265. if (contact_city == "") this.addrRequest.contact_city = "请选择";
  266. }
  267. } else {
  268. if (!item.shop_type) item.shop_type = 1;
  269. const _cloneItem = JSON.parse(JSON.stringify(item));
  270. this.addrRequest = _cloneItem;
  271. const provinces = getProvincesIndex(item.contact_province);
  272. const city = getCityIndex(item.contact_city, provinces.city, provinces.index);
  273. const area = getAreaIndex(item.contact_area, city.area);
  274. this.cityValue = [provinces.index, city.index, area.index];
  275. this.cityArray.splice(1, 1, provinces.city);
  276. this.cityArray.splice(2, 1, city.area);
  277. }
  278. // 显示下单弹出层
  279. this.$refs.addrForm.open("bottom");
  280. },
  281. // 默认开关
  282. defaultSwitch(e) {
  283. this.addrRequest.is_default = e.detail.value ? 1 : 0;
  284. },
  285. // 弹出层
  286. popupChange(e) {
  287. // 禁止滚动穿透
  288. this.show = e.show;
  289. },
  290. // 城市修改
  291. cityChange(e) {
  292. // 替换三个选项
  293. this.cityValue.splice(e.detail.column, 1, e.detail.value);
  294. // 下一级设置为0
  295. if (e.detail.column == 0) {
  296. this.cityValue.splice(1, 1, 0);
  297. this.cityValue.splice(2, 1, 0);
  298. }
  299. if (e.detail.column == 1) {
  300. this.cityValue.splice(2, 1, 0);
  301. }
  302. // 替换对应的数据
  303. this.cityArray.splice(0, 1, getProvinces());
  304. this.cityArray.splice(1, 1, getMyCity(this.cityValue[0]));
  305. this.cityArray.splice(2, 1, getAreas(this.cityValue[0], this.cityValue[1]));
  306. },
  307. // 城市选中
  308. cityFinish(e) {
  309. this.addrRequest.contact_province = this.cityArray[0][e.detail.value[0]];
  310. this.addrRequest.contact_city = this.cityArray[1][[e.detail.value[1]]];
  311. this.addrRequest.contact_area = this.cityArray[2][[e.detail.value[2]]];
  312. },
  313. // 保存地址
  314. saveAddr() {
  315. // 判断姓名
  316. if (!this.addrRequest.contact_name) {
  317. uni.showToast({ icon: "none", title: "请填写收件人" });
  318. return;
  319. }
  320. if (this.addrRequest.contact_name.length < 2) {
  321. uni.showToast({ icon: "none", title: "请填写收件人完整姓名" });
  322. return;
  323. }
  324. if (!this.addrRequest.contact_shop) {
  325. uni.showToast({ icon: "none", title: "请填写店铺名称" });
  326. return;
  327. }
  328. if (!this.addrRequest.contact_phone) {
  329. uni.showToast({ icon: "none", title: "请填写收件人手机号" });
  330. return;
  331. }
  332. if (!this.addrRequest.contact_addr) {
  333. uni.showToast({ icon: "none", title: "请填写联系地址" });
  334. return;
  335. }
  336. if (this.addrRequest.contact_addr.length < 3) {
  337. uni.showToast({ icon: "none", title: "请填写可用联系地址" });
  338. return;
  339. }
  340. const _clone = JSON.parse(JSON.stringify(this.addrRequest));
  341. if (_clone.contact_province == "请选择" || _clone.contact_city == "请选择" || _clone.contact_area == "请选择") {
  342. uni.showToast({
  343. icon: "none",
  344. title: "请选择完整的省市区信息",
  345. });
  346. return;
  347. }
  348. // 请求状态
  349. uni.showLoading({ mask: true });
  350. // 请求路径
  351. var url = this.addrRequest.id ? "api/custom_addr/edit" : "api/custom_addr/add";
  352. // 授权成功以后,调用绑定
  353. this.$http.request(url, this.addrRequest, "post").then((re) => {
  354. // 关闭
  355. uni.hideLoading();
  356. // 成功的话
  357. if (re.code != "success") {
  358. // 跳转
  359. uni.showToast({ title: re.msg, icon: "none" });
  360. return;
  361. }
  362. // 地址变动
  363. this.getList();
  364. // 显示下单弹出层
  365. this.$refs.addrForm.close();
  366. });
  367. },
  368. // 删除地址
  369. removeAddr(index) {
  370. // 授权成功以后,调用绑定
  371. this.$http.request("api/custom_addr/del", { id: this.addrList[index].id }, "post").then((re) => {
  372. // 关闭
  373. uni.hideLoading();
  374. // 成功的话
  375. if (re.code != "success") {
  376. // 跳转
  377. uni.showToast({ title: re.msg, icon: "none" });
  378. return;
  379. }
  380. // 成功删除该项
  381. this.addrList.splice(index, 1);
  382. // 通知地址变更
  383. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  384. });
  385. },
  386. // 设置默认
  387. setDefault(index) {
  388. // 如果已经是默认状态
  389. if (this.addrList[index].is_default) return;
  390. // 授权成功以后,调用绑定
  391. this.$http.request("api/custom_addr/set_default", { id: this.addrList[index].id }, "post").then((re) => {
  392. // 关闭
  393. uni.hideLoading();
  394. // 成功的话
  395. if (re.code != "success") {
  396. // 跳转
  397. uni.showToast({ title: re.msg, icon: "none" });
  398. return;
  399. }
  400. // 更新其他的默认项
  401. for (let i in this.addrList) {
  402. this.addrList[i].is_default = 0;
  403. }
  404. // 成功修改值
  405. this.addrList[index].is_default = 1;
  406. // 通知地址变更
  407. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  408. });
  409. },
  410. handleRadioChange(key) {
  411. this.addrRequest.shop_type = Number(key);
  412. },
  413. },
  414. };
  415. </script>
  416. <style lang="less">
  417. .addr_list {
  418. width: 730rpx;
  419. display: block;
  420. overflow: hidden;
  421. margin: 0rpx auto;
  422. .addr_item {
  423. display: block;
  424. font-size: 24rpx;
  425. overflow: hidden;
  426. line-height: 40rpx;
  427. padding: 15rpx 10rpx;
  428. border-radius: 15rpx;
  429. padding-bottom: 0rpx;
  430. margin-bottom: 10rpx;
  431. background-color: #ffffff;
  432. .contact_user {
  433. display: block;
  434. font-size: 24rpx;
  435. line-height: 50rpx;
  436. .contact_name {
  437. font-size: 26rpx;
  438. font-weight: bold;
  439. margin-right: 16rpx;
  440. }
  441. .contact_shop {
  442. float: right;
  443. }
  444. }
  445. .contact_addr {
  446. display: block;
  447. font-size: 24rpx;
  448. line-height: 30rpx;
  449. padding: 10rpx 5rpx;
  450. border-bottom: 2rpx solid #dddddd;
  451. }
  452. .contact_handler {
  453. height: 60rpx;
  454. display: block;
  455. line-height: 60rpx;
  456. font-size: 20rpx;
  457. .radio_label {
  458. float: left;
  459. height: 60rpx;
  460. line-height: 60rpx;
  461. .radio_icon {
  462. width: 40rpx;
  463. height: 40rpx;
  464. vertical-align: middle;
  465. }
  466. .radioed_text {
  467. color: #e03519;
  468. }
  469. }
  470. .addr_right_btn {
  471. float: right;
  472. height: 40rpx;
  473. display: block;
  474. line-height: 40rpx;
  475. padding: 10rpx 35rpx;
  476. }
  477. }
  478. }
  479. }
  480. .create_box {
  481. left: 0rpx;
  482. width: 750rpx;
  483. height: 140rpx;
  484. display: block;
  485. position: fixed;
  486. bottom: var(--window-bottom);
  487. .create_addr {
  488. width: 700rpx;
  489. height: 80rpx;
  490. display: block;
  491. color: #ffffff;
  492. font-size: 30rpx;
  493. overflow: hidden;
  494. line-height: 80rpx;
  495. padding: 0rpx 0rpx;
  496. text-align: center;
  497. margin: 0rpx auto;
  498. margin-top: 20rpx;
  499. border-radius: 40rpx;
  500. background-color: #e03519;
  501. }
  502. }
  503. .popup {
  504. .info_form {
  505. display: block;
  506. overflow: hidden;
  507. padding: 20rpx 0rpx;
  508. padding-top: 80rpx;
  509. background: #ffffff;
  510. .form_group {
  511. display: block;
  512. line-height: 60rpx;
  513. padding: 20rpx 35rpx;
  514. height: 56rpx;
  515. .group_title {
  516. float: left;
  517. width: 160rpx;
  518. display: block;
  519. overflow: hidden;
  520. font-size: 30rpx;
  521. margin-right: 20rpx;
  522. }
  523. .group_box {
  524. float: left;
  525. width: 480rpx;
  526. display: block;
  527. .radio_icon {
  528. width: 50rpx;
  529. height: 50rpx;
  530. vertical-align: middle;
  531. }
  532. .form_ctrl {
  533. height: 56rpx;
  534. font-size: 24rpx;
  535. padding: 0rpx 20rpx;
  536. line-height: 56rpx;
  537. border: 2rpx solid #dddddd;
  538. }
  539. .area_text {
  540. width: 446rpx;
  541. font-size: 20rpx;
  542. overflow: hidden;
  543. white-space: nowrap;
  544. text-overflow: ellipsis;
  545. }
  546. .group_image {
  547. width: 200rpx;
  548. height: 200rpx;
  549. }
  550. .choose_image {
  551. display: block;
  552. width: 200rpx;
  553. height: 200rpx;
  554. font-size: 38rpx;
  555. text-align: center;
  556. line-height: 200rpx;
  557. border: 2rpx solid #dddddd;
  558. }
  559. .terminalType_radio {
  560. display: flex;
  561. justify-content: space-around;
  562. }
  563. }
  564. }
  565. .submit_btn {
  566. width: 700rpx;
  567. height: 80rpx;
  568. display: block;
  569. color: #ffffff;
  570. font-size: 30rpx;
  571. overflow: hidden;
  572. line-height: 80rpx;
  573. padding: 0rpx 0rpx;
  574. text-align: center;
  575. margin: 20rpx auto;
  576. margin-top: 40rpx;
  577. border-radius: 40rpx;
  578. background-color: #e03519;
  579. }
  580. }
  581. }
  582. </style>