• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Java小程序]聊天室——Socket和ServerSocket的使用发布时间:2022-07-18
下一篇:
微信小程序指示条跟随滑动发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap