index.vue 20 KB

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