{"id":13699153,"url":"https://github.com/zwczou/weixin-python","last_synced_at":"2025-05-14T21:09:39.683Z","repository":{"id":47123427,"uuid":"67014734","full_name":"zwczou/weixin-python","owner":"zwczou","description":"微信SDK - 包括微信支付,微信公众号,微信登陆,微信消息处理等","archived":false,"fork":false,"pushed_at":"2023-08-06T11:19:54.000Z","size":121,"stargazers_count":1644,"open_issues_count":9,"forks_count":401,"subscribers_count":63,"default_branch":"master","last_synced_at":"2025-04-06T14:06:56.497Z","etag":null,"topics":["flask","python","sdk","weixin"],"latest_commit_sha":null,"homepage":"http://weixin-python.rtfd.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zwczou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-31T07:35:15.000Z","updated_at":"2025-04-04T16:20:06.000Z","dependencies_parsed_at":"2024-06-19T16:01:49.965Z","dependency_job_id":"4b31fa91-8118-4001-9ed0-69a97d201528","html_url":"https://github.com/zwczou/weixin-python","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zwczou%2Fweixin-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zwczou%2Fweixin-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zwczou%2Fweixin-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zwczou%2Fweixin-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zwczou","download_url":"https://codeload.github.com/zwczou/weixin-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248750008,"owners_count":21155682,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flask","python","sdk","weixin"],"created_at":"2024-08-02T19:01:05.604Z","updated_at":"2025-04-13T16:53:59.577Z","avatar_url":"https://github.com/zwczou.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"微信SDK\n======\n\n提供微信登录，公众号管理，微信支付，微信消息的全套功能\n\n## 文档目录\n\n* [快速开始](#目录)\n* [微信消息](https://github.com/zwczou/weixin-python/wiki/%E5%BE%AE%E4%BF%A1%E6%B6%88%E6%81%AF)\n* [微信支付](https://github.com/zwczou/weixin-python/wiki/%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98)\n* [微信登录](https://github.com/zwczou/weixin-python/wiki/%E5%BE%AE%E4%BF%A1%E7%99%BB%E9%99%86)\n* [微信公众平台](https://github.com/zwczou/weixin-python/wiki/%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%B9%B3%E5%8F%B0)\n\n欢迎提交[Pull requests](https://github.com/zwczou/weixin-python/pulls)\n\n\n如果需要单独使用其中的某些模块，可以见[文档目录](#文档目录)的具体模块\n\n如果需要组合在一起可以参考[快速开始](#目录)\n\n## 目录\n\n* [安装](#安装)\n* [功能](#功能)\n* [异常](#异常)\n* [用法](#用法)\n\t* [参数](#参数)\n\t* [初始化](#初始化)\n\t* [微信消息](#微信消息)\n\t* [微信登录](#微信登录)\n\t* [微信支付](#微信支付)\n\t* [微信公众号](#微信公众号)\n\n## 安装\n\n使用pip\n\n`sudo pip install weixin-python`\n\n使用easy_install\n\n`sudo easy_install weixin-python`\n\n安装最新版本\n`pip3 install git+https://github.com/zwczou/weixin-python`\n\n当前版本v0.5.7\n\n## 功能\n\n* 微信登录\n* 微信支付\n* 微信公众号\n* 微信消息\n\n## 异常\n\n父异常类名为 `WeixinError`\n子异常类名分别为 `WeixinLoginError` `WeixinPayError` `WeixinMPError` `WeixinMsgError`\n\n## 用法\n\n### 参数\n\n* `WEIXIN_TOKEN` 必填，微信主动推送消息的TOKEN\n* `WEIXIN_SENDER` 选填，微信发送消息的发送者\n* `WEIXIN_EXPIRES_IN` 选填，微信推送消息的有效时间\n* `WEIXIN_MCH_ID` 必填，微信商户ID，纯数字\n* `WEIXIN_MCH_KEY` 必填，微信商户KEY\n* `WEIXIN_NOTIFY_URL` 必填，微信回调地址\n* `WEIXIN_MCH_KEY_FILE` 可选，如果需要用退款等需要证书的api，必选\n* `WEIXIN_MCH_CERT_FILE` 可选\n* `WEIXIN_APP_ID` 必填，微信公众号appid\n* `WEIXIN_APP_SECRET` 必填，微信公众号appkey\n\n上面参数的必填都是根据具体开启的功能有关, 如果你只需要微信登录，就只要选择 `WEIXIN_APP_ID` `WEIXIN_APP_SECRET`\n\n* 微信消息\n   * `WEIXIN_TOKEN`\n   * `WEIXIN_SENDER`\n   * `WEIXIN_EXPIRES_IN`\n\n* 微信登录\n    * `WEIXIN_APP_ID`\n    * `WEIXIN_APP_SECRET`\n\n* 微信公众平台\n    * `WEIXIN_APP_ID`\n    * `WEIXIN_APP_SECRET`\n\n* 微信支付\n    * `WEIXIN_APP_ID`\n    * `WEIXIN_MCH_ID`\n    * `WEIXIN_MCH_KEY`\n    * `WEIXIN_NOTIFY_URL`\n    * `WEIXIN_MCH_KEY_FILE`\n    * `WEIXIN_MCH_CERT_FILE`\n\n### 初始化\n\n如果使用flask\n\n```\n# -*- coding: utf-8 -*-\n\n\nfrom datetime import datetime, timedelta\nfrom flask import Flask, jsonify, request, url_for\nfrom weixin import Weixin, WeixinError\n\n\napp = Flask(__name__)\napp.debug = True\n\n\n# 具体导入配\n# 根据需求导入仅供参考\napp.config.from_object(dict(WEIXIN_APP_ID='', WEIXIN_APP_SECRET=''))\n\n\n# 初始化微信\nweixin = Weixin()\nweixin.init_app(app)\n# 或者\n# weixin = Weixin(app)\n\n```\n\n如果不使用flask\n\n```\n# 根据需求导入仅供参考\nconfig = dict(WEIXIN_APP_ID='', WEIXIN_APP_SECRET='')\nweixin = Weixin(config)\n```\n\n### 微信消息\n\n如果使用django，添加视图函数为\n\n```\nurl(r'^/$', weixin.django_view_func(), name='index'),\n\n```\n\n如果为flask，添加视图函数为\n\n```\napp.add_url_rule(\"/\", view_func=weixin.view_func)\n```\n\n\n```\n@weixin.all\ndef all(**kwargs):\n\t\"\"\"\n\t监听所有没有更特殊的事件\n\t\"\"\"\n    return weixin.reply(kwargs['sender'], sender=kwargs['receiver'], content='all')\n\n\n@weixin.text()\ndef hello(**kwargs):\n\t\"\"\"\n\t监听所有文本消息\n\t\"\"\"\n    return \"hello too\"\n\n\n@weixin.text(\"help\")\ndef world(**kwargs):\n\t\"\"\"\n\t监听help消息\n\t\"\"\"\n    return dict(content=\"hello world!\")\n\n\n@weixin.subscribe\ndef subscribe(**kwargs):\n\t\"\"\"\n\t监听订阅消息\n\t\"\"\"\n    print kwargs\n    return \"欢迎订阅我们的公众号\"\n```\n\n### 微信登录\n\n```\n@app.route(\"/login\")\ndef login():\n    \"\"\"登录跳转地址\"\"\"\n\topenid = request.cookies.get(\"openid\")\n    next = request.args.get(\"next\") or request.referrer or \"/\",\n    if openid:\n        return redirect(next)\n\n    callback = url_for(\"authorized\", next=next, _external=True)\n    url = weixin.authorize(callback, \"snsapi_base\")\n    return redirect(url)\n\n\n@app.route(\"/authorized\")\ndef authorized():\n\t\"\"\"登录回调函数\"\"\"\n    code = request.args.get(\"code\")\n    if not code:\n        return \"ERR_INVALID_CODE\", 400\n    next = request.args.get(\"next\", \"/\")\n    data = weixin.access_token(code)\n    openid = data.openid\n    resp = redirect(next)\n    expires = datetime.now() + timedelta(days=1)\n    resp.set_cookie(\"openid\", openid, expires=expires)\n    return resp\n```\n\n### 微信支付\n\n注意: 微信网页支付的timestamp参数必须为字符串\n\n```\n\n\n@app.route(\"/pay/jsapi\")\ndef pay_jsapi():\n\t\"\"\"微信网页支付请求发起\"\"\"\n\ttry:\n        out_trade_no = weixin.nonce_str\n        raw = weixin.jsapi(openid=\"openid\", body=u\"测试\", out_trade_no=out_trade_no, total_fee=1)\n        return jsonify(raw)\n    except WeixinError, e:\n        print e.message\n        return e.message, 400\n\n\n@app.route(\"/pay/notify, methods=['POST'])\ndef pay_notify():\n    \"\"\"\n    微信异步通知\n    \"\"\"\n    data = weixin.to_dict(request.data)\n    if not weixin.check(data):\n        return weixin.reply(\"签名验证失败\", False)\n    # 处理业务逻辑\n    return weixin.reply(\"OK\", True)\n\n\nif __name__ == '__main__':\n    app.run(host=\"0.0.0.0\", port=9900)\n```\n\n### 微信公众号\n\n**注意**: 如果使用分布式，需要自己实现`access_token`跟`jsapi_ticket`函数\n\n`access_token`默认保存在`~/.access_token`\n`jsapi_ticket`默认保存在`~/.jsapi_ticket`\n\n默认在(HOME)目录下面，如果需要更改到指定的目录，可以导入库之后修改，如下\n\n```\nimport weixin\n\nDEFAULT_DIR = \"/tmp\"\n```\n\n获取公众号唯一凭证\n\n\tweixin.access_token\n\n\n获取ticket\n\n\tweixin.jsapi_ticket\n\n\n创建临时qrcode\n\n\tdata = weixin.qrcode_create(123, 30)\n\tprint weixin.qrcode_show(data.ticket)\n\n创建永久性qrcode\n\n\t# scene_id类型\n\tweixin.qrcode_create_limit(123)\n\t# scene_str类型\n\tweixin.qrcode_create_limit(\"456\")\n\n长链接变短链接\n\n\tweixin.shorturl(\"http://example.com/test\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzwczou%2Fweixin-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzwczou%2Fweixin-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzwczou%2Fweixin-python/lists"}