index.vue 8.4 KB

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