index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 ref="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" @click="onFocus" :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" @focus="onFocus" 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. this.onFocus()
  303. // 替换三个选项
  304. this.cityValue.splice(e.detail.column, 1, e.detail.value);
  305. // 下一级设置为0
  306. if (e.detail.column == 0) {
  307. this.cityValue.splice(1, 1, 0);
  308. this.cityValue.splice(2, 1, 0);
  309. }
  310. if (e.detail.column == 1) {
  311. this.cityValue.splice(2, 1, 0);
  312. }
  313. // 替换对应的数据
  314. this.cityArray.splice(0, 1, getProvinces());
  315. this.cityArray.splice(1, 1, getMyCity(this.cityValue[0]));
  316. this.cityArray.splice(2, 1, getAreas(this.cityValue[0], this.cityValue[1]));
  317. },
  318. // 城市选中
  319. cityFinish(e) {
  320. this.addrRequest.contact_province = this.cityArray[0][e.detail.value[0]];
  321. this.addrRequest.contact_city = this.cityArray[1][[e.detail.value[1]]];
  322. this.addrRequest.contact_area = this.cityArray[2][[e.detail.value[2]]];
  323. },
  324. // 保存地址
  325. saveAddr() {
  326. // 判断姓名
  327. if (!this.addrRequest.contact_name) {
  328. uni.showToast({ icon: "none", title: "请填写收件人" });
  329. return;
  330. }
  331. if (this.addrRequest.contact_name.length < 2) {
  332. uni.showToast({ icon: "none", title: "请填写收件人完整姓名" });
  333. return;
  334. }
  335. if (!this.addrRequest.contact_shop) {
  336. uni.showToast({ icon: "none", title: "请填写店铺名称" });
  337. return;
  338. }
  339. if (!this.addrRequest.contact_phone) {
  340. uni.showToast({ icon: "none", title: "请填写收件人手机号" });
  341. return;
  342. }
  343. if (!this.addrRequest.contact_addr) {
  344. uni.showToast({ icon: "none", title: "请填写联系地址" });
  345. return;
  346. }
  347. if (this.addrRequest.contact_addr.length < 3) {
  348. uni.showToast({ icon: "none", title: "请填写可用联系地址" });
  349. return;
  350. }
  351. const _clone = JSON.parse(JSON.stringify(this.addrRequest));
  352. if (_clone.contact_province == "请选择" || _clone.contact_city == "请选择" || _clone.contact_area == "请选择") {
  353. uni.showToast({
  354. icon: "none",
  355. title: "请选择完整的省市区信息",
  356. });
  357. return;
  358. }
  359. // 请求状态
  360. uni.showLoading({ mask: true });
  361. // 请求路径
  362. var url = this.addrRequest.id ? "api/custom_addr/edit" : "api/custom_addr/add";
  363. // 授权成功以后,调用绑定
  364. this.$http.request(url, this.addrRequest, "post").then((re) => {
  365. // 关闭
  366. uni.hideLoading();
  367. // 成功的话
  368. if (re.code != "success") {
  369. // 跳转
  370. uni.showToast({ title: re.msg, icon: "none" });
  371. return;
  372. }
  373. // 地址变动
  374. this.getList();
  375. // 显示下单弹出层
  376. this.$refs.addrForm.close();
  377. });
  378. },
  379. // 删除地址
  380. removeAddr(index) {
  381. // 授权成功以后,调用绑定
  382. this.$http.request("api/custom_addr/del", { id: this.addrList[index].id }, "post").then((re) => {
  383. // 关闭
  384. uni.hideLoading();
  385. // 成功的话
  386. if (re.code != "success") {
  387. // 跳转
  388. uni.showToast({ title: re.msg, icon: "none" });
  389. return;
  390. }
  391. // 成功删除该项
  392. this.addrList.splice(index, 1);
  393. // 通知地址变更
  394. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  395. });
  396. },
  397. // 设置默认
  398. setDefault(index) {
  399. // 如果已经是默认状态
  400. if (this.addrList[index].is_default) return;
  401. // 授权成功以后,调用绑定
  402. this.$http.request("api/custom_addr/set_default", { id: this.addrList[index].id }, "post").then((re) => {
  403. // 关闭
  404. uni.hideLoading();
  405. // 成功的话
  406. if (re.code != "success") {
  407. // 跳转
  408. uni.showToast({ title: re.msg, icon: "none" });
  409. return;
  410. }
  411. // 更新其他的默认项
  412. for (let i in this.addrList) {
  413. this.addrList[i].is_default = 0;
  414. }
  415. // 成功修改值
  416. this.addrList[index].is_default = 1;
  417. // 通知地址变更
  418. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  419. });
  420. },
  421. handleRadioChange(key) {
  422. this.onFocus()
  423. this.addrRequest.shop_type = Number(key);
  424. },
  425. onFocus(){
  426. this.$refs.combox.close()
  427. }
  428. },
  429. };
  430. </script>
  431. <style lang="less">
  432. .addr_list {
  433. width: 730rpx;
  434. display: block;
  435. overflow: hidden;
  436. margin: 0rpx auto;
  437. .addr_item {
  438. display: block;
  439. font-size: 24rpx;
  440. overflow: hidden;
  441. line-height: 40rpx;
  442. padding: 15rpx 10rpx;
  443. border-radius: 15rpx;
  444. padding-bottom: 0rpx;
  445. margin-bottom: 10rpx;
  446. background-color: #ffffff;
  447. .contact_user {
  448. display: block;
  449. font-size: 24rpx;
  450. line-height: 50rpx;
  451. .contact_name {
  452. font-size: 26rpx;
  453. font-weight: bold;
  454. margin-right: 16rpx;
  455. }
  456. .contact_shop {
  457. float: right;
  458. }
  459. }
  460. .contact_addr {
  461. display: block;
  462. font-size: 24rpx;
  463. line-height: 30rpx;
  464. padding: 10rpx 5rpx;
  465. border-bottom: 2rpx solid #dddddd;
  466. }
  467. .contact_handler {
  468. height: 60rpx;
  469. display: block;
  470. line-height: 60rpx;
  471. font-size: 20rpx;
  472. .radio_label {
  473. float: left;
  474. height: 60rpx;
  475. line-height: 60rpx;
  476. .radio_icon {
  477. width: 40rpx;
  478. height: 40rpx;
  479. vertical-align: middle;
  480. }
  481. .radioed_text {
  482. color: #e03519;
  483. }
  484. }
  485. .addr_right_btn {
  486. float: right;
  487. height: 40rpx;
  488. display: block;
  489. line-height: 40rpx;
  490. padding: 10rpx 35rpx;
  491. }
  492. }
  493. }
  494. }
  495. .create_box {
  496. left: 0rpx;
  497. width: 750rpx;
  498. height: 140rpx;
  499. display: block;
  500. position: fixed;
  501. bottom: var(--window-bottom);
  502. .create_addr {
  503. width: 700rpx;
  504. height: 80rpx;
  505. display: block;
  506. color: #ffffff;
  507. font-size: 30rpx;
  508. overflow: hidden;
  509. line-height: 80rpx;
  510. padding: 0rpx 0rpx;
  511. text-align: center;
  512. margin: 0rpx auto;
  513. margin-top: 20rpx;
  514. border-radius: 40rpx;
  515. background-color: #e03519;
  516. }
  517. }
  518. .popup {
  519. .info_form {
  520. display: block;
  521. overflow: hidden;
  522. padding: 20rpx 0rpx;
  523. padding-top: 80rpx;
  524. background: #ffffff;
  525. .form_group {
  526. display: block;
  527. line-height: 60rpx;
  528. padding: 20rpx 35rpx;
  529. height: 56rpx;
  530. .group_title {
  531. float: left;
  532. width: 160rpx;
  533. display: block;
  534. overflow: hidden;
  535. font-size: 30rpx;
  536. margin-right: 20rpx;
  537. }
  538. .group_box {
  539. float: left;
  540. width: 480rpx;
  541. display: block;
  542. .radio_icon {
  543. width: 50rpx;
  544. height: 50rpx;
  545. vertical-align: middle;
  546. }
  547. .form_ctrl {
  548. height: 56rpx;
  549. font-size: 24rpx;
  550. padding: 0rpx 20rpx;
  551. line-height: 56rpx;
  552. border: 2rpx solid #dddddd;
  553. }
  554. .area_text {
  555. width: 446rpx;
  556. font-size: 20rpx;
  557. overflow: hidden;
  558. white-space: nowrap;
  559. text-overflow: ellipsis;
  560. }
  561. .group_image {
  562. width: 200rpx;
  563. height: 200rpx;
  564. }
  565. .choose_image {
  566. display: block;
  567. width: 200rpx;
  568. height: 200rpx;
  569. font-size: 38rpx;
  570. text-align: center;
  571. line-height: 200rpx;
  572. border: 2rpx solid #dddddd;
  573. }
  574. .terminalType_radio {
  575. display: flex;
  576. justify-content: space-around;
  577. }
  578. }
  579. }
  580. .submit_btn {
  581. width: 700rpx;
  582. height: 80rpx;
  583. display: block;
  584. color: #ffffff;
  585. font-size: 30rpx;
  586. overflow: hidden;
  587. line-height: 80rpx;
  588. padding: 0rpx 0rpx;
  589. text-align: center;
  590. margin: 20rpx auto;
  591. margin-top: 40rpx;
  592. border-radius: 40rpx;
  593. background-color: #e03519;
  594. }
  595. }
  596. }
  597. </style>