liangjin
2021-03-31 15dedf22b81b08e627dc3c176b32fadc75536dde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * @Author: 小明丶
 * @Date: 2020-05-07 18:35:25
 * @LastEditors: 小明丶
 * @LastEditTime: 2020-11-10 11:38:44
 * @Description: 微信授权
 */
 
/**
 * 微信授权
 * @param appid 公众号的唯一标识
 * @param redirect_uri 授权后重定向的回调链接地址(登录后授权了最好返回到登录跳转的主页面), 请使用 urlEncode 对链接进行处理,用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。
 * @param scope 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
 * @param state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
 */
    export class  GoWeChat {
        constructor(appid,redirect_uri){
            this.appid = appid
            this.redirect_uri = redirect_uri
        }
        gocontract(){
            // 拼接微信授权页面链接
            var url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.appid}&redirect_uri=${this.redirect_uri}&response_type=code&scope=snsapi_base&state=1#wechat_redirect`
             // 判断用户是否已经授权
            if(window.localStorage.sib_wx_openId == 'undefined' || !localStorage.sib_wx_openId){
                window.location.href = url
                // 在回调页面去获取到授权返回的code,并且传递给后端
            }
        }
    }