Exemplo n.º 1
0
    def gen_user_mqtt(user_id):
        from application.services.setting_service import SettingService
        import hashlib, time, json
        mqtt_setting = SettingService.get_setting_by_name('mqtt')
        mqtt_pub_setting = json.loads(mqtt_setting.value)
        user_uniq = hashlib.md5(str(user_id).encode()).hexdigest()
        resp = {
            "mqtt_server":
            mqtt_pub_setting['mqtt_server'],
            "mqtt_username":
            mqtt_pub_setting['mqtt_username'],
            "mqtt_password":
            mqtt_pub_setting['mqtt_password'],
            "mqtt_port":
            mqtt_pub_setting['mqtt_port'],
            "mqtt_client_id":
            user_uniq + '_' + str(int(time.time())),
            "mqtt_qos":
            mqtt_pub_setting['mqtt_qos'],
            "mqtt_up_topic":
            mqtt_pub_setting['mqtt_up_topic_prefix'] + user_uniq,
            "mqtt_down_topic":
            mqtt_pub_setting['mqtt_down_topic_prefix'] + user_uniq
        }

        return resp if resp else None
Exemplo n.º 2
0
    def process(self):
        password_reset = request.args.get('key', type=str)

        if not password_reset:
            return make_response(render_template('error.html'), 200)

        #判断password_reset是否还有效,如有效则取出来user_id
        user = UserService.get_user_by_pwreset(password_reset)
        website_proxy = SettingService.get_setting_by_name('api_gateway_0')
        if website_proxy:
            url = website_proxy.value
            if url[-1:] != '/':
                url += '/'
            url = url + "app/passwordreset"
        else:
            logging.error(
                "ERROR: can not get website_proxy on appPasswordresetView")
            return make_response(render_template('error.html', flag="busy"),
                                 200)

        #for debug
        # url = "http://127.0.0.1:8080/app/passwordreset"

        if not user or (int(time.time()) - user.password_reset_timestamp >
                        24 * 3600):  #密码重置链接已经超过24小时
            return make_response(render_template('error.html', flag="invalid"),
                                 200)
        return make_response(
            render_template('resetpassword.html',
                            email=user.email,
                            passwordreset_url=url), 200)
Exemplo n.º 3
0
    def process(self):
        # TODO 用于多语言及切换货币
        Country = "cn"

        data = request.args
        logging.info(f"appPopupHtmlQRcode(GET: /app/app_popup/qrcode):{data}")

        # 检查入参及取到用户信息
        try:
            token = data['token']
            thunderServiceID = data['thunderserviceID']
            data = jwt.decode(token, config.settings.SECRET_KEY)
            user_info = UserService.get_user(data['user_id'])
        except:
            # invalid token or no thunderServiceID input
            return "Token expired or no thunderServiceID find, please try again", 200

        # 生成order_id
        if str(thunderServiceID) not in thunder_service_ID['FUFEI']:
            logging.error(f"ERROR: addExpressorder: 5005")
            return {
                       "code": 5005,
                       "message": returncode['5005']
                   },200

        ts = time.time()
        ts_ms = int(ts*1000-int(ts)*1000)
        order_id = 'EX'+time.strftime("%Y%m%d%H%M%S",time.localtime(ts))+'S'+str(ts_ms)+'U'+user_info.email[:1]+'P'+str(thunderServiceID)

        # 取出定价,根据coupon,调整amount
        thunderservice_selected = GetThunderservice.get_thunderservice(thunderServiceID)
        amount = thunderservice_selected.price_cn if Country == 'cn' else thunderservice_selected.price
        coupon = data.get('coupon')
        # TODO coupon
        amount = amount

        # 取到thunderservice中对应的desc
        description = thunderservice_selected.description if thunderservice_selected else None

        # expressorder不需要发送订单邮件
        emailNotification = False

        # 添加订单记录
        OrderService.add_order(order_id,user_info.id, thunderServiceID,time.time()*1000,coupon,amount,emailNotification,description)
        db.session.commit()

        # 增加记录到K线图
        KService_action = '201'
        KService.add_record(action=KService_action,parameter1=amount,parameter2='New',timestamp=int(time.time()))

        # 取到要拼接的跳转域名
        website_proxy = SettingService.get_setting_by_name('api_gateway_0')
        if website_proxy:
            url = website_proxy.value
            if url[-1:] != '/':
                url += '/'
            url = url+"app/passwordreset?key="+password_reset
        else:
            raise Exception('5007')

        url = 'lasdjfljdslkasjflkasjflkjdslkfjlksdjafkljdasfkljkljdfkljsdklfjalfkjdlskfj'

        # 生成qrcode
        qr = qrcode.QRCode(
            version=4,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=5,
            border=1
        )
        qr.add_data(url)
        qr.make(fit=True)
        qrimage = qr.make_image()

        byte_io=BytesIO()
        qrimage.save(byte_io, 'png')
        byte_io.seek(0)

        # 方法1:返回base64以后的字符串
        # return {"qr":base64.b64encode(byte_io.getvalue()).decode()}

        # 方法2:或者返回图片字节,可以在浏览器直接加载显示
        res = make_response(byte_io.getvalue()) # 设置响应体
        res.status = '200' # 设置http返回状态码
        res.headers['Content-Type'] = "image/png" # 设置响应头,必要步骤
        # return res

        # 方法3:结合2试试看
        # return u"data:image/png;base64," + base64.b64encode(byte_io.getvalue()).decode('ascii')

        logging.info(f"appPopupHtmlQRcode, response:{res}")
        return res
