index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
  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>
  35. </scroll-view>
  36. </view>
  37. <view class="two">
  38. <view class="two_1">
  39. <button type="default" @click="toWxaddress()">获取微信地址</button>
  40. </view>
  41. <view class="two_1">
  42. <button type="default" @click="toCommon()">新增收货地址</button>
  43. </view>
  44. </view>
  45. </view>
  46. </mobile-frame>
  47. </template>
  48. <script>
  49. export default {
  50. data() {
  51. return {
  52. user: {},
  53. list: [],
  54. total: 0,
  55. skip: 0,
  56. limit: 5,
  57. page: 0
  58. };
  59. },
  60. onShow: function() {
  61. const that = this;
  62. that.watchLogin()
  63. },
  64. methods: {
  65. // 监听用户是否登录
  66. watchLogin() {
  67. const that = this;
  68. uni.getStorage({
  69. key: 'token',
  70. success: function(res) {
  71. let user = that.$jwt(res.data);
  72. that.$set(that, `user`, user);
  73. that.search()
  74. },
  75. fail: function(err) {
  76. uni.reLaunch({
  77. url: `/pages/login/index`
  78. })
  79. }
  80. })
  81. },
  82. // 查询列表
  83. async search() {
  84. const that = this;
  85. let user = that.user;
  86. let info = {
  87. skip: that.skip,
  88. limit: that.limit,
  89. customer: user._id
  90. }
  91. const res = await that.$api(`/address`, 'GET', {
  92. ...info
  93. })
  94. if (res.errcode == '0') {
  95. let list = [...that.list, ...res.data];
  96. that.$set(that, `list`, list);
  97. that.$set(that, `total`, res.total)
  98. }
  99. },
  100. // 分页
  101. toPage() {
  102. const that = this;
  103. let list = that.list;
  104. let limit = that.limit;
  105. if (that.total > list.length) {
  106. uni.showLoading({
  107. title: '加载中',
  108. mask: true
  109. })
  110. let page = that.page + 1;
  111. that.$set(that, `page`, page)
  112. let skip = page * limit;
  113. that.$set(that, `skip`, skip)
  114. that.search();
  115. uni.hideLoading();
  116. } else uni.showToast({
  117. title: '没有更多数据了'
  118. });
  119. },
  120. // 获取微信地址
  121. toWxaddress() {
  122. const that = this;
  123. let user = that.user;
  124. uni.getStorage({
  125. key: 'system',
  126. success: function(res) {
  127. if (res.data.uniPlatform == 'app') {
  128. uni.showToast({
  129. title: '请进入微信小程序进行获取微信地址',
  130. icon: 'none'
  131. })
  132. } else if (res.data.uniPlatform == 'mp-weixin') {
  133. uni.chooseAddress({
  134. success: async function(add) {
  135. let params = {
  136. customer: user._id,
  137. name: add.userName,
  138. phone: add.telNumber,
  139. province: add.provinceName,
  140. city: add.cityName,
  141. area: add.countyName,
  142. address: add.detailInfo
  143. }
  144. const arr = await that.$api(`/address`, 'POST', params);
  145. if (arr.errcode == '0') {
  146. uni.showToast({
  147. title: '添加收货地址成功',
  148. icon: 'none'
  149. })
  150. that.clearPage();
  151. } else {
  152. uni.showToast({
  153. title: arr.errmsg,
  154. icon: 'none'
  155. })
  156. }
  157. },
  158. fail: function(err) {
  159. console.log(err);
  160. }
  161. })
  162. }
  163. }
  164. })
  165. },
  166. // 设置默认
  167. toDefa(e) {
  168. const that = this;
  169. uni.showModal({
  170. title: '提示',
  171. content: '确定设置该地址为默认地址吗?',
  172. success: async function(res) {
  173. if (res.confirm) {
  174. const arr = await that.$api(`/address/toDefault/${e._id}`, `POST`);
  175. if (arr.errcode == '0') {
  176. uni.showToast({
  177. title: '添加默认成功',
  178. icon: 'none'
  179. })
  180. that.clearPage();
  181. that.search();
  182. } else {
  183. uni.showToast({
  184. title: arr.errmsg,
  185. icon: 'none'
  186. })
  187. }
  188. }
  189. }
  190. });
  191. },
  192. // 编辑
  193. toCommon(e) {
  194. const that = this;
  195. that.clearPage();
  196. uni.navigateTo({
  197. url: `/pagesMy/address/add?id=${e&&e.id?e.id:''}`
  198. })
  199. },
  200. // 删除
  201. toDel(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/${e._id}`, 'DELETE');
  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. clearPage() {
  228. const that = this;
  229. that.$set(that, `list`, [])
  230. that.$set(that, `skip`, 0)
  231. that.$set(that, `limit`, 5)
  232. that.$set(that, `page`, 0)
  233. }
  234. }
  235. }
  236. </script>
  237. <style lang="scss">
  238. .main {
  239. display: flex;
  240. flex-direction: column;
  241. width: 100vw;
  242. height: 100vh;
  243. .one {
  244. position: relative;
  245. flex-grow: 1;
  246. background-color: var(--f5Color);
  247. .list {
  248. position: relative;
  249. background: var(--fffColor);
  250. padding: 2vw;
  251. width: 92vw;
  252. margin: 2vw 2vw 0 2vw;
  253. border-radius: 5px;
  254. .name {
  255. font-size: var(--font16Size);
  256. margin: 0 0 2vw 0;
  257. text {
  258. padding: 0 2vw 0 0;
  259. }
  260. }
  261. .address {
  262. font-size: var(--font14Size);
  263. margin: 0 0 1vw 0;
  264. text {
  265. padding: 0 2vw 0 0;
  266. }
  267. }
  268. .btn {
  269. display: flex;
  270. flex-direction: row;
  271. justify-content: space-around;
  272. border-top: 1px solid var(--font16Size);
  273. padding: 2vw 0 0 0;
  274. .btn_1 {
  275. button {
  276. width: 100%;
  277. color: var(--fffColor);
  278. background-color: var(--f35BColor);
  279. }
  280. }
  281. .btn_1:nth-child(2) {
  282. button {
  283. background-color: var(--f0fColor);
  284. }
  285. }
  286. .btn_1:last-child {
  287. button {
  288. background-color: var(--fFB1Color);
  289. }
  290. }
  291. }
  292. .default {
  293. position: absolute;
  294. top: 0;
  295. right: 0;
  296. text {
  297. background: var(--ff0Color);
  298. color: var(--fffColor);
  299. font-size: var(--font12Size);
  300. padding: 1vw;
  301. border-radius: 5px;
  302. }
  303. }
  304. }
  305. }
  306. .two {
  307. display: flex;
  308. flex-direction: row;
  309. justify-content: space-between;
  310. .two_1 {
  311. width: 50vw;
  312. button {
  313. width: 100%;
  314. color: var(--fffColor);
  315. background-color: var(--f08Color);
  316. border-radius: 0;
  317. }
  318. }
  319. .two_1:nth-child(2) {
  320. button {
  321. background-color: var(--fFB1Color);
  322. }
  323. }
  324. }
  325. }
  326. .scroll-view {
  327. position: absolute;
  328. top: 0;
  329. left: 0;
  330. right: 0;
  331. bottom: 0;
  332. .list-scroll-view {
  333. display: flex;
  334. flex-direction: column;
  335. }
  336. }
  337. </style>