|
6 | 6 | CORS(app)
|
7 | 7 |
|
8 | 8 |
|
| 9 | +# 1. GPS定位 |
| 10 | +@app.route('/gps', methods=['GET']) |
| 11 | +def get_location(): |
| 12 | + # 这里需要调用GPS定位的API,获取用户的经纬度信息 |
| 13 | + # 由于GPS定位需要在移动设备上才能实现,这里我们只返回一个模拟数据 |
| 14 | + location = {'latitude': 31.2304, 'longitude': 121.4737} |
| 15 | + return jsonify(location) |
| 16 | + |
| 17 | + |
| 18 | +# 2. EPF (Employee Provident Fund) 公积金计算 |
| 19 | +@app.route('/epf', methods=['POST']) |
| 20 | +def calculate_epf(): |
| 21 | + data = request.get_json(silent=True) or {} |
| 22 | + if 'salary' not in data: |
| 23 | + raise BadRequest('Missing field: salary') |
| 24 | + try: |
| 25 | + salary = float(data.get('salary')) |
| 26 | + except (TypeError, ValueError): |
| 27 | + raise BadRequest('Invalid salary') |
| 28 | + |
| 29 | + # EPF的计算逻辑,这里假设员工和雇主各缴纳11%和13% |
| 30 | + employee_contribution = round(salary * 0.11, 2) |
| 31 | + employer_contribution = round(salary * 0.13, 2) |
| 32 | + total_contribution = round(employee_contribution + employer_contribution, 2) |
| 33 | + return jsonify({ |
| 34 | + 'employee_contribution': employee_contribution, |
| 35 | + 'employer_contribution': employer_contribution, |
| 36 | + 'total_contribution': total_contribution |
| 37 | + }) |
| 38 | + |
| 39 | + |
| 40 | +# 3. PERKESO (Social Security Organization) 社险计算 |
| 41 | +@app.route('/perkeso', methods=['POST']) |
| 42 | +def calculate_perkeso(): |
| 43 | + data = request.get_json(silent=True) or {} |
| 44 | + if 'salary' not in data: |
| 45 | + raise BadRequest('Missing field: salary') |
| 46 | + try: |
| 47 | + salary = float(data.get('salary')) |
| 48 | + except (TypeError, ValueError): |
| 49 | + raise BadRequest('Invalid salary') |
| 50 | + |
| 51 | + # PERKESO的计算逻辑,具体费率需要参考官方数据 |
| 52 | + employee_contribution = round(salary * 0.005, 2) |
| 53 | + employer_contribution = round(salary * 0.01, 2) |
| 54 | + total_contribution = round(employee_contribution + employer_contribution, 2) |
| 55 | + return jsonify({ |
| 56 | + 'employee_contribution': employee_contribution, |
| 57 | + 'employer_contribution': employer_contribution, |
| 58 | + 'total_contribution': total_contribution |
| 59 | + }) |
| 60 | + |
| 61 | + |
| 62 | +# 4. PCB (Potongan Cukai Bulanan) 月度所得税计算 |
| 63 | +@app.route('/pcb', methods=['POST']) |
| 64 | +def calculate_pcb(): |
| 65 | + data = request.get_json(silent=True) or {} |
| 66 | + if 'salary' not in data: |
| 67 | + raise BadRequest('Missing field: salary') |
| 68 | + try: |
| 69 | + salary = float(data.get('salary')) |
| 70 | + except (TypeError, ValueError): |
| 71 | + raise BadRequest('Invalid salary') |
| 72 | + |
| 73 | + # PCB的计算逻辑非常复杂,需要参考LHDN的官方指南 |
| 74 | + # 这里我们只返回一个模拟数据 |
| 75 | + pcb = round(salary * 0.1, 2) |
| 76 | + return jsonify({'pcb': pcb}) |
| 77 | + |
| 78 | + |
| 79 | +# 5. 打卡功能 |
| 80 | +@app.route('/clock_in', methods=['POST']) |
| 81 | +def clock_in(): |
| 82 | + data = request.get_json(silent=True) or {} |
| 83 | + employee_id = data.get('employee_id') |
| 84 | + if not employee_id: |
| 85 | + raise BadRequest('Missing field: employee_id') |
| 86 | + # 这里需要记录打卡的时间和地点 |
| 87 | + return jsonify({'message': 'Clock in successful'}) |
| 88 | + |
| 89 | + |
| 90 | +# 6. 考勤表 |
| 91 | +@app.route('/attendance', methods=['GET']) |
| 92 | +def get_attendance(): |
| 93 | + employee_id = request.args.get('employee_id') |
| 94 | + if not employee_id: |
| 95 | + raise BadRequest('Missing query param: employee_id') |
| 96 | + # 这里需要从数据库中查询考勤记录 |
| 97 | + attendance_records = [ |
| 98 | + {'date': '2023-11-01', 'clock_in': '09:00', 'clock_out': '18:00'}, |
| 99 | + {'date': '2023-11-02', 'clock_in': '09:05', 'clock_out': '17:55'} |
| 100 | + ] |
| 101 | + return jsonify(attendance_records) |
| 102 | + |
| 103 | + |
| 104 | +# 7. 个人Payroll,加入Advance |
| 105 | +@app.route('/payroll', methods=['GET']) |
| 106 | +def get_payroll(): |
| 107 | + employee_id = request.args.get('employee_id') |
| 108 | + if not employee_id: |
| 109 | + raise BadRequest('Missing query param: employee_id') |
| 110 | + # 这里需要从数据库中查询Payroll记录 |
| 111 | + payroll_data = { |
| 112 | + 'salary': 5000, |
| 113 | + 'epf': 550, |
| 114 | + 'perkeso': 50, |
| 115 | + 'pcb': 500, |
| 116 | + 'advance': 200, |
| 117 | + 'net_salary': 4600 |
| 118 | + } |
| 119 | + return jsonify(payroll_data) |
| 120 | + |
| 121 | + |
| 122 | +# 8. Advance 预支工资 |
| 123 | +@app.route('/advance', methods=['POST']) |
| 124 | +def request_advance(): |
| 125 | + data = request.get_json(silent=True) or {} |
| 126 | + employee_id = data.get('employee_id') |
| 127 | + amount = data.get('amount') |
| 128 | + if not employee_id: |
| 129 | + raise BadRequest('Missing field: employee_id') |
| 130 | + try: |
| 131 | + amount = float(amount) |
| 132 | + except (TypeError, ValueError): |
| 133 | + raise BadRequest('Invalid amount') |
| 134 | + # 这里需要记录预支工资的请求 |
| 135 | + return jsonify({'message': 'Advance request submitted'}) |
| 136 | + |
| 137 | + |
| 138 | +# 9. 个人资料 |
| 139 | +@app.route('/profile', methods=['GET']) |
| 140 | +def get_profile(): |
| 141 | + employee_id = request.args.get('employee_id') |
| 142 | + if not employee_id: |
| 143 | + raise BadRequest('Missing query param: employee_id') |
| 144 | + # 这里需要从数据库中查询个人资料 |
| 145 | + profile_data = { |
| 146 | + 'name': 'John Doe', |
| 147 | + |
| 148 | + 'phone': '123-456-7890' |
| 149 | + } |
| 150 | + return jsonify(profile_data) |
| 151 | + |
| 152 | + |
| 153 | +# 10. 聊天功能 |
| 154 | +@app.route('/chat', methods=['POST']) |
| 155 | +def send_message(): |
| 156 | + data = request.get_json(silent=True) or {} |
| 157 | + sender_id = data.get('sender_id') |
| 158 | + receiver_id = data.get('receiver_id') |
| 159 | + message = data.get('message') |
| 160 | + if not sender_id or not receiver_id or not message: |
| 161 | + raise BadRequest('Missing sender_id, receiver_id or message') |
| 162 | + # 这里需要将消息保存到数据库中,并通知接收方 |
| 163 | + return jsonify({'message': 'Message sent'}) |
| 164 | + |
| 165 | + |
| 166 | +# 11. 语言翻译 |
| 167 | +@app.route('/translate', methods=['POST']) |
| 168 | +def translate_text(): |
| 169 | + data = request.get_json(silent=True) or {} |
| 170 | + text = data.get('text') |
| 171 | + target_language = data.get('target_language') |
| 172 | + if not text or not target_language: |
| 173 | + raise BadRequest('Missing text or target_language') |
| 174 | + # 这里需要调用翻译API,将文本翻译成目标语言 |
| 175 | + # 这里我们只返回一个模拟数据 |
| 176 | + translated_text = f'Translated to {target_language}: {text}' |
| 177 | + return jsonify({'translated_text': translated_text}) |
| 178 | + |
| 179 | + |
| 180 | +@app.errorhandler(BadRequest) |
| 181 | +def handle_bad_request(err): |
| 182 | + return jsonify({'error': str(err)}), 400 |
| 183 | + |
| 184 | + |
| 185 | +@app.route('/healthz') |
| 186 | +def healthz(): |
| 187 | + return jsonify({'status': 'ok'}) |
| 188 | +from flask import Flask, request, jsonify |
| 189 | +from werkzeug.exceptions import BadRequest |
| 190 | +from flask_cors import CORS |
| 191 | + |
| 192 | +app = Flask(__name__) |
| 193 | +CORS(app) |
| 194 | + |
| 195 | + |
9 | 196 | # In-memory data store for employees (for demo purposes)
|
10 | 197 | employees = {
|
11 | 198 | 'E001': {
|
@@ -277,3 +464,8 @@ def healthz():
|
277 | 464 | # For local development; in production, use gunicorn with wsgi:app
|
278 | 465 | app.run(host='0.0.0.0', port=8000, debug=True)
|
279 | 466 |
|
| 467 | + |
| 468 | +if __name__ == '__main__': |
| 469 | + # For local development; in production, use gunicorn with wsgi:app |
| 470 | + app.run(host='0.0.0.0', port=8000, debug=True) |
| 471 | + |
0 commit comments