personInfo.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <template>
  2. <div class="page_user">
  3. <div class="page_jg_20"></div>
  4. <div class="user_title">
  5. <span >我的资料</span>
  6. </div>
  7. <div class="user_module">
  8. <div class="user_img">
  9. <el-avatar
  10. shape="circle"
  11. :size="80"
  12. :src="headImage"
  13. :key="headImage"
  14. >
  15. 头像
  16. </el-avatar>
  17. </div>
  18. <div class="user_info">
  19. <div class="info_item" >
  20. <div class="item_row">
  21. <span class="span_label">账号:</span>
  22. <span class="span_value">{{ userInfo.loginName }}</span>
  23. </div>
  24. <div class="item_row">
  25. <span class="span_label">姓名:</span>
  26. <span class="span_value">{{ userInfo.userName }}</span>
  27. </div>
  28. </div>
  29. <div class="info_item">
  30. <div class="item_row">
  31. <span class="span_label">学校:</span>
  32. <span class="span_value">{{ userInfo.schoolName }}</span>
  33. </div>
  34. <div class="item_row">
  35. <span class="span_label">班级:</span>
  36. <span class="span_value">{{roleNames || '-'}}</span>
  37. </div>
  38. </div>
  39. <div class="info_item">
  40. <div class="item_row">
  41. <span class="span_label">手机号:</span>
  42. <span class="span_value">{{userInfo.phone || '未绑定'}}</span>
  43. <span class="span_blue" @click="OpenPhoneDialog" v-if="!userInfo.phone">绑定手机号</span>
  44. <span class="span_blue" @click="OpenPhoneDialog" v-else>更换手机号</span>
  45. </div>
  46. <div class="item_row">
  47. <span class="span_label">微信:</span>
  48. <span class="span_value">{{ userInfo.wxName || '未绑定'}}</span>
  49. <span class="span_blue" v-if="userInfo.wxName" @click="WechatUnBind">解除绑定</span>
  50. <span class="span_blue" v-else @click="OpenWeChartDialog">绑定微信</span>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. <div class="page_jg_20"></div>
  56. <div class="page_dialog">
  57. <el-dialog title="绑定手机号(用于找回密码)" center :visible.sync="bindingPhoneData.showDialog" width="400px">
  58. <div class="dialog_center padding_20">
  59. <div class="bingding_center">
  60. <div class="bingding_input">
  61. <el-input v-model="bindingPhoneData.phone" placeholder="请输入手机号" maxlength="11" prefix-icon="iconfont icon_shoujihao" />
  62. </div>
  63. <div class="bingding_input">
  64. <el-input v-model="bindingPhoneData.code" placeholder="请输入验证码" prefix-icon="iconfont icon_yanzhengma">
  65. <template slot="suffix">
  66. <span :class="isCount?'code_gray':'get_code'" @click="GetCode">{{ codeText }}</span>
  67. </template>
  68. </el-input>
  69. </div>
  70. <div class="bingding_button" :disabled="!bindingPhoneData.phone || !bindingPhoneData.code" @click="EnterBingdingPhone" v-loading="bindPhoneLoading">绑定手机号</div>
  71. </div>
  72. </div>
  73. </el-dialog>
  74. <el-dialog title="绑定微信(快捷登录)" center :visible.sync="showWeChartDialog" width="400px" @close="CloseWeChartDialog">
  75. <div class="dialog_center padding_20">
  76. <div class="bingding_center wechar_qr" id="wechat-qr-container" v-loading="qrLoading"></div>
  77. <div class="wechart_img">
  78. <img src="@/assets/login/login_wechat.webp" alt="" />
  79. <p class="wechar_text">微信扫描</p>
  80. </div>
  81. </div>
  82. </el-dialog>
  83. <SlideCode v-model="showSlideCode" title="安全验证" :sliderUUID="sliderUUID" :sliderBgUrl="sliderBgUrl" :sliderPositionY="sliderPositionY" :sliderImgUrl="sliderImgUrl" @close="CloseSlideCode" @refresh="ObtainSliderImg" />
  84. </div>
  85. </div>
  86. </template>
  87. <script>
  88. import { mapGetters } from "vuex";
  89. import heada_Image from "../../assets/user_img.png";
  90. import SlideCode from "@/views/login/components/SlideCode.vue";//滑动验证码组件
  91. export default {
  92. computed: {
  93. ...mapGetters(["userInfo"]),
  94. },
  95. components: {
  96. SlideCode
  97. },
  98. data() {
  99. return {
  100. // userInfo: {},
  101. tableData: [],
  102. teacherClassData: [],
  103. teacherManaData: [],
  104. checkedRenKe: true,//是否有效任课信息
  105. checkedMana: true,//是否有效管理权限
  106. rowsData: {},
  107. bindEmailData: { dialogShow: false },
  108. headImage: heada_Image,
  109. roleNames:'',//用户角色
  110. userWidth:'100px',
  111. showWeChartDialog:false,//是否显示微信绑定弹窗
  112. bindingPhoneData: {
  113. showDialog: false,
  114. phone: "",//手机号
  115. code: "",//验证码
  116. },
  117. bindPhoneLoading: false, // 绑定手机号loading
  118. codeText: '获取验证码', // 获取验证码按钮文字
  119. count: 300, // 倒数秒数
  120. isCount: false, // 是否倒计时
  121. wechatUrl: '', // 获取微信二维码返回地址
  122. qrLoading: false, // 二维码加载loading
  123. wechatCode: '', // 扫码获取code
  124. timer:null,//定时器
  125. showSlideCode:false,//是否显示滑动验证码
  126. sliderUUID:'',//图片唯一凭证
  127. sliderBgUrl:'',//画框背景图片
  128. sliderImgUrl:'',//滑块图片
  129. sliderPositionY:0,//滑块y轴位置
  130. };
  131. },
  132. mounted() {
  133. console.log("打印userInfo",this.userInfo);
  134. if(this.userInfo.roleNames)
  135. {
  136. this.roleNames=this.userInfo.roleNames.join("、");
  137. }
  138. //判断是否绑定了手机号
  139. if(this.userInfo.phone)
  140. {
  141. }
  142. else
  143. {
  144. this.OpenPhoneDialog();
  145. }
  146. this.LoadSdk() //加载微信sdk
  147. // 监听微信中间页面传递参数
  148. window.addEventListener('message', this.WechatBind);
  149. },
  150. beforeDestroy() {
  151. window.removeEventListener('message', this.WechatBind);
  152. if (this.timer) {
  153. clearInterval(this.timer);
  154. this.timer = null;
  155. }
  156. // 【新增】清理微信二维码定时器
  157. if (this.resizeTimer) {
  158. clearInterval(this.resizeTimer);
  159. this.resizeTimer = null;
  160. }
  161. // 清理微信 DOM
  162. const container = document.getElementById("wechat-qr-container");
  163. if (container) {
  164. container.innerHTML = '';
  165. }
  166. },
  167. methods: {
  168. //获取用户信息
  169. GetUserInfo()
  170. {
  171. this.$store.dispatch('user/getUserInfoByToken').then(user => {
  172. console.log("打印userInfo",user);
  173. }) //获取用户登录信息
  174. },
  175. //打开绑定手机号弹窗
  176. OpenPhoneDialog()
  177. {
  178. // 先清除已存在的定时器
  179. if (this.timer) {
  180. clearInterval(this.timer);
  181. this.timer = null;
  182. this.codeText = '获取验证码'
  183. this.isCount = false
  184. this.count = 300;
  185. }
  186. this.bindingPhoneData.showDialog = true;
  187. },
  188. //确定绑定手机号
  189. EnterBingdingPhone()
  190. {
  191. console.log("绑定手机号",this.bindingPhoneData);
  192. if(!this.bindingPhoneData.phone) {
  193. this.$message.warning('请输入手机号!')
  194. return
  195. }
  196. if(!this.bindingPhoneData.code){
  197. this.$message.warning('请输入验证码!')
  198. return
  199. }
  200. try {
  201. this.bindPhoneLoading = true
  202. this.$api.user.bindPhoneNumber({
  203. phone: this.bindingPhoneData.phone,
  204. verificationCode: this.bindingPhoneData.code,
  205. }).then(res => {
  206. if(res.code == 200) {
  207. this.$message.success(res.msg)
  208. this.$store.dispatch("user/getUserInfoByToken");//获取用户登录信息
  209. this.bindingPhoneData.showDialog = false
  210. this.bindingPhoneData.phone = ''
  211. this.bindingPhoneData.code = ''
  212. this.codeText = '获取验证码'
  213. this.isCount = false
  214. this.count = 300;
  215. if(this.userInfo.wxName)
  216. {
  217. }
  218. else
  219. {
  220. this.OpenWeChartDialog();
  221. }
  222. }
  223. else
  224. {
  225. this.$message.error(res.msg)
  226. }
  227. })
  228. this.bindPhoneLoading = false
  229. } catch {
  230. this.bindPhoneLoading = false
  231. }
  232. },
  233. //关闭滑块验证码弹窗
  234. CloseSlideCode(sliderVerified,phoneNumber)
  235. {
  236. console.log("关闭滑动验证码弹窗",sliderVerified);
  237. this.sliderVerified=sliderVerified;
  238. if(sliderVerified==1)
  239. {
  240. //绑定手机验证
  241. this.StartCountdown();//启动倒计时
  242. }
  243. this.showSlideCode=false;
  244. },
  245. //h获取滑动验证码返回
  246. ObtainSliderImg()
  247. {
  248. let param={
  249. sliderUUID:this.sliderUUID,//图片唯一凭证
  250. };
  251. this.$api.user.obtainSliderImg(param).then(res => {
  252. console.log("打印重新获取滑动验证码结果", res);
  253. if(res.code == 200)
  254. {
  255. this.sliderUUID = res.data.sliderUUID
  256. this.sliderBgUrl = res.data.backgroundBase64;
  257. this.sliderImgUrl = res.data.blockBase64;
  258. this.sliderPositionY = res.data.y;//验证码的y坐标
  259. }
  260. else
  261. {
  262. this.$message.error(res.msg);
  263. }
  264. })
  265. },
  266. //启动全局倒计时定时器
  267. StartCountdown()
  268. {
  269. // 先清除已存在的定时器
  270. if (this.timer) {
  271. clearInterval(this.timer);
  272. this.timer = null;
  273. }
  274. this.isCount = true;
  275. this.count = 300;
  276. this.codeText = '300 秒';
  277. this.timer = setInterval(() => {
  278. this.count--;
  279. this.codeText = `${this.count} 秒`;
  280. if (this.count <= 0) {
  281. this.StopCountdown();
  282. }
  283. }, 1000);
  284. },
  285. //停止全局倒计时定时器
  286. StopCountdown()
  287. {
  288. if (this.timer) {
  289. clearInterval(this.timer);
  290. this.timer = null;
  291. }
  292. this.isCount = false;
  293. this.count = 300;
  294. this.codeText = '重新获取';
  295. },
  296. // 获取手机验证码
  297. GetCode()
  298. {
  299. // 如果已经有定时器在运行,先清除它,防止叠加
  300. if (this.timer) {
  301. clearInterval(this.timer);
  302. this.timer = null;
  303. }
  304. if(!this.bindingPhoneData.phone) {
  305. this.$message.warning('请输入手机号码');
  306. return
  307. }
  308. // 验证手机号格式和位数
  309. const phoneRegex = /^1[3-9]\d{9}$/;
  310. if (!phoneRegex.test(this.bindingPhoneData.phone)) {
  311. this.$message.warning('请输入正确的手机号码');
  312. return;
  313. }
  314. if(this.isCount) return
  315. this.$api.user.getBindPhoneSlider({
  316. phoneNumber: this.bindingPhoneData.phone
  317. }).then(res => {
  318. if(res.code == 200)
  319. {
  320. this.sliderUUID = res.data.sliderUUID;
  321. this.sliderBgUrl = res.data.backgroundBase64;
  322. this.sliderImgUrl = res.data.blockBase64;
  323. this.sliderPositionY=res.data.y;
  324. this.showSlideCode=true;
  325. }else {
  326. this.$message.error(res.msg)
  327. }
  328. })
  329. },
  330. //打开绑定微信弹窗
  331. OpenWeChartDialog()
  332. {
  333. this.showWeChartDialog = true;
  334. try {
  335. this.qrLoading = true
  336. this.$api.user.getWechatCode().then(res => {
  337. if(res.code == 200 && res.data) {
  338. this.wechatUrl = res.data
  339. if(window.WxLogin) {
  340. this.RenderWechatQRCode()
  341. }
  342. }
  343. })
  344. this.qrLoading = false
  345. } catch {
  346. this.qrLoading = false
  347. }
  348. },
  349. // 动态加载微信SDK
  350. LoadSdk() {
  351. const script = document.createElement('script');
  352. script.src = 'https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js';
  353. document.body.appendChild(script);
  354. },
  355. // 微信二维码内置到弹窗内
  356. RenderWechatQRCode() {
  357. const container = document.getElementById("wechat-qr-container");
  358. console.log(container)
  359. if (!container || container.children.length > 0) return; // 防止重复渲染
  360. const queryString = this.wechatUrl.split('?')[1]?.split('#')[0];
  361. const urlParams = new URLSearchParams(queryString)
  362. const appid = urlParams.get('appid')
  363. new window.WxLogin({
  364. self_redirect: true,
  365. id: "wechat-qr-container", // 容器 ID
  366. appid: appid, // 替换为你的微信开放平台 AppID
  367. scope: "snsapi_login", // 授权作用域
  368. redirect_uri: encodeURIComponent(process.env.VUE_APP_BASE+"/#/wechatLogin"), // 回调 URL
  369. state: "STATE", // 可选状态值
  370. style: "black", // 样式风格
  371. stylelite: 1 , // 新版二维码样式
  372. });
  373. // 定时调整 iframe 大小
  374. const resizeInterval = setInterval(() => {
  375. const iframe = container.querySelector('iframe');
  376. if (iframe) {
  377. iframe.style.width = '250px';
  378. iframe.style.height = '160px';
  379. iframe.style.border = 'none';
  380. clearInterval(resizeInterval);
  381. }
  382. }, 100);
  383. // 设置超时防止无限循环
  384. setTimeout(() => {
  385. clearInterval(resizeInterval);
  386. }, 3000);
  387. },
  388. // 扫码绑定微信
  389. WechatBind(event) {
  390. let param = event.data
  391. if (param.type === 'wechatLoginSuccess') {
  392. this.wechatCode = param.data.code
  393. this.$api.user.bindWechat({
  394. code: param.data.code
  395. }).then(res => {
  396. if(res.code == 200) {
  397. this.$message.success('绑定成功!')
  398. this.$store.dispatch("user/getUserInfoByToken");//获取用户登录信息
  399. this.showWeChartDialog = false
  400. }
  401. })
  402. }
  403. },
  404. // 解除微信绑定
  405. WechatUnBind() {
  406. this.$confirm('确认解绑微信?', '提示', {
  407. type: 'warning',
  408. confirmButtonText: '确定',
  409. cancelButtonText: '取消',
  410. customClass: "page_dialog",
  411. }).then(() => {
  412. this.$api.user.unBindWechat({}).then(res => {
  413. if(res.code == 200) {
  414. this.$message.success(res.msg)
  415. this.$store.dispatch("user/getUserInfoByToken");//获取用户登录信息
  416. }
  417. })
  418. })
  419. },
  420. CloseWeChartDialog() {
  421. console.log("关闭微信弹窗")
  422. const container = document.getElementById("wechat-qr-container");
  423. if (container) {
  424. container.innerHTML = ''; // 清空容器内容
  425. }
  426. window.removeEventListener('message', this.WechatBind);
  427. }
  428. },
  429. };
  430. </script>
  431. <style lang="scss" scoped>
  432. .wechart_img
  433. {
  434. margin: auto;
  435. display: flex;
  436. justify-content: center;
  437. align-items: center;
  438. height: 40px;
  439. img
  440. {
  441. width: 32px;
  442. height: 32px;
  443. }
  444. .wechar_text
  445. {
  446. text-align: center;
  447. margin-left: 5px;
  448. }
  449. }
  450. #wechat-qr-container {
  451. display: flex;
  452. justify-content: center;
  453. align-items: center;
  454. width: 100%;
  455. min-height: 160px;
  456. iframe {
  457. width: 250px !important;
  458. height: 250px !important;
  459. border: none;
  460. }
  461. }
  462. </style>