def setbook(user, book_id): """ 设置用户教材 :param book_id: 教材/教辅ID :param user: 用户对象 """ if user.subject_id == 51: data = db.tbkt_yw.yw_chinesetextbooks.get(ct_ID=book_id) if not data: return ubook = db.tbkt_active.mid_term_user_book.select('id').get( user_id=user.id, subject_id=51, is_work_book=0) if ubook: db.tbkt_active.mid_term_user_book.filter(id=ubook.id).update( book_id=book_id) else: db.tbkt_active.mid_term_user_book.create( book_id=book_id, subject_id=51, user_id=user.id, add_time=int(time.time()), is_work_book=0, ) else: thread_pool.call(_set_book, user.id, book_id)
def p_update_student(request): """ @api {post} /class/student/update [班级]更新学生信息 @apiGroup class @apiParamExample {json} 请求示例 {"user_id":学生ID, "name":姓名, "sex":1男2女} @apiSuccessExample {json} 成功返回 {"message": "", "next": "", "data": "", "response": "ok", "error": ""} @apiSuccessExample {json} 失败返回 { "message": "名字必须是2-5个汉字", "next": "", "data": "", "response": "fail", "error": "" } """ args = request.QUERY.casts(user_id=int, name=unicode, sex=int) student_id = args.user_id or 0 name = args.name or u'' sex = args.sex or 0 if name and not is_chinese_word(name): return ajax.jsonp_fail(request, message='姓名长度为2-5个汉字') user = request.user if not common.is_mystudent(user, student_id): return ajax.jsonp_fail(request, message='权限错误') thread_pool.call(common.update_student_info, student_id, name, sex) return ajax.jsonp_ok(request)
def send_sms(request, user, status, unit_ids, title, begin_time, sendpwd, sendscope, object_ids, title_array): """ :param user: :param status: 1 立即发送 2 定时发送 :param unit_ids: 学生班级 数组 :param content: 短信内容 :param begin_time: 发送时间 :param sendpwd: 是否下发账号密码 :param sendscope: 短信发送范围 0给所有学生发短信 1只给开通的学生发短信 :param object_ids: 试卷id 数组 :return: """ nowt = int(time.time()) with db.tbkt_active as dt: for num, i in enumerate(object_ids): titles = title_array[num] for k in unit_ids: # create task dt.yw_reading_sms.create( object_id=i, add_user=user.id, unit_id=k, status=status, add_time=nowt, begin_time=begin_time, is_sendpwd=sendpwd, sendscope=sendscope, title=titles ) # send sms or password from libs.utils.thread_pool import call call(send_sms_method_with_ids, request, status, unit_ids, sendscope, title, sendpwd) return None
def setbook(user_id, book_id): """ 设置用户教材 --------------------- 王晨光 2017-1-4 --------------------- :param book_id: 教材/教辅ID """ thread_pool.call(_set_book, user_id, book_id)
def process_response(self, request, response): # 更新token if getattr(request, '_newtoken', None): auth.login_response(response, request._newtoken) # 添加跨域头 self.cross_domain(request, response) # 查询新版本 if getattr(request, '_app_version', None): response['App-Version'] = json.dumps(request._app_version) # 写接口访问日志 thread_pool.call(com_sys.add_access_log, request) return response
def phone_login(request): """ @api {post} /account/login/web [登录]WEB登录 @apiGroup account @apiParamExample {json} 请求示例 { "username":"******", "password":"******", "pass_flag": 1 # 0 以前的老接口,不加密, 1 现在的新程序,加密 } @apiParam {String} username 手机号或帐号 @apiParam {String} password 密码 @apiSuccessExample {json} 成功返回 { "next": "", "error": "", "message": "", "data": [ { "real_name": "张三", "school_name": "创恒中学", "unit_name": "100班", "type":1, "portrait": "头像", "tbkt_token": "有效期7天的token" }, ], "response": "ok" } * type = 1 学生 * type = 3 教师 """ args = request.QUERY.casts(username=str, password=str, pass_flag=int) username = args.username or '' password = args.password or '' pass_flag = int(args.pass_flag or 0) username = username.strip().lower() out = [] if not username: return ajax.jsonp_fail(request, message='请输入用户名或手机号') if not password: return ajax.jsonp_fail(request, message='请输入密码') if pass_flag: password = auth.safe_pass_decode(password) # 模糊匹配(根据手机号+密码猜测具体帐号) if username.isdigit(): encoded_password = auth.encode_plain_password(password) sql = """ select u.username, u.status from auth_user u inner join auth_profile p on p.user_id=u.id and p.password='******' where u.phone='%s' """ % (encoded_password, username) binds = db.user_slave.fetchall_dict(sql) if not binds: return ajax.jsonp_fail(request, message="账号或密码错误!") now_binds = [b for b in binds if int(b.status) != 2] if not now_binds: return ajax.jsonp_fail(request, message="该账号已被禁用,请联系客服") else: user, auth_user = com_user.authenticate(username=username, password=password) if not user: return ajax.jsonp_fail(request, message='账号或密码错误,是否找回密码?') if int(user.status) == 2: return ajax.jsonp_fail(request, message='该账号已被禁用,请联系客服') # 登录检查 error = com_user.web_login_check(user) if error: return ajax.jsonp_fail(request, message=error) # 登录日志 thread_pool.call(common.login_handle, request, args, user) # 输入后缀xs,js 准确返回用户信息 if not username.isdigit(): d = dict( real_name=user.real_name, school_name=user.school_name if user.school_name else "", unit_name=user.unit.name if user.unit else "", portrait=user.portrait, type=user.type, tbkt_token=auth.login(request, user.id), dept_id=user.dept_id, ) out.append(d) return ajax.jsonp_ok(request, out) # 手机号下的所有账号 sql = """ select u.id, u.real_name, p.portrait, u.type,p.password,u.sid,u.dept_id, u.status from auth_user u inner join auth_profile p on u.id = p.user_id and u.phone = "%s" where u.type in (1,3) and u.dept_id in (1,2) and u.status != 2 order by u.type desc """ % username users = db.user_slave.fetchall_dict(sql) if not users: return ajax.jsonp_fail(request, message='账号或密码错误,是否找回密码?') user_ids = [i.id for i in users] regions = db.ketang.mobile_order_region.select("school_name", "unit_class_id", "user_id") \ .filter(user_id__in=user_ids).group_by("user_id")[:] units_id = [i.unit_class_id for i in regions] region_map = {i.user_id: i for i in regions} units = db.slave.school_unit_class.select("unit_name", "id").filter(id__in=units_id)[:] units_map = {i.id: i.unit_name for i in units} for i in users: region = region_map.get(i.id) unit_id = region.unit_class_id if region else 0 school_name = region.school_name if region else "" unit_name = units_map.get(unit_id, "") d = dict( real_name=i.real_name, school_name=school_name, unit_name=unit_name, type=i.type, portrait=get_portrait(i, i.type), # 'portrait': com_user.get_portrait(u, u.type), tbkt_token=create_token(i.id), sid=i.sid, dept_id=i.dept_id ) out.append(d) return ajax.jsonp_ok(request, out)
def p_login(request, role): """ @api {post} /account/login/? [登录]登录 @apiGroup account @apiParamExample {json} 请求示例 /account/login/s 学生登录 /account/login/t 老师登录 { "username":"******", "password":"******", "pass_flag": 1 # 0 以前的老接口,不加密, 1 现在的新程序,加密 "login_type": 90, # 登录类型 1是网站 其他是手机APP "version": "6.0.1", "name": "865f8521b18fe9b", "model": "MI 4LTE", "platform": "Android", "uuid": "A100005064E391", "appversion": "1.0.0" } @apiParam {String} username 手机号或帐号 @apiParam {String} password 密码 @apiParam {Integer} login_type (可选)登录类型 88:Android教辅学生 90:Android教辅教师 89:IOS教辅学生 91:IOS教辅教师 @apiParam {String} version (可选)系统版本号 @apiParam {String} name (可选)设备名称 @apiParam {String} model (可选)手机型号 @apiParam {String} platform (可选)平台名称 @apiParam {String} uuid (可选)设备序列号 @apiParam {String} appversion (可选)app版本 @apiSuccessExample {json} 成功返回 { "next": "", "error": "", "message": "", "tbkt_token": "eHl3dHR3PHF0eHh5dXlzcnY8cXR4eHF4dHVwdQ", "app_version": { "h5": 0, "api": 1, "must": 0 }, "data": "", "response": "ok" } 登录成功后, 服务器会在响应头中添加 Tbkt-Token: xxxx 头域, 分配给客户端一个token. 客户端应记录这个 tbkt_token 并在之后的请求中带上 Tbkt-Token: xxxx 到时如果服务器没有收到 tbkt_token 或者 tbkt_token 过期(7天), 则返回: { "message": "请您先登录", "next": "", "data": "", "response": "fail", "error": "no_user" } 其他请求也要看下响应头里有没有给新token, 有就更新token. 检测新版本机制: 客户端每个请求头加一个 App-Type: 9安卓学生 10安卓教师 11苹果学生 12苹果教师 13 H5学生 14 H5教师 服务器返回头会加一个 App-Version: {"h5": H5版本号, "api": 客户端版本号, "must": 0不强制升级客户端 1强制升级客户端} @apiSuccessExample {json} 失败返回 {"message": "帐号或密码错误", "error": "", "data": "", "response": "fail", "next": ""} 如果error=switch就进入切换身份页面让他选角色 @apiSuccessExample {json} Python示例 import requests r = requests.post('http://127.0.0.1:4000/account/login/t', params={'username':'******', 'password':'******'}) cookies = r.cookies r = requests.post('http://127.0.0.1:4000/class/getpwd', params={'bind_id':'4210824'}, cookies=cookies) print r.json() @apiSuccessExample {json} Java示例 public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .addHeader("Cookie","tbkt_token="+tbkt_token) .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); } } // webview CookieManage cookiemanager = CookieManager.getInstance(); cookiemanager.setAcceptCookie(true); cookiemanager.setCookie(url,"sessionid="+sessionid); @apiSuccessExample {json} C#示例 HttpClient client = CreateClientASMtoken(""); HttpResponseMessage response = client.GetAsync("http://localhost").Result; IEnumerable<string> rawCookies = response.Headers.GetValues("Set-Cookie"); """ # return ajax.jsonp_fail(request, message=u'系统升级,暂停服务 20点至次日18点', error=u'升级') args = request.QUERY.casts(username=str, password=str, login_type=int, version=str, name=unicode, model=unicode, platform=str, uuid=str, appversion=str, pass_flag=int) username = args.username or '' password = args.password or '' pass_flag = int(args.pass_flag or 0) username = username.strip().lower() if not username: return ajax.jsonp_fail(request, message='请输入用户名或手机号') if not password: return ajax.jsonp_fail(request, message='请输入密码') login_type = args.login_type if pass_flag: password = auth.safe_pass_decode(password) # 模糊匹配(根据手机号+密码猜测具体帐号) if username.isdigit(): type = 3 if role == 't' else 1 encoded_password = auth.encode_plain_password(password) sql = """ select u.username, u.status from auth_user u inner join auth_profile p on p.user_id=u.id and p.password='******' where u.phone='%s' and u.type=%s """ % (encoded_password, username, type) binds = db.user_slave.fetchall_dict(sql) if not binds: return ajax.jsonp_fail(request, message='账号或密码错误,是否找回密码?') now_binds = [b for b in binds if int(b.status) != 2] if not now_binds: return ajax.jsonp_fail(request, message="该账号已被禁用,请联系客服") bind = now_binds[0] if bind: username = bind.username elif role == 't': username += 'js' else: username += 'xs' elif role == 't' and not RE_TEACHER_USERNAME.match(username): return ajax.jsonp_fail(request, message='请输入正确的教师帐号') elif role == 's' and not RE_STUDENT_USERNAME.match(username): return ajax.jsonp_fail(request, message='请输入正确的学生帐号') user, auth_user = com_user.authenticate(username=username, password=password) if not user: return ajax.jsonp_fail(request, message='账号或密码错误,是否找回密码?') if int(user.status) == 2: return ajax.jsonp_fail(request, message='该账号已被禁用,请联系客服') # 登录检查 if login_type > 1: error = com_user.app_login_check(user) if error: # 如果当前角色不让进, 通知他切换到别的角色 n = db.user_slave.auth_user.filter(phone=user.phone_number, type=user.type).count() if n > 1: auth.login(request, user.id) return ajax.jsonp_fail(request, 'switch', message=error) return ajax.jsonp_fail(request, message=error) else: error = com_user.web_login_check(user) if error: return ajax.jsonp_fail(request, message=error) # 登录日志 thread_pool.call(common.login_handle, request, args, user) data = { 'flag': "0", 'user_id': user.id } auth.login(request, user.id) if user.type==1: result=common.real_name_filter(user.real_name) if not result: data = { 'flag': "1", 'user_id': user.id } return ajax.jsonp_ok(request,data) return ajax.jsonp_ok(request, data)
def p_give_web(request): """ @api {post} /huodong/mid_term/sx_task/send [期中提分试卷数学]布置数学提分试卷 @apiGroup mid_term @apiParamExample {json} 请求示例 { "type": 2 "unit_id": "555428,515192", # 班级ID "title": "作业标题", "content": "短信内容", "begin_time": "2017-01-05 09:10:00", # (可选)定时作业时间 "end_time": "2017-02-05 23:59:59", # 作业截止时间(必须大于当前时间和begin_time) "sendpwd": 1/0, # 是否下发帐号密码 "sendscope": 1/0, # 短信发送范围 0给所有学生发短信 1只给开通的学生发短信 "object_id":"试卷ID,可多个" } @apiSuccessExample {json} 成功返回 { "message": "", "next": "", "data":"", "response": "ok", "error": "" } @apiSuccessExample {json} 失败返回 { "message": "请设置作业的完成时间", "next": "", "data": "", "response": "fail", "error": "" } """ user = request.user grade_id = request.user.grade_id args = request.QUERY.casts(type=int, unit_id=str, title=unicode, content=unicode, begin_time='timestamp', end_time='timestamp', sendpwd=int, sendscope=int, details='json', object_id=str) type = 102 unit_id = args.unit_id or '' unit_ids = [int(s) for s in unit_id.split(',') if s] content = args.content or u'' now = int(time.time()) begin_time = args.begin_time or now end_time = args.end_time sendpwd = args.sendpwd or 0 sendscope = args.sendscope or 0 object_id = args.object_id or '' object_ids = [int(s) for s in object_id.split(',') if s] title = [s for s in args.title.split('|') if s] tea_name = db.tbkt_user_slave.auth_user.select('real_name').get(id=user.id) tea_name = tea_name.real_name open_content = get_text_content(content, sendscope, grade_id, tea_name, end_time, subject_id=21) if not type: return ajax.jsonp_fail(request, message='作业类型不能为空') if not unit_id: return ajax.jsonp_fail(request, message='请选择您要发布的班级') if not content: return ajax.jsonp_fail(request, message='短信内容不能为空') if len(content) > 200: return ajax.jsonp_fail(request, message='短信内容不能超过200字') if not end_time: return ajax.jsonp_fail(request, message='请设置作业的完成时间') if begin_time < now: return ajax.jsonp_fail(request, message='定时发布时间不得小于当前系统时间') if begin_time >= end_time: return ajax.jsonp_fail(request, message='定时发布时间不得大于完成时间') user = request.user # title = title or "%s作业" % begin_time.strftime("%m月%d日") # 1已发 2待发 status = 1 if begin_time <= now else 2 with db.tbkt_shuxue as dt: for k, i in enumerate(object_ids): # create task task_id = dt.sx_task.create( type=type, object_id=i, title=title[k], add_user=user.id, sms_content=content, status=status, add_time=now, begin_time=begin_time, end_time=end_time, is_sendpwd=sendpwd, ) if status == 1: get_ticket(user.id) # create task_class class_details = [] for unit_id in unit_ids: d = { 'task_id': task_id, 'unit_class_id': unit_id, 'type': sendscope, 'add_time': now, 'student_id': '', 'status': status } class_details.append(d) dt.sx_task_class.bulk_create(class_details, ignore=True) sql = """ select d.paper_id,d.question_id from sx_paper_question_number n inner join sx_paper_detail d on n.paper_id = %s and n.paper_id = d.paper_id and n.type = 2 and n.object_id = d.question_id """ % i papers = db.ziyuan_slave.fetchall_dict(sql) create_ordinary_task(task_id, papers, user.id, now, dt) create_message(task_id, user.id, title[k], content, begin_time, end_time, type, unit_ids) # send sms or password call(send_sms_method_with_ids, request, status, unit_ids, sendscope, content, open_content, sendpwd) return ajax.jsonp_ok(request)
def r_yy_send(request): """ @api {post} /huodong/terminal/yy/send [期末提分试卷英语]教师发试卷 @apiGroup terminal @apiParamExample {json} 请求示例 { "type": 103, # 期末作业类型 "unit_id": "622216,578298", # 班级id 多个以逗号分割 "title": "试卷标题", # 试卷标题 以竖线|分割 "content": "短信内容", # 短信内容 "begin_time": "2017-04-8 09:10:00", # 开始时间 "end_time": "2017-04-9 23:59:59", # 结束时间 "sendpwd": 0, # 是否下发账号密码 "sendscope": 0, # 短信发送范围 0给所有学生发短信 1只给开通的学生发短信 "ids":"1" # 试卷id,多个试卷id以逗号分割 } @apiSuccessExample {json} 成功返回 { "message": "", "next": "", "data": {}, "response": "ok", "error": "" } @apiSuccessExample {json} 失败返回 {"message": "", "error": "", "data": "", "response": "fail", "next": ""} """ user = request.user grade_id = request.user.grade_id args = request.QUERY.casts(type=int, unit_id=str, title=unicode, content=unicode, begin_time='timestamp', end_time='timestamp', sendpwd=int, sendscope=int, ids=str, agent=int) typ = args.type # 1网上作业 2试卷作业 (本函数都是2) unit_id = args.unit_id or '' # 班级id unit_ids = [int(s) for s in unit_id.split(',') if s] content = args.content or u'' # 短信内容 now = int(time.time()) # 现在的时间 begin_time = args.begin_time or now # 发布时间 end_time = args.end_time # 完成时间 sendpwd = args.sendpwd or 0 # 下发账号密码 0/1 sendscope = args.sendscope or 0 # 短信发送范围 0给所有学生发短信 1只给开通的学生发短信 ids = args.ids or '' ids = [int(id) for id in ids.split(',') if id] # 试卷 title = [s for s in args.title.split('|') if s] agent = args.agent or 2 tea_name = user.name open_content = get_text_content(grade_id, tea_name, end_time, subject_id=91) if not ids: return jsonp_fail(request, message='请选择试卷') if not typ and typ != 2: return jsonp_fail(request, message='作业类型不能为空') if not unit_id: return jsonp_fail(request, message='请选择您要发布的班级') if not content: return jsonp_fail(request, message='短信内容不能为空') if len(content) > 200: return jsonp_fail(request, message='短信内容不能超过200字') if not end_time: return jsonp_fail(request, message='请设置作业的完成时间') if begin_time < now: return jsonp_fail(request, message='定时发布时间不得小于当前系统时间') if begin_time >= end_time: return jsonp_fail(request, message='定时发布时间不得大于完成时间') title = title or "%s试卷" % begin_time.strftime("%m月%d日") # 1已发 2待发 status = 1 if begin_time <= now else 2 # 查询试卷发送作业状态 sql = """ select distinct object_id, c.unit_class_id from yy_task t, yy_task_class c where t.type = 103 and t.add_user = %s and t.id = c.task_id and t.status > -1 and c.status > -1 and t.object_id in (%s) """ % (user.id, join(ids)) rows = db.tbkt_yingyu.fetchall_dict(sql) done_map = {(i.object_id, i.unit_class_id): 1 for i in rows} with db.tbkt_yingyu as dt: for k, i in enumerate(ids): # create task task_id = dt.yy_task.create( type=103, object_id=i, add_user=user.id, title=title[k], sms_content=content, status=status, add_time=now, begin_time=begin_time, end_time=end_time, is_sendpwd=sendpwd, ) # create task_class # 查询试卷是否发送过 和发送的班级 class_details = [] unsent = set() for unit_id in unit_ids: if done_map.get((i, unit_id)): continue d = { 'task_id': task_id, 'unit_class_id': unit_id, 'type': sendscope, 'add_time': now, 'student_id': '', 'status': status } # 判断用户试卷发送过的班级 如果发送过 跳过 unsent.add(unit_id) class_details.append(d) if class_details: dt.yy_task_class.bulk_create(class_details) # create task_details task_details = [] questions = PAPER_QUESTION[i] # questions.sort() questions_array = [] d = {} d['task_id'] = task_id # content_type 学生端作业列表显示 d['content_type'] = 11 for qid in questions: questions_array.append({"cid": i, "tcid": i, "qid": qid}) d['text'] = json.dumps(questions_array) task_details.append(d) dt.yy_task_detail.bulk_create(task_details) pub_ids = [] questions = PAPER_QUESTION[i] for qid in questions: d = {} d['user_id'] = user.id d['subject_id'] = 91 d['question_id'] = qid d['add_time'] = time.time() pub_ids.append(d) dt.yy_publish_question.bulk_create( pub_ids, ignore=True) # !!!!!!!!!!!!!!!!!!!!!!!! create_message(task_id, user.id, title[k], content, begin_time, end_time, unsent, agent, 91) user_phone = db.tbkt_user_slave.auth_user.select('phone').get( id=user.id) # send sms or password if status == 1 or sendpwd: call(send_sms_method, request, unit_ids, status, user_phone, open_content, content, sendpwd) data = {} return jsonp_ok(request, data)
def outside_task_info(user, message_id, unit_id): """ 获取课外活动作业详情 :param user: :param message_id: :param unit_id: :return: """ sql = """ select m.id, m.content, m.title, m.begin_time, m.end_time endtime, m.add_user user_id, images from message m inner join message_class c on m.id = c.message_id where m.id = %s and c.unit_class_id = %s; """ % (message_id, unit_id) msg = db.slave.fetchone_dict(sql) if not msg: return now = int(time.time()) if msg.endtime <= now: # 更新教师端检查作业状态 call(update_outside_status, user, msg.id, unit_id) time_sub = now - int(msg.begin_time) msg.begin_time = from_unixtime( msg.begin_time) if time_sub >= 24 * 60 * 60 else change_time(time_sub) msg.end_time = from_unixtime(msg.endtime) msg.is_upload = 1 if not msg.images: msg.figure_images = [] msg.images = [] else: images = is_empty( map(lambda x: format_outside_url(x, is_figure=False), json.loads(msg.images))) msg.figure_images = is_empty( map(lambda x: format_outside_url(x, is_figure=True), json.loads(msg.images))) msg.images = images stu_ids = user.get_mobile_order_region().select('user_id').\ filter(unit_class_id=unit_id, user_type=1).flat("user_id")[:] stu_tests = db.slave.outside_task_test.filter(user_id__in=stu_ids, message_id=message_id).\ select("user_id", "media_url", "type", "content", "thumb_up", "add_time").order_by("-thumb_up", "add_time")[:] stu_tests_map = {i.user_id: i for i in stu_tests} if user.is_teacher: msg.user_name = user.real_name msg.portrait = user.portrait else: tea_user = com_user.get_user(msg.user_id) msg.user_name = tea_user.real_name msg.portrait = tea_user.portrait msg.status = 0 if user.id in stu_tests_map or msg.endtime <= now: msg.status = 1 userd = com_user.get_users(stu_ids) msg.finish = [] msg.unfinish = [] for user_id, stu_user in userd.iteritems(): stu_test = stu_tests_map.get(user_id) if not stu_test: d = dict(real_name=stu_user.real_name, portrait=stu_user.portrait) msg.unfinish.append(d) continue media_url = is_empty(json.loads(stu_test.media_url)) if not media_url: stu_test.figure_url = [] stu_test.media_url = [] else: stu_test.media_url = map(lambda x: format_outside_url(x), media_url) if stu_test.type == 1: stu_test.figure_url = map( lambda x: format_outside_url(x, is_figure=True), media_url) stu_test.user_name = stu_user.real_name stu_test.add_time = from_unixtime(stu_test.add_time) stu_test.portrait = stu_user.portrait msg.finish.append(stu_test) msg.finish.sort(key=lambda m: (-m.thumb_up, m.add_time)) return msg
def send_class_message(user, unit_ids, student_ids, type, object_id, title, content, images, begin_time, end_time): """ 发送班级消息 --------------------- 王晨光 2017-6-7 --------------------- :param user: 发布者User :param unit_ids: [班级ID] :param student_ids: [指定学生ID] :param type: 消息类型 :param object_id: 业务表ID :param title: 标题 :param images: 通知图片 :param content: 内容 :param begin_time: 开始时间 :param end_time: 结束时间 :return: 消息ID """ if not unit_ids: return if student_ids: regions = user.get_mobile_order_region().filter( unit_class_id__in=unit_ids, user_type=1).select('unit_class_id', 'user_id')[:] else: regions = [] student_id_set = set(student_ids) nowt = int(time.time()) images = json.dumps(images) if images else '' # status = 2 if begin_time > nowt else 1 with db.default as c: message_id = c.message.create( type=type, subject_id=user.subject_id, object_id=object_id, add_user=user.id, title=title, content=content, status=1, images=images, add_time=nowt, end_time=end_time, begin_time=begin_time, ) details = [] stu_id_list = [] for unit_id in unit_ids: stu_ids = [] for r in regions: if r.unit_class_id == unit_id: stu_ids.append(r.user_id) stu_ids_s = '' stu_ids = [sid for sid in student_id_set if sid in stu_ids] stu_id_list += stu_ids if stu_ids: stu_ids_s = '|' + '|'.join(str(i) for i in stu_ids) + '|' d = { 'message_id': message_id, 'unit_class_id': unit_id, 'student_ids': stu_ids_s } details.append(d) c.message_class.bulk_create(details) call(message_send_sms, unit_ids, stu_id_list, content, user.platform_id) if type == 10: if user.subject_id == 21: create_sx_task(user.id, message_id, title, content, nowt, end_time, begin_time, unit_ids) if user.subject_id == 91: create_yy_task(user.id, message_id, title, content, nowt, end_time, begin_time, unit_ids) if user.subject_id == 51: create_yw_task(user.id, message_id, title, content, nowt, end_time, begin_time, unit_ids) return message_id