Esempio n. 1
0
    async def get(self):
        current_user_id = self.cache_session['user']
        # """:type:User"""

        mongo_conn = self.get_async_mongo()

        user_col = mongo_conn['g_users']

        current_auth_user = await user_col.find_one(
            {'_id': ObjectId(current_user_id)})
        # print("user_col.find_one({'user_id': %s})" % current_user_id)

        if not current_auth_user:
            return ConstData.msg_fail

        res_dict = dict(
            user_id=current_auth_user["user_id"],
            nickname=current_auth_user["nickname"],
            rc_time=current_auth_user["rc_time"],
            # status=current_auth_user["status"],
        )

        res_data = jsontool.dumps(res_dict, ensure_ascii=False)
        dlog.debug(res_data)

        return get_std_json_response(data=res_data)
Esempio n. 2
0
    async def post(self):
        appid = self.get_argument('appid_form', None)
        appkey = self.get_argument('appkey_form', None)

        if list_have_none_mem(*[appid, appkey]):
            return ConstData.msg_args_wrong

        db = self.get_async_mongo()
        app_col = db.test_data_app

        test_data_app = await app_col.find_one({'app_id': str(appid)}
                                               )  # 后面要为app_id建立index
        if test_data_app is None:
            return ConstData.msg_none

        # 可以和数据库连接形成动态的验证
        if str(appkey) == str(test_data_app['app_key']):
            # todo:后面对于自动化的工具应用,要隔离部署,单独做一套体系,先默认使用某个人的信息了

            app_session = await self.create_app_session(
                app_id=appid, client_type=ClientTypeDict.api)

            if app_session is None:
                return ConstData.msg_forbidden

            # todo 后面要做高频的api接口的统计系统

            res_data = jsontool.dumps(app_session)
            dlog.debug(res_data)
            return get_std_json_response(data=res_data)
        else:
            return ConstData.msg_fail
Esempio n. 3
0
 def debug_out(self):
     """
     打印调试输出
     :return:
     """
     user_info = 'nickname:%s,\nsex:%s,\nprovice:%s,\nlogin succeed by wechat' \
                 % (self.nickname, self.sex, self.province)
     dlog.debug(user_info)
Esempio n. 4
0
    async def wrapper(*args, **kwargs):
        start_time = time.time()

        await method(*args, **kwargs)

        end_time = time.time()

        dlog.debug('run time consuming:%s(s)' % (end_time - start_time))
Esempio n. 5
0
 def create_menu(self, post_menu_data):
     """
     创建自定义菜单
     :return:
     """
     menu_url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s' % self.access_token
     menu_res = requests.post(menu_url, data=post_menu_data)
     dlog.debug(menu_res.text)
Esempio n. 6
0
 def send_tpl_msg(self, post_msg):
     """
     发送模板消息
     """
     url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s' % self.access_token
     res = requests.post(url, data=post_msg.encode('utf-8'))
     # res.encoding = 'utf-8'
     dlog.debug(res.text)  # todo 后面来个判断返回值
Esempio n. 7
0
 def build(self, project_name):
     """
     触发项目构建
     提供了阻塞的方式
     :param project_name:
     :return:
     """
     jen = jenkins.Jenkins(self.url,
                           username=self.user,
                           password=self.passwd)
     jen.build_job(project_name)
     dlog.debug("build %s succeed" % project_name)
Esempio n. 8
0
    def get_auth_user_info(self, **kwargs):
        """
        根据认证token来获取用户详细信息,
        因為目前不清楚,access_token的权限是仅限于当前用户,还是当前的app,
        所以暂先假设是公对本授权用户有访问权限吧。
        
        .. code::
        
            { 
                "openid":"OPENID",
                "nickname":"NICKNAME",
                "sex":1,
                "province":"PROVINCE",
                "city":"CITY",
                "country":"COUNTRY",
                "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
                "privilege":[
                "PRIVILEGE1", 
                "PRIVILEGE2"
                ],
                "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
            
            }
        
        
        
        """
        auth_access_token_res = kwargs.get('auth_access_token_res', None)
        if auth_access_token_res is None:
            dlog.debug('auth_access_token_res is None')
            return None

        dlog.debug(auth_access_token_res)
        auth_access_token = auth_access_token_res.get('access_token', None)
        if auth_access_token is None:
            return None
        # refresh_token = res_json['refresh_token']
        openid = auth_access_token_res['openid']
        # unionid = auth_access_token_res['unionid']

        wx_user_info_api = 'https://api.weixin.qq.com/sns/userinfo'
        user_info_param = dict(access_token=auth_access_token,
                               openid=openid,
                               lang='zh_CN')
        res_info = requests.get(wx_user_info_api, params=user_info_param)
        res_info.encoding = "utf-8"  # 不然就是乱码
        res_info_dict = json.loads(res_info.text)

        return res_info_dict
Esempio n. 9
0
    def get_jsapi_ticket(self):
        """
        GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket)
        :return:
        """
        url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi' \
              % self.access_token

        res = requests.get(url)
        dlog.debug(res.text)
        res_json = json.loads(res.text)
        if res_json.get('errmsg', None) == 'ok':
            self.jsapi_ticket = str(res_json.get('ticket'))  # unicode转str
            self.jsapi_timestamp = time.time()
            self.jsapi_ticket_expires_in = int(res_json.get('expires_in'))
Esempio n. 10
0
        def wrapper(self):  # 函数参数
            """
            :type self base.handlers.BaseHandler
            """
            wx_mp_help = kwargss.get('wx_mp_app', None)
            relative_path = kwargss.get('relative_path', '')

            dlog.debug("session_id:%s" % self.session.id)

            next_url = self.request.uri  # 回调之后继续重定向的路径,带参数
            dlog.debug('request.uri:%s', next_url)
            # next_url = '/%s%s' % (relative_path, next_url)  # 加上一个相对路径,因为有nginx做了一次匹配的,此项目只是一个二级路径
            if wx_mp_help is None:
                self.write('No wechat media platform')
                return

            next_para = dict(next=next_url)

            redirect_url = '%s?%s' % (wx_mp_help.mp_auth_cb_url,
                                      urllib.urlencode(next_para))

            if ('login_status' in self.session) and (
                    self.session['login_status'] is True):
                return method(self)

            # 如果不是登录的状态,则进行微信第三方认证
            state = self.session.id

            wx_webapp_auth_url = wx_mp_help.get_auth_callback_url(
                redirect_url=redirect_url, state=state)

            dlog.debug(wx_webapp_auth_url)
            self.redirect(wx_webapp_auth_url)
            return
Esempio n. 11
0
    def create_temp_qrcode(self):
        """
        创建临时二维码
        :return:
        """

        url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s' \
              % self.access_token

        scene_id = '123'

        post_data = """{
            "expire_seconds": 604800,
            "action_name": "QR_SCENE",
            "action_info": {"scene":
                {
                    "scene_id": %s
                }
            }
        }""" % (scene_id)

        res = requests.post(url, data=post_data)
        dlog.debug(res.text)
Esempio n. 12
0
    def set_title(self, mail_title='email-notice'):
        self.mail_title = mail_title

    def set_body(self, mail_body='noteice-body'):
        self.mail_body = mail_body

    def set_to_mail(self, to_mail=''):
        self.to_mail = to_mail

    def send(self):
        """
        发送邮件,title,body
        """
        server = smtplib.SMTP(self.smtp)
        server.login(self.account, self.psw)

        mail_content = string.join(
            ("From: %s" % self.from_mail, "To: %s" % self.to_mail,
             "Subject: %s" % self.mail_title, "", self.mail_body), "\r\n")

        server.sendmail(self.from_mail, [self.to_mail], mail_content)
        server.close()


if __name__ == '__main__':
    dlog.debug('run in main')

    mail = MailMsg()
    mail.send()
    dlog.debug('finished in main')
Esempio n. 13
0
 def test_get_time_tag(self):
     dlog.debug(get_time_tag())