index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. uni.navigateTo({
  195. url: `/pagesMy/address/add?id=${e&&e.id?e.id:''}`
  196. })
  197. },
  198. // 删除
  199. toDel(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/${e._id}`, 'DELETE');
  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. clearPage() {
  226. const that = this;
  227. that.$set(that, `list`, [])
  228. that.$set(that, `skip`, 0)
  229. that.$set(that, `limit`, 5)
  230. that.$set(that, `page`, 0)
  231. }
  232. }
  233. }
  234. </script>
  235. <style lang="scss">
  236. .main {
  237. display: flex;
  238. flex-direction: column;
  239. width: 100vw;
  240. height: 100vh;
  241. .one {
  242. position: relative;
  243. flex-grow: 1;
  244. background-color: var(--f5Color);
  245. .list {
  246. position: relative;
  247. background: var(--fffColor);
  248. padding: 2vw;
  249. width: 92vw;
  250. margin: 2vw 2vw 0 2vw;
  251. border-radius: 5px;
  252. .name {
  253. font-size: var(--font16Size);
  254. margin: 0 0 2vw 0;
  255. text {
  256. padding: 0 2vw 0 0;
  257. }
  258. }
  259. .address {
  260. font-size: var(--font14Size);
  261. margin: 0 0 1vw 0;
  262. text {
  263. padding: 0 2vw 0 0;
  264. }
  265. }
  266. .btn {
  267. display: flex;
  268. flex-direction: row;
  269. justify-content: space-around;
  270. border-top: 1px solid var(--font16Size);
  271. padding: 2vw 0 0 0;
  272. .btn_1 {
  273. button {
  274. width: 100%;
  275. color: var(--fffColor);
  276. background-color: var(--f35BColor);
  277. }
  278. }
  279. .btn_1:nth-child(2) {
  280. button {
  281. background-color: var(--f0fColor);
  282. }
  283. }
  284. .btn_1:last-child {
  285. button {
  286. background-color: var(--fFB1Color);
  287. }
  288. }
  289. }
  290. .default {
  291. position: absolute;
  292. top: 0;
  293. right: 0;
  294. text {
  295. background: var(--ff0Color);
  296. color: var(--fffColor);
  297. font-size: var(--font12Size);
  298. padding: 1vw;
  299. border-radius: 5px;
  300. }
  301. }
  302. }
  303. }
  304. .two {
  305. display: flex;
  306. flex-direction: row;
  307. justify-content: space-between;
  308. .two_1 {
  309. width: 50vw;
  310. button {
  311. width: 100%;
  312. color: var(--fffColor);
  313. background-color: var(--f08Color);
  314. }
  315. }
  316. .two_1:nth-child(2) {
  317. button {
  318. background-color: var(--fFB1Color);
  319. }
  320. }
  321. }
  322. }
  323. .scroll-view {
  324. position: absolute;
  325. top: 0;
  326. left: 0;
  327. right: 0;
  328. bottom: 0;
  329. .list-scroll-view {
  330. display: flex;
  331. flex-direction: column;
  332. }
  333. }
  334. </style>