index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  6. <view class="list-scroll-view">
  7. <radio-group @change="addressChange" v-if="type=='shopping'">
  8. <label class="radio" v-for="(item, index) in list" :key="index">
  9. <radio :value="item._id" :checked="item._id === address" />
  10. <view class="radio_1">
  11. <view class="name">
  12. <text>{{item.name}}</text>
  13. <text>{{item.phone}}</text>
  14. </view>
  15. <view class="address">
  16. <text>{{item.province}}</text>
  17. <text>{{item.city}}</text>
  18. <text>{{item.area}}</text>
  19. <text>{{item.address}}</text>
  20. <text>{{item.number}}</text>
  21. </view>
  22. <view class="btn">
  23. <view class="btn_1" v-if="item.is_default==false">
  24. <button type="default" size="mini" @click="toDefa(item)">设为默认</button>
  25. </view>
  26. <view class="btn_1">
  27. <button type="default" size="mini" @click="toCommon(item)">编辑</button>
  28. </view>
  29. <view class="btn_1">
  30. <button type="default" size="mini" @click="toDel(item)">删除</button>
  31. </view>
  32. </view>
  33. <view class="default" v-if="item.is_default==true">
  34. <text>默认</text>
  35. </view>
  36. </view>
  37. </label>
  38. </radio-group>
  39. <view v-else class="list" v-for="(item,index) in list" :key="index">
  40. <view class="name">
  41. <text>{{item.name}}</text>
  42. <text>{{item.phone}}</text>
  43. </view>
  44. <view class="address">
  45. <text>{{item.province}}</text>
  46. <text>{{item.city}}</text>
  47. <text>{{item.area}}</text>
  48. <text>{{item.address}}</text>
  49. <text>{{item.number}}</text>
  50. </view>
  51. <view class="btn">
  52. <view class="btn_1" v-if="item.is_default==false">
  53. <button type="default" size="mini" @click="toDefa(item)">设为默认</button>
  54. </view>
  55. <view class="btn_1">
  56. <button type="default" size="mini" @click="toCommon(item)">编辑</button>
  57. </view>
  58. <view class="btn_1">
  59. <button type="default" size="mini" @click="toDel(item)">删除</button>
  60. </view>
  61. </view>
  62. <view class="default" v-if="item.is_default==true">
  63. <text>默认</text>
  64. </view>
  65. </view>
  66. <view class="is_bottom" v-if="is_bottom">
  67. <text>{{config.bottom_title}}</text>
  68. </view>
  69. </view>
  70. </scroll-view>
  71. </view>
  72. <view class="two">
  73. <view class="two_1">
  74. <button type="default" @click="toWxaddress()">获取微信地址</button>
  75. </view>
  76. <view class="two_1">
  77. <button type="default" @click="toCommon()">新增收货地址</button>
  78. </view>
  79. </view>
  80. </view>
  81. </mobile-frame>
  82. </template>
  83. <script>
  84. export default {
  85. data() {
  86. return {
  87. // 购物选地址
  88. type: '',
  89. address: '',
  90. // 系统设置
  91. config: {},
  92. user: {},
  93. list: [],
  94. total: 0,
  95. skip: 0,
  96. limit: 5,
  97. page: 0,
  98. // 数据是否触底
  99. is_bottom: false,
  100. scrollTop: 0,
  101. };
  102. },
  103. onLoad: async function(e) {
  104. const that = this;
  105. that.$set(that, `type`, e && e.type || '');
  106. that.$set(that, `address`, e && e.id || '');
  107. that.searchConfig();
  108. that.watchLogin();
  109. },
  110. onShow: function() {},
  111. onHide: function() {},
  112. methods: {
  113. // 查询基本设置
  114. searchConfig() {
  115. const that = this;
  116. uni.getStorage({
  117. key: 'config',
  118. success: function(res) {
  119. if (res.data) that.$set(that, `config`, res.data)
  120. },
  121. fail: function(err) {
  122. console.log(err);
  123. }
  124. })
  125. },
  126. // 监听用户是否登录
  127. watchLogin() {
  128. const that = this;
  129. uni.getStorage({
  130. key: 'token',
  131. success: function(res) {
  132. let user = that.$jwt(res.data);
  133. that.$set(that, `user`, user);
  134. that.search()
  135. },
  136. fail: function(err) {
  137. uni.reLaunch({
  138. url: `/pages/login/index`
  139. })
  140. }
  141. })
  142. },
  143. // 查询列表
  144. async search() {
  145. const that = this;
  146. let user = that.user;
  147. let info = {
  148. skip: that.skip,
  149. limit: that.limit,
  150. customer: user._id
  151. }
  152. const res = await that.$api(`/address`, 'GET', {
  153. ...info
  154. })
  155. if (res.errcode == '0') {
  156. let list = [...that.list, ...res.data];
  157. that.$set(that, `list`, list);
  158. that.$set(that, `total`, res.total)
  159. if (list.length == 0) uni.$emit("id", '')
  160. if (that.id) {
  161. for (let i = 0; i < list.length; i++) {
  162. if (list[i]._id === that.id) {
  163. that.address = that.id;
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. },
  170. // 分页
  171. toPage() {
  172. const that = this;
  173. let list = that.list;
  174. let limit = that.limit;
  175. if (that.total > list.length) {
  176. uni.showLoading({
  177. title: '加载中',
  178. mask: true
  179. })
  180. let page = that.page + 1;
  181. that.$set(that, `page`, page)
  182. let skip = page * limit;
  183. that.$set(that, `skip`, skip)
  184. that.search();
  185. uni.hideLoading();
  186. } else that.$set(that, `is_bottom`, true)
  187. },
  188. toScroll(e) {
  189. const that = this;
  190. let up = that.scrollTop;
  191. that.$set(that, `scrollTop`, e.detail.scrollTop);
  192. let num = Math.sign(up - e.detail.scrollTop);
  193. if (num == 1) that.$set(that, `is_bottom`, false);
  194. },
  195. // 选择收货地址
  196. addressChange(e) {
  197. const that = this;
  198. for (let i = 0; i < that.list.length; i++) {
  199. if (that.list[i]._id === e.detail.value) {
  200. that.address = e.detail.value;
  201. break;
  202. }
  203. }
  204. uni.$emit("id", e.detail.value)
  205. uni.navigateBack({
  206. delta: 1
  207. })
  208. },
  209. // 获取微信地址
  210. toWxaddress() {
  211. const that = this;
  212. let user = that.user;
  213. uni.getStorage({
  214. key: 'system',
  215. success: function(res) {
  216. if (res.data.uniPlatform == 'app') {
  217. uni.showToast({
  218. title: '请进入微信小程序进行获取微信地址',
  219. icon: 'none'
  220. })
  221. } else if (res.data.uniPlatform == 'mp-weixin') {
  222. uni.chooseAddress({
  223. success: async function(add) {
  224. let params = {
  225. customer: user._id,
  226. name: add.userName,
  227. phone: add.telNumber,
  228. province: add.provinceName,
  229. city: add.cityName,
  230. area: add.countyName,
  231. address: add.detailInfo
  232. }
  233. const arr = await that.$api(`/address`, 'POST', params);
  234. if (arr.errcode == '0') {
  235. uni.showToast({
  236. title: '添加收货地址成功',
  237. icon: 'none'
  238. })
  239. that.clearPage();
  240. that.search();
  241. } else {
  242. uni.showToast({
  243. title: arr.errmsg,
  244. icon: 'none'
  245. })
  246. }
  247. },
  248. fail: async function(err) {
  249. that.clearPage();
  250. that.search();
  251. }
  252. })
  253. }
  254. }
  255. })
  256. },
  257. // 设置默认
  258. toDefa(e) {
  259. const that = this;
  260. uni.showModal({
  261. title: '提示',
  262. content: '确定设置该地址为默认地址吗?',
  263. success: async function(res) {
  264. if (res.confirm) {
  265. const arr = await that.$api(`/address/toDefault/${e._id}`, `POST`);
  266. if (arr.errcode == '0') {
  267. uni.showToast({
  268. title: '添加默认成功',
  269. icon: 'none'
  270. })
  271. that.clearPage();
  272. that.search();
  273. } else {
  274. uni.showToast({
  275. title: arr.errmsg,
  276. icon: 'none'
  277. })
  278. }
  279. }
  280. }
  281. });
  282. },
  283. // 编辑
  284. toCommon(e) {
  285. const that = this;
  286. uni.navigateTo({
  287. url: `/pagesMy/address/add?id=${e&&e.id?e.id:''}`
  288. })
  289. },
  290. // 删除
  291. toDel(e) {
  292. const that = this;
  293. uni.showModal({
  294. title: '提示',
  295. content: '确定删除该地址吗?',
  296. success: async function(res) {
  297. if (res.confirm) {
  298. const arr = await that.$api(`/address/${e._id}`, 'DELETE');
  299. if (arr.errcode == '0') {
  300. uni.showToast({
  301. title: '删除信息成功',
  302. icon: 'none'
  303. })
  304. that.clearPage();
  305. that.search();
  306. } else {
  307. uni.showToast({
  308. title: arr.errmsg,
  309. icon: 'none'
  310. })
  311. }
  312. }
  313. }
  314. });
  315. },
  316. // 清空列表
  317. clearPage() {
  318. const that = this;
  319. that.$set(that, `list`, [])
  320. that.$set(that, `skip`, 0)
  321. that.$set(that, `limit`, 6)
  322. that.$set(that, `page`, 0)
  323. }
  324. },
  325. onPullDownRefresh: async function() {
  326. const that = this;
  327. that.$set(that, `list`, [])
  328. that.$set(that, `skip`, 0)
  329. that.$set(that, `limit`, 6)
  330. that.$set(that, `page`, 0)
  331. await that.search();
  332. uni.stopPullDownRefresh();
  333. }
  334. }
  335. </script>
  336. <style lang="scss">
  337. .main {
  338. display: flex;
  339. flex-direction: column;
  340. width: 100vw;
  341. height: 100vh;
  342. .one {
  343. position: relative;
  344. flex-grow: 1;
  345. background-color: var(--f5Color);
  346. .radio {
  347. position: relative;
  348. display: flex;
  349. background: var(--fffColor);
  350. padding: 2vw;
  351. width: 92vw;
  352. margin: 2vw 2vw 0 2vw;
  353. border-radius: 5px;
  354. .radio_1 {
  355. width: 80vw;
  356. .name {
  357. font-size: var(--font16Size);
  358. margin: 0 0 2vw 0;
  359. text {
  360. padding: 0 2vw 0 0;
  361. }
  362. }
  363. .address {
  364. font-size: var(--font14Size);
  365. margin: 0 0 1vw 0;
  366. text {
  367. padding: 0 2vw 0 0;
  368. }
  369. }
  370. .btn {
  371. display: flex;
  372. flex-direction: row;
  373. justify-content: space-around;
  374. border-top: 1px solid var(--font16Size);
  375. padding: 2vw 0 0 0;
  376. .btn_1 {
  377. button {
  378. width: 100%;
  379. color: var(--fffColor);
  380. background-color: var(--f35BColor);
  381. }
  382. }
  383. .btn_1:nth-child(2) {
  384. button {
  385. background-color: var(--f0fColor);
  386. }
  387. }
  388. .btn_1:last-child {
  389. button {
  390. background-color: var(--fFB1Color);
  391. }
  392. }
  393. }
  394. .default {
  395. position: absolute;
  396. top: 0;
  397. right: 0;
  398. text {
  399. background: var(--ff0Color);
  400. color: var(--fffColor);
  401. font-size: var(--font12Size);
  402. padding: 1vw;
  403. border-radius: 5px;
  404. }
  405. }
  406. }
  407. }
  408. .list {
  409. position: relative;
  410. background: var(--fffColor);
  411. padding: 2vw;
  412. width: 92vw;
  413. margin: 2vw 2vw 0 2vw;
  414. border-radius: 5px;
  415. .name {
  416. font-size: var(--font16Size);
  417. margin: 0 0 2vw 0;
  418. text {
  419. padding: 0 2vw 0 0;
  420. }
  421. }
  422. .address {
  423. font-size: var(--font14Size);
  424. margin: 0 0 1vw 0;
  425. text {
  426. padding: 0 2vw 0 0;
  427. }
  428. }
  429. .btn {
  430. display: flex;
  431. flex-direction: row;
  432. justify-content: space-around;
  433. border-top: 1px solid var(--font16Size);
  434. padding: 2vw 0 0 0;
  435. .btn_1 {
  436. button {
  437. width: 100%;
  438. color: var(--fffColor);
  439. background-color: var(--f35BColor);
  440. }
  441. }
  442. .btn_1:nth-child(2) {
  443. button {
  444. background-color: var(--f0fColor);
  445. }
  446. }
  447. .btn_1:last-child {
  448. button {
  449. background-color: var(--fFB1Color);
  450. }
  451. }
  452. }
  453. .default {
  454. position: absolute;
  455. top: 0;
  456. right: 0;
  457. text {
  458. background: var(--ff0Color);
  459. color: var(--fffColor);
  460. font-size: var(--font12Size);
  461. padding: 1vw;
  462. border-radius: 5px;
  463. }
  464. }
  465. }
  466. }
  467. .two {
  468. display: flex;
  469. flex-direction: row;
  470. justify-content: space-between;
  471. .two_1 {
  472. width: 50vw;
  473. button {
  474. width: 100%;
  475. color: var(--fffColor);
  476. background-color: var(--f08Color);
  477. border-radius: 0;
  478. }
  479. }
  480. .two_1:nth-child(2) {
  481. button {
  482. background-color: var(--fFB1Color);
  483. }
  484. }
  485. }
  486. }
  487. .scroll-view {
  488. position: absolute;
  489. top: 0;
  490. left: 0;
  491. right: 0;
  492. bottom: 0;
  493. .list-scroll-view {
  494. display: flex;
  495. flex-direction: column;
  496. }
  497. }
  498. .is_bottom {
  499. text-align: center;
  500. text {
  501. padding: 2vw 0;
  502. display: inline-block;
  503. color: #858585;
  504. font-size: 14px;
  505. }
  506. }
  507. </style>