def create_company(self, arguments): """ @apiVersion 1.0.0 @api {post} /api/V1/user/create_company 用户创建公司 @apiName create_company @apiGroup User @apiParam {str} name 公司名称 """ new_company = arguments['name'] company = Company.find_one({'company_name': new_company}) if company: return self.fail(u"公司名已存在") else: # params = {'use': [current_user['uid']]} company = Company.create_company(new_company) user = current_user user['company_id'].append(company['company_id']) result = User.update_one( {'_id': user['_id']}, {'$set': { 'company_id': user['company_id'] }}) if result.raw_result['ok'] == 1: return self.done(u"添加成功") else: return self.fail(u"添加失败")
def company_list(self): """ @apiVersion 1.0.0 @api {get} /api/V1/user/company_list 用户公司列表 @apiName company_list @apiGroup User """ items = [] for i in xrange(len(current_user['company_id'])): company_id = current_user['company_id'][i] company_name = (Company.find_one({'company_id': company_id}))['company_name'] items.append({company_id: company_name}) query = { 'invite': { '$elemMatch': { str(current_user['mobile']): { '$exists': True } } } } invite_list = Company.find(query) return self.done({ 'items': items, 'invite': [Company.as_dict(item) for item in invite_list], 'uid': current_user['uid'] })
def disable_register(self, arguments): """ @apiVersion 1.0.0 @api {post} /api/V1/user/disable_register 被禁用户的注册接口 @apiName disable_register @apiGroup User @apiParam {int} mobile 用户手机号 @apiParam {int} company_name 公司名称 """ company_name = arguments['company_name'] mobile = arguments['mobile'] user = User.find_one({'mobile': mobile}) company = Company.find_one({'company_name': company_name}) if company: return self.fail(message=u"该公司已存在") else: params = {'admin': user['uid']} company = Company.create_company(company_name, **params) user['company_id'].append(company['company_id']) result = User.update_one( {'_id': user['_id']}, {'$set': { 'company_id': user['company_id'] }}) if result.raw_result['ok'] == 1: user = load_user(user['uid']) login_user(user, remember=True) return self.done(u"登录成功") else: return self.fail(u"添加失败")
def set_company(self, arguments): """ 设置session的companyId """ company_id = current_user['company_id'][0] if arguments['cId'] and Company.find_one( {'company_id': int(arguments['cId'])}): company_id = arguments['cId'] session["current_company"] = company_id session["current_user_company"] = Company.company_find_one( {'company_id': int(company_id)}) return self.done(company_id)
def current_user_bind_58(self): """ @apiVersion 1.0.0 @api {get} /api/V1/user/current_user_bind_58 当前用户是否绑定58同城 @apiName current_user_bind_58 @apiGroup User """ company = current_user['company_id'][0] account_58 = Company.find({'company_id': company})[0]['account'] password_58 = Company.find({'company_id': company})[0]['password'] bind_58_state = False if account_58 and password_58: bind_58_state = True return self.done({ "bind_58_state": bind_58_state, })
def disable_user_invited(self, arguments): """ @apiVersion 1.0.0 @api {post} /api/V1/user/disable_user_invited 被禁用户接受邀请 @apiName disable_user_invited @apiGroup User @apiParam {int} mobile 用户手机号 @apiParam {int} company_id 公司id """ mobile = arguments['mobile'] user_result = User.find_one({'mobile': int(mobile)}) company_list = ast.literal_eval(arguments['company_id']) if not user_result: return self.fail(u"该手机号码不存在") if not company_list: return self.fail(u"公司不存在") for i in company_list: company = Company.find_one({'company_id': int(i)}) invite_list = [] for j in company['invite']: if str(mobile) not in j: invite_list.append(j) else: permission = j[str(mobile)].get('permission') if not user_result['permission']: user_result['permission'] = {} user_result['permission'][str(i)] = permission if int(i) not in user_result['company_id']: user_result['company_id'].append(int(i)) User.update_one({'uid': user_result['uid']}, { '$set': { 'company_id': user_result['company_id'], 'permission': user_result['permission'] } }) use_list = company.get('use') if company.get('use') else [] use_list.append(user_result['uid']) Company.update_one( {'_id': company['_id']}, {'$set': { 'invite': invite_list, 'use': use_list }}) user = load_user(user_result['uid']) login_user(user, remember=True) return self.done()
def user_add_company(self, arguments): """ @apiVersion 1.0.0 @api {post} /api/V1/user/user_add_company 添加新的公司 @apiName user_add_company @apiGroup User @apiParam {int} mobile 用户手机号 @apiParam {int} company_id 公司id """ mobile = arguments['mobile'] company_id = arguments['company_id'] user_result = User.find_one({'mobile': int(mobile)}) company_result = Company.find_one({'company_id': int(company_id)}) if not user_result: return self.fail(u"该手机号码不存在") if not company_result: return self.fail(u"公司不存在") if int(company_id) not in user_result['company_id']: user_result['company_id'].append(int(company_id)) invite_list = [] for j in company_result['invite']: if str(mobile) not in j: invite_list.append(j) else: permission = j[str(mobile)].get('permission') if not user_result['permission']: user_result['permission'] = {} user_result['permission'][str(company_id)] = permission User.update_one({'uid': user_result['uid']}, { '$set': { 'company_id': user_result['company_id'], 'permission': user_result['permission'] } }) if not company_result.get('use'): company_result['use'] = [] company_result['use'].append(user_result['uid']) Company.update_one( {'_id': company_result['_id']}, {'$set': { 'invite': invite_list, 'use': company_result['use'] }}) return self.done()
def invite(self, arguments): """ @apiVersion 1.0.0 @api {post} /api/V1/user/invite 邀请用户的公司列表 @apiName invite @apiGroup User @apiParam {int} mobile 用户手机号 """ mobile = arguments['mobile'] verify_code = arguments['code'] if verify_code != int(session[str(mobile)]): return self.fail(message=u'验证码不正确') # 找到所以 "invite" : [ { "12333" : 18702156534} , { "324" : 18702156534}] 里面的324的数据 query = {'invite': {'$elemMatch': {str(mobile): {'$exists': True}}}} result = Company.find(query) total = result.count() return self.done({ 'items': [Company.as_dict(item) for item in result], 'total': total })
def new_register(self, arguments): """ @apiVersion 1.0.0 @api {post} /api/V1/user/new_register 新的注册接口 @apiName new_register @apiGroup User @apiParam {int} mobile 用户手机号 @apiParam {int} company_name 公司名称 """ mobile = arguments['mobile'] if User.find_one({'mobile': mobile}): return self.fail(message=u"该手机号码已注册") password = str(mobile) company_name = arguments['company_name'] company = Company.find_one({'company_name': company_name}) if company: return self.fail(message=u"该公司已存在") else: new_company = Company.create_company(company_name) company_id = new_company['company_id'] for item in ['mobile', 'company_name']: del arguments[item] user = User.create_user(int(mobile), password, [company_id], **arguments) Company.update_one({'_id': new_company['_id']}, {'$set': { 'admin': user['uid'] }}) permission = {str(company_id): {'shouzu': '允许', 'zhaozu': '允许'}} User.update_one({'uid': user['uid']}, {'$set': { 'permission': permission }}) user = load_user(user['uid']) login_user(user, remember=True) session["current_company"] = company_id session["current_user_company"] = Company.company_find_one( {'company_id': int(company_id)}) return self.done("success")
def register(self, arguments): mobile = arguments['mobile'] if User.find_one({'mobile': mobile}): return self.fail(message=u"该手机号码已注册") password = arguments['password'] company_name = arguments['company_name'] verify_code = arguments['verify_code'] if verify_code != int(session[str(mobile)]): return self.fail(message=u'验证码不正确') company = Company.find_one({'company_name': company_name}) if company: company_id = company['company_id'] else: new_company = Company.create_company(company_name) company_id = new_company['company_id'] for item in ['mobile', 'verify_code', 'password', 'company_name']: del arguments[item] user = User.create_user(int(mobile), password, company_id, **arguments) user = load_user(user['uid']) login_user(user, remember=True) del user['password'] return self.done(data=user)
def add_company(self, arguments): """ @apiVersion 1.0.0 @api {post} /api/V1/user/add_company 用户注册并添加新的公司 @apiName add_company @apiGroup User @apiParam {int} mobile 用户手机号 @apiParam {int} company_id 公司id """ mobile = arguments['mobile'] if User.find_one({'mobile': mobile}): return self.fail(message=u"该手机号码已注册") password = str(mobile) company_list = ast.literal_eval(arguments['company_id']) permission = {} for i in company_list: company = Company.find_one({'company_id': int(i)}) invite_list = [] for j in company['invite']: if str(mobile) not in j: invite_list.append(j) else: permission[str(i)] = j[str(mobile)].get('permission') company['invite'] = invite_list Company.update_one({'_id': company['_id']}, {'$set': company}) user = User.create_user(int(mobile), password, company_list) user.update_one({'_id': user['_id']}, {'$set': { 'permission': permission }}) user = load_user(user['uid']) for i in company_list: company = Company.find_one({'company_id': int(i)}) use_list = company.get('use') if company.get('use') else [] use_list.append(user['uid']) company['use'] = use_list Company.update_one({'_id': company['_id']}, {'$set': company}) login_user(user, remember=True) return self.done()