index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. that.clearPage()
  161. }
  162. })
  163. }
  164. }
  165. })
  166. },
  167. // 设置默认
  168. toDefa(e) {
  169. const that = this;
  170. uni.showModal({
  171. title: '提示',
  172. content: '确定设置该地址为默认地址吗?',
  173. success: async function(res) {
  174. if (res.confirm) {
  175. const arr = await that.$api(`/address/toDefault/${e._id}`, `POST`);
  176. if (arr.errcode == '0') {
  177. uni.showToast({
  178. title: '添加默认成功',
  179. icon: 'none'
  180. })
  181. that.clearPage();
  182. that.search();
  183. } else {
  184. uni.showToast({
  185. title: arr.errmsg,
  186. icon: 'none'
  187. })
  188. }
  189. }
  190. }
  191. });
  192. },
  193. // 编辑
  194. toCommon(e) {
  195. const that = this;
  196. that.clearPage();
  197. uni.navigateTo({
  198. url: `/pagesMy/address/add?id=${e&&e.id?e.id:''}`
  199. })
  200. },
  201. // 删除
  202. toDel(e) {
  203. const that = this;
  204. uni.showModal({
  205. title: '提示',
  206. content: '确定删除该地址吗?',
  207. success: async function(res) {
  208. if (res.confirm) {
  209. const arr = await that.$api(`/address/${e._id}`, 'DELETE');
  210. if (arr.errcode == '0') {
  211. uni.showToast({
  212. title: '删除信息成功',
  213. icon: 'none'
  214. })
  215. that.clearPage();
  216. that.search();
  217. } else {
  218. uni.showToast({
  219. title: arr.errmsg,
  220. icon: 'none'
  221. })
  222. }
  223. }
  224. }
  225. });
  226. },
  227. // 清空列表
  228. clearPage() {
  229. const that = this;
  230. that.$set(that, `list`, [])
  231. that.$set(that, `skip`, 0)
  232. that.$set(that, `limit`, 5)
  233. that.$set(that, `page`, 0)
  234. }
  235. }
  236. }
  237. </script>
  238. <style lang="scss">
  239. .main {
  240. display: flex;
  241. flex-direction: column;
  242. width: 100vw;
  243. height: 100vh;
  244. .one {
  245. position: relative;
  246. flex-grow: 1;
  247. background-color: var(--f5Color);
  248. .list {
  249. position: relative;
  250. background: var(--fffColor);
  251. padding: 2vw;
  252. width: 92vw;
  253. margin: 2vw 2vw 0 2vw;
  254. border-radius: 5px;
  255. .name {
  256. font-size: var(--font16Size);
  257. margin: 0 0 2vw 0;
  258. text {
  259. padding: 0 2vw 0 0;
  260. }
  261. }
  262. .address {
  263. font-size: var(--font14Size);
  264. margin: 0 0 1vw 0;
  265. text {
  266. padding: 0 2vw 0 0;
  267. }
  268. }
  269. .btn {
  270. display: flex;
  271. flex-direction: row;
  272. justify-content: space-around;
  273. border-top: 1px solid var(--font16Size);
  274. padding: 2vw 0 0 0;
  275. .btn_1 {
  276. button {
  277. width: 100%;
  278. color: var(--fffColor);
  279. background-color: var(--f35BColor);
  280. }
  281. }
  282. .btn_1:nth-child(2) {
  283. button {
  284. background-color: var(--f0fColor);
  285. }
  286. }
  287. .btn_1:last-child {
  288. button {
  289. background-color: var(--fFB1Color);
  290. }
  291. }
  292. }
  293. .default {
  294. position: absolute;
  295. top: 0;
  296. right: 0;
  297. text {
  298. background: var(--ff0Color);
  299. color: var(--fffColor);
  300. font-size: var(--font12Size);
  301. padding: 1vw;
  302. border-radius: 5px;
  303. }
  304. }
  305. }
  306. }
  307. .two {
  308. display: flex;
  309. flex-direction: row;
  310. justify-content: space-between;
  311. .two_1 {
  312. width: 50vw;
  313. button {
  314. width: 100%;
  315. color: var(--fffColor);
  316. background-color: var(--f08Color);
  317. border-radius: 0;
  318. }
  319. }
  320. .two_1:nth-child(2) {
  321. button {
  322. background-color: var(--fFB1Color);
  323. }
  324. }
  325. }
  326. }
  327. .scroll-view {
  328. position: absolute;
  329. top: 0;
  330. left: 0;
  331. right: 0;
  332. bottom: 0;
  333. .list-scroll-view {
  334. display: flex;
  335. flex-direction: column;
  336. }
  337. }
  338. </style>