Пример #1
0
 def getAppauthToken(cls, admin_id=None):
     if admin_id:
         ali_app_auth_token = "ali_app_auth_token:%s" % admin_id
         ali_app_refresh_token = "ali_app_refresh_token:%s" % admin_id
     else:
         ali_app_auth_token = "ali_app_auth_token_senguo"
         ali_app_refresh_token = "ali_app_refresh_token_senguo"
     if redis.get(ali_app_auth_token
                  ) and redis.ttl(ali_app_auth_token) > 3600 * 24 * 30:
         ali_auth_token = redis.get(ali_app_auth_token).decode('utf-8')
     elif redis.get(ali_app_refresh_token):
         ali_refresh_token = redis.get(ali_app_refresh_token).decode(
             'utf-8')
         if_success, ret_dict = cls.get_ali_auth_token(
             "refresh_token", ali_refresh_token)
         if if_success:
             ali_auth_token = ret_dict["app_auth_token"]
             redis.set(ali_app_auth_token, ali_auth_token,
                       ret_dict["expires_in"])
             redis.set(ali_app_refresh_token, ret_dict["app_refresh_token"],
                       ret_dict["re_expires_in"])
         else:
             ali_auth_token = None
     else:
         ali_auth_token = None
     return ali_auth_token
Пример #2
0
def gen_msg_token(phone, use):
    """生成并发送验证码,将验证码存储到redis中,缓存5分钟。

    输入参数:
    phone 发送对象的手机号
    use 验证码用途标志,会存储在redis中,同时通过此标志获取用途文本并展示在短信中

    返回值:
    发送成功时返回True,否则返回False或错误提示
    """
    population_seq = "0123456789"  # 组成验证码元素的序列
    code_length = 4  # 验证码长度
    expire_minutes = 5  # 验证码有效时间

    # 获取验证码用途文本
    use_text = VerifyCodeUse.get_use_text(use)
    if not use_text:
        return "invalid use type"
    # 生成验证码
    code = "".join([random.choice(population_seq) for i in range(code_length)])
    # 发送验证码
    if options.debug:
        print("VerifyCode:", code)
        status = True
    else:
        status = send_yunpian_verify_code(phone, code, use_text)
    # 存储验证码
    if status is True:
        h = "cf_verify_code:{}:{}".format(use, phone)
        redis.set(h, code, expire_minutes * 60)
        return True
    else:
        return status
Пример #3
0
    def send_server_error(request_uri, **kwargs):
        # 获取错误消息
        server_error_messsage = traceback.format_exception(*kwargs["exc_info"])
        error_type = server_error_messsage[-1].strip()
        server_error_messsage = json.dumps(server_error_messsage)

        # 存入 Redis
        error_key = UrlShorten.get_hex(server_error_messsage)
        server_error = "Ph_Server_Error:%s" % error_key
        server_error_times = "Ph_Server_Error_Times:%s" % error_key

        # 报错已存在,增加报错计数
        if redis.exists(server_error):
            redis.incr(server_error_times)
        else:
            redis.set(server_error, server_error_messsage, 72 * 60 * 60)
            redis.set(server_error_times, 1, 72 * 60 * 60)

            try:
                ServerAlarm.send_dingtalk_msg(
                    request_uri,
                    datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                    error_key,
                    error_type,
                )
            except:
                pass
Пример #4
0
def set_latest_version(param):
    info = {
        "Android": {
            "verCode": "1801220947",
            "verName": "1.0.9",
            "verDate": "2018-01-23",
            "url": "http://d.senguo.cc/android/SenguoSecretary-V1.0.9-1801220947-release.apk",
            "log": "增加老板直接开票功能"
        }
    }

    redis.set("cgapp_update_info", json.dumps(info))
Пример #5
0
 def get_client_access_token(cls):  # 微信接口调用所需要的access_token,不需要用户授权
     access_token = redis.get('pf_access_token')
     if access_token:
         return access_token.decode('utf-8')
     else:
         data = json.loads(urllib.request.urlopen(cls.client_access_token_url).read().decode("utf-8"))
         if 'access_token' in data:
             access_token = data['access_token']
             redis.set("pf_access_token",access_token,3600)
             return access_token
         else:
             return None