Exemplo n.º 4
0
    def process(self):
        trackinginput = self.parameters.get('body')
        user_body = self.parameters.get('body')
        try:
            #检查入参
            try:
                email = user_body.get('email')
            except:
                raise Exception("5004")

            #检查是否有此用户,有的话,发送重置密码链接,否则发送推广链接。
            user = UserService.get_user_by_email(email)
            if user:
                #生成重置密码的唯一值
                password_reset = hashlib.md5(str(
                    time.time()).encode()).hexdigest()

                #构造邮件内容
                website_proxy = SettingService.get_setting_by_name(
                    'api_gateway_0')
                if website_proxy:
                    url = website_proxy.value
                    if url[-1:] != '/':
                        url += '/'
                    url = url + "app/passwordreset?key=" + password_reset
                else:
                    raise Exception('5007')

                subject = "ThunderNetwork密码重置邮件"
                text = f"<p>尊敬的用户:<br/><br/> \
                        您请求了密码重置,请点击<a href='{url}'> {url} </a>重新设置该账户在ThunderNetwork中的密码。\
                        <br/><br/> \
                        如果没有自动打开浏览器页面,也可以复制上述链接到浏览器中进行设置。 \
                        <br/><br/> \
                        本次密码重置的请求,在24小时内有效。"

                #发重置密码邮件
                if PushService.sendMail("user.id", email, {
                        "type": "passwordreset",
                        "subject": subject,
                        "text": text
                }):
                    #把password_reset写入到user表
                    update_data = {
                        "password_reset": password_reset,
                        "password_reset_timestamp": int(time.time())
                    }
                    UserService.modify_user_by_id(user.id, update_data)
                    db.session.commit()

                #操作结束
                trackingoutput = "发送找回密码邮件成功"
                TrackingService.tracking(trackinginput, trackingoutput)

            else:
                #发推广邮件
                PushService.sendMail("", email, "")

            return {
                "code": 200,
                "message": "已经为您的邮箱发送了重置密码的邮件,请根据邮件内容进行操作。",
                "results": {}
            }

        except Exception as ex:
            logging.error(ex)
            logging.error(returncode[ex.args[0]])
            return {"code": ex.args[0], "message": returncode[ex.args[0]]}, 200