def get_new_at(self): ''' 获取新的access token ''' try: url = config.WEIXIN_ACCESS_TOKEN_URL data = dict(appid=config.APPID, secret=config.SECRET, grant_type='client_credential') result = requests.get(url, params=data, timeout=10) if result.status_code == 200: ret = result.json() at_data = dict( access_token=ret['access_token'], expires_in=ret['expires_in'], get_time=int(time.time()), ) wechat_at_cache.set(self.ACCESS_TOKEN_KEY, at_data) return at_data except: self.e_log.error(traceback.format_exc()) self.e_log.error(u'【获取at】失败') raise ext.OtherError(u'获取微信access_token失败') raise ext.OtherError(u'获取微信access_token失败')
def reply(self, user_id, c_type, content, img_uri, audio_uri, task_id, pwd): """回复""" self.check_content(c_type, content, img_uri, audio_uri) task = self.get_db_task(task_id, pwd) if datetime.now() > task.ddl: raise ext.ParamError(u'任务已过期') # if user_id == task.user_id: # raise ext.OtherError(u'本人不能操作') if task.type != Task.TYPE_REPLY: raise ext.OtherError(U'不允许接龙') r = Reply.query.filter(Reply.user_id == user_id, Reply.task_id == task_id, Reply.status == Reply.STATUS_ON).first() if r: raise ext.OtherError(u'您已经接龙了') user_cache = user_mgr.get_cache(user_id) r = Reply( user_id=user_id, c_type=c_type, content=content, img_uri=img_uri, audio_uri=audio_uri, task_id=task_id, img_url=user_cache['img_url'], nickname=user_cache['nickname'] ) db.session.add(r) utils.commit_db(db, e_log, u'【回复】user_id:%s' % user_id) return dict()
def login_unionid(self, unionid, nickname, img_url, openid='', wxa_openid=''): """登录 unionid版本""" if len(unionid) < 5: raise ext.OtherError(u'获取unionid失败') user = User.query.filter(User.unionid == unionid).first() if not user: user = User(openid=unionid, unionid=unionid, nickname=nickname, img_url=img_url) db.session.add(user) user.last_login = datetime.now() user.nickname = nickname # self.e_log.error('nickname %s',repr(nickname)) user.img_url = img_url if len(openid) > 5: #小程序和js支付都需要用到openid,所以要更新openid user.openid = openid if len(wxa_openid) > 5: #小程序和js支付都需要用到openid,所以要更新openid user.wxa_openid = wxa_openid log_msg = u'【登陆】入库失败,unionid:%s,nickname:%s' % (unionid, nickname) if not utils.commit_db(db, self.e_log, log_msg): return False return session_mgr.set_new_session(user.key)
def wxa_login(self, code, nickname, img_url): """小程序登录""" from ktask.wx_utils import get_detail_info_wxa ret = get_detail_info_wxa(code) #ret=dict(openid=code,) #TODO if 'openid' not in ret: raise ext.OtherError(u'登陆失败') return self.login(ret['openid'], nickname, img_url)
def add_attend(self, user_id, to_id): """关注""" cache = user_mgr.get_cache(to_id) if self.is_attend_exist(user_id, to_id): raise ext.OtherError(U'您已关注TA') a = Attend(from_id=user_id, to_id=to_id) db.session.add(a) utils.commit_db(db, e_log, u'【关注】user_id:%s' % user_id) return dict()
def commit_db(db, error_logger, log_msg_if_fail, is_raise=1): """写入数据库,返回成功或失败""" try: db.session.commit() return True except: import traceback error_logger.error(traceback.format_exc()) error_logger.error(log_msg_if_fail) db.session.rollback() if is_raise: raise ext.OtherError(u'服务器开小差(002)') return False
def upload_file_online1(self, mime_type, data, suffix): # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 auth = oss2.Auth(config.ALI_APPID, config.ALI_SCRE) # Endpoint以杭州为例,其它Region请按实际情况填写。 bucket = oss2.Bucket(auth, 'http://oss-cn-beijing.aliyuncs.com', 'ktask') key = self.get_file_name_online(data, suffix) result = bucket.put_object(key, data) if result.status == 200: return key else: raise ext.OtherError(u'上传错误')
def read(self, user_id, task_id, pwd): """已读""" task = self.get_db_task(task_id, pwd) if user_id == task.user_id: return dict() raise ext.OtherError(u'本人不能已读') user_info = user_mgr.get_cache(user_id) r = Read.query.filter(Read.user_id == user_id, Read.task_id == task_id).first() if r: return dict() r = Read(user_id=user_id, task_id=task_id, img_url=user_info['img_url'], nickname=user_info['nickname']) db.session.add(r) utils.commit_db(db, e_log, u'【已读】user_id:%s' % user_id) return dict(read_suc=1)
def get_new_js_ticket(self): ''' 获取新的jsapi_ticket ''' # print 'get_new_js_ticket' try: at = self.get_at() url = '%s?access_token=%s&type=jsapi' % (config.JS_API_TICKET_URL, at) result = requests.get(url, timeout=10) if result.status_code == 200: ret = result.json() at_data = dict( ticket=ret['ticket'], expires_in=ret['expires_in'], get_time=int(time.time()), ) wechat_at_cache.set(self.JS_TICKET_KEY, at_data) return at_data except: self.e_log.error(traceback.format_exc()) self.e_log.error(u'【获取at】失败') raise ext.OtherError(u'获取微信access_token失败') raise ext.OtherError(u'获取微信access_token失败')
def login(self, openid, nickname, img_url): """登录""" if len(openid) < 5: raise ext.OtherError(u'获取OPENID失败') user = User.query.filter(User.wxa_openid == openid).first() if not user: user = User(wxa_openid=openid, nickname=nickname, img_url=img_url) db.session.add(user) user.last_login = datetime.now() user.nickname = nickname user.img_url = img_url log_msg = u'【登陆】入库失败,openid:%s,nickname:%s' % (openid, nickname) if not utils.commit_db(db, self.e_log, log_msg): return False return session_mgr.set_new_session(user.id)