Пример #6
0
    def get(self):
        # 用途
        action = self.args["action"]

        if action == "login":
            ticket_url, scene_id = WxTicketUrl.get_ticket_url(source="login")
        elif action == "bind":
            if not self.current_user:
                return self.write_error(401)
            ticket_url, scene_id = WxTicketUrl.get_ticket_url(source="bind")
            # 使用随机场景值记录用户 ID
            h = "ph_scene_bind_account:%s" % scene_id
            redis.set(h, self.current_user.id, 10 * 60)
        else:
            return self.send_fail("action invalid")

        return self.send_success(ticket_url=ticket_url, scene_id=scene_id)
Пример #7
0
 def send_server_error(cls, slave_session, request_uri, **kwargs):
     """服务器出现500错误时发送模板消息提醒
         服务器错误使用redis缓存72小时
         错误字典使用错误内容生成的md5值做key
         查看路径使用key参数用于查看不同的错误
     """
     import traceback
     import time
     import json
     from urllib import parse
     from handlers.base.pub_func import UrlShorten
     from dal.db_configs import redis
     server_error_messsage = traceback.format_exception(*kwargs["exc_info"])
     intro = '采购助手服务器500报错'
     content = '出错地址 ' + str(request_uri)
     time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
     describe = '请尽快检查错误并修正'
     hour_now = int(datetime.datetime.now().strftime('%H'))
     timestamp = int(time.time())
     to_users = [104, 105, 156, 1085]
     acs = slave_session.query(models.Accountinfo.wx_openid).filter(
         models.Accountinfo.id.in_(to_users)).all()
     server_error_messsage = json.dumps(server_error_messsage)
     errror_key = UrlShorten.get_hex(server_error_messsage)
     server_error = "Server_Error:%s" % errror_key
     server_error_times = "Server_Error_Times:%s" % errror_key
     if redis.get(server_error):
         redis.incr(server_error_times)
     else:
         redis.set(server_error, server_error_messsage, 72 * 60 * 60)
         redis.set(server_error_times, 1, 72 * 60 * 60)
         for account in acs:
             try:
                 if hour_now in [4, 5, 6, 7]:
                     countdown = (9 - hour_now) * 3600 - 1800
                 else:
                     countdown = 0
                 # 暂时使用的
                 cls.send_servererror_msg(
                     account[0], "https://pf.senguo.cc/super/servererror/" +
                     str(errror_key), intro, content, time_now, describe)
             except:
                 pass
Пример #8
0
    def record_purchasing_dynamics(self, record_type, purchase_goods,
                                   **last_data_dict):
        # 记录采购动态
        purchase_goods_dict = purchase_goods.to_dict()

        # 当前操作用户
        purchase_goods_dict["creator_id"] = self.current_user.id

        # 获取上一次的采购单价、数量、重量、小计
        purchase_goods_dict["last_price"] = last_data_dict.get("last_price")
        purchase_goods_dict["last_actual_amount"] = last_data_dict.get(
            "last_actual_amount")
        purchase_goods_dict["last_actual_weight"] = last_data_dict.get(
            "last_actual_weight")
        purchase_goods_dict["last_subtotal"] = last_data_dict.get(
            "last_subtotal")

        # 上一次的商品名称
        purchase_goods_dict["last_goods_name"] = last_data_dict.get(
            "last_goods_name")
        # 上一次的供货商id
        purchase_goods_dict["last_firm_id"] = last_data_dict.get(
            "last_firm_id")

        purchasing_dynamics.delay(record_type, purchase_goods_dict)

        # 发送采购动态更新提醒
        staff_ids = self.session.query(models.Staff)\
            .filter(models.Staff.purchaser_status == 1,
                    models.Staff.station_id == self.current_station.id,
                    models.Staff.status == 0)\
            .all()
        for staff in staff_ids:
            redis.set(
                KEY_PURCHASING_DYNAMICS_NOTIFICATIONS.format(
                    purchase_goods.purchase_order_id, staff.id,
                    self.current_station.id), "purchasing_dynamics")