def put(self): id = self.get_argument('id', '0') type_id = self.get_argument('type_id', '0') discount = self.get_argument('discount', '') score = self.get_argument('score', '0') count = self.get_argument('count', '0') rule = self.get_argument('rule', '') state = self.get_argument('state', 'false') sql = """ update wx_discount set type_id = %d, discount = '%s', score = %d, count = %d, rule = '%s', state = %s where id = %d """ % (int(type_id), discount, int(score), int(count), rule, state, int(id)) res = { 'code': 0 } try: conn = Postgres() conn.execute(sql) res['msg'] = '保存成功!' except Exception as e: logger.error('优惠券保存失败:%s' % e) res['code'] = -1 res['msg'] = '保存失败!' finally: return self.finish(res)
def post(self): username = self.get_argument('username', None) password = self.get_argument('password', None) res = { 'code': 0 } if not username or not password: res['code'] = -1 res['msg'] = '账号或密码不能为空!' return self.finish(res) conn = Postgres() user = conn.fetchone("select id from admin_user where username = '******'" % username) if user: res['code'] = -1 res['msg'] = '该用户已经存在!' return self.finish(res) password_md5 = hashlib.md5(password.encode('utf-8')).hexdigest() try: sql = """ insert into admin_user (username, password) values ('%s', '%s') """ % (username, password_md5) conn.execute(sql) res['msg'] = '用户注册成功!' except Exception as e: res['code'] = -1 res['msg'] = '用户注册失败!' logger.error(e) finally: return self.finish(res)
def post(self): type_id = self.get_argument('type_id', None) discount = self.get_argument('discount', None) score = self.get_argument('score', None) state = self.get_argument('state', None) rule = self.get_argument('rule', None) res = { 'code': 0 } if not type_id: res['code'] = -1 res['msg'] = '优惠券类型不能为空!' return self.finish(res) sql = """ insert into wx_discount (type_id, discount, score, state, rule) values (%d, '%s', %d, %s, '%s') """ % (int(type_id), discount, int(score), state, rule) try: conn = Postgres() conn.execute(sql) res['msg'] = '添加成功' except Exception as e: logger.error('优惠券添加失败:%s' % e) res['code'] = -1 res['msg'] = '添加失败!' finally: return self.finish(res)
def post(self): type = self.get_argument('type', None) name = self.get_argument('name', None) res = {'code': 0} if not type: res['code'] = -1 res['msg'] = '类型不能为空!' return self.finish(res) if not name: res['code'] = -1 res['msg'] = '名称不能为空!' return self.finish(res) sql = """ insert into wx_discount_type (type, name) values ('%s', '%s') """ % (type, name) try: conn = Postgres() conn.execute(sql) res['msg'] = '添加成功' except Exception as e: logger.error('优惠券类型添加失败:%s' % e) res['code'] = -1 res['msg'] = '添加失败!' finally: return self.finish(res)
def _query(self, sql, param): try: if param is None: count = self._cursor.execute(sql) else: count = self._cursor.execute(sql, param) except MysqlError as e: logger.error('[ERROR] SQL语句执行失败: %s', sql) raise MysqlError() return count
def delete(self): id = self.get_argument('id', '0') sql = """ delete from wx_discount_type where id = %d """ % int(id) res = {'code': 0} try: conn = Postgres() conn.execute(sql) res['msg'] = '删除成功!' except Exception as e: logger.error('优惠券类型删除失败:%s' % e) res['code'] = -1 res['msg'] = '删除失败!' finally: self.finish(res)
def get(self): try: signature = self.get_argument('signature') timestamp = self.get_argument('timestamp') nonce = self.get_argument('nonce') echostr = self.get_argument('echostr') token = "LiJiaF" tmp = [token, timestamp, nonce] tmp.sort() tmp = "".join(tmp) hashcode = hashlib.sha1(tmp.encode('utf8')).hexdigest() if hashcode == signature: logger.info('微信公众号接入成功') self.write(echostr) else: logger.error('微信公众号接入失败') self.write('') except Exception as err: self.write(err)
def post(self): name = self.get_argument('name', '') res = {'code': 0} try: name_check = FiledsCheck(name, msg='分类名称', min_lenght=2, max_length=10) name_check.check_null() name_check.check_length() except FiledsError as msg: res['code'] = 1 res['msg'] = str(msg) logger.warning('[ERROR] %s' % str(msg)) return self.finish(res) try: sql = 'insert into article_class (name) values ("%s")' % name count = db.insert(sql) if count: logger.info('[SUCCESS] %s 添加成功' % name) res['msg'] = '添加成功!' else: raise MysqlError except MysqlError as e: logger.error('[ERROR] %s 添加失败' % name) res['code'] = 1 res['msg'] = '添加失败,请重新添加!' print(e) except Exception as e: logger.error('[ERROR] %s 添加失败' % name) res['code'] = 1 res['msg'] = '添加失败,请重新添加!' print(e) return self.finish(res)
def put(self): id = self.get_argument('id', '0') type = self.get_argument('type', '') name = self.get_argument('name', '') sql = """ update wx_discount_type set type = '%s', name = '%s' where id = %d """ % (type, name, int(id)) res = {'code': 0} try: conn = Postgres() conn.execute(sql) res['msg'] = '保存成功!' except Exception as e: logger.error('优惠券类型保存失败:%s' % e) res['code'] = -1 res['msg'] = '保存失败!' finally: return self.finish(res)
def delete(self): openid = self.get_argument('openid', None) sql = """ delete from wx_user where openid = '%s'; """ % openid sql += """ delete from wx_user_discount_rel where openid = '%s'; """ % openid res = {'code': 0} try: conn = Postgres() conn.execute(sql) res['msg'] = '删除成功!' except Exception as e: logger.error('用户管理删除失败:%s' % e) res['code'] = -1 res['msg'] = '删除失败!' finally: self.finish(res)
def put(self): id = self.get_argument('id', '0') score = self.get_argument('score', '0') experience = self.get_argument('experience', '0') is_admin = self.get_argument('is_admin', 'false') sql = """ update wx_user set score = %d, experience = %d, is_admin = %s where id = %d """ % (int(score), int(experience), is_admin, int(id)) res = {'code': 0} try: conn = Postgres() conn.execute(sql) res['msg'] = '保存成功!' except Exception as e: logger.error('用户管理保存失败:%s' % e) res['code'] = -1 res['msg'] = '保存失败!' finally: return self.finish(res)
def put(self): id = self.get_argument('id', '0') state = self.get_argument('state', 'false') use_time = datetime.strftime(datetime.now(), '%Y-%m-%d') sql = """ update wx_user_discount_rel set state = %s, use_time = '%s' where id = %d """ % (state, use_time, int(id)) res = {'code': 0} try: conn = Postgres() conn.execute(sql) res['msg'] = '保存成功!' except Exception as e: logger.error('优惠券保存失败:%s' % e) res['code'] = -1 res['msg'] = '保存失败!' finally: return self.finish(res)
def get_media_count(self): access_token = self.get_access_token() postUrl = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=%s" % access_token urlResp = request.urlopen(url=postUrl) urlResp = json.loads(urlResp.read().decode('utf-8')) if not urlResp.get('errcode'): logger.info('media_count获取成功') logger.info('media_count: %s' % urlResp) else: logger.error('media_count获取失败') logger.error('errcode: %s' % urlResp['errcode']) logger.error('errmsg: %s' % urlResp['errmsg'])
def get_media_list(self, type='image', offset=0, count=10): access_token = self.get_access_token() postUrl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=%s" % access_token postData = {'type': type, 'offset': offset, 'count': count} headers = {'content-type': 'application/json'} req = request.Request(url=postUrl, headers=headers, data=json.dumps(postData).encode('utf-8'), method='POST') urlResp = json.loads(request.urlopen(req).read().decode('utf-8')) if not urlResp.get('errcode'): logger.info('media_list获取成功') logger.info('media_list: %s' % urlResp) return urlResp else: logger.error('media_list获取失败') logger.error('errcode: %s' % urlResp['errcode']) logger.error('errmsg: %s' % urlResp['errmsg'])
def get_jsapi_ticket(self): jsapi_ticket = '' if not redis.exists('token:jsapi_ticket'): access_token = self.get_access_token() postUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi" % access_token urlResp = request.urlopen(postUrl) urlResp = json.loads(urlResp.read().decode('utf-8')) if not urlResp.get('errcode'): redis.set('token:jsapi_ticket', urlResp['ticket']) redis.expire('token:jsapi_ticket', 7000) jsapi_ticket = urlResp['ticket'] logger.info('jsapi_ticket获取成功') else: logger.error('jsapi_ticket获取失败') logger.error('errcode: %s' % urlResp['errcode']) logger.error('errmsg: %s' % urlResp['errmsg']) else: jsapi_ticket = redis.get('token:jsapi_ticket').decode('utf-8') logger.info('jsapi_ticket获取成功') return jsapi_ticket
def get_access_token(self): access_token = '' if not redis.exists('token:access_token'): postUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % ( APPID, APPSECRET) urlResp = request.urlopen(postUrl) urlResp = json.loads(urlResp.read().decode('utf-8')) if not urlResp.get('errcode'): redis.set('token:access_token', urlResp['access_token']) redis.expire('token:access_token', 7000) access_token = urlResp['access_token'] logger.info('access_token获取成功') else: logger.error('access_token获取失败') logger.error('errcode: %s' % urlResp['errcode']) logger.error('errmsg: %s' % urlResp['errmsg']) else: access_token = redis.get('token:access_token').decode('utf-8') logger.info('access_token获取成功') return access_token