tabbar_common.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view class="">
  3. <u-tabbar :value="initIndex" @click="handleLogin" @change="onTabChange" :fixed="true" :placeholder="true"
  4. :safeAreaInsetBottom="true" class="custom-tabbar" activeColor="#d81e06" inactiveColor="#fff">
  5. <u-tabbar-item v-for="(item, index) in items" :key="index" :text="item.text"
  6. :icon="require(`@/static/images/tabbar/${getDirectoryName()}/${item.icon}`)" />
  7. </u-tabbar>
  8. <u-popup :show="showTabbar" mode="bottom" class="popup-view" @close="close" @open="open">
  9. <view style="margin: 20px">
  10. <view class="login-text">
  11. {{getTextByDirName()}}
  12. </view>
  13. <button class="login-btn" open-type="getPhoneNumber" @getphonenumber="handlePhone">
  14. 授权登录
  15. </button>
  16. </view>
  17. </u-popup>
  18. </view>
  19. </template>
  20. <script>
  21. import {
  22. getUserInfo
  23. } from '@/api/login'
  24. export default {
  25. data() {
  26. return {
  27. dirName: '',
  28. showTabbar: false,
  29. loginDate: {
  30. jsCode: '',
  31. encryptedData: '',
  32. code: '',
  33. iv: ''
  34. },
  35. // 选中的 tab 索引
  36. initIndex: 4,
  37. selectedIndex: 4,
  38. // 自定义 tab 数据
  39. items: [
  40. {
  41. text: '溯源信息',
  42. icon: 'traceability.png'
  43. },
  44. {
  45. text: '意见反馈',
  46. icon: 'opinion.png'
  47. },
  48. {
  49. text: '公司简介',
  50. icon: 'company.png'
  51. },
  52. {
  53. text: '个人中心',
  54. icon: 'my.png'
  55. },
  56. ]
  57. };
  58. },
  59. created() {
  60. // // 监听事件
  61. // uni.$on('updateSelectedIndex', (index) => {
  62. // this.selectedIndex = index;
  63. // });
  64. },
  65. methods: {
  66. open() {
  67. // console.log('open');
  68. },
  69. close() {
  70. this.showTabbar = false
  71. // console.log('close');
  72. },
  73. onTabChange(index) {
  74. //判断是否已经授权
  75. // if (uni.getStorageSync('Authorization-status')) {
  76. this.handleTabSwitch(index);
  77. // return;
  78. // }
  79. // this.handleLogin();
  80. // this.showTabbar = true;
  81. // this.selectedIndex = index;
  82. return;
  83. },
  84. async handleLogin() {
  85. const [loginRes, profileRes] = await Promise.all([
  86. uni.login({
  87. provider: 'weixin'
  88. }),
  89. uni.getUserProfile({
  90. desc: '登录需要'
  91. })
  92. ])
  93. this.loginDate.iv = profileRes.iv;
  94. this.loginDate.encryptedData = profileRes.encryptedData;
  95. this.loginDate.jsCode = loginRes.code;
  96. },
  97. handlePhone(e) {
  98. if (e.detail.code) {
  99. this.loginDate.code = e.detail.code;
  100. // this.loginDate.iv = e.detail.iv;
  101. // this.loginDate.encryptedData = e.detail.encryptedData;
  102. getUserInfo(this.loginDate).then(res => {
  103. if (res.code === 0) {
  104. uni.setStorageSync('Authorization-status', true);
  105. this.showTabbar = false;
  106. this.handleTabSwitch(this.selectedIndex);
  107. } else {
  108. }
  109. })
  110. }
  111. },
  112. handleTabSwitch(index) {
  113. let dirName = this.getDirectoryName();
  114. let url = '';
  115. switch (index) {
  116. case 0:
  117. url = '/pages/'+dirName+'/traceability/traceability';
  118. break;
  119. case 1:
  120. url = '/pages/'+dirName+'/feedback/feedback';
  121. break;
  122. case 2:
  123. url = '/pages/'+dirName+'/introduction/introduction';
  124. break;
  125. case 3:
  126. url = '/pages/'+dirName+'/my/my';
  127. break;
  128. default:
  129. break;
  130. }
  131. uni.navigateTo({
  132. url: url
  133. });
  134. },
  135. getDirectoryName() {
  136. if (this.dirName !== ''){
  137. return this.dirName;
  138. }
  139. const pages = getCurrentPages();
  140. console.log(pages);
  141. if (pages.length > 0) {
  142. const currentPage = pages[pages.length - 1];
  143. const route = currentPage.route;
  144. const pathParts = route.split('/');
  145. if (pathParts.length >= 2) {
  146. this.dirName = pathParts[1];
  147. return pathParts[1];
  148. }
  149. }
  150. return 'cjx'; // 默认值
  151. },
  152. getTextByDirName(){
  153. switch (this.dirName) {
  154. case 'cjx':
  155. return '超吉炫';
  156. case 'lgz':
  157. return '梅山古镇';
  158. case 'qilang':
  159. return '七榔';
  160. default:
  161. return '超吉炫';
  162. }
  163. }
  164. }
  165. };
  166. </script>
  167. <style scoped>
  168. /* 修复内容区域样式 */
  169. ::v-deep .u-tabbar__content {
  170. background-color: transparent !important;
  171. box-shadow: none !important;
  172. height: 140rpx;
  173. border: none;
  174. }
  175. /* 修复图标尺寸 - 精确选择器路径 */
  176. ::v-deep .custom-tabbar .u-tabbar-item .u-tabbar-item__icon .u-icon__img {
  177. width: 80rpx !important;
  178. height: 80rpx !important;
  179. }
  180. .login-text {
  181. text-align: center;
  182. font-size: 18px;
  183. font-weight: 800;
  184. margin-bottom: 10px;
  185. }
  186. .login-btn {
  187. border-radius: 22px;
  188. border: none !important;
  189. background-color: #5ac725;
  190. color: white;
  191. font-size: 15px;
  192. margin-bottom: 15px;
  193. }
  194. </style>