my.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <!-- 个人信息 -->
  3. <view class="page-container">
  4. <!-- 背景图片 -->
  5. <image class="bg-image " src="@/static/images/bg.png" mode="aspectFill"></image>
  6. <!-- 内容区域 -->
  7. <view class="content-area">
  8. <!-- 访客信息表单 -->
  9. <view class="visitor-form" @tap="goLogin">
  10. <view v-if="isLogin">
  11. <view class="form-item">
  12. <text class="label">姓名</text>
  13. <text class="value">{{user.nickName || '-'}}</text>
  14. </view>
  15. <view class="form-item">
  16. <text class="label">手机号</text>
  17. <text class="value">{{user.phonenumber || '-'}}</text>
  18. </view>
  19. <view class="form-item">
  20. <text class="label">公司名称</text>
  21. <text class="value">{{user.dept && user.dept.deptName || '-'}}</text>
  22. </view>
  23. <view class="form-item">
  24. <text class="label">部门</text>
  25. <text class="value">{{user.dept && user.dept.deptName || '-'}}</text>
  26. </view>
  27. </view>
  28. <view v-else class="login-container">
  29. <view class="login-prompt">
  30. <text class="login-title">欢迎使用知己访客</text>
  31. <text class="login-subtitle">登录后可使用更多功能</text>
  32. </view>
  33. <view class="login-button" @tap="goLogin">
  34. <text class="login-text">点击登录</text>
  35. <text class="login-arrow"></text>
  36. </view>
  37. </view>
  38. </view>
  39. <!-- <view v-if="isLogin" class="edit-button" @tap="goUserDetail">-->
  40. <!-- <text class="edit-text">编辑</text>-->
  41. <!-- </view>-->
  42. <!-- 退出登录按钮 -->
  43. <view v-if="isLogin" class="logout-button" @tap="handlerLogout">
  44. <text class="logout-text">退出登录</text>
  45. </view>
  46. <!-- 功能菜单 -->
  47. </view>
  48. <uni-popup ref="checkLoginDialog" type="dialog">
  49. <uni-popup-dialog type="warn" cancelText="继续浏览" confirmText="确认" title="提示" content="体验更多功能,请先登录"
  50. @confirm="goLogin()"></uni-popup-dialog>
  51. </uni-popup>
  52. </view>
  53. </template>
  54. <script>
  55. import {
  56. getUserInfo, wxLogin,
  57. } from '../../config/api';
  58. import {
  59. mapActions,
  60. mapGetters
  61. } from 'vuex';
  62. import store from '@/store';
  63. import customNavbar from '../../components/custom-navbar/custom-navbar.vue'
  64. import {
  65. shareImg
  66. } from '@/common/config.js'
  67. export default {
  68. data() {
  69. return {
  70. user: {}
  71. }
  72. },
  73. computed: {
  74. ...mapGetters(['isLogin', 'userInfo', 'access_token', 'refresh_token'])
  75. },
  76. onLoad() {
  77. },
  78. onShow() {
  79. this.getUserInfo()
  80. },
  81. // 微信小程序分享配置
  82. onShareAppMessage() {
  83. return {
  84. title: `邀请您加入知己访客`,
  85. path: `/pages/index/index`,
  86. imageUrl: shareImg // 分享图片,需要添加
  87. }
  88. },
  89. onShareTimeline(res) {
  90. let that = this;
  91. let shareInfo = store.state.vuex_shareInfo;
  92. let query = shareInfo.query;
  93. //携带当前页面资源ID参数
  94. let currentPage = getCurrentPages()[getCurrentPages().length - 1];
  95. let options = currentPage.options;
  96. if (JSON.stringify(options) != '{}' && options.id) {
  97. query += `&id=${options.id}`;
  98. }
  99. return {
  100. title: shareInfo.title,
  101. query: query,
  102. imageUrl: shareImg,
  103. success(res) {
  104. uni.showToast({
  105. title: '分享成功'
  106. })
  107. },
  108. fail(res) {
  109. uni.showToast({
  110. title: '分享失败',
  111. icon: 'none'
  112. })
  113. },
  114. complete() {}
  115. }
  116. },
  117. methods: {
  118. ...mapActions(['logout', 'setToken', 'setUserInfo']),
  119. // async detail() {
  120. // const data = await memberDetail()
  121. // if (data.code == 200) {
  122. // this.user = data.data
  123. // uni.setStorageSync('user', this.user)
  124. // }
  125. // },
  126. async getUserInfo() {
  127. const user = uni.getStorageSync('user');
  128. if (user) {
  129. this.user = user;
  130. }
  131. },
  132. // 登录处理
  133. async goLogin() {
  134. console.log(this.isLogin)
  135. if (!this.isLogin) {
  136. try {
  137. // 1. 尝试微信授权登录
  138. const loginRes = await new Promise((resolve, reject) => {
  139. uni.login({
  140. provider: 'weixin',
  141. success: (res) => {
  142. if (res.code) {
  143. resolve(res); // 成功获取 code
  144. } else {
  145. reject(new Error('未获取到微信授权码'));
  146. }
  147. },
  148. fail: (err) => {
  149. reject(new Error('微信授权失败'));
  150. }
  151. });
  152. });
  153. // 2. 调用微信登录接口
  154. const wxRes = await wxLogin({ code: loginRes.code });
  155. console.log('微信登录成功:', wxRes);
  156. // 3. 登录成功后的逻辑(如存储用户信息、跳转首页等)
  157. if (wxRes && wxRes.message === "登录成功"&& wxRes.user) {
  158. uni.setStorageSync('user', wxRes.data);
  159. if (wxRes.user) {
  160. uni.setStorageSync('user', wxRes.user);
  161. }
  162. // 更新store状态
  163. this.$store.commit('isLogin', true);
  164. this.isEmployeeLogin = true
  165. this.getUserInfo()
  166. // uni.switchTab({
  167. // url: '/pages/tabbar/home' // 替换为实际首页路径
  168. // });
  169. } else {
  170. throw new Error('微信登录接口返回异常');
  171. }
  172. } catch (error) {
  173. console.error('登录失败:', error);
  174. // 4. 微信授权或登录失败,跳转到手机号登录页
  175. uni.navigateTo({
  176. url: '/pagesA/public/phone-login'
  177. });
  178. }
  179. // uni.navigateTo({
  180. // url: '/pagesA/public/login'
  181. // });
  182. } else {
  183. this.$route('/pagesA/public/login');
  184. }
  185. },
  186. // 跳转用户信息
  187. goUserDetail() {
  188. uni.navigateTo({
  189. url: '/pagesA/user/index'
  190. });
  191. },
  192. // 退出登录
  193. handlerLogout() {
  194. uni.showModal({
  195. title: '提示',
  196. content: '确定要退出登录吗?',
  197. success: (res) => {
  198. if (res.confirm) {
  199. // 清除缓存
  200. uni.removeStorageSync('access_token');
  201. uni.removeStorageSync('user');
  202. // 调用vuex的logout方法
  203. this.logout();
  204. uni.showToast({
  205. title: '退出成功',
  206. icon: 'success'
  207. });
  208. // 刷新当前页面
  209. this.user = {};
  210. }
  211. }
  212. });
  213. }
  214. },
  215. }
  216. </script>
  217. <style>
  218. /* 页面容器 */
  219. .page-container {
  220. min-height: 100vh;
  221. background-color: #F5F5F5;
  222. }
  223. /* 背景图片 */
  224. .bg-image {
  225. top: 0;
  226. left: 0;
  227. width: 100%;
  228. height: 300rpx;
  229. z-index: 0;
  230. }
  231. /* 内容区域 */
  232. .content-area {
  233. position: relative;
  234. z-index: 1;
  235. padding: 32rpx;
  236. }
  237. /* 访客信息表单 */
  238. .visitor-form {
  239. background: #FFFFFF;
  240. border-radius: 24rpx;
  241. padding: 0 32rpx;
  242. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  243. }
  244. .form-item {
  245. display: flex;
  246. align-items: center;
  247. min-height: 120rpx;
  248. border-bottom: 1rpx solid #F5F5F5;
  249. }
  250. .form-item:last-child {
  251. border-bottom: none;
  252. }
  253. .label {
  254. width: 140rpx;
  255. font-size: 28rpx;
  256. color: #999999;
  257. }
  258. .value {
  259. flex: 1;
  260. font-size: 28rpx;
  261. color: #333333;
  262. }
  263. /* 未登录状态样式 */
  264. .login-container {
  265. padding: 40rpx 0;
  266. display: flex;
  267. flex-direction: column;
  268. align-items: center;
  269. justify-content: center;
  270. gap: 40rpx;
  271. }
  272. .login-prompt {
  273. display: flex;
  274. flex-direction: column;
  275. align-items: center;
  276. gap: 16rpx;
  277. }
  278. .login-title {
  279. font-size: 36rpx;
  280. color: #333333;
  281. font-weight: 600;
  282. }
  283. .login-subtitle {
  284. font-size: 28rpx;
  285. color: #999999;
  286. }
  287. .login-button {
  288. width: 100%;
  289. height: 88rpx;
  290. background: linear-gradient(90deg, #FF5B05 0%, #FF8E3C 100%);
  291. border-radius: 44rpx;
  292. display: flex;
  293. align-items: center;
  294. justify-content: center;
  295. gap: 8rpx;
  296. box-shadow: 0 4rpx 16rpx rgba(255, 91, 5, 0.2);
  297. }
  298. .login-text {
  299. color: #FFFFFF;
  300. font-size: 32rpx;
  301. font-weight: 500;
  302. }
  303. .login-arrow {
  304. color: #FFFFFF;
  305. font-size: 32rpx;
  306. font-weight: 500;
  307. margin-left: 8rpx;
  308. }
  309. .edit-button {
  310. margin: 80rpx 32rpx 0;
  311. height: 88rpx;
  312. display: flex;
  313. align-items: center;
  314. justify-content: center;
  315. border: 2rpx solid #FF5B05;
  316. border-radius: 44rpx;
  317. }
  318. .edit-text {
  319. color: #FF5B05;
  320. font-size: 32rpx;
  321. }
  322. .logout-button {
  323. margin: 30rpx 32rpx 0;
  324. height: 88rpx;
  325. display: flex;
  326. align-items: center;
  327. justify-content: center;
  328. background: #FF4748;
  329. border-radius: 44rpx;
  330. }
  331. .logout-text {
  332. color: #FFFFFF;
  333. font-size: 32rpx;
  334. }
  335. </style>