index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view>
  3. <u-toast ref="uToast" />
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. }
  11. },
  12. onLoad() {
  13. var self = this;
  14. //#ifdef MP-WEIXIN
  15. wx.login({
  16. success (res) {
  17. if (res.code) {
  18. self.request({
  19. action : 'wx/login',
  20. data : {
  21. jsCode : res.code
  22. },
  23. success(resData) {
  24. // 用户已绑定
  25. if (resData.data.code == "200") {
  26. var userInfo = resData.data.data.userInfo;
  27. uni.setStorageSync('userInfo', userInfo);
  28. uni.setStorage({
  29. key: 'Authorization',
  30. data: resData.data.data.wxLoginToken,
  31. success: function () {
  32. // 跳转主页
  33. uni.redirectTo({
  34. url: '/pages/home/task'
  35. });
  36. }
  37. });
  38. // 用户未绑定,跳转绑定页
  39. } else if (resData.data.code == "100") {
  40. uni.redirectTo({
  41. url: '/pages/index/checking?openId='+resData.data.data.openId + '&unionid='+resData.data.data.unionid
  42. });
  43. // 数据异常
  44. } else {
  45. this.$refs.uToast.show({
  46. title: resData.data.message,
  47. type: 'error'
  48. })
  49. }
  50. }
  51. })
  52. } else {
  53. console.log('登录失败!' + res.errMsg)
  54. }
  55. }
  56. })
  57. // #endif
  58. //#ifdef H5
  59. let backUrl = this.getQueryString("backUrl");
  60. let openId = this.getQueryString("openId");
  61. var domain = window.location.href;
  62. var checkingUrl = "";
  63. if(!!backUrl) {
  64. checkingUrl = domain.split("#")[0] + "#/pages/index/gzhGo?backUrl=" + encodeURIComponent(backUrl);
  65. } else {
  66. checkingUrl = domain.split("#")[0] + "#" + '/pages/index/gzhGo';
  67. }
  68. window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+this.getWxconfig().appID+'&redirect_uri='+encodeURIComponent(checkingUrl)+'&response_type=code&scope=snsapi_userinfo&state='+openId+'#wechat_redirect';
  69. // #endif
  70. },
  71. methods: {
  72. getQueryString(name) {
  73. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  74. if(window.location.hash.indexOf("?") < 0){
  75. return null;
  76. }
  77. let r = window.location.hash.split("?")[1].match(reg);   
  78. if (r != null) return decodeURIComponent(r[2]);
  79.    return null;
  80. }
  81. }
  82. }
  83. </script>
  84. <style>
  85. </style>