def deleteCoupon(token, item_ids): uid = getUserId(token) tb_user_coupon.update_many({'item_id': { '$in': item_ids }}, {"$pull": { 'uid': uid }})
def is_favorite(token, item_id): uid = getUserId(token) u_info = tb_user.find_one({'_id': ObjectId(uid)}) if item_id in u_info.get('favorite', []): return True else: return False
def add_coupon(token, data): uid = getUserId(token) item = tb_user_coupon.find_one({'item_id': data['item_id']}) if item: tb_user_coupon.update_one({'item_id': data['item_id']}, {'$addToSet': { "uid": uid }}) else: data['uid'] = [uid] tb_user_coupon.insert_one(data)
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 get_withdraw_log(token): uid = getUserId(token) results = tb_withdraw_log.find({'uid': ObjectId(uid), 'status': 1}) data = [] for result in results: data.append({ 'atime': result['atime'], 'alipay': result['alipay'], 'name': result['name'], 'amount': result['amount'], }) return data
def get_coupon_list(token, skip, limit): uid = getUserId(token) results = tb_user_coupon.find({ 'uid': ObjectId(uid) }, { "uid": 0, '_id': 0 }).skip(skip).limit(limit) data = [] for result in results: data.append(result) return data
def getInvitedList(token, skip=0, limit=20): u_info = getUserInfo(getUserId(token)) invite_code = u_info.get('invite_code', '') data = [] results = tb_user.find({ 'use_invite_code': invite_code }).skip(skip).limit(limit) for result in results: temp = { 'avatar': result.get('avatar', ''), 'nickname': result.get('nickname', ''), 'invite_time': result.get('invite_time', ''), } data.append(temp) return data
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 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 auth_callback(token, code, type): u_info = getUserInfo(getUserId(token)) data = { 'code': code, 'client_id': 25330814, 'client_secret': 'ab31504958f529b9c1b78a50b2e4fec5', 'grant_type': 'authorization_code', 'redirect_uri': 'http://taobaosavemoney.adesk.com/auth_callback', } resp = requests.post(url="https://oauth.taobao.com/token", data=data, timeout=10) data = resp.json() access_token = data['access_token'] status, result = tbk.user_register(type, access_token) if status: if type == 'vip': if u_info and u_info.get('vip', 0) == 1: return status, result special_id = result['special_id'] new_data = { 'special_id': special_id, 'vip': 1, 'commission_rate': str(commission_rate.get('user_v', '0')), } update({'_id': ObjectId(u_info['_id'])}, {'$set': new_data}) return status, result else: if u_info and u_info.get('channel', 0) != 0: return status, result relation_id = result['relation_id'] new_data = { 'relation_id': relation_id, 'channel': 1, 'channel_task': { 'finish': 0, 'total': 3 }, 'commission_rate': str(commission_rate.get('user_nc_v', '0')), } update({'_id': ObjectId(u_info['_id'])}, {'$set': new_data}) return status, result else: return status, result
def getFavoriteList(token, skip=None, limit=None): uid = getUserId(token) u_info = tb_user.find_one({'_id': ObjectId(uid)}) item_ids = u_info.get('favorite', []) if item_ids and len(item_ids) > 0: if skip is not None and limit is not None: item_ids = item_ids[skip:limit] items = tbk.item_query(item_ids, token) data = [] for result in items: nums = re.findall('[0-9\.]{1,}', result.get('coupon_info', '')) if len(nums) == 2: result['coupon_start_fee'] = int(nums[0]) result['coupon_amount'] = int(nums[1]) elif len(nums) == 1: result['coupon_start_fee'] = 0 result['coupon_amount'] = int(nums[0]) else: result['coupon_start_fee'] = 0 result['coupon_amount'] = 0 data.append(result) return item_list_format(data, token) else: return []
def updateUserInfo(token, data): uid = getUserId(token) update({'_id': ObjectId(uid)}, {"$set": data})
def delete_favorite(token, item_ids): uid = getUserId(token) update({'_id': ObjectId(uid)}, {"$pull": {'favorite': {'$in': item_ids}}})
def addToFavorite(token, item_id): uid = getUserId(token) update({'_id': ObjectId(uid)}, {"$addToSet": {'favorite': item_id}})
def firstLogin(token): uid = getUserId(token) u_info = tb_user.find_one({'_id': uid}) return not u_info.get('passwd', '')
def bind(token, data): uid = getUserId(token) update({'_id': ObjectId(uid)}, {"$set": data}) u_info = tb_user.find_one({'_id': ObjectId(uid)}) return u_info.get('vip', 0)