index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="index">
  3. <capsule :showBorder="true" bgColor="transparent">
  4. <template v-slot:top>
  5. <view class="detail-top">
  6. <view class="top-left" @click="openDrawer">
  7. <image class="" src="@/static/images/catalogue_icon.png" />
  8. </view>
  9. <view class="top-text">团蜂电子名片</view>
  10. </view>
  11. </template>
  12. </capsule>
  13. <view class="seat"></view>
  14. <view class="box-bg">
  15. <!-- <view class="bg"></view> -->
  16. </view>
  17. <view class="container">
  18. <view
  19. class="card-list"
  20. :style="{ height: cardListHeight + 'rpx' }"
  21. v-if="cardList.length > 0"
  22. >
  23. <view
  24. class="card-item"
  25. v-for="(item, index) in cardList"
  26. :key="index"
  27. :style="{ top: index * 120 + 'rpx', zIndex: index }"
  28. @click.stop="goCard('/pages_module/myCard/index?id=' + item.id)"
  29. >
  30. <cardCom :cardData="item" :imgSerial="item.style"></cardCom>
  31. </view>
  32. <!-- <view class="card-item" v-for="item in 3" :key="item" :style="{top: (item)*120 + 'rpx'}">
  33. <cardCom></cardCom>
  34. </view> -->
  35. </view>
  36. <view class="empty-box" v-else>
  37. <image src="@/static/images/empty-card.png" />
  38. <text>暂未添加名片</text>
  39. </view>
  40. <view class="card-btn">
  41. <button @click="addCard">+&nbsp; &nbsp;&nbsp;新增名片</button>
  42. </view>
  43. </view>
  44. <tui-drawer mode="left" :visible="visible" @close="closeDrawer">
  45. <view class="d-container">
  46. <view class="d-image">
  47. <template v-if="cardList[0].head">
  48. <image class="" :src="cardList[0].head" />
  49. </template>
  50. <template v-else>
  51. <image class="" src="@/static/images/default_avatar.png" />
  52. </template>
  53. <text>{{ cardList[0].name }}</text>
  54. </view>
  55. <view class="quit" @click="quitLogin">退出登录</view>
  56. </view>
  57. </tui-drawer>
  58. <tui-modal
  59. :show="modal"
  60. :button="[{ text: '确定', type: 'red', plain: false }]"
  61. @click="handleClick"
  62. @cancel="hideModal"
  63. title="提示"
  64. content="您还未登录,是否需要登录?"
  65. ></tui-modal>
  66. </view>
  67. </template>
  68. <script>
  69. import cardCom from "@/components/cardCom/index.vue";
  70. import { wxLoginApi, getCardListApi } from "@/api/index.js";
  71. export default {
  72. components: {
  73. cardCom,
  74. },
  75. onShow() {
  76. // 判断是否登录
  77. if (!this.$cache.getCache("token")) return;
  78. this.getCardList();
  79. },
  80. computed: {
  81. // 计算高度
  82. cardListHeight() {
  83. let num = 308 + this.cardList.length * 120;
  84. return num;
  85. },
  86. },
  87. data() {
  88. return {
  89. visible: false,
  90. top: null,
  91. modal: false,
  92. // 卡片列表
  93. cardList: [],
  94. };
  95. },
  96. methods: {
  97. // 获取当前用户所有名片
  98. async getCardList() {
  99. let { data } = await getCardListApi();
  100. this.cardList = data;
  101. },
  102. // 打开侧边弹框
  103. openDrawer() {
  104. if (!this.$cache.getCache("token")){
  105. this.modal = true;
  106. return
  107. }
  108. this.visible = true;
  109. },
  110. // 关闭侧边弹框
  111. closeDrawer() {
  112. this.visible = false;
  113. },
  114. // 新增卡片
  115. addCard() {
  116. // 获取本地 token
  117. const flag = this.$cache.getCache("token");
  118. if (!flag) {
  119. this.modal = true;
  120. return;
  121. }
  122. uni.navigateTo({
  123. url: "/pages_module/establish/index",
  124. });
  125. },
  126. // 退出登录
  127. quitLogin() {
  128. // #ifdef MP-WEIXIN
  129. wx.showModal({
  130. title: "提示",
  131. content: "是否退出当前登录的账号",
  132. success: async (res) => {
  133. if (res.confirm) {
  134. this.$cache.removeCache("token");
  135. this.cardList = [];
  136. this.visible = false;
  137. }
  138. },
  139. });
  140. // #endif
  141. },
  142. handleClick(e) {
  143. // 微信登录
  144. this.$loading.show("登录中...");
  145. // #ifdef MP-WEIXIN
  146. wx.login({
  147. success: async (res) => {
  148. let codeRes = await wxLoginApi({ code: res.code });
  149. // 获取名片列表
  150. this.getCardList();
  151. // 存储 token 到本地
  152. this.$cache.setCache("token", codeRes.data);
  153. this.modal = false;
  154. },
  155. complete: () => {
  156. this.$loading.hide();
  157. },
  158. });
  159. // #endif
  160. },
  161. hideModal() {
  162. this.modal = false;
  163. },
  164. goCard(url) {
  165. uni.navigateTo({
  166. url: url,
  167. });
  168. },
  169. },
  170. };
  171. </script>
  172. <style lang="scss" scoped>
  173. @import "./index.scss";
  174. </style>