index.vue 8.6 KB

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