文章目录
显示
通过code获取openid
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
appid可以登录微信后台获取。
base_setting.py 定義配置:
MINA_APP = {
'appid' : 'wxaaaf50c8265cf721',
'appkey' : '3671f6b2eaf7946e7bb2eac521be403'
}
python安裝扩展发送请求
pip install requests
member.py发送请求
import requests
@route_api.route("/member/login", methods = ["GET","POST"])
def login():
resp = {'code':200, 'msg':'操作成功', 'data':{}}
req = request.values
code = req['code'] if 'code' in req else ''
if not code or len(code)<1:
resp['code'] = -1
resp['msg'] = '需要code'
return jsonify(resp)
url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code"\
.format(app.config['MINA_APP']['appid'], app.config['MINA_APP']['appkey'], code)
r = requests.get(url)
res = r.text
app.logger.info(res)
return jsonify(resp)
启动小程序 并且启动服务
成功返回session_key和openid
我们只需要openid
将字符串转换为json数据
@route_api.route("/member/login", methods = ["GET","POST"])
def login():
resp = {'code':200, 'msg':'操作成功', 'data':{}}
req = request.values
code = req['code'] if 'code' in req else ''
if not code or len(code)<1:
resp['code'] = -1
resp['msg'] = '需要code'
return jsonify(resp)
url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code"\
.format(app.config['MINA_APP']['appid'], app.config['MINA_APP']['appkey'], code)
r = requests.get(url)
res = json.loads(r.text)
openid = res['openid']
return jsonify(resp)
创建member表和绑定表
show databases;
use food_db
CREATE TABLE `member` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`nickname` varchar(100) NOT NULL DEFAULT '' COMMENT '会员名',
`mobile` varchar(11) NOT NULL DEFAULT '' COMMENT '会员手机号码',
`sex` tinyint(1) NOT NULL DEFAULT '0' COMMENT '性别 1:男 2:女',
`avatar` varchar(200) NOT NULL DEFAULT '' COMMENT '会员头像',
`salt` varchar(32) NOT NULL DEFAULT '' COMMENT '随机salt',
`reg_ip` varchar(100) NOT NULL DEFAULT '' COMMENT '注册ip',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1:有效 0:无效',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次更新时间',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '插入时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员表';
CREATE TABLE `oauth_member_bind` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`member_id` int(11) NOT NULL DEFAULT '0' COMMENT '会员id',
`client_type` varchar(20) NOT NULL DEFAULT '' COMMENT '客户端来源类型。qq,weibo,weixin',
`type` tinyint(3) NOT NULL DEFAULT '0' COMMENT '类型 type 1:wechat ',
`openid` varchar(80) NOT NULL DEFAULT '' COMMENT '第三方id',
`unionid` varchar(100) NOT NULL DEFAULT '',
`extra` text NOT NULL COMMENT '额外字段',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '插入时间',
PRIMARY KEY (`id`),
KEY `idx_type_openid` (`type`,`openid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='第三方登录绑定关系';
创建数据库orm
創建文件夾
flask-sqlacodegen "mysql://root:123456@127.0.0.1/food_db" --tables member --outfile "order/common/models/member/Member.py" --flask
flask-sqlacodegen "mysql://root:123456@127.0.0.1/food_db" --tables oauth_member_bind --outfile "order/common/models/member/OauthMemberBind.py" --flask
创建好文件后,统一替换db变量: