class QuestionsNewListHandler(BaseHandler): _get_handler_fields = [ Field('page', T_INT, False), Field('maxnum', T_INT, False), Field('name', T_STR, True), Field('parent', T_INT, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data = {} params = self.validator.data info, num = Questions.page(**params) data['num'] = num if info: for item in info: item['id'] = str(item['id']) item['parent'] = str(item['parent']) item['ctime'] = tools.trans_datetime(item['ctime']) item['utime'] = tools.trans_datetime(item['utime']) item['status_desc'] = define.QUESTION_STATUS[item['status']] item['category_desc'] = define.QUESTION_MAP[item['category']] if item['parent'] != -1: question = Questions(item['id']) question.load() parent_parent = question.find_parent_parent() item['parent_parent'] = parent_parent else: item['parent_parent'] = -1 data['info'] = info return success(data=data)
class OrderCreateHandler(BaseHandler): _post_handler_fields = [ Field('box_id', T_INT, False), Field('goods_name', T_STR, False), Field('goods_price', T_INT, False), Field('goods_desc', T_STR, False), Field('goods_picture', T_STR, False), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): params = self.validator.data box_id = params.get('box_id') box = BoxList(box_id) box.load() if not box.data: return error(errcode=RESP_CODE.DATAERR) # 检查名称 # 只能创建一个先检查 order = Order.load_by_box_id(box_id) if order.data: return error(errcode=RESP_CODE.DATAEXIST) ret = Order.create(params) log.debug('class=OrderCreateHandler|create ret=%s', ret) if ret != 1: return error(errcode=RESP_CODE.DATAERR) return success(data={})
class BoxListHandler(BaseHandler): _get_handler_fields = [ Field('page', T_INT, False), Field('maxnum', T_INT, False), Field('name', T_STR, True), Field('parent', T_INT, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data = {} params = self.validator.data info, num = BoxList.page(**params) data['num'] = num if info: parents = [item['parent'] for item in info] parent_parent = find_parent_parent(parents) for item in info: item['id'] = str(item['id']) item['parent'] = str(item['parent']) icon_name = item['icon'] item['icon'] = BASE_URL + icon_name item['icon_name'] = icon_name if item['parent'] != -1: item['parent_parent'] = str(parent_parent[int( item['parent'])]) if parent_parent else -1 else: item['parent_parent'] = -1 base_tools.trans_time(item, BoxList.DATETIME_KEY) data['info'] = info return success(data=data)
class QuestionAddHandler(BaseHandler): _post_handler_fields = [ Field('name', T_STR, False), Field('category', T_STR, False), Field('parent', T_INT, False), Field('save_type', T_INT, False), Field('content', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): data = {} params = self.validator.data log.debug('class=QuestionAddHandler|params=%s', params) name = self.req.input()['name'] save_type = params['save_type'] category = params['category'] content = self.req.input()['content'] # 如果是答案且存储类型为富文本校验content if content: base64_str = base64.b64encode(content) params['content'] = base64_str params['name'] = name params.update({ 'utime': tools.gen_now_str(), 'ctime': tools.gen_now_str(), }) ret = Questions.create(params) return success(data=data)
class LoginHandler(BaseHandler): _post_handler_fields = [ Field('mobile', T_REG, False, match=r'^(1\d{10})$'), Field('password', T_STR, False), ] @house_set_cookie(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): params = self.validator.data mobile = params['mobile'] password = params["password"] # if mobile not in ALLOW_LOGIN_MOBILE: # log.info('mobile=%s forbidden', mobile) # return error(RESP_CODE.USERFORBIDDEN) user = User.load_user_by_mobile(mobile) if user.data and user.userid: if user.data['state'] != HOUSE_USER_STATE_OK: log.info('userid=%s|state=%s|forbidden login', user.userid, user.data['state']) return error(RESP_CODE.USERSTATEERR) if user.data['user_type'] != HOUSE_USER_TYPE_ADMIN: log.info('userid=%s|user_type=%s|forbidden login', user.userid, user.data['user_type']) return error(RESP_CODE.ROLEERR) flag = check_password(password, user.data.get('password')) if not flag: return error(RESP_CODE.PWDERR) return success(data={'userid': user.userid}) return error(RESP_CODE.DATAERR)
class RateViewHandler(BaseHandler): _post_handler_fields = [ Field('rate_id', T_INT, False), Field('rate', T_FLOAT, False) ] _get_handler_fields = [Field('rate_id', T_INT, False)] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): data = {} params = self.validator.data rate_id = params.pop('rate_id') rate = RateInfo(rate_id) rate.load() if not rate.data: return error(RESP_CODE.DATAERR) ret = rate.update(params) if ret != 1: return error(RESP_CODE.DATAERR) return success(data=data) @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): params = self.validator.data rate_id = params.get('rate_id') rate = RateInfo(rate_id) rate.load() if not rate.data: return error(RESP_CODE.DATAERR) return success(data=rate.data)
class OrderListHandler(BaseHandler): _get_handler_fields = [ Field('page', T_INT, False), Field('maxnum', T_INT, False), Field('goods_name', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data = {} params = self.validator.data info, num = Order.page(**params) data['num'] = num if info: boxs = BoxList.load_all(where={}) box_name_map = {} for box in boxs: box_name_map[box['id']] = box['name'] for item in info: item['id'] = str(item['id']) item['box_id'] = str(item['box_id']) item['box_name'] = box_name_map.get(item['box_id'], '') goods_picture = item['goods_picture'] item['goods_picture'] = BASE_URL + goods_picture item['goods_picture_name'] = goods_picture base_tools.trans_time(item, BoxList.DATETIME_KEY) data['info'] = info return success(data=data)
class UserCreateHandler(BaseHandler): _post_handler_fields = [ Field('mobile', T_STR, False), Field('password', T_STR, False), Field('user_type', T_STR, False), Field('email', T_STR, False), Field('name', T_STR, False), Field('idnumber', T_STR, True), Field('province', T_STR, True), Field('city', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): params = self.validator.data # 先检查该用户是否有权限添加用户 admin = self.user.userid if admin not in ALLOW_ADD_USER_ID: log.info('admin=%s|is denied', admin) return error(RESP_CODE.DATAERR, resperr='该用户没有权限添加用户') # 是否已经注册 mobile = params.get('mobile') user = User.load_user_by_mobile(mobile) if user.data: return error(RESP_CODE.DATAEXIST, resperr='手机号已存在') # 默认用用户名称和手机号一样 params['username'] = params['mobile'] params['nickname'] = params['name'] flag, userid = tools.create_merchant(params) if flag: return success(data={'userid': userid}) return error(RESP_CODE.DATAERR)
class QuestionViewHandler(BaseHandler): _get_handler_fields = [ Field('question_id', T_INT, False), ] _post_handler_fields = [ Field('name', T_STR, True), Field('status', T_INT, True), Field('content', T_STR, True), Field('question_id', T_INT, False), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): params = self.validator.data log.debug('class=QuestionViewHandler|params=%s', params) name = self.req.input()['name'] params['name'] = name question_id = params.get('question_id') question = Questions(question_id) question.load() if question.data['content']: content = base64.decode(question.data['content']) params['content'] = content return success(data=question.data) @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): data = {} values = {} params = self.validator.data question_id = params.pop('question_id') status = params.pop('status') name = params.pop('name') content = self.req.input().get('content') if status: values['status'] = status if name: values['name'] = name if content: content_str = base64.b64encode(content) values['content'] = content_str values['utime'] = tools.gen_now_str() question = Questions(question_id) ret = question.update(values) return success(data=data)
class QuestionsLazyLoadHandler(BaseHandler): ''' 默认返回所有parent=-1的根问题,其它点击加载 ''' _get_handler_fields = [Field('parent', T_INT, False)] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): ret = [] params = self.validator.data parent = params.get('parent') if not parent: parent = -1 data = Questions.load_current_children(parent=parent) if data: for record in data: if not record['content']: continue else: content = base64.b64decode(record['content']) record['content'] = content ret.extend(data) return json.dumps(ret)
class TradeOrderListHandler(BaseHandler): _get_handler_fields = [ Field('page', T_INT, False), Field('maxnum', T_INT, False), Field('syssn', T_STR, True), Field('start_time', T_STR, True), Field('end_time', T_STR, True), Field('consumer_mobile', T_STR, True), Field('consumer_name', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data ={} params = self.validator.data info, num = TradeOrder.page(**params) data['num'] = num data['info'] = info if data['info']: for record in data['info']: trans_amt(record) return success(data=data)
class UserStateChangeHandler(BaseHandler): _post_handler_fields = [ Field('state', T_INT, False), Field('user_id', T_INT, False), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): params = self.validator.data user_state = params['state'] user_id = params['user_id'] user = User(user_id) ret = user.update(value={'state': user_state}) if ret != 1: return error(RESP_CODE.DBERR) return success(data={})
class RefundHandler(BaseHandler): _post_handler_fields = [ Field('syssn', T_STR, False), Field('txamt', T_INT, False) ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): data = {} params = self.validator.data syssn = params['syssn'] txamt = params['txamt'] r = Refund(config.APPID, config.SECRET, config.MCH_ID, config.API_KEY) flag, msg = r.run(syssn, txamt) if not flag: return error(RESP_CODE.UNKOWNERR, respmsg=msg) return success(data=data)
class TextInfoListHandler(BaseHandler): _get_handler_fields = [ Field('page', T_INT, False), Field('maxnum', T_INT, False), Field('name', T_STR, True), Field('box_name', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data = {} params = self.validator.data info, num = TextInfo.page_new(**params) data['num'] = num if info: # boxs = BoxList.load_all(where={}) # box_name_map = {} # for box in boxs: # box_name_map[box['id']] = box['name'] for item in info: text_id = item['id'] item['id'] = str(item['id']) item['box_id'] = str(item['box_id']) # item['box_name'] = box_name_map.get(item['box_id'], '') icon_name = item['icon'] item['icon'] = BASE_URL + icon_name item['icon_name'] = icon_name base_tools.trans_time(item, BoxList.DATETIME_KEY) detail = TextDetail.load_by_text_id(text_id) # item['content'] = detail.data.get('content') if detail.data else '' try: content_str = detail.data.get( 'content') if detail.data else '' content = base64.b64decode(content_str) except Exception: log.warn(traceback.format_exc()) content = detail.data.get('content') if detail.data else '' item['content'] = content data['info'] = info return success(data=data)
class TradeListHandler(BaseHandler): _get_handler_fields = [Field('openid', T_STR, False)] @with_validator_self def _get_handler(self): params = self.validator.data openid = params['openid'] data = TradeOrder.load_by_openid(openid) return success(data=data)
class CarouselCreateHandler(BaseHandler): _post_handler_fields = [ Field('name', T_STR, False), Field('available', T_INT, False), Field('priority', T_INT, False), Field('icon', T_STR, False), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): params = self.validator.data # 检查名称 ret = Carousel.create(params) log.debug("class=%s|create ret=%s", self.__class__.__name__, ret) if ret != 1: return error(errcode=RESP_CODE.DATAERR) return success(data={})
class LoginHandler(core.Handler): _post_handler_fields = [ Field('mobile', T_REG, False, match=r'^(1\d{10})$'), Field('password', T_STR, False), ] @with_database('uyu_core') def _get_div_type(self, userid): ret = self.db.select_one('stores', {"userid": userid}) chan_id = ret["channel_id"] ret = self.db.select_one("channel", {"id": chan_id}) is_prepayment = ret["is_prepayment"] return is_prepayment @uyu_set_cookie(g_rt.redis_pool, cookie_conf, UYU_SYS_ROLE_STORE) @with_validator_self def _post_handler(self, *args): params = self.validator.data mobile = params['mobile'] password = params["password"] u_op = UUser() ret = u_op.call("check_userlogin", mobile, password, UYU_SYS_ROLE_STORE) if not u_op.login or ret == UYU_OP_ERR: log.warn("mobile: %s login forbidden", mobile) return error(UAURET.USERERR) log.debug("get user data: %s", u_op.udata) log.debug("userid: %d login succ", u_op.udata["id"]) is_prepayment = self._get_div_type(u_op.udata["id"]) return success({ "userid": u_op.udata["id"], "is_prepayment": is_prepayment }) def POST(self, *args): ret = self._post_handler(args) log.debug("ret: %s", ret) return ret
class QueryHandler(BaseHandler): _get_handler_fields = [Field('syssn', T_STR, False)] @with_validator_self def _get_handler(self): params = self.validator.data syssn = params['syssn'] q = Query(config.APPID, config.SECRET, config.MCH_ID, config.API_KEY) result = q.run(syssn) return success(data=result)
class RateListHandler(BaseHandler): _get_handler_fields = [ Field('page', T_INT, False), Field('maxnum', T_INT, False), Field('name', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data = {} params = self.validator.data info, num = RateInfo.page(**params) data['num'] = num if info: data['info'] = info else: data['info'] = [] return success(data=data)
class RateAddHandler(BaseHandler): _post_handler_fields = [ Field('name', T_STR, False), Field('rate', T_FLOAT, False) ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): data = {} params = self.validator.data # 先检查是否有同名的数据存在 rate = RateInfo.load_by_name(params.get('name')) if rate.data: return error(RESP_CODE.DATAEXIST) ret = RateInfo.create(params) if ret != 1: return error(RESP_CODE.DATAERR) return success(data=data)
class PrecreateHandler(BaseHandler): _post_handler_fields = [ Field('openid', T_STR, False), Field('consumer_name', T_STR, True), Field('consumer_mobile', T_STR, True), Field('order_name', T_STR, False), Field('order_desc', T_STR, True), Field('txamt', T_INT, False), ] @with_validator_self def _post_handler(self): params = self.validator.data # check txamt txamt = params['txamt'] openid = params['openid'] consumer_name = params['consumer_name'] consumer_mobile = params['consumer_mobile'] order_name = params['order_name'] # order_desc是个json串带空格, validator返回的是个列表,存的有问题 # order_desc = params['order_desc'] order_desc = self.req.input().get('order_desc', '') p = Precreate(config.APPID, config.SECRET, config.MCH_ID, config.API_KEY) flag, msg, result = p.run(openid=openid, txamt=txamt, consumer_name=consumer_name, consumer_mobile=consumer_mobile, order_name=order_name, order_desc=order_desc) if not flag: return error(RESP_CODE.UNKOWNERR, resperr=msg, respmsg=msg) return success(data=result)
class UserPasswordChange(BaseHandler): _post_handler_fields = [ Field('user_id', T_INT, False), Field('password', T_STR, False), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): values = {} params = self.validator.data user_id = params['user_id'] password = params['password'] h = hashlib.md5(password) md5_password = h.hexdigest() log.info('md5_password=%s', md5_password) values['password'] = gen_passwd(md5_password) user = User(user_id) ret = user.update(value=values) if ret != 1: return error(RESP_CODE.DBERR) return success(data={})
class CarouselListHandler(BaseHandler): _get_handler_fields = [ Field('page', T_INT, False), Field('maxnum', T_INT, False), Field('name', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data = {} params = self.validator.data info, num = Carousel.page(**params) data['num'] = num if info: for item in info: item['id'] = str(item['id']) icon_name = item['icon'] item['icon'] = BASE_URL + icon_name item['icon_name'] = icon_name base_tools.trans_time(item, Carousel.DATETIME_KEY) data['info'] = info return success(data=data)
class UserViewHandler(BaseHandler): _get_handler_fields = [ Field('user_id', T_INT, False), ] _post_handler_fields = [ Field('user_id', T_INT, False), Field('mobile', T_STR, True), Field('user_type', T_STR, True), Field('email', T_STR, True), Field('name', T_STR, True), Field('idnumber', T_STR, True), Field('province', T_STR, True), Field('city', T_STR, True), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): try: params = self.validator.data user_id = params.get('user_id') data = tools.get_merchant(user_id) return success(data=data) except Exception: log.warn(traceback.format_exc()) return error(RESP_CODE.SERVERERR) @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): try: params = self.validator.data user_id = params.pop('user_id') values = params tools.update_merchant(user_id, values) return success(data={}) except Exception: log.warn(traceback.format_exc()) return error(RESP_CODE.SERVERERR)
class TradeOrderViewHandler(BaseHandler): _get_handler_fields = [ Field('syssn', T_STR, False), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): params = self.validator.data syssn = params['syssn'] trade = TradeOrder(syssn) if not trade.data: return error(RESP_CODE.DATAERR) return success(data=trade.data)
class GenOpenidHandler(BaseHandler): _get_handler_fields = [Field('js_code', T_STR, False)] @with_validator_self def _get_handler(self): data = {} params = self.validator.data js_code = params['js_code'] g = GenOpenid(config.APPID, config.SECRET, config.MCH_ID, config.API_KEY) openid, msg = g.run(js_code) if not openid: return error(RESP_CODE.UNKOWNERR, respmsg=msg) data['openid'] = openid return success(data)
class BoxInfoHandler(BaseHandler): _get_handler_fields = [ Field('box_id', T_INT, False), ] @with_validator_self def _get_handler(self): data = {} params = self.validator.data box_id = params.get('box_id') box = BoxList(box_id) box.load() if not box.data: return error(errcode=RESP_CODE.DATAERR) box_type = box.data.get('box_type') data['box_type'] = box_type if box_type == define.BOX_TYPE_ORDER: order = Order.load_by_box_id(box_id) if order.data: log.info('order data=%s', order.data) order.data['box_id'] = str(order.data['box_id']) order.data['goods_picture'] = config.BASE_URL + order.data[ 'goods_picture'] data['info'] = order.data if order.data else {} elif box_type == define.BOX_TYPE_TEXT: text_info = TextInfo.load_by_box_id(box_id) if text_info.data: log.info('text_info data=%s', text_info.data) for item in text_info.data: item['box_id'] = str(item['box_id']) item['icon'] = config.BASE_URL + item['icon'] # param_str = 'text_id=%s' % item['text_id'] # item['text_detail_url'] = config.TEXT_DETAIL_PREFIX_URL + '?' + param_str item['text_detail_url'] = config.TEXT_DETAIL_PREFIX_URL data['info'] = text_info.data if text_info.data else [] else: where = {'available': define.BOX_ENABLE, 'parent': box_id} box = BoxList.load_all(where=where) for item in box: icon_name = item['icon'] item['icon'] = config.BASE_URL + icon_name data['info'] = box return success(data=data)
class QuestionSingleHandler(BaseHandler): _get_handler_fields = [ Field('parent', T_INT, False), ] @with_validator_self def _get_handler(self): data = {} params = self.validator.data parent = params.get('parent') ret = Questions.load_by_parent_single(parent) if ret: for record in ret: if not record['content']: continue content = base64.b64decode(record['content']) record['content'] = content data['children'] = ret if ret else [] return success(data=data)
class TextInfoDeleteHandler(BaseHandler): _post_handler_fields = [ Field('text_id', T_INT, False), ] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): params = self.validator.data text_id = params['text_id'] text = TextInfo(text_id) text.load() if not text.data: return error(RESP_CODE.DATAERR) delete_values = {'available': define.TEXT_TITLE_DISABLE} ret = text.update(delete_values) if ret != 1: return error(RESP_CODE.DBERR) return success(data={})
class BannerViewHandler(BaseHandler): _get_handler_fields = [] _post_handler_fields = [Field('content', T_STR, False)] @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _get_handler(self): data = {'content': ''} with open(BANNER_TEXT, 'rb') as f: content = f.read() data['content'] = content return success(data=data) @house_check_session(g_rt.redis_pool, cookie_conf) @with_validator_self def _post_handler(self): params = self.validator.data content = params['content'] with open(BANNER_TEXT, 'wb') as f: f.write(content) return success(data={})