index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. <input type="text" class="form_ctrl" placeholder="请输入营业执照全称" maxlength="20" required="" v-model="addrRequest.contact_shop" />
  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. export default {
  100. components: { Empty },
  101. data() {
  102. return {
  103. // 产品列表
  104. addrList: [],
  105. // 请求参数
  106. requestParam: {
  107. page: 1,
  108. status: 0,
  109. },
  110. // 保存地址
  111. addrRequest: {
  112. id: 0,
  113. contact_name: "",
  114. contact_phone: "",
  115. contact_province: "",
  116. contact_city: "",
  117. contact_area: "",
  118. contact_addr: "",
  119. contact_shop: "",
  120. is_default: 0,
  121. shop_type: 1,
  122. },
  123. // 是否最后一页
  124. isLast: false,
  125. // 是否请求中
  126. isReqing: false,
  127. // 所在地区
  128. cityArray: [[], [], []],
  129. cityValue: [0, 0, 0],
  130. // 自动弹出
  131. autoShowForm: false,
  132. // 异步通知
  133. AddrEmit: false,
  134. //终端类型
  135. terminalType: [
  136. { key: 1, label: "单店" },
  137. { key: 2, label: "连锁" },
  138. { key: 3, label: "诊所、社康等" },
  139. ],
  140. };
  141. },
  142. onLoad(param) {
  143. this.AddrEmit = param.notify == "addr" ? true : false;
  144. this.autoShowForm = param.type == "create" ? true : false;
  145. // 获取列表
  146. this.getList();
  147. },
  148. onReady() {
  149. if (this.autoShowForm) {
  150. var that = this;
  151. // 500毫秒后自动弹出
  152. setTimeout(function () {
  153. that.openForm();
  154. }, 500);
  155. }
  156. },
  157. onShow() {
  158. // 替换对应的数据
  159. this.cityArray.splice(0, 1, getProvinces());
  160. this.cityArray.splice(1, 1, getMyCity(this.cityValue[0]));
  161. this.cityArray.splice(2, 1, getAreas(this.cityValue[0], this.cityValue[1]));
  162. // 默认地址
  163. this.addrRequest.contact_province = this.cityArray[0][this.cityValue[0]];
  164. this.addrRequest.contact_city = this.cityArray[1][this.cityValue[1]];
  165. this.addrRequest.contact_area = this.cityArray[2][this.cityValue[2]];
  166. },
  167. methods: {
  168. // 获取列表
  169. getList() {
  170. // 登录提示
  171. if (!this.$checkAccess.alterLogin()) return;
  172. // 判断数据
  173. this.$http.request("api/custom_addr/get_list").then((callback) => {
  174. // 获取成功
  175. if (callback.code == "success") {
  176. this.addrList = callback.data;
  177. // 通知地址变更
  178. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  179. }
  180. });
  181. },
  182. // 打开弹窗
  183. openForm(item) {
  184. if (!item) {
  185. this.addrRequest.id = 0;
  186. this.addrRequest.contact_name = "";
  187. this.addrRequest.contact_phone = "";
  188. this.addrRequest.contact_shop = "";
  189. this.addrRequest.contact_addr = "";
  190. this.addrRequest.is_default = 0;
  191. this.addrRequest.shop_type = 1;
  192. this.cityValue = [0, 0, 0];
  193. this.addrRequest.contact_province = "请选择";
  194. this.addrRequest.contact_city = "请选择";
  195. this.addrRequest.contact_area = "请选择";
  196. } else {
  197. if (!item.shop_type) item.shop_type = 1;
  198. const _cloneItem = JSON.parse(JSON.stringify(item));
  199. this.addrRequest = _cloneItem;
  200. const provinces = getProvincesIndex(item.contact_province);
  201. const city = getCityIndex(item.contact_city, provinces.city, provinces.index);
  202. const area = getAreaIndex(item.contact_area, city.area);
  203. this.cityValue = [provinces.index, city.index, area.index];
  204. this.cityArray.splice(1, 1, provinces.city);
  205. this.cityArray.splice(2, 1, city.area);
  206. }
  207. // 显示下单弹出层
  208. this.$refs.addrForm.open("bottom");
  209. },
  210. // 默认开关
  211. defaultSwitch(e) {
  212. this.addrRequest.is_default = e.detail.value ? 1 : 0;
  213. },
  214. // 弹出层
  215. popupChange(e) {
  216. // 禁止滚动穿透
  217. this.show = e.show;
  218. },
  219. // 城市修改
  220. cityChange(e) {
  221. // 替换三个选项
  222. this.cityValue.splice(e.detail.column, 1, e.detail.value);
  223. // 下一级设置为0
  224. if (e.detail.column == 0) {
  225. this.cityValue.splice(1, 1, 0);
  226. this.cityValue.splice(2, 1, 0);
  227. }
  228. if (e.detail.column == 1) {
  229. this.cityValue.splice(2, 1, 0);
  230. }
  231. // 替换对应的数据
  232. this.cityArray.splice(0, 1, getProvinces());
  233. this.cityArray.splice(1, 1, getMyCity(this.cityValue[0]));
  234. this.cityArray.splice(2, 1, getAreas(this.cityValue[0], this.cityValue[1]));
  235. },
  236. // 城市选中
  237. cityFinish(e) {
  238. this.addrRequest.contact_province = this.cityArray[0][e.detail.value[0]];
  239. this.addrRequest.contact_city = this.cityArray[1][[e.detail.value[1]]];
  240. this.addrRequest.contact_area = this.cityArray[2][[e.detail.value[2]]];
  241. },
  242. // 保存地址
  243. saveAddr() {
  244. // 判断姓名
  245. if (!this.addrRequest.contact_name) {
  246. uni.showToast({ icon: "none", title: "请填写收件人" });
  247. return;
  248. }
  249. if (this.addrRequest.contact_name.length < 2) {
  250. uni.showToast({ icon: "none", title: "请填写收件人完整姓名" });
  251. return;
  252. }
  253. if (!this.addrRequest.contact_shop) {
  254. uni.showToast({ icon: "none", title: "请填写店铺名称" });
  255. return;
  256. }
  257. if (!this.addrRequest.contact_phone) {
  258. uni.showToast({ icon: "none", title: "请填写收件人手机号" });
  259. return;
  260. }
  261. if (!this.addrRequest.contact_addr) {
  262. uni.showToast({ icon: "none", title: "请填写联系地址" });
  263. return;
  264. }
  265. if (this.addrRequest.contact_addr.length < 3) {
  266. uni.showToast({ icon: "none", title: "请填写可用联系地址" });
  267. return;
  268. }
  269. const _clone = JSON.parse(JSON.stringify(this.addrRequest));
  270. if (_clone.contact_province == "请选择" || _clone.contact_city == "请选择" || _clone.contact_area == "请选择") {
  271. uni.showToast({
  272. icon: "none",
  273. title: "请选择完整的省市区信息",
  274. });
  275. return;
  276. }
  277. // 请求状态
  278. uni.showLoading({ mask: true });
  279. // 请求路径
  280. var url = this.addrRequest.id ? "api/custom_addr/edit" : "api/custom_addr/add";
  281. // 授权成功以后,调用绑定
  282. this.$http.request(url, this.addrRequest, "post").then((re) => {
  283. // 关闭
  284. uni.hideLoading();
  285. // 成功的话
  286. if (re.code != "success") {
  287. // 跳转
  288. uni.showToast({ title: re.msg, icon: "none" });
  289. return;
  290. }
  291. // 地址变动
  292. this.getList();
  293. // 显示下单弹出层
  294. this.$refs.addrForm.close();
  295. });
  296. },
  297. // 删除地址
  298. removeAddr(index) {
  299. // 授权成功以后,调用绑定
  300. this.$http.request("api/custom_addr/del", { id: this.addrList[index].id }, "post").then((re) => {
  301. // 关闭
  302. uni.hideLoading();
  303. // 成功的话
  304. if (re.code != "success") {
  305. // 跳转
  306. uni.showToast({ title: re.msg, icon: "none" });
  307. return;
  308. }
  309. // 成功删除该项
  310. this.addrList.splice(index, 1);
  311. // 通知地址变更
  312. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  313. });
  314. },
  315. // 设置默认
  316. setDefault(index) {
  317. // 如果已经是默认状态
  318. if (this.addrList[index].is_default) return;
  319. // 授权成功以后,调用绑定
  320. this.$http.request("api/custom_addr/set_default", { id: this.addrList[index].id }, "post").then((re) => {
  321. // 关闭
  322. uni.hideLoading();
  323. // 成功的话
  324. if (re.code != "success") {
  325. // 跳转
  326. uni.showToast({ title: re.msg, icon: "none" });
  327. return;
  328. }
  329. // 更新其他的默认项
  330. for (let i in this.addrList) {
  331. this.addrList[i].is_default = 0;
  332. }
  333. // 成功修改值
  334. this.addrList[index].is_default = 1;
  335. // 通知地址变更
  336. if (this.AddrEmit) uni.$emit("addr_list_change", { list: this.addrList });
  337. });
  338. },
  339. handleRadioChange(key) {
  340. this.addrRequest.shop_type = Number(key);
  341. },
  342. },
  343. };
  344. </script>
  345. <style lang="less">
  346. .addr_list {
  347. width: 730rpx;
  348. display: block;
  349. overflow: hidden;
  350. margin: 0rpx auto;
  351. .addr_item {
  352. display: block;
  353. font-size: 24rpx;
  354. overflow: hidden;
  355. line-height: 40rpx;
  356. padding: 15rpx 10rpx;
  357. border-radius: 15rpx;
  358. padding-bottom: 0rpx;
  359. margin-bottom: 10rpx;
  360. background-color: #ffffff;
  361. .contact_user {
  362. display: block;
  363. font-size: 24rpx;
  364. line-height: 50rpx;
  365. .contact_name {
  366. font-size: 26rpx;
  367. font-weight: bold;
  368. margin-right: 16rpx;
  369. }
  370. .contact_shop {
  371. float: right;
  372. }
  373. }
  374. .contact_addr {
  375. display: block;
  376. font-size: 24rpx;
  377. line-height: 30rpx;
  378. padding: 10rpx 5rpx;
  379. border-bottom: 2rpx solid #dddddd;
  380. }
  381. .contact_handler {
  382. height: 60rpx;
  383. display: block;
  384. line-height: 60rpx;
  385. font-size: 20rpx;
  386. .radio_label {
  387. float: left;
  388. height: 60rpx;
  389. line-height: 60rpx;
  390. .radio_icon {
  391. width: 40rpx;
  392. height: 40rpx;
  393. vertical-align: middle;
  394. }
  395. .radioed_text {
  396. color: #e03519;
  397. }
  398. }
  399. .addr_right_btn {
  400. float: right;
  401. height: 40rpx;
  402. display: block;
  403. line-height: 40rpx;
  404. padding: 10rpx 35rpx;
  405. }
  406. }
  407. }
  408. }
  409. .create_box {
  410. left: 0rpx;
  411. width: 750rpx;
  412. height: 140rpx;
  413. display: block;
  414. position: fixed;
  415. bottom: var(--window-bottom);
  416. .create_addr {
  417. width: 700rpx;
  418. height: 80rpx;
  419. display: block;
  420. color: #ffffff;
  421. font-size: 30rpx;
  422. overflow: hidden;
  423. line-height: 80rpx;
  424. padding: 0rpx 0rpx;
  425. text-align: center;
  426. margin: 0rpx auto;
  427. margin-top: 20rpx;
  428. border-radius: 40rpx;
  429. background-color: #e03519;
  430. }
  431. }
  432. .popup {
  433. .info_form {
  434. display: block;
  435. overflow: hidden;
  436. padding: 20rpx 0rpx;
  437. padding-top: 80rpx;
  438. background: #ffffff;
  439. .form_group {
  440. display: block;
  441. overflow: hidden;
  442. line-height: 60rpx;
  443. padding: 20rpx 35rpx;
  444. .group_title {
  445. float: left;
  446. width: 160rpx;
  447. display: block;
  448. overflow: hidden;
  449. font-size: 30rpx;
  450. margin-right: 20rpx;
  451. }
  452. .group_box {
  453. float: left;
  454. width: 480rpx;
  455. display: block;
  456. .radio_icon {
  457. width: 50rpx;
  458. height: 50rpx;
  459. vertical-align: middle;
  460. }
  461. .form_ctrl {
  462. height: 56rpx;
  463. font-size: 24rpx;
  464. padding: 0rpx 20rpx;
  465. line-height: 56rpx;
  466. border: 2rpx solid #dddddd;
  467. }
  468. .area_text {
  469. width: 446rpx;
  470. font-size: 20rpx;
  471. overflow: hidden;
  472. white-space: nowrap;
  473. text-overflow: ellipsis;
  474. }
  475. .group_image {
  476. width: 200rpx;
  477. height: 200rpx;
  478. }
  479. .choose_image {
  480. display: block;
  481. width: 200rpx;
  482. height: 200rpx;
  483. font-size: 38rpx;
  484. text-align: center;
  485. line-height: 200rpx;
  486. border: 2rpx solid #dddddd;
  487. }
  488. .terminalType_radio {
  489. display: flex;
  490. justify-content: space-around;
  491. }
  492. }
  493. }
  494. .submit_btn {
  495. width: 700rpx;
  496. height: 80rpx;
  497. display: block;
  498. color: #ffffff;
  499. font-size: 30rpx;
  500. overflow: hidden;
  501. line-height: 80rpx;
  502. padding: 0rpx 0rpx;
  503. text-align: center;
  504. margin: 20rpx auto;
  505. margin-top: 40rpx;
  506. border-radius: 40rpx;
  507. background-color: #e03519;
  508. }
  509. }
  510. }
  511. </style>