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. },
  73. onShow: function() {},
  74. onHide: function() {},
  75. methods: {
  76. // 查询基本设置
  77. searchConfig() {
  78. const that = this;
  79. uni.getStorage({
  80. key: 'config',
  81. success: function(res) {
  82. if (res.data) that.$set(that, `config`, res.data)
  83. },
  84. fail: function(err) {
  85. console.log(err);
  86. }
  87. })
  88. },
  89. // 监听用户是否登录
  90. watchLogin() {
  91. const that = this;
  92. uni.getStorage({
  93. key: 'token',
  94. success: function(res) {
  95. let user = that.$jwt(res.data);
  96. that.$set(that, `user`, user);
  97. that.search()
  98. },
  99. fail: function(err) {
  100. uni.reLaunch({
  101. url: `/pages/login/index`
  102. })
  103. }
  104. })
  105. },
  106. // 查询列表
  107. async search() {
  108. const that = this;
  109. let user = that.user;
  110. let info = {
  111. skip: that.skip,
  112. limit: that.limit,
  113. customer: user._id
  114. }
  115. const res = await that.$api(`/address`, 'GET', {
  116. ...info
  117. })
  118. if (res.errcode == '0') {
  119. let list = [...that.list, ...res.data];
  120. that.$set(that, `list`, list);
  121. that.$set(that, `total`, res.total)
  122. }
  123. },
  124. // 分页
  125. toPage() {
  126. const that = this;
  127. let list = that.list;
  128. let limit = that.limit;
  129. if (that.total > list.length) {
  130. uni.showLoading({
  131. title: '加载中',
  132. mask: true
  133. })
  134. let page = that.page + 1;
  135. that.$set(that, `page`, page)
  136. let skip = page * limit;
  137. that.$set(that, `skip`, skip)
  138. that.search();
  139. uni.hideLoading();
  140. } else that.$set(that, `is_bottom`, true)
  141. },
  142. toScroll(e) {
  143. const that = this;
  144. let up = that.scrollTop;
  145. that.$set(that, `scrollTop`, e.detail.scrollTop);
  146. let num = Math.sign(up - e.detail.scrollTop);
  147. if (num == 1) that.$set(that, `is_bottom`, false);
  148. },
  149. // 获取微信地址
  150. toWxaddress() {
  151. const that = this;
  152. let user = that.user;
  153. uni.getStorage({
  154. key: 'system',
  155. success: function(res) {
  156. if (res.data.uniPlatform == 'app') {
  157. uni.showToast({
  158. title: '请进入微信小程序进行获取微信地址',
  159. icon: 'none'
  160. })
  161. } else if (res.data.uniPlatform == 'mp-weixin') {
  162. uni.chooseAddress({
  163. success: async function(add) {
  164. let params = {
  165. customer: user._id,
  166. name: add.userName,
  167. phone: add.telNumber,
  168. province: add.provinceName,
  169. city: add.cityName,
  170. area: add.countyName,
  171. address: add.detailInfo
  172. }
  173. const arr = await that.$api(`/address`, 'POST', params);
  174. if (arr.errcode == '0') {
  175. uni.showToast({
  176. title: '添加收货地址成功',
  177. icon: 'none'
  178. })
  179. that.clearPage();
  180. that.search();
  181. } else {
  182. uni.showToast({
  183. title: arr.errmsg,
  184. icon: 'none'
  185. })
  186. }
  187. },
  188. fail: async function(err) {
  189. that.clearPage();
  190. that.search();
  191. }
  192. })
  193. }
  194. }
  195. })
  196. },
  197. // 设置默认
  198. toDefa(e) {
  199. const that = this;
  200. uni.showModal({
  201. title: '提示',
  202. content: '确定设置该地址为默认地址吗?',
  203. success: async function(res) {
  204. if (res.confirm) {
  205. const arr = await that.$api(`/address/toDefault/${e._id}`, `POST`);
  206. if (arr.errcode == '0') {
  207. uni.showToast({
  208. title: '添加默认成功',
  209. icon: 'none'
  210. })
  211. that.clearPage();
  212. that.search();
  213. } else {
  214. uni.showToast({
  215. title: arr.errmsg,
  216. icon: 'none'
  217. })
  218. }
  219. }
  220. }
  221. });
  222. },
  223. // 编辑
  224. toCommon(e) {
  225. const that = this;
  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`, 6)
  262. that.$set(that, `page`, 0)
  263. }
  264. },
  265. onPullDownRefresh: async function() {
  266. const that = this;
  267. that.$set(that, `list`, [])
  268. that.$set(that, `skip`, 0)
  269. that.$set(that, `limit`, 6)
  270. that.$set(that, `page`, 0)
  271. await that.search();
  272. uni.stopPullDownRefresh();
  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>