def send_tml_msg_to_user(self, param): """向用户发送模板消息""" user_id, req_data = param req_data = copy.deepcopy(req_data) form_ids = self.get_form_id(user_id) for form_id in form_ids: req_data['touser'] = form_id.openid req_data['form_id'] = form_id.form_id ret = self.request_tml_msg(req_data) # print ret, 'RET', requests if ret == 1: form_id.status = FormId.STATUS_USE e_log.error(u'【向用户发送模板消息】成功,user_id:%s,req_data:%s' % (user_id, req_data)) break elif ret == -1: form_id.status = FormId.STATUS_INVALID else: self.e_log.error( u'【向用户发送模板消息】用户没有可用的form_id,user_id:%s,req_data:%s' % (user_id, req_data)) utils.commit_db(db, self.e_log, u'【发送通知关注人提醒通知】入库失败,user_id:%s' % (user_id)) db.session.remove()
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 add_task(self, user_id, type_, title, c_type, content, img_uri, audio_uri, ddl): """发布任务""" self.check_content(c_type, content, img_uri, audio_uri) user_info = user_mgr.get_cache(user_id) task = self.init_task(user_id, type_, title, c_type, content, img_uri, audio_uri, user_info, ddl) tls = self.add_task_list(task, user_id) utils.commit_db(db, e_log, u'【发布任务】user_id:%s' % user_id) return dict(task_id=task.id, pwd=task.pwd)
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 send_attend_msg(self, task, attends): """发送 通知关注人 提醒通知""" print 'task', task.id, 'attend', [attend.from_id for attend in attends] data = self.get_attend_req_data(task) data = [[attend.from_id, data] for attend in attends] self.multi_thread(self.send_tml_msg_to_user, data) # for attend in attends: # self.send_tml_msg_to_user(attend.from_id, data) utils.commit_db(db, self.e_log, u'【发送通知关注人提醒通知】入库失败,task_id:%s' % (task.id))
def del_task(self, user_id, task_id, pwd): """删除任务""" task = self.get_db_task(task_id, pwd) if task.user_id != user_id: raise ext.ParamError(u'没有权限') if task.ddl < datetime.now(): raise ext.ParamError(u'过期任务不能删除') task.status = Task.STATUS_DEL utils.commit_db(db, e_log, u'【删除任务】task_id:%s' % task_id) return dict()
def add_form_id(self, user_id, form_id): """新增form_id""" if len(form_id) < 1: return cache = user_mgr.get_cache(user_id) ddl = datetime.now() + timedelta(days=7) form_id = FormId(user_id=user_id, openid=cache['wxa_openid'], ddl=ddl, form_id=form_id) db.session.add(form_id) utils.commit_db(db, self.e_log, u'【新增form_id】入库失败')
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 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 del_reply(self, user_id, task_id, pwd, reply_id): """删除接龙""" task = self.get_db_task(task_id, pwd) if task.ddl < datetime.now(): raise ext.ParamError(u'过期任务不能删除') reply = Reply.query.filter(Reply.id == reply_id).first() if not reply: raise ext.NotExistsError(u'接龙不存在') if reply.status != reply.STATUS_ON: raise ext.NotExistsError(U'接龙已删除') if task.id != reply.task_id: raise ext.NotExistsError(u'id异常') if reply.user_id != user_id: raise ext.ParamError(u'没有权限') reply.status = Reply.STATUS_DEL utils.commit_db(db, e_log, u'【删除任务】task_id:%s' % task_id) return dict()
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)