Files
2026-08-02 14:07:04 +08:00

74 lines
2.3 KiB
Python

"""
Author: xiao 792282@qq.com
Date: 2025-08-10 11:36:05
LastEditors: xiao 792282@qq.com
LastEditTime: 2025-08-17 13:38:39
FilePath: django_小程序/同城配送工具/小程序用户授权数据.py
Description: 这是默认设置,可以在设置》工具》File Description中进行配置
"""
from wechat_项目工具包.微信支付工具包.微信支付类 import paymentCategory
"""功能模块"""
import jmespath
from requests import post, get
class wei小程序服务端工具:
"""微信登陆"""
@classmethod
def 获取OPENID(cls, secret: str, js_code: str, appid: str, ) -> dict:
"""
@param secret: 小程序 appSecret
@type secret: string
@param js_code: 登录时获取的 code,可通过wx.login获取
@type js_code: string
@param appid: 小程序的app🆔
@type appid: string
@return:{"session_key":"etC7EOml89Pd8XIhTFyAOA==","expires_in":7200,"openid":"oNTAd0fm1GQAdohfbvywhGstsIPc","unionid":"ojWkM1MnLrdty0S3Vks2pYorp4MY"}
@rtype: dict
"""
url = "https://api.weixin.qq.com/sns/jscode2session"
data = {
"appid": appid,
"secret": secret,
"js_code": js_code,
"grant_type": "authorization_code"
}
res_data = get(url=url, params=data)
res = res_data.json()
errcode = jmespath.search(data=res, expression="errcode", )
if res_data.status_code == 200:
if errcode is None:
return res
else:
raise Exception(f"登陆错误码:{res}")
raise Exception("请求login登陆时出错了")
@classmethod
def 获取手机号(cls, url, code: str):
"""
@param url:
@type url:
@param code:
@type code:
@return: {'session_key': 'etC7EOml89Pd8XIhTFyAOA==', 'expires_in': 7200, 'openid': 'oNTAd0fm1GQAdohfbvywhGstsIPc', 'unionid': 'ojWkM1MnLrdty0S3Vks2pYorp4MY'}
@rtype:
"""
data = {
"code": code
}
res_data = post(url=url, json=data)
res = res_data.json() # type: dict
return res
if __name__ == '__main__':
pass
res = wei小程序服务端工具.获取OPENID(
js_code="0e1sYCkl2bCmhg4DG2ll2kheh10sYCkn", appid=paymentCategory.appid,
secret=paymentCategory.appsecret,
)