index.vue 13 KB

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