微信小程序:登录-python处理

1 基本流程

https://developers.weixin.qq.com/miniprogram/dev/api/api-login.html#wxloginobject

2 code 获取 session_key 和 openid 

json格式获取js_code,通过接口获取session_key和openid,符合条件返回unionID,具体看文档

@bp.route("/auth/weapp_login", methods=['GET','POST'])
def mini_program():
    if request.method == 'POST':

        data = request.get_data()
        json_data = json.loads(data)
        print(json_data)
        js_code = json_data.get("js_code")

        APP_ID = 'xxxxxx'
        APP_SECRET = "yyyyyyy"

        if js_code:
            url = 'https://api.weixin.qq.com/sns/jscode2session'
            params = {}
            params['appid'] = APP_ID
            params['secret'] = APP_SECRET
            params['js_code'] = js_code
            params['grant_type'] = "authorization_code"
            ret = requests.get(url=url,params=params)
            print(ret.status_code)
            print(ret.content)
      
            token = "xxxxxxxx"
            return jsonify({"token",token})
         

3 js_code 获取unionid

@bp.route("/auth/weapp_login", methods=['GET','POST'])
def mini_program():
    if request.method == 'POST':

        data = request.get_data()
        json_data = json.loads(data)
        print(json_data)
        js_code = json_data.get("js_code")
        encrypted_data = json_data.get("encryptedData")
        iv = json_data.get("iv")

        APP_ID = 'xxxx'
        APP_SECRET = "yyyyyyy"

        if js_code and encrypted_data is None and iv is None:
            url = 'https://api.weixin.qq.com/sns/jscode2session'
            params = {}
            params['appid'] = APP_ID
            params['secret'] = APP_SECRET
            params['js_code'] = js_code
            params['grant_type'] = "authorization_code"

            ret = requests.get(url=url,params=params)
            print(ret.status_code)
            print(ret.content)
            content = json.loads(ret.content)
            unionid = content.get('unionid')
            global session_key_backup
            session_key_backup = content.get('session_key')

            if unionid:
                print("unionid",unionid)

               
                unionid_obj = mongo.db.users.find_one({'unionid': unionid})
                if unionid_obj:
                    consumer_id = unionid_obj.get("consumer_id")
                else:
                    user_id = str(uuid.uuid4())
                    consumer_id = get_consumer_id(user_id)
                    user = {}
                    user['user_id'] = user_id
                    user['consumer_id'] = consumer_id
                    user['user_type'] = GUEST
                    mongo.db.users.insert_one(user)

                key, secret, created_at = get_consumer_credentials(consumer_id)
                token = get_jwt_token(consumer_id, key, secret, created_at)

                data = {}
                data['token'] = token
                return my_jsonify(data)
            else:
                data = {}
                data['token'] = None
                return my_jsonify(data)

        else:
           
            # pip install python-weixin
            from weixin import WXAPPAPI
            from weixin.lib.wxcrypt import WXBizDataCrypt

            api = WXAPPAPI(appid=APP_ID,
                           app_secret=APP_SECRET)
            # session_info = api.exchange_code_for_session_key(code=js_code)
            # # 获取session_info 后
            # session_key = session_info.get('session_key')
            session_key = session_key_backup


            crypt = WXBizDataCrypt(APP_ID, session_key)

            # encrypted_data 包括敏感数据在内的完整用户信息的加密数据
            # iv 加密算法的初始向量
            # 这两个参数需要js获取
            user_info = crypt.decrypt(encrypted_data, iv)
            # # dict
            # print(user_info)

            unionid = user_info.get('unionId')
            print("uninonid",unionid)

            # 之前有没有扫码登录网络
            unionid_obj = mongo.db.users.find_one({'unionid': unionid})
            if unionid_obj:
                consumer_id = unionid_obj.get("consumer_id")
            else:
                user_id = str(uuid.uuid4())
                consumer_id = get_consumer_id(user_id)
                user = {}
                user['user_id'] = user_id
                user['unionid'] = unionid
                user['consumer_id'] = consumer_id
                user['user_type'] = GUEST
                mongo.db.users.insert_one(user)

            key, secret, created_at = get_consumer_credentials(consumer_id)
            token = get_jwt_token(consumer_id, key, secret, created_at)
            # set_limit_by_consumer_id(consumer_id)


            data = {}
            data['token'] = token
            return my_jsonify(data)

 

posted @ 2018-06-06 10:38  Adamanter  阅读(3074)  评论(0编辑  收藏  举报