async def auth_verify_login(openid, access_token, platform): ''' 验证accesstoken合法性数据''' if '1' == platform: success, result = await auth_verify_weixin(openid, access_token) elif '2' == platform: success, result = await auth_verify_weibo(openid, access_token) elif '3' == platform: success, result = auth_verify_apple(openid, access_token) else: return True, 1011, '未知平台类型', None access_token = access_token[:100] # success = True if success: # 认证通过 db_res = await check_openid_exists(openid, platform) # 查询openid是否已存在 if db_res is False: # 未添加过的openid,直接插入 insert_sql = "INSERT INTO auth_info (`openid`, `access_token`, `platform`, `updatetime`, `createtime`)" \ " VALUES (?, ?, ?, ?, ?)" sql_result = await dbins.execute( insert_sql, (openid, access_token, platform, curDatetime(), curDatetime())) if sql_result is not None: return True, 0, '认证添加成功', None else: return False, 3089, '添加失败,请重试', None else: # 已存在openid,更新accesstoken,并登录系统返回管饭token userid = db_res.get("userid") recordid = db_res.get("id") update_accesstoken_sql = "UPDATE auth_info set access_token=? WHERE id=?" update_result = await dbins.execute(update_accesstoken_sql, (access_token, recordid)) if update_result is None: return False, 3089, '更新失败,请重试', None else: # 创建token 登录成功并返回 if userid == 0: ''' 认证成功,未绑定的用户 ''' return True, 0, '认证更新成功', None success, token = await login_create_token(userid) if success: return True, 1, '登录成功', { 'token': token.replace('token:', '') } else: return False, 3099, token, None else: # 认证失败 return False, 2088, '认证失败', result
async def campaign_join(myid, campaignid, jointype, joinid): ''' 参加活动 myid 用户ID, campaignid 主题ID, jointype 参与类型(动态 主题 菜谱 等), joinid 参与ID(动态 主题 菜谱 等) ''' exists_id_status = await check_campaign_real(campaignid) if exists_id_status == None: return False, 1662, '错误的活动内容,请核对后重试', None elif exists_id_status == 0: return False, 1668, '活动尚未开始, 谢谢参与!', None elif exists_id_status == 2: return False, 1669, '活动已结束, 谢谢参与!', None elif exists_id_status != 1: return False, 1667, '未知活动状态,请核对后重试!', None join_sql = ''' INSERT INTO campaign_join ( `campaignid`, `userid`, `jointype`, `joinid`,`createtime` ) VALUES ( ?, ?, ?, ?, ? ) ''' add_like_res = await dbins.execute( join_sql, (campaignid, myid, jointype, joinid, curDatetime())) if add_like_res is None: return False, 3668, '活动参加失败,请重试', None return True, 0, '活动参与成功', None
async def cancel_collection(userid, itemid, itemtype): ''' 取消收藏 ''' # 判断是否已收藏 exists_sql = 'select id,status from collection_info where userid=? and `type`=? and itemid=? limit 1' exists_res = await dbins.selectone(exists_sql, (userid, itemtype, itemid)) if exists_res: # 取消收藏已存在 if exists_res.get('status') == -1: # 有记录,但本身就是取消收藏的 return True, 0, '已取消收藏', None coid = exists_res.get('id') up_collection_sql = 'update collection_info set `status` = -1, createtime=? where id=?' cancel_collection_res = await dbins.execute(up_collection_sql, (curDatetime(), coid)) if cancel_collection_res is None: return False, 3010, '取消收藏失败,请重试', None else: if itemtype == 2: # 食谱 收藏-1 up_recipe_sql = 'update recipe_info set collectionCount = collectionCount - 1 where id=? and isenable=1 and `status` in (1,0)' up_res = await dbins.execute(up_recipe_sql, (itemid)) if itemtype == 3: # 主题 收藏-1 up_topic_sql = 'update topic_info set collectionCount = collectionCount - 1 where id=? and isenable=1 and `status` != -1' up_res = await dbins.execute(up_topic_sql, (itemid)) return True, 0, '取消收藏成功', None else: return False, 1021, '错误的收藏数据', None
async def cancel_like(userid, itemid, itemtype): ''' 取消点赞 ''' # 判断是否已点赞 exists_sql = 'select id,`status` from like_info where userid=? and `likeType`=? and itemid=? limit 1' exists_res = await dbins.selectone(exists_sql, (userid, itemtype, itemid)) if exists_res is None: return False, 3018, '取消点赞失败,请重试', None if exists_res.get('status') == -1: # 已存在 return True, 0, '已取消点赞', None if exists_res.get('status') == 0: # 已存在 # 动态likeCount 点赞数字+1 if itemtype == 1: likes_num_result = await likes_num_update(itemid, -1, itemtype) if likes_num_result is False: return False, 3010, '点赞失败,请重试', None coid = exists_res.get('id') up_collection_sql = 'update like_info set `status` = -1, createtime=? where id=?' cancel_like_res = await dbins.execute(up_collection_sql, (curDatetime(), coid)) if cancel_like_res is None: likes_num_result = await likes_num_update(itemid, +1, itemtype) return False, 3011, '取消点赞失败,请重试', None else: return True, 0, '取消点赞成功', None else: return False, 1021, '错误的点赞数据', None
async def add_hotkeyword(keyword): ''' 添加热词 ''' up_key_sql = ''' update hot_keyword set visitcount = visitcount + 1, updatetime = ? where keyword = ? ''' curtime = curDatetime() result = await dbins.execute(up_key_sql, (curtime, keyword)) if result is None: return False, 3001, '热词更新异常,请重试', None if result == 1: return True, 0, '更新成功', None if result == 0: # 新词 ins_key_sql = ''' INSERT INTO hot_keyword (keyword, visitCount, updateTime, createTime, sort, status) VALUES (?, ?, ?, ?, ?, ?) ''' ins_res = await dbins.execute(ins_key_sql, (keyword, 1, curtime, curtime, 0, 0)) if ins_res is None: return False, 3002, '添加搜索词异常,请重试', None else: return True, 0, '添加成功', None
async def add_collection(userid, itemid, itemtype): ''' 添加收藏 itemid 收藏ID, itemtype的类型: 1 动态,2 食谱,3 主题''' res_exists = await item_exists(itemid, itemtype) if res_exists is False: return False, 3006, '收藏的数据不存在或不合法,请核对后重试', None # 判断是否已收藏 exists_sql = 'select id,status from collection_info where userid=? and `type`=? and itemid=? limit 1' exists_res = await dbins.selectone(exists_sql, (userid, itemtype, itemid)) if exists_res: # 已存在 if exists_res.get('status') == 0: # 有记录,但本身就是收藏专题的 return True, 0, '已收藏', None coid = exists_res.get('id') up_collection_sql = 'update collection_info set `status` = 0, createtime=? where id=?' add_collection_res = await dbins.execute(up_collection_sql, (curDatetime(), coid)) if add_collection_res is None: return False, 3009, '收藏失败,请重试', None else: if itemtype == 2: # 食谱 收藏+1 up_recipe_sql = 'update recipe_info set collectionCount = collectionCount + 1 where id=? and isenable=1 and `status` in (1,0)' up_res = await dbins.execute(up_recipe_sql, (itemid)) if itemtype == 3: # 主题 收藏+1 up_topic_sql = 'update topic_info set collectionCount = collectionCount + 1 where id=? and isenable=1 and `status` != -1' up_res = await dbins.execute(up_topic_sql, (itemid)) return True, 0, '收藏成功', None # 添加收藏 add_collection_sql = ''' insert into collection_info(userid, `type`, itemid, `status`) values(?, ?, ?, ?) ''' add_collection_res = await dbins.execute(add_collection_sql, (userid, itemtype, itemid, 0)) if add_collection_res is None: return False, 3008, '收藏失败,请重试', None if itemtype == 2: # 食谱 收藏+1 up_recipe_sql = 'update recipe_info set collectionCount = collectionCount + 1 where id=? and isenable=1 and `status` in (1,0)' up_res = await dbins.execute(up_recipe_sql, (itemid)) if itemtype == 3: # 主题 收藏+1 up_topic_sql = 'update topic_info set collectionCount = collectionCount + 1 where id=? and isenable=1 and `status` != -1' up_res = await dbins.execute(up_topic_sql, (itemid)) return True, 0, '收藏成功', None
async def create_video_id(myid, videoid, filename, filesize): ''' 新建阿里云视频ID,以及相关信息 ''' insert_video_sql = "INSERT INTO video_info (`userid`, `videoid`, `filename`, `filesize`, `updatetime`, `createtime`)" \ "VALUES(?, ?, ?, ?, ?, ?)" curtime = curDatetime() video_result = await dbins.execute( insert_video_sql, (myid, videoid, filename, filesize, curtime, curtime)) if video_result is None: log.warning("视频记录失败,userid:{}, videoid:{}".format(myid, videoid)) return False, "视频记录失败" else: return True, "ok"
async def add_like(userid, itemid, itemtype): ''' 添加点赞 itemid 收藏ID, itemtype的类型: 1 动态,2 食谱,3 主题''' res_exists = await item_exists(itemid, itemtype) if res_exists is False: return False, 3006, '点赞的数据不存在或不合法,请核对后重试', None # 判断是否已点赞 exists_sql = 'select id,`status` from like_info where userid=? and `likeType`=? and itemid=? limit 1' exists_res = await dbins.selectone(exists_sql, (userid, itemtype, itemid)) if exists_res: if exists_res.get('status') == 0: # 已存在 return True, 0, '已点赞', None if exists_res.get('status') == -1: # 已存在 # 动态likeCount 点赞数字+1 if itemtype == 1: likes_num_result = await likes_num_update(itemid, 1, itemtype) if likes_num_result is False: return False, 3010, '点赞失败,请重试', None coid = exists_res.get('id') up_collection_sql = 'update like_info set `status` = 0, createtime=? where id=?' add_like_res = await dbins.execute(up_collection_sql, (curDatetime(), coid)) if add_like_res is None: likes_num_result = await likes_num_update(itemid, -1, itemtype) return False, 3009, '点赞失败,请重试', None else: return True, 0, '点赞成功', None # 添加点赞 if itemtype == 1: likes_num_result = await likes_num_update(itemid, 1, itemtype) if likes_num_result is False: return False, 3010, '点赞失败,请重试', None add_collection_sql = ''' insert into like_info(userid, `likeType`, itemid, `status`) values(?, ?, ?, ?) ''' add_like_res = await dbins.execute(add_collection_sql, (userid, itemtype, itemid, 0)) if add_like_res is None: return False, 3008, '点赞失败,请重试', None return True, 0, '点赞成功', None
async def message_read(myid, msgid): ''' 将消息设置为已读 ''' msg_exists_sql = ''' select id from reply_info where id = ? and haveRead=0 and status=0 and userid!=? and ((beUserId=? ) or (itemUserID=? and beuserid=0)) limit 1; ''' msg_exists_res = await dbins.selectone(msg_exists_sql, (msgid, myid, myid, myid)) if msg_exists_res is None: return False, 1022, '消息数据异常,请核对后重试', None up_msg_sql = 'update reply_info set haveRead = 1, updatetime=? where id=?' up_msg_res = await dbins.execute(up_msg_sql, (curDatetime(), msgid)) if up_msg_res is None: return False, 3019, '消息已读失败,请重试', None else: return True, 0, '消息已读', None
async def message_del(myid, msgid): ''' 将消息设置为删除, haveread 标记为 -1''' msg_exists_sql =''' select id from reply_info where id = ? and haveRead!=-1 and status=0 and userid!=? and ((beUserId=? ) or (itemUserID=? and beuserid=0)) limit 1; ''' msg_exists_res = await db_ins.selectone(msg_exists_sql, (msgid, myid, myid, myid)) if msg_exists_res is None: # 不属于你的消息或者异常的数据 return False, 1102, '消息数据异常,请核对后重试', None up_msg_sql = 'update reply_info set haveRead = -1, updatetime=? where id=?' up_msg_res = await db_ins.execute(up_msg_sql, (curDatetime(), msgid)) if up_msg_res is None: return False, 3102, '消息删除失败,请重试', None else: return True, 0, '消息已删除', None
async def post(self, *args, **kwargs): result = [] userid = self.get_session().get('id', 0) did = self.verify_arg_legal(self.get_body_argument('did'), '地址id', False, is_num=True) name = self.verify_arg_legal( self.get_body_argument('name'), '收件人', False, ) mobile = self.verify_arg_legal( self.get_body_argument('mobile'), '手机号', False, ) pid = self.verify_arg_num( self.get_body_argument('pid'), '省id', is_num=True, ) cid = self.verify_arg_num( self.get_body_argument('cid'), '市id', is_num=True, ) aid = self.verify_arg_num( self.get_body_argument('aid'), '县区id', is_num=True, ) address = self.verify_arg_legal(self.get_body_argument('address'), '详细地址', False, is_len=True, olen=50) is_default = self.verify_arg_num(self.get_body_argument('is_default'), '是否默认', is_num=True) try: address_obj = await self.application.objects.get(My_Address, id=did, user_id=userid) except My_Address.DoesNotExist as e: return self.send_message(False, 404, '地址不存在', result) verify_city_ = Area.select().where(Area.id == aid and Area.pid == cid) verify_province_ = Area.select().where(Area.id == cid and Area.pid == pid) verify_city_wrappers = await self.application.objects.execute( verify_city_) verify_province_wrappers = await self.application.objects.execute( verify_province_) if not verify_city_wrappers or not verify_province_wrappers: return self.send_message(False, 404, '编辑失败 省市区参数错误', result) add_data = { "name": name, "mobile": mobile, "province_id": pid, "city_id": cid, "area_id": aid, "address": address, "is_default": is_default, "updatetime": curDatetime(), } # 如果要设置为默认地址,先查询数据库是否有存在默认地址,有改为非默认 async with await DATABASE.transaction() as transaction: try: if is_default: query = (My_Address.use(transaction).update({ My_Address.is_default: 0 }).where(My_Address.user == userid, My_Address.is_default == 1)) await query.execute() await My_Address.use(transaction).update(**add_data).where( My_Address.id == did) except Exception as e: # 日志 log.info('{} 地址修改失败:{}-{}'.format(userid, add_data, e)) success, code, message, result = False, 404, '修改失败', '' return self.send_message(success, code, message, result) success, code, message, result = True, 0, '修改成功', result return self.send_message(success, code, message, result)
async def auth_bind_user(openid, access_token, platform, faceimg, nickname, phone, verify): ''' 绑定第三方用户 ''' # 校验 绑定验证码是否正确 access_token = access_token[:100] rdskey = "phone.verify.{}:{}".format("bind", phone) rdget = await RedisOperate().instance().get_data(rdskey) if rdget != verify: # 手机验证码正确 return False, 1036, "错误的验证码", None else: # 验证码正确的话,删除 await RedisOperate().instance().del_data(rdskey) # 判断第三方信息是否已存在 plat_user = await check_openid_exists(openid, platform, access_token) # print(openid, platform, access_token) if plat_user is False: # 不存在绑定信息 return False, 1037, "错误的第三方登录信息,绑定失败", None # 获取根据手机号获取用户ID ures, userid = await get_userid(phone) nickname_only = await nickname_rename(nickname, userid) if ures is False: # 未注册用户,插入用户 insert_auth_sql = "INSERT INTO user (`username`, `headImg`, `mobile`, `sex`, `createTime`, `updateTime`)" \ "VALUES(?, ?, ?, ?, ?, ?)" sqllist = [] sqllist.append((insert_auth_sql, (nickname_only, faceimg, phone, 0, curDatetime(), curDatetime()))) sqllist.append(('select last_insert_id() as nid', ())) result = await dbins.execute_many(sqllist) if result is None: return False, 3015, "添加用户失败,请重试", None userid = result[1][1][0] else: # 已注册的用户,先校验是否绑定了第三方登录账号 bind_is_exists, bindchekmsg = await check_user_already_bind( userid, platform, plat_user.get('id', 0)) if bind_is_exists: if '1' == platform: log.warning("手机号:{} 已绑定微信账号".format(phone)) return False, 1047, "该手机号已绑定微信账号", None else: log.warning("手机号:{} 已绑定微博账号".format(phone)) return False, 1048, "该手机号已绑定微博账号", None # 已注册用户,更新用户信息 up_auth_sql = "UPDATE user set headImg = ?, username = ? where id = ? " up_user_result = await dbins.execute(up_auth_sql, (faceimg, nickname_only, userid)) if up_user_result is None: return False, 3016, "更新用户数据失败", None # 更新绑定信息 bind_sql = "UPDATE auth_info set userid=? where id=?" bind_sql_result = await dbins.execute(bind_sql, (userid, plat_user.get("id"))) if bind_sql_result is None: log.error("绑定用户失败,userid:{}, openid:{}".format(userid, openid)) return False, 3017, "绑定用户失败", None else: # return True, 0, '绑定成功', None # 绑定成功后直接去登录 success, token = await login_create_token(userid) if success: return True, 0, '登录成功', {'token': token.replace('token:', '')} else: return False, 3099, token, None