personInfo.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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: 60, // 倒数秒数
  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. OpenPhoneDialog()
  170. {
  171. this.bindingPhoneData.showDialog = true;
  172. },
  173. //确定绑定手机号
  174. EnterBingdingPhone()
  175. {
  176. console.log("绑定手机号",this.bindingPhoneData);
  177. if(!this.bindingPhoneData.phone) {
  178. this.$message.warning('请输入手机号!')
  179. return
  180. }
  181. if(!this.bindingPhoneData.code){
  182. this.$message.warning('请输入验证码!')
  183. return
  184. }
  185. try {
  186. this.bindPhoneLoading = true
  187. this.$api.user.bindPhoneNumber({
  188. phone: this.bindingPhoneData.phone,
  189. verificationCode: this.bindingPhoneData.code,
  190. }).then(res => {
  191. if(res.code == 200) {
  192. this.$message.success(res.msg)
  193. this.$store.dispatch("user/getUserInfoByToken");//获取用户登录信息
  194. this.bindingPhoneData.showDialog = false
  195. this.bindingPhoneData.phone = ''
  196. this.bindingPhoneData.code = ''
  197. this.codeText = '获取验证码'
  198. this.isCount = false
  199. this.count = 60;
  200. if(this.userInfo.wxName)
  201. {
  202. }
  203. else
  204. {
  205. this.OpenWeChartDialog();
  206. }
  207. }
  208. else
  209. {
  210. this.$message.error(res.msg)
  211. }
  212. })
  213. this.bindPhoneLoading = false
  214. } catch {
  215. this.bindPhoneLoading = false
  216. }
  217. },
  218. //关闭滑块验证码弹窗
  219. CloseSlideCode(sliderVerified,phoneNumber)
  220. {
  221. console.log("关闭滑动验证码弹窗",sliderVerified);
  222. this.sliderVerified=sliderVerified;
  223. if(sliderVerified==1)
  224. {
  225. //绑定手机验证
  226. this.StartCountdown();//启动倒计时
  227. }
  228. this.showSlideCode=false;
  229. },
  230. //h获取滑动验证码返回
  231. ObtainSliderImg()
  232. {
  233. let param={
  234. sliderUUID:this.sliderUUID,//图片唯一凭证
  235. };
  236. this.$api.user.obtainSliderImg(param).then(res => {
  237. console.log("打印重新获取滑动验证码结果", res);
  238. if(res.code == 200)
  239. {
  240. this.sliderUUID = res.data.sliderUUID
  241. this.sliderBgUrl = res.data.backgroundBase64;
  242. this.sliderImgUrl = res.data.blockBase64;
  243. this.sliderPositionY = res.data.y;//验证码的y坐标
  244. }
  245. else
  246. {
  247. this.$message.error(res.msg);
  248. }
  249. })
  250. },
  251. //启动全局倒计时定时器
  252. StartCountdown()
  253. {
  254. // 先清除已存在的定时器
  255. if (this.timer) {
  256. clearInterval(this.timer);
  257. this.timer = null;
  258. }
  259. this.isCount = true;
  260. this.count = 300;
  261. this.codeText = '300 秒';
  262. this.timer = setInterval(() => {
  263. this.count--;
  264. this.codeText = `${this.count} 秒`;
  265. if (this.count <= 0) {
  266. this.StopCountdown();
  267. }
  268. }, 1000);
  269. },
  270. //停止全局倒计时定时器
  271. StopCountdown()
  272. {
  273. if (this.timer) {
  274. clearInterval(this.timer);
  275. this.timer = null;
  276. }
  277. this.isCount = false;
  278. this.count = 300;
  279. this.codeText = '重新获取';
  280. },
  281. // 获取手机验证码
  282. GetCode()
  283. {
  284. // 如果已经有定时器在运行,先清除它,防止叠加
  285. if (this.timer) {
  286. clearInterval(this.timer);
  287. this.timer = null;
  288. }
  289. if(!this.bindingPhoneData.phone) {
  290. this.$message.warning('请输入手机号码');
  291. return
  292. }
  293. // 验证手机号格式和位数
  294. const phoneRegex = /^1[3-9]\d{9}$/;
  295. if (!phoneRegex.test(this.bindingPhoneData.phone)) {
  296. this.$message.warning('请输入正确的手机号码');
  297. return;
  298. }
  299. if(this.isCount) return
  300. this.$api.user.getBindPhoneSlider({
  301. phoneNumber: this.bindingPhoneData.phone
  302. }).then(res => {
  303. if(res.code == 200)
  304. {
  305. this.sliderUUID = res.data.sliderUUID;
  306. this.sliderBgUrl = res.data.backgroundBase64;
  307. this.sliderImgUrl = res.data.blockBase64;
  308. this.sliderPositionY=res.data.y;
  309. this.showSlideCode=true;
  310. }else {
  311. this.$message.error(res.msg)
  312. }
  313. })
  314. },
  315. //打开绑定微信弹窗
  316. OpenWeChartDialog()
  317. {
  318. this.showWeChartDialog = true;
  319. try {
  320. this.qrLoading = true
  321. this.$api.user.getWechatCode().then(res => {
  322. if(res.code == 200 && res.data) {
  323. this.wechatUrl = res.data
  324. if(window.WxLogin) {
  325. this.RenderWechatQRCode()
  326. }
  327. }
  328. })
  329. this.qrLoading = false
  330. } catch {
  331. this.qrLoading = false
  332. }
  333. },
  334. // 动态加载微信SDK
  335. LoadSdk() {
  336. const script = document.createElement('script');
  337. script.src = 'https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js';
  338. document.body.appendChild(script);
  339. },
  340. // 微信二维码内置到弹窗内
  341. RenderWechatQRCode() {
  342. const container = document.getElementById("wechat-qr-container");
  343. console.log(container)
  344. if (!container || container.children.length > 0) return; // 防止重复渲染
  345. const queryString = this.wechatUrl.split('?')[1]?.split('#')[0];
  346. const urlParams = new URLSearchParams(queryString)
  347. const appid = urlParams.get('appid')
  348. new window.WxLogin({
  349. self_redirect: true,
  350. id: "wechat-qr-container", // 容器 ID
  351. appid: appid, // 替换为你的微信开放平台 AppID
  352. scope: "snsapi_login", // 授权作用域
  353. redirect_uri: encodeURIComponent(process.env.VUE_APP_BASE+"/#/wechatLogin"), // 回调 URL
  354. state: "STATE", // 可选状态值
  355. style: "black", // 样式风格
  356. stylelite: 1 , // 新版二维码样式
  357. });
  358. // 定时调整 iframe 大小
  359. const resizeInterval = setInterval(() => {
  360. const iframe = container.querySelector('iframe');
  361. if (iframe) {
  362. iframe.style.width = '250px';
  363. iframe.style.height = '160px';
  364. iframe.style.border = 'none';
  365. clearInterval(resizeInterval);
  366. }
  367. }, 100);
  368. // 设置超时防止无限循环
  369. setTimeout(() => {
  370. clearInterval(resizeInterval);
  371. }, 3000);
  372. },
  373. // 扫码绑定微信
  374. WechatBind(event) {
  375. let param = event.data
  376. if (param.type === 'wechatLoginSuccess') {
  377. this.wechatCode = param.data.code
  378. this.$api.user.bindWechat({
  379. code: param.data.code
  380. }).then(res => {
  381. if(res.code == 200) {
  382. this.$message.success('绑定成功!')
  383. this.$store.dispatch("user/getUserInfoByToken");//获取用户登录信息
  384. this.showWeChartDialog = false
  385. }
  386. })
  387. }
  388. },
  389. // 解除微信绑定
  390. WechatUnBind() {
  391. this.$confirm('确认解绑微信?', '提示', {
  392. type: 'warning',
  393. confirmButtonText: '确定',
  394. cancelButtonText: '取消',
  395. customClass: "page_dialog",
  396. }).then(() => {
  397. this.$api.user.unBindWechat({}).then(res => {
  398. if(res.code == 200) {
  399. this.$message.success(res.msg)
  400. this.$store.dispatch("user/getUserInfoByToken");//获取用户登录信息
  401. }
  402. })
  403. })
  404. },
  405. CloseWeChartDialog() {
  406. console.log("关闭微信弹窗")
  407. const container = document.getElementById("wechat-qr-container");
  408. if (container) {
  409. container.innerHTML = ''; // 清空容器内容
  410. }
  411. window.removeEventListener('message', this.WechatBind);
  412. }
  413. },
  414. };
  415. </script>
  416. <style lang="scss" scoped>
  417. .wechart_img
  418. {
  419. margin: auto;
  420. display: flex;
  421. justify-content: center;
  422. align-items: center;
  423. height: 40px;
  424. img
  425. {
  426. width: 32px;
  427. height: 32px;
  428. }
  429. .wechar_text
  430. {
  431. text-align: center;
  432. margin-left: 5px;
  433. }
  434. }
  435. #wechat-qr-container {
  436. display: flex;
  437. justify-content: center;
  438. align-items: center;
  439. width: 100%;
  440. min-height: 160px;
  441. iframe {
  442. width: 250px !important;
  443. height: 250px !important;
  444. border: none;
  445. }
  446. }
  447. </style>