def setPasswdAndCode(token, passwd, code): uid = getUserId(token) u_info = tb_user.find_one({'_id': uid}) if not u_info: raise ResponseMsg(-9, '登录已失效') if len(passwd) != 32: raise ResponseMsg(-1, '密码无效') doc = {'passwd': passwd} if not u_info.get('usecode', ''): doc['usecode'] = code update({'_id': uid}, {'$set': doc})
def setInviteCode(token, code): uid = getUserId(token) result = checkInviteCode(code) if result: if str(result['_id']) == uid: raise ResponseMsg(-1, '不可使用自己的邀请码!') else: update({'_id': ObjectId(uid)}, { "$set": { 'use_invite_code': code, 'invite_time': datetime.datetime.now() } }) else: raise ResponseMsg(-1, '无效的邀请码')
def GET(self): url = self.get_argument('url', None) text = self.get_argument('text', None) img = self.get_argument('img', None) if not url and not text: raise ResponseMsg(-1, "参数异常") if len(text) < 5: raise ResponseMsg(-1, "口令内容长度不得小于5个字符") status, result = tbk.tpwd_create(url, text, img) if status: self._data = {'model': result} else: self._code = -1 self._msg = result
def request_withdraw(token, amount): u_info = getUserInfo(getUserId(token)) balance = float(u_info.get('balance', 0)) # 账户余额 amount = float(amount) # 提现金额 if amount < 0.1: raise ResponseMsg(-1, "提现金额不得低于0.1元") if amount > balance: raise ResponseMsg(-1, "账户余额不足") else: data = { 'uid': u_info['_id'], 'order_id': '', 'pay_date': '', 'amount': amount, 'alipay': u_info.get('alipay', ''), 'name': u_info.get('name', ''), 'status': 0, 'atime': datetime.datetime.now() } new_balance = str((int(balance * 100) - int(amount * 100)) / 100) tb_user.update_one({'_id': ObjectId(u_info['_id'])}, {'$set': { 'balance': new_balance }}) wid = str(tb_withdraw_log.insert_one(data).inserted_id) result = trans_to_account(wid, amount, data['alipay'], data['name']) out_biz_no = result.get('out_biz_no') if result.get('code') == '10000': new_data = { 'order_id': result['order_id'], 'pay_date': result['pay_date'], 'status': 1, 'reason': result['msg'], 'result': result } tb_withdraw_log.update_one({'_id': ObjectId(out_biz_no)}, {"$set": new_data}) return True, "提现成功!" else: # TODO 加个邮件通知 new_data = { 'status': 2, 'reason': result['sub_msg'], 'result': result } tb_withdraw_log.update_one({'_id': ObjectId(out_biz_no)}, {"$set": new_data}) return False, result['sub_msg']
def login(tel, code, type): tel = int(tel) if (type == 'code' and checkTelCode(tel, code)) or (type == 'passwd' and checkPasswd(tel, code)): u_info = tb_user.find_one({'tel': tel}) if not u_info: uid = tb_user.insert({ 'tel': tel, 'nickname': '', 'avatar': '', 'invite_code': '', 'use_invite_code': '', 'passwd': None, 'vip': 0, 'channel': 0, 'atime': datetime.datetime.now(), }) vip = 0 else: uid = u_info['_id'] vip = u_info.get('vip', 0) token = genToken(uid) tb_usertoken.insert({'uid': uid, 'token': token}) return token, vip else: raise ResponseMsg(-1, '验证码不正确' if type == 'code' else '密码不正确')
def uploadImgToTieTuKu(file_path): exception = None Num = 0 while Num < 3: try: Num += 1 # 贴图库的token在http://www.tietuku.com/manager/createtoken可以获取到, 上传方式选择本地上传; # aid在http://www.tietuku.com/manager/album可以获取到, 就是相册的ID token, aid = setting.TieTuKuTOKEN, setting.TieTuKuAID data = { 'deadline': int(time.time()) + 60, 'from': 'file', 'aid': aid, 'Token': token } files = {'file': open(file_path, "rb")} reponse = requests.post('http://up.imgapi.com/', data=data, files=files) json_data = json.loads(reponse.text) if json_data.__len__() > 5: os.remove(file_path) return json_data['linkurl'] else: exception = json_data except: pass raise ResponseMsg(-1, exception)
def POST(self): # TODO token = self.get_argument('token') data = self.get_argument('data', '{}') try: data = json.loads(data) print(data) except: raise ResponseMsg(-1, "无效的数据格式") user.add_coupon(token, data)
def checkTelCode(tel, code): # if code == '1234': # return True tel = int(tel) code = str(code) info = tb_telcode.find_one({'tel': tel, 'code': code}) if info: tb_telcode.remove({'tel': tel}) return True raise ResponseMsg(-1, '验证码不正确')
def getUserId(token): uid = Redis.getRedisValue(token) if uid: return ObjectId(uid) token_info = tb_usertoken.find_one({'token': token}) if token_info: uid = token_info.get('uid') Redis.setRedisValue(token, uid, 86400) return ObjectId(uid) raise ResponseMsg(-9, '登录已失效')
def sendTelCode(tel): tel = int(tel) code = str(random.random())[3:7] if SMS.send_sms(tel, 'SMS_91850019', code): tb_telcode.insert({ 'tel': tel, 'code': code, }) return True else: raise ResponseMsg(-1, '验证码发送失败,请稍后再试')
def GET(self): # 本月预估收益,今日预估收益,昨日预估收益,昵称,头像 token = self.get_argument('token', '') uid = user.getUserId(token) user_info = user.getUserInfo(uid) if user_info: user_info.pop('favorite') data = brokerage.detail(uid) user_info['channel_task'] = user_info['channel_task'] if user_info[ 'channel_task'] else {} self._data = { 'user': user_info, } self._data.update(data) else: raise ResponseMsg(-9, '登录已失效')
def detail(uid): uid = ObjectId(uid) now = datetime.datetime.now() last_month_one = datetime.datetime(now.year, now.month, 1) + relativedelta(months=-1) now_month_one = datetime.datetime(now.year, now.month, 1) today_start = datetime.datetime(now.year, now.month, now.day) yestoday_start = datetime.datetime(now.year, now.month, now.day) + relativedelta(days=-1) _ = tb_brokerage.find({ 'uid': uid, 'status': 'create', 'create_time': { '$gte': last_month_one, '$lt': now_month_one } }) last_moth_reckon = sum([float(f.get('pub_share_pre_fee', 0)) for f in _]) _ = tb_brokerage.find({ 'uid': uid, 'status': 'pay', 'create_time': { '$gte': last_month_one, '$lt': now_month_one } }) last_moth_pay = sum([float(f.get('pub_share_pre_fee', 0)) for f in _]) _ = tb_brokerage.find({ 'uid': uid, 'status': 'create', 'create_time': { '$gte': now_month_one } }) now_moth_reckon = sum([float(f.get('pub_share_pre_fee', 0)) for f in _]) _ = tb_brokerage.find({ 'uid': uid, 'status': 'create', 'create_time': { '$gte': today_start } }) today_reckon = sum([float(f.get('pub_share_pre_fee', 0)) for f in _]) today_num = _.count() _ = tb_brokerage.find({ 'uid': uid, 'status': 'create', 'create_time': { '$gte': yestoday_start, '$lt': today_start } }) yestoday_reckon = sum([float(f.get('pub_share_pre_fee', 0)) for f in _]) yestoday_num = _.count() u_info = tb_user.find_one({'_id': uid}) if not u_info: raise ResponseMsg(-9, '登录已失效!') return { 'balance': u_info.get('balance', 0), 'total_balance': '%.2f' % u_info.get('total_balance', 0), 'last_moth_pay': '%.2f' % last_moth_pay, # 上月结算到账 'last_moth_reckon': '%.2f' % last_moth_reckon, # 上月预估 'now_moth_reckon': '%.2f' % now_moth_reckon, # 本月预估 'today_reckon': '%.2f' % today_reckon, # 今天预估 'today_num': today_num, # 今天付款笔数 'yestoday_reckon': '%.2f' % yestoday_reckon, # 昨天预估收益 'yestoday_num': yestoday_num, # 昨天付款笔数 }
def response_msg(self, msg='', code=1): raise ResponseMsg(code, msg)