博主信息
博文 51
粉丝 0
评论 1
访问量 87988
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Uni-app 微信登录获取用户code
鱼的熊掌
原创
3038人浏览过
  1. import { WxCode , WxLogin } from '@/api/user.js'
  2. WxCode({
  3. callback_url: 'http://m.test.com/#/pages/index/wxlogin' //回调地址
  4. }).then(data => {
  5. location.href=data;
  6. // console.log(data);
  7. })
  8. import { WxCode , WxLogin } from '@/api/user.js'
  9. export default {
  10. data() {
  11. return {
  12. code:''
  13. }
  14. },
  15. methods: {
  16. getUrlParam(name) {
  17. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  18. var r = window.location.search.substr(1).match(reg)
  19. if (r != null) return unescape(r[2])
  20. return null
  21. },
  22. getRequestParams(name) {
  23. let url = location.href;
  24. let requestParams = {};
  25. if (url.indexOf('?') !== -1) {
  26. let str = url.substr(url.indexOf('?') + 1); //截取?后面的内容作为字符串
  27. // console.log(str, '?后面的内容');
  28. let strs = str.split('&'); //将字符串内容以&分隔为一个数组
  29. // console.log(strs, '以&切割的数组');
  30. for (let i = 0; i < strs.length; i++) {
  31. requestParams[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
  32. // 将数组元素中'='左边的内容作为对象的属性名,'='右边的内容作为对象对应属性的属性值
  33. }
  34. }
  35. // console.log(requestParams, '处理后的对象');
  36. if(name){
  37. return requestParams[name];
  38. }else{
  39. return requestParams;
  40. }
  41. },
  42. wxlogin(){
  43. WxLogin({
  44. code: this.code
  45. }).then(data => {
  46. console.log(data);
  47. uni.setStorageSync('token', data.userinfo.token)
  48. uni.redirectTo({
  49. url:'/pages/evaluate/index'
  50. });
  51. })
  52. }
  53. },
  54. created(){
  55. // this.code=this.getUrlParam('cdoe');
  56. // console.log(this.code);
  57. },
  58. onLoad(options) {
  59. this.code=this.getUrlParam('code');
  60. console.log(this.code);
  61. this.wxlogin();
  62. }
  63. }
  1. 返回code 链接
  2. getUrlParam:m.test.com/?code=hZVwoVJCy8cIqBFTNEPmUvpG_csDDL7X9gE799M07PY&state=1#/pages/index/wxlogin
  1. getRequestParams:m.test.com/#/?state=1 getRequestParams('state');
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学