index.vue 8.7 KB

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