def get(self, *args, **kwargs):
     jsCode = self.get_argument('jsCode')
     nickName = self.get_argument('nickName')
     sign = self.get_argument('sign')
     time = self.get_argument('time')
     md5Util = MD5Util()
     if sign == md5Util.md5Signature(time):
         logging = LogUtil().getLogging()
         # logging.info('----> jsCode: ' + jsCode)
         # logging.info('----> nickName: ' + nickName)
         # logging.info('----> sign: ' + sign)
         # logging.info('----> time: ' + time)
         httpUrl = 'https://api.weixin.qq.com/sns/jscode2session'
         param = {
             "appid": WxToken.APP_ID,
             "secret": WxToken.APP_SECRET,
             "js_code": str(jsCode),
             "grant_type": 'authorization_code'
         }
         body = HttpUtil.http_get(httpUrl, params=param)
         jsonBody = json.loads(body, "utf8")
         if isinstance(jsonBody, dict):
             if jsonBody.has_key('openid'):
                 jsonBody['result'] = True
             else:
                 jsonBody['result'] = False
         # logging.info(type(jsonBody))
         logging.info('--->session json: ' + str(jsonBody))
     else:
         jsonBody = json.loads(u'校验失败', "utf8")
     self.write(jsonBody)
Beispiel #2
0
 def get(self, *args, **kwargs):
     page = int(self.get_argument('page'))
     size = int(self.get_argument('size'))
     sign = self.get_argument('sign')
     time = self.get_argument('time')
     logging = LogUtil().getLogging()
     baseResponse = BaseResponse()
     md5Util = MD5Util()
     if sign == md5Util.md5Signature(time):
         getCategory = GetCategory()
         homeDiscoverList = getCategory.getHomeDiscoverList(page_num=page, page_size=size)
         baseResponse.code = ResponseCode.op_success
         baseResponse.desc = ResponseCode.op_success_desc
         homeDiscoverCount = getCategory.getHomeDiscoverCount().get('count')
         logging.info('---> homeDiscoverCount: ' + str(homeDiscoverCount))
         page_total = (homeDiscoverCount / size) + (1 if homeDiscoverCount % size > 0 else 0)
         baseResponse.pageNum = page
         baseResponse.pageSize = size
         baseResponse.page_total = page_total
         baseResponse.totalCount = homeDiscoverCount
         if homeDiscoverList:
             for homeDiscover in homeDiscoverList:
                 baseResponse.append(homeDiscover)
     else:
         baseResponse.code = ResponseCode.fail_check_api_md5
         baseResponse.desc = ResponseCode.fail_check_api_md5_desc
     json_str = json.dumps(baseResponse, cls=HomeDiscoverEncoder)
     self.write(json_str)
Beispiel #3
0
class WxBotUtil:
    def __init__(self, qr_code_path=def_qr_path):
        self.logging = LogUtil().getLogging()
        self.qr_code_path = def_qr_path
        self.call_times = 0

    def __call__(self, *args, **kwargs):
        self.login()

    def login_callback(self):
        pass

    def qr_callback(self, **kwargs):
        uuid = kwargs['uuid']
        status = kwargs['status']
        qrcode = kwargs['qrcode']
        # self.logging.info("---> uuid: " + uuid)
        # self.logging.info("---> status: " + status)
        # self.logging.info("---> qrcode: " + qrcode)
        # print "---> uuid: ", uuid
        # print "---> status: ", status
        # print "---> qrcode: ", qrcode
        if self.call_times < 3:
            with open(self.qr_code_path, 'wb') as qr_file:
                qr_file.write(qrcode)
                qr_file.close()
            NotifyAdmin().sendMsg(u'请网页扫码登陆运维微信', subject=SMS_SUBJECT_WX_LOGIN)
            self.call_times = self.call_times + 1

    def login_by_thread(self):
        thr = threading.Thread(target=self.login)
        thr.start()
        thr.join()

    def login(self):
        # remove the qr_code.png first
        if os.path.exists(self.qr_code_path):
            self.logging.info("---> remove qr code: " + str(self.qr_code_path))
            os.remove(self.qr_code_path)
        try:
            weichatListen = WeiChatListen(console_qr=True,
                                          qr_path=self.qr_code_path,
                                          qr_callback=self.qr_callback,
                                          login_callback=self.login_callback)
            # 启用puid
            weichatListen.bot.enable_puid('wxpy_puid.pkl')
            # my = weichatListen.bot.friends().search('asher')[0]
            # print weichatListen.bot.friends()
            my = weichatListen.bot.friends()[0]
            weichatListen.bot.self.add()
            weichatListen.bot.self.accept()
            weichatListen.listen(receivers=my)
            weichatListen.bot.join()
        except Exception as ex:
            self.logging.warn(ex)
            self.logging.warn("---> qr code exception: " +
                              str(sys.exc_info()[0]))
 def get(self, *args, **kwargs):
     sign = self.get_argument('sign')
     time = self.get_argument('time')
     logging = LogUtil().getLogging()
     baseResponse = BaseResponse()
     md5Util = MD5Util()
     if sign == md5Util.md5Signature(time):
         getAdverts = GetAdverts()
         netAdvertsList = getAdverts.getLastAdverts(6)
         logging.info("---> netAdvertsList: " + str(netAdvertsList))
         baseResponse.code = ResponseCode.op_success
         baseResponse.desc = ResponseCode.op_success_desc
         baseResponse.data = netAdvertsList
     else:
         baseResponse.code = ResponseCode.fail_check_api_md5
         baseResponse.desc = ResponseCode.fail_check_api_md5_desc
     json_str = json.dumps(baseResponse, cls=AdvertsEncoder)
     self.write(json_str)
    def post(self, *args, **kwargs):
        logging = LogUtil().getLogging()
        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        sign = param['sign']
        time = param['time']
        admin_tel = param['tel']
        sms_pwd = param['sms']

        permissionMgr = PermissionManager()
        baseResponse = permissionMgr.checkAdminPermissionWithLoginStatus(sign=sign, time=time,
                                                                         admin_tel=admin_tel, sms_pwd=sms_pwd)
        logging.info('baseResponse code: ', baseResponse.code)
        # 登录还在有效期内。
        if baseResponse.code == ResponseCode.success_check_admin_permission:
            # 登录成功
            baseResponse.code = ResponseCode.op_success
            baseResponse.desc = ResponseCode.op_success_desc
            baseResponse.data = u'恭喜! 系统登陆成功.'
            # 通知超管(我)
            admin_sms_msg = '用户 {tel} 已成功登陆后台系统.'.format(tel=admin_tel)
            notifyAdmin = NotifyAdmin()
            notifyAdmin.sendMsg(sms_msg=admin_sms_msg, subject=SMS_SUBJECT_LOGIN)
            notifyAdmin.sendWxMsg(msg=admin_sms_msg)
        elif baseResponse.code == ResponseCode.fail_admin_out_of_date \
                or baseResponse.code == ResponseCode.fail_admin_login:
            adminMgr = AdminManager()
            loginResult = adminMgr.login(admin_tel=admin_tel, sms_pwd=sms_pwd)
            if loginResult:
                baseResponse.data = u'您已成功登录!'
                baseResponse.code = ResponseCode.op_success
                baseResponse.desc = ResponseCode.op_success_desc
            else:
                baseResponse.data = u'新密码已发送至手机, 请按新密码重新登录。'
        json_str = json.dumps(baseResponse, cls=StrEncoder)
        self.write(json_str)