authWeb.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // pages/testQQ/testQQ.js
  2. import md5 from "./md5";
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. nvabarData: {
  9. showCapsule: 1,
  10. title: '授权页',
  11. callback: () => {
  12. console.log("返回界面3:");
  13. }
  14. },
  15. // qrcodeUrl: "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20250328100356565128956.png",
  16. qrcodeUrl: "",
  17. },
  18. _goBack() {
  19. console.log("返回界面5", constant.routePath.index);
  20. this.callback();
  21. },
  22. callback() {
  23. // 重置小程序界面到首页
  24. console.log("返回界面4", constant.routePath.index);
  25. const pages = getCurrentPages();
  26. const prevPage = pages[pages.length - 2];
  27. // 修改授权
  28. prevPage.getAuthStatus();
  29. constant.routeUtil.goBack();
  30. },
  31. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onLoad: function (options) {
  35. // https://y.qq.com/m/unionlogin/dist/index.html?appId=2&encryptString=%7B%22response_type%22%3A%22code%22%2C%22state%22%3A%22123456%22%7D&packageName=com.tencent.qqmusic.api.demo&callbackUrl=https%3a%2f%2fwww.example.com&changeLogin=0&needNotifyErr=0
  36. let url = options.url;
  37. // let callbackUrl = `http://testweb.radio1964.com/pages/ximalaya/index`;
  38. // // 将response_type、state等组装成json格式,得到encryptString字段,需要对encryptString做一次urlencode
  39. // var encryptString = encodeURIComponent(
  40. // JSON.stringify({
  41. // response_type: "code",
  42. // state: Date.now().toString,
  43. // callbackUrl: callbackUrl
  44. // })
  45. // );
  46. // // 2000000454
  47. // console.log("解密:", encryptString)
  48. // 从 options 中获取传入的 url 参数
  49. // var baseUrl = getApp().globalData.baseUrl + "/web";
  50. // const qqUrl = options.param;
  51. // http://testweb.radio1964.com/pages/ximalaya/index?ximalayaAccessToken=a87c56fb4e2c914cfeebca611a7f6998&ximalayaUid=511052575&ximalayaRefreshToken=5fcf78529bc2bc0e440dfdc17e3fbe9a&ximalayaExpiresIn=7200#deviceid=MW-SR2(4GWIFI):AIrSMArT145060808230
  52. // let qqUrl = "https://y.qq.com/iot_auth/?a=2000000454&c=auh-OMPRpkBvrnUa61LjvuKu"
  53. // let qqUrl = `https://y.qq.com/m/unionlogin/dist/index.html?appId=${this.data.QQ_MUSIC_APP_ID}&encryptString=${encryptString}&packageName=${this.data.QQ_DEV_NAME}&changeLogin=1&callbackUrl=${callbackUrl}&needNotifyErr=1&authForce=0&loginType=weixin`;
  54. // https://y.qq.com/m/unionlogin/dist/index.html?appId=120&encryptString=%7B%22response_type%22%3A%22code%22%7D&packageName=猫王妙播&changeLogin=0&callbackUrl=${callbackUrl}&needNotifyErr=0
  55. // let qqUrl = `https://y.qq.com/m/unionlogin/dist/index.html?appId=2&encryptString=%7B%22response_type%22%3A%22code%22%2C%22state%22%3A%22123456%22%7D&packageName=com.tencent.qqmusic.api.demo&callbackUrl=${callbackUrl}&changeLogin=0&needNotifyErr=1`
  56. // https://y.qq.com/m/unionlogin/dist/index.html?appId=120&encryptString=%7B%22response_type%22%3A%22code%22%2C%22state%22%3A%22123456%22%7D&packageName=com.Muzen.MaoKingRadioPlayer&changeLogin=0&callbackUrl=https%3a%2f%2fwww.example.com&needNotifyErr=0
  57. // https://y.qq.com/m/unionlogin/dist/index.html?appId=2&encryptString=%7B%22response_type%22%3A%22code%22%2C%22state%22%3A%22123456%22%7D&packageName=com.tencent.qqmusic.api.demo&callbackUrl=https%3a%2f%2fwww.example.com&changeLogin=0&needNotifyErr=0
  58. // let qqUrl = "https://m.ximalaya.com/login?hide_third_login=null&fromUri=https%3A%2F%2Fapi.ximalaya.com%2Foauth2%2Fv2%2Fauthorize%3Fclient_id%3Db55fe439798052676e61fbf0f0f649b7%26response_type%3Dcode%26redirect_uri%3Dhttps://api.ximalaya.com/elderly-ximalayaos-api/openapi-fmxos/callback/oauth2/get_access_token%26device_id%3DMW-SR2(4GWIFI):AIrSMArT145060808230%26client_os_type%3D3%26state%3D1.0.0_47.107.237.154_c65af766a9744b6098c3aec86187023c_3_http://testweb.radio1964.com/pages/ximalaya/index_deviceid=MW-SR2(4GWIFI):AIrSMArT145060808230_null"
  59. //
  60. // let qqUrl = "https://y.qq.com/m/unionlogin/dist/index.html?appId=xx&encryptString=xx&packageName=xx&callbackUrl=xx&changeLogin=xx&needNotifyErr=xx"
  61. //
  62. // const url = options.param;
  63. // http://testweb.radio1964.com/pages/
  64. // http://192.168.198.116:1024/callback/ximalaya/reportAuthInfo
  65. // https://o3tapi.radio1964.com/callback/ximalaya/reportAuthInfo
  66. // wx.showModal({
  67. // title: '复制打开浏览器',
  68. // content: '',
  69. // complete: (res) => {
  70. // if (res.cancel) {
  71. // }
  72. // if (res.confirm) {
  73. // wx.setClipboardData({
  74. // data: qqUrl,
  75. // })
  76. // }
  77. // }
  78. // })
  79. url = decodeURIComponent(url);
  80. console.log("打开授权页:", url)
  81. // https://m.ximalaya.com/login?hide_third_login=null&fromUri=https%3A%2F%2Fapi.ximalaya.com%2Foauth2%2Fv2%2Fauthorize%3Fclient_id%3Db55fe439798052676e61fbf0f0f649b7%26response_type%3Dcode%26redirect_uri%3Dhttps://api.ximalaya.com/elderly-ximalayaos-api/openapi-fmxos/callback/oauth2/get_access_token%26device_id%3DMW-2AXD:AIrSMArTd031105fe3c0%26client_os_type%3D3%26state%3D1.0.0_47.107.237.154_c65af766a9744b6098c3aec86187023c_3_http://testweb.radio1964.com/pages/ximalaya/index_deviceid=MW-2AXD:AIrSMArTd031105fe3c0_null
  82. // http://testweb.radio1964.com/pages/ximalaya/index_deviceid=MW-2AXD:AIrSMArTd031105fe3c0_null
  83. url = url.replace("http://testweb.radio1964.com/", "http://192.168.198.116:1024/");
  84. console.log("打开授权页2:", url)
  85. this.setData({
  86. qrcodeUrl: url
  87. });
  88. },
  89. handleWebviewMessage(e) {
  90. // e.detail.data 是网页 postMessage 发送的数据数组
  91. console.log('收到 web-view 消息:', e);
  92. var that = this;
  93. const msgData = e.detail.data[0]; // 消息数据
  94. if (msgData.channel === "webChannel") {
  95. console.log(" 收到 webChannel 消息:", msgData.content);
  96. }
  97. var channel = e.detail.message;
  98. switch (channel) {
  99. case "webChannel":
  100. // message=====?name=authXmlyWeb&code=1====webChannel
  101. var msg = message.message;
  102. var params = JSON.parse(msg);
  103. var channelName = params["name"] ?? "";
  104. // var externParams = params["externParams"] ?? {};
  105. if (channelName === "authXmlyWeb") {
  106. var code = params["code"] ?? 0;
  107. // 授权成功
  108. wx.showToast({
  109. title: code === 1 ? '授权成功' : '授权失败,请稍后再试',
  110. icon: code === 1 ? 'success' : 'fail'
  111. });
  112. const pages = getCurrentPages();
  113. const prevPage = pages[pages.length - 2];
  114. // 修改授权
  115. prevPage.setData({
  116. isAuthXmly: code === 1,
  117. });
  118. }
  119. default:
  120. break;
  121. }
  122. // 可以根据需要处理消息
  123. },
  124. })