1 importos2 importsys3 importjson4 importtime5 from modules importshopping_center, admin_center6
7 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))8 sys.path.append(BASE_DIR)9
10 logger =admin_center.get_logger()11
12 db_user = BASE_DIR + r'/database/user'
13 db_credit_info = BASE_DIR + r'/database/creditcard_info'
14 credit_record = BASE_DIR + r'/database/credicard_record'
15 user_log = BASE_DIR + r'/database/user_flowlog'
16
17
18 #卡信息
19 defcreditcard_info():20 whileTrue:21 with open(db_credit_info, 'r+') as f:22 creditcard_data =json.loads(f.read())23 creditcard_num = input('请输入需要查询的信用卡卡号(6位数字)').strip()24 if creditcard_num increditcard_data.keys():25 print('您要查询的信用卡信息如下:'.center(50, '-'))26 print('信用卡卡号:%s\n个人信息:%s\n可用额度:%s\n取现额度:%s\n' %(27 creditcard_num, creditcard_data[creditcard_num]['personalinfo'],28 creditcard_data[creditcard_num]['limit'],29 creditcard_data[creditcard_num]['limitcash']30 ))31 break
32 else:33 print('信用卡卡号:%s不存在' %creditcard_num)34
35
36 #提现
37 defwithdraw():38 whileTrue:39 with open(db_credit_info, 'r+') as f:40 creditcard_data =json.loads(f.read())41 creditcard_num = input('请输入需要提现的信用卡卡号(6位数字):').strip()42 if creditcard_num increditcard_data.keys():43 password = input('请输入需要提现的信用卡密码(6位数字):').strip()44 if password == creditcard_data[creditcard_num]['password']:45 print('可用额度:% s\n取现额度:% s\n' % (creditcard_data[creditcard_num]['limit'],46 int((creditcard_data[creditcard_num]["limitcash"]*0.95))47 ))48 withdraw_money = input('请输入需要提现的金额(收取%5手续费)或返回b')49 withdraw_money =int(withdraw_money)50 if withdraw_money == 'b':51 break
52 elif int(withdraw_money) <= creditcard_data[creditcard_num]['limitcash']:53 with open(db_credit_info, 'r') as f1:54 new_limitcash = creditcard_data[creditcard_num]['limitcash'] -\55 (withdraw_money + 0.05 *withdraw_money)56 new_limit = creditcard_data[creditcard_num]['limit'] - (withdraw_money+0.05*withdraw_money)57 print('您已经成功提现人民币:¥%s,手续费:%s,可用额度:%s,取现额度:%s' %
58 (withdraw_money, 0.05 *withdraw_money, new_limit, new_limitcash)59 )60 creditcard_data[creditcard_num]['limit'] =new_limit61 creditcard_data[creditcard_num]['limitcash'] =int(new_limitcash)62 data =json.dumps(creditcard_data)63 f1.seek(0)64 f1.write(data)65 withdraw_log = [str(creditcard_data[creditcard_num]), '金额', str(withdraw_money), '提现成功']66 message = "--".join(withdraw_log)67 logger.info(message)68 break
69 elif int(withdraw_money) >= creditcard_data[creditcard_num]['limitcash']:70 print("已经超出最大提现金额,请重试")71 else:72 print('提现密码不正确,重新输入')73 else:74 print('系统没有次卡号,重新输入')75
76
77 #转账
78 deftransfer():79 whileTrue:80 with open(db_credit_info, 'r+') as f:81 creditcard_data =json.loads(f.read())82 creditcard_num = input('请输入信用卡卡号(6位数字):').strip()83 if creditcard_num increditcard_data.keys():84 trans_creditcard_num = input('请输入对方的信用卡卡号:')85 if trans_creditcard_num increditcard_data.keys():86 transfer_money = input('请输入转账金额:')87 transfer_money =int(transfer_money)88 if transfer_money <= creditcard_data[creditcard_num]['limint']:89 password = input('请输入您的信用卡密码(6位数字):').strip()90 if password == creditcard_data[creditcard_num]['password']:91 creditcard_data[creditcard_num]['limit'] -=transfer_money92 creditcard_data[creditcard_num]['limitcash'] =\93 creditcard_data[creditcard_num]['limit'] // 2
94 creditcard_data[trans_creditcard_num]['limit'] +=transfer_money95 creditcard_data[trans_creditcard_num]['limitcash'] =\96 creditcard_data[trans_creditcard_num]['limit'] // 2
97 print('您已成功向信用卡:%s转账:%s\n可用额度:%s\n取现额度:%s\n'
98 %(trans_creditcard_num, transfer_money,99 creditcard_data[creditcard_num]['limit'],100 creditcard_data[creditcard_num]['limitcash']101 )102 )103 data =json.dumps(creditcard_data)104 f.seek(0)105 f.write(data)106 transfer_log = ['对方卡号', trans_creditcard_num, '金额',107 str(transfer_money), '信用卡转账成功'
108 ]109 creditcard_record(creditcard_num, '转账%s' %transfer_money)110 message = '--'.join(transfer_log)111 logger.info(message)112 break
113 else:114 print('您的密码不正确,重新输入')115 else:116 print('该卡号%s 不存在,请重新输入' %trans_creditcard_num)117 else:118 print('该卡号%s不存在,重新输入' %creditcard_num)119
120
121 #还款
122 defrepay():123 whileTrue:124 with open(db_credit_info, 'r+') as f:125 creditcard_data =json.loads(f.read())126 creditcard_num = input('请输入需要还款的信用卡卡号(6位数字):').strip()127 if creditcard_num increditcard_data.keys():128 repay_money = input('请输入您的还款金额:')129 ifrepay_money.isdigit():130 repay_money =int(repay_money)131 password = input('请输入您的信用卡的还款密码')132 if password == creditcard_data[creditcard_num]['password']:133 with open(db_credit_info, 'r+') as f2:134 creditcard_data[creditcard_num]['limit'] +=repay_money135 creditcard_data[creditcard_num]['limitcash'] =creditcard_data\136 [creditcard_num]['limit'] // 2
137 print('信用卡:%s已经成功还款,金额:%s\n新的可用额度:%s,新的取现额度:%s' %
138 (creditcard_num, repay_money, creditcard_data[creditcard_num]['limit'],139 creditcard_data[creditcard_num]['limitcash']140 )141 )142 data =json.dumps(creditcard_data)143 f2.seek(0)144 f2.write(data)145 repay_log = [creditcard_data[creditcard_num]['personalinfo'],146 creditcard_num, '还款', str(repay_money)147 ]148 credit_record(creditcard_num, '还款%s' %repay_money)149 message = '--'.join(repay_log)150 logger.info(message)151 break
152 else:153 print("您的还款密码不正确,重新输入")154 else:155 print('金额为数字,重新输入')156 else:157 print('该号%s不存在,重新输入' %creditcard_num)158
159
160 #申请信用卡
161 def apply_creditcard(limit=15000, locked='true'):162 whileTrue:163 with open(db_credit_info, 'r+', encoding='utf-8') as f:164 creditcard_dic =json.loads(f.read())165 for key increditcard_dic.keys():166 print('系统已有信用卡:%s,持卡人:%s' % (key, creditcard_dic[key]['personalinfo']))167 choice = input('是否申请信用卡,是y否b:')168 if choice == 'y':169 creditcard = input('请输入要申请的信用卡卡号(6位数字):').strip()170 if creditcard not increditcard_dic:171 if creditcard.isdigit() and len(creditcard) == 6:172 password = input('输入要申请的信用卡密码(6位数字):')173 if len(password) == 6:174 personalinfo = input('输入要申请的信用卡持卡人姓名:')175 ifpersonalinfo.isalpha():176 creditcard_dic[creditcard] = {'creditcard': creditcard,177 'password': password,178 'personalinfo': personalinfo,179 'limit': limit,180 'limitcash': limit // 2,181 'locked': locked182 }183 dic =json.dumps(creditcard_dic)184 f.seek(0)185 f.write(dic)186 print('信用卡:%s\n持卡人:%s\n可用额度:%s\n取现额度:%s申请成功' %
187 (creditcard, personalinfo, limit, limit//2)188 )189 apply_creditcard_log = [creditcard, personalinfo, '信用卡申请成功']190 message = '--'.join(apply_creditcard_log)191 logger.info(message)192 else:193 print('无效字符')194 else:195 print('密码规则不符合')196 else:197 print('卡号不符合规则')198 elif choice == 'b':199 break
200
201
202 #信用卡绑定API
203 defcredit_api():204 whileTrue:205 with open(db_user, 'r+', encoding='utf-8') as f, open(db_credit_info, 'r+', encoding='utf-8') as f1:206 user_dic =json.loads(f.read())207 credit_dic =json.loads(f1.read())208 username = input('请输入需要绑定的信用卡用户名或返回b')209 if username inuser_dic.keys():210 creditcard_num = user_dic[username]['creditcard']211 if creditcard_num ==0:212 print('当前用户%s没有绑定信用卡' %username)213 attach_creditcard = input("请输入需要绑定的信用卡卡号:")214 if username == credit_dic[attach_creditcard]['personalinfo']:215 attach_password = input('请输入需要绑定的信用卡密码:')216 with open(db_user, 'r+', encoding='utf-8') as f:217 if attach_password == credit_dic[attach_password]['password']:218 user_dic[username]['creditcard'] =attach_password219 dic =json.dumps(user_dic)220 f.seek(0)221 f.write(dic)222 print('持卡人:%s\n信用卡号:%s\n已成功绑定' %(username, attach_password))223 attach_log = [username, attach_creditcard, '信用卡绑定成功']224 message = '--'.join(attach_log)225 logger.info(message)226 break
227 else:228 print('密码错误,请重新输入')229 else:230 print('信用卡用户名不存在,请重新输入')231 else:232 print('当前用户%s有绑定信用卡' %username)233 break
234
235
236 #信用卡付款API
237 defcredit_pay(sum, login_user):238 print('尊敬的信用卡绑定用户,欢迎来到信用卡结算中心'.center(50, '-'))239 with open(db_credit_info, 'r+', encoding='utf-8') as credit_payment, open(db_user, 'r+', encoding='utf-8') as f:240 credit_dic =json.loads(credit_payment.read())241 user_dic =json.loads(f.read())242 pwd = input('请输入信用卡密码完成支付:')243 creditcard_num = str(user_dic[login_user]['creditcard'])244 limit = credit_dic[creditcard_num]['limit']245 limitcash = credit_dic[creditcard_num]['limitcash']246 if pwd == credit_dic[creditcard_num]['password']:247 credit_dic[creditcard_num]['limit'] = limit -sum248 credit_dic[creditcard_num]['limitcash'] = limitcash -sum249 print('信用卡:%s已经付款成功:¥%s' %(creditcard_num, sum))250 data =json.dumps(credit_dic)251 credit_payment.seek(0)252 credit_payment.write(data)253 shopping_log = [login_user, creditcard_num, '信用卡结账', str(sum)]254 credit_record(creditcard_num, '购物支付%s' %sum)255 message = '--'.join(shopping_log)256 logger.info(message)257 else:258 print('支付密码不对,请重新输入')259
260
261 #信用卡流水创建
262 defcreditcard_record(creditcard_num, record):263 with open(credit_record, 'r+') as f1:264 record_dict =json.loads(f1.read())265 month = time.strftime('%Y-%m-%d', time.localtime())266 times = time.strftime('%H:%M:%S')267 if str(creditcard_num) not inrecord_dict.keys():268 record_dict[creditcard_num] ={month: {times:record}}269 else:270 if month not inrecord_dict[creditcard_num].keys():271 record_dict[creditcard_num][month]={times: record}272 else:273 record_dict[creditcard_num][month][times] =record274 dict =json.dumps(record_dict)275 f1.seek(0)276 f1.write(dict)277
278
279 #信用卡流水查询
280 defcredit_check(creditcard_num):281 whileTrue:282 print('信用卡流水单'.center(50, '-'))283 with open(credit_record, 'r+') as f1:284 record_dict =json.loads(f1.read())285 print('流水单日期')286 if creditcard_num inrecord_dict.keys():287 for key inrecord_dict[creditcard_num]:288 print(key)289 date = input('流水单查询,返回b, 输入流水单日期2018-06-03')290 if date == 'b': break
291 if date inrecord_dict[creditcard_num].keys():292 keys =record_dict[creditcard_num][date]293 print('当前信用卡 %s 交易记录>>>' %creditcard_num)294 for key inkeys:295 print('时间:%s %s' %(key, record_dict[creditcard_num][date][key]))296 else:297 print('输入日期有误')298 else:299 print('信用卡%s还没有进行过消费' %creditcard_num)300 break