def get_create_order(order_id=None): if not current_user.is_authenticated: raise models.GlobalApiException(code_msg.USER_NO_LOGIN) if request.method == 'GET': order = Order.objects(id=order_id).first() if not order: return jsonify(code_msg.ORDER_NOT_EXIST) return jsonify(models.R.ok(data=order.dict_data)) else: user = current_user.user user_id = str(user['_id']) with cache_lock("user_create_order_%s" % user_id): product_code = request.values.get('product_code') num = int(request.values.get('num', 1)) product = Product.objects(product_code=product_code).first() if product.inventory < num: return jsonify(code_msg.PRODUCT_INVENTORY_EMPTY) if product.limit: params, msg = {}, code_msg.ORDER_SURPASS_LIMIT if product.limit_type == LIMIT_TYPE_DAY: now = datetime.now() params = dict( created__gte=datetime(now.year, now.month, now.day)) msg = code_msg.ORDER_SURPASS_DAY_LIMIT history_orders = Order.objects(user_id=user_id, product=product, status__in=ORDER_STATUS_VALID, **params) if len(history_orders) >= product.limit: return jsonify(msg) if product.require_perm and not user["promote_perm"]: raise models.GlobalApiException(code_msg.PERM_ERROR) if product.price_type == PRICE_TYPE_COIN: recharge_handler = coin.recharge_coin not_enough_msg = code_msg.COIN_BALANCE_NOT_ENOUGH else: recharge_handler = coin.recharge_credit not_enough_msg = code_msg.CREDIT_BALANCE_NOT_ENOUGH result = recharge_handler(user, product.price * num) if not result: return jsonify(not_enough_msg) order = Order(user_id=user_id, product=product, cd_key=utils.generate_cd_key(), num=num, price=product.price) order.save() product.sale_num += num product.inventory -= num product.save() return jsonify( models.R.ok(data=order.dict_data).put( 'action', url_for('user.user_order')))
def exchange_order(): exchange_form = forms.ExchangeForm() if not exchange_form.validate(): return jsonify( models.R.fail(code_msg.PARAM_ERROR.get_msg(), str(exchange_form.errors))) login_name = exchange_form.login_name.data zone_id = exchange_form.zone_id.data cd_key = exchange_form.cd_key.data role_id = exchange_form.role_id.data if zone_id not in ZONE_SSH: raise models.GlobalApiException(code_msg.PARAM_ERROR) user = mongo.db.users.find_one({'loginname': login_name}) if not user: return jsonify(code_msg.USER_NOT_EXIST) order = Order.objects(cd_key=cd_key).first() if not order: return jsonify(code_msg.ORDER_NOT_EXIST) if order.user_id != str(current_user.user["_id"]): return jsonify(code_msg.ORDER_USER_INVALID) if order.product.status != PRODUCT_STATUS_NORMAL: # 商品下架 订单处理无效 order.status = ORDER_STATUS_CANCEL order.save() return jsonify(code_msg.PRODUCT_OUT) if order.status != ORDER_STATUS_INIT: return jsonify(code_msg.ORDER_EXCHANGE_INVALID) MailSend().send(role_id, order.product.item, order.product.num * order.num, order.product.title, order.product.text) # 物品发送成功,订单已使用 order.status = ORDER_STATUS_USED order.save() return jsonify( code_msg.ORDER_EXCHANGE_SUCCESS.put('action', url_for('index.exchange_page')))
def login(): user_form = forms.LoginForm() if user_form.is_submitted(): if not user_form.validate(): return jsonify( models.R.fail(code_msg.PARAM_ERROR.get_msg(), str(user_form.errors))) utils.verify_num(user_form.vercode.data) user = mongo.db.users.find_one({'loginname': user_form.loginname.data}) if not user: return jsonify(code_msg.USER_NOT_EXIST) if not models.User.validate_login(user['password'], user_form.password.data): raise models.GlobalApiException(code_msg.PASSWORD_ERROR) if not user.get('is_active', False): return jsonify(code_msg.USER_UN_ACTIVE) if user.get('is_disabled', False): return jsonify(code_msg.USER_DISABLED) login_user(models.User(user)) action = request.values.get('next') if not action: action = url_for('index.index') return jsonify(code_msg.LOGIN_SUCCESS.put('action', action)) logout_user() ver_code = utils.gen_verify_num() # session['ver_code'] = ver_code['answer'] return render_template('user/login.html', ver_code=ver_code['question'], form=user_form, title='登录')
def user_pass_forget(): if request.method == 'POST': forget_form = forms.ForgetPasswordForm() if not forget_form.validate(): return jsonify( models.R.fail(code_msg.PARAM_ERROR.get_msg(), str(forget_form.errors))) loginname = forget_form.loginname.data password = forget_form.password.data phone = forget_form.phone.data ver_code = forget_form.vercode.data user = mongo.db.users.find_one({'loginname': loginname}) if not user: return jsonify(code_msg.USER_ID_NOT_EXIST) if phone != user["phone"]: return jsonify(code_msg.PHONE_INVALID) if not verify_phone_code( loginname, phone, ver_code, sms_type=SMS_TYPE_BACK_PASS): raise models.GlobalApiException(code_msg.VERIFY_CODE_ERROR) mongo.db.users.update( {'_id': user['_id']}, {'$set': { 'password': generate_password_hash(password) }}) update_zx_user_password(loginname, password) return jsonify( code_msg.CHANGE_PWD_SUCCESS.put('action', url_for('user.login'))) else: ver_code = utils.gen_verify_num() return render_template('user/forget.html', user=None, ver_code=ver_code['question'])
def query_role_list(): zone_id = str(request.values.get('zone_id')) if zone_id not in ZONE_SSH: raise models.GlobalApiException(code_msg.PARAM_ERROR) login_name = request.values.get('login_name') user = mongo.db.users.find_one({'loginname': login_name}) if not user: return jsonify(code_msg.USER_NOT_EXIST) game_user = user_sql.get_user_by_name(login_name) if not game_user: return jsonify(code_msg.GAME_USER_NOT_EXIST) flag, user_roles = query_user_roles(zone_id, user) if not flag: return jsonify(models.R(status=50001, msg=user_roles)) if not user_roles: return jsonify(code_msg.ROLE_NOT_EXIST) return jsonify(models.R.ok(data=user_roles))
def user_repass(): if not current_user.is_authenticated: return redirect(url_for('user.login')) pwd_form = forms.ChangePassWordForm() if not pwd_form.validate(): return jsonify( models.R.fail(code_msg.PARAM_ERROR.get_msg(), str(pwd_form.errors))) nowpassword = pwd_form.nowpassword.data password = pwd_form.password.data user = current_user.user if not models.User.validate_login(user['password'], nowpassword): raise models.GlobalApiException(code_msg.PASSWORD_ERROR) mongo.db.users.update( {'_id': user['_id']}, {'$set': { 'password': generate_password_hash(password) }}) update_zx_user_password(user["loginname"], password) return jsonify(models.R.ok())
def register(): if db_utils.get_option('open_user', {}).get('val') != '1': abort(404) user_form = forms.RegisterForm() if user_form.is_submitted(): if not user_form.validate(): return jsonify( models.R.fail(code_msg.PARAM_ERROR.get_msg(), str(user_form.errors))) phone = user_form.phone.data if not filter_phone(phone): return jsonify(code_msg.SMS_PHONE_ERROR) user_count = mongo.db.users.find({'phone': phone}).count() reg_limit = int(db_utils.get_option_val('phone_register_limit', 0)) if reg_limit and user_count >= reg_limit: return jsonify(code_msg.SMS_PHONE_LIMIT) loginname = user_form.loginname.data password = user_form.password.data if not verify_phone_code(loginname, phone, user_form.vercode.data): raise models.GlobalApiException(code_msg.VERIFY_CODE_ERROR) user = mongo.db.users.find_one({'loginname': loginname}) if user: return jsonify(code_msg.USER_ID_EXIST) if not filter_phone(phone): return jsonify(code_msg.SMS_PHONE_ERROR) game_user = register_zx_user(loginname, password, "123", "123", "123") user = dict({ 'is_active': True, 'coin': 0, 'credit': 0, 'credit_used': 0, 'credit_balance': 0, 'game_user_id': game_user["ID"], 'phone': phone, 'loginname': loginname, 'username': user_form.username.data or loginname, 'vip': 0, 'reply_count': 0, 'avatar': url_for('static', filename='images/avatar/' + str(randint(0, 12)) + '.jpg'), 'password': generate_password_hash(password), 'create_at': datetime.now(), 'perms': [], "promote_perm": False }) mongo.db.users.insert_one(user) award_coin(user, user["_id"], AWARD_TYPE_REGISTER) return jsonify( code_msg.REGISTER_SUCCESS.put('action', url_for('user.login'))) ver_code = utils.gen_verify_num() # session['ver_code'] = ver_code['answer'] return render_template('user/reg.html', ver_code=ver_code['question'], form=user_form)
def verify_num(code): from ..code_msg import VERIFY_CODE_ERROR if code != session['ver_code']: raise models.GlobalApiException(VERIFY_CODE_ERROR)