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