How to use addUser method in devicefarmer-stf

Best JavaScript code snippet using devicefarmer-stf

user.service.js

Source:user.service.js Github

copy

Full Screen

1import {Constant} from "../config/constant.js";2import {ErrorCode} from "../config/errorcode.js";3import {ServiceError} from "../models/service.error.model.js";4import mongodb from "mongodb";5var ObjectID = mongodb.ObjectID;6import crypto from "crypto";7import md5 from "md5";8import querystring from "querystring";9import {PhoneUtil} from "../util/phone.util.js";10import {RandomUtil} from "../util/random.util.js";11import {SmsUtil} from "../util/sms.util.js";12import {User,ClientUser,AddUser} from "../models/user.model";13import {EasyChat} from "../util/easychat.util.js";14export class UserService {15 constructor(userCollection,cache) {16 this._users = userCollection;17 this._cache = cache;18 this._constant = Constant();19 this._errorCode = ErrorCode();20 }21 /**22 * 获得手机验证码23 * @param phone24 * @returns {{phone: *, smsCode: *, expireTime: number}}25 */26 * getSmsCode(phone) {27 if (PhoneUtil.checkPhoneFormat(phone).status) {28 //生成随机四位验证码29 var smsCode = RandomUtil.getRandomNumber(1000, 9999);30 //将验证码,手机号,过期时间保存31 var passport = {32 phone: phone,33 smsCode: smsCode34 };35 //将验证码等保存到memcached,一分钟过期36 yield this._cache.$set("sms"+phone,passport,this._constant.ONE_MINUTE);37 //调用网络接口,发送验证码38 try{39 SmsUtil.sendSmsCode(phone,smsCode);40 }catch(error){41 throw new ServiceError(this._errorCode.ERROR_CODE_SERVER_NET_FAIL,"发送验证码失败");42 }43 //返回passport对象44 return passport;45 }46 }47 /**48 * 注册49 * @param user50 * @returns {*}51 */52 * signUp(adduser) {53 //根据用户手机号和验证码获得passport,检查是否过期54 var passport = yield this._cache.$get("sms"+adduser.phone);55 if (passport) {56 if (adduser.smsCode == passport.smsCode) {57 adduser.password = md5(adduser.password);58 try {59 //检查phone是否重复注册60 var oldUser = User.fromMongo( yield this._users.findOne({phone: adduser.phone}) );61 if(oldUser && oldUser.id){62 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"该手机已注册");63 //yield this._users.remove({phone: adduser.phone});64 }65 adduser.smsCode=undefined;66 var result = yield this._users.insert(adduser);67 var now = new Date();68 adduser.id=result._id;69 adduser.token = md5(now.getTime() );70 var oldToken = yield this._cache.$get(adduser.phone );71 yield this._cache.$del(oldToken);72 yield this._cache.$set(adduser.token,adduser.id,this._constant.TWO_HOURS);73 yield this._cache.$set(adduser.phone,adduser.token,this._constant.TWO_HOURS);74 yield EasyChat.createUser(adduser.id,adduser.password);75 return ClientUser.fromAddUser(adduser);76 } catch (err) {77 throw err;78 }79 } else {80 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"验证码错误");81 }82 } else {83 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"验证码已经过期");84 }85 }86 /**87 * 重置密码88 * @param adduser89 * @returns {*}90 */91 * resetPassword(adduser) {92 //根据用户手机号和验证码获得passport,检查是否过期93 var passport = yield this._cache.$get("sms"+adduser.phone);94 if (passport) {95 if (adduser.smsCode == passport.smsCode) {96 //使用随机数生成password和token97 var oldUser = User.fromMongo(yield this._users.findOne({phone: adduser.phone}));98 console.log(oldUser);99 if(!oldUser){100 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"用户不存在,请检查手机号码");101 }102 oldUser.password = md5(adduser.password);103 oldUser.phone = adduser.phone;104 //新增用户到数据库105 try {106 //用户写入到数据库107 var result = yield this._users.update({phone: adduser.phone}, oldUser.toMongo() );108 var now = new Date();109 oldUser.token = md5(now.getTime() );110 var oldToken = yield this._cache.$get(adduser.phone );111 yield this._cache.$del(oldToken);112 yield this._cache.$set(oldUser.token,oldUser.id,this._constant.TWO_HOURS);113 yield this._cache.$set(oldUser.phone,oldUser.token,this._constant.TWO_HOURS);114 yield EasyChat.resetPassword(oldUser.id,oldUser.password);115 return ClientUser.fromUser(oldUser);116 } catch (err) {117 throw err;118 }119 } else {120 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"验证码错误");121 }122 } else {123 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"验证码已经过期");124 }125 }126 /**127 * 登陆128 * @param user129 * @returns {*}130 */131 * signIn(addUser) {132 var oldUser = User.fromMongo(yield this._users.findOne({phone: addUser.phone}));133 if (!oldUser) {134 throw ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"账户不存在");135 }136 addUser.password = md5(addUser.password);137 if (addUser.password == oldUser.password) {138 //验证成功,生成新token139 var now = new Date();140 oldUser.token = md5(now.getTime());141 var oldToken = yield this._cache.$get(addUser.phone );142 yield this._cache.$del(oldToken);143 yield this._cache.$set(oldUser.phone ,oldUser.token,this._constant.TWO_HOURS);144 yield this._cache.$set(oldUser.token ,oldUser.id,this._constant.TWO_HOURS);145 return ClientUser.fromUser(oldUser);146 } else {147 throw ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"用户登陆失败,密码不正确");148 }149 }150 /**151 * 更新用户信息152 * @param user153 */154 *update(user){155 var oldUser = User.fromMongo( yield this._users.findOne({_id: ObjectID(user.id)}) );156 if(user){157 oldUser.avatar = user.avatar;158 oldUser.username = user.username;159 oldUser.gender = user.gender;160 try{161 var result = yield this._users.update({_id: ObjectID(user.id)}, oldUser.toMongo());162 yield this._cache.$set(user.token ,oldUser.id,this._constant.TWO_HOURS);163 }catch(err){164 throw new ServiceError(this._errorCode.ERROR_CODE_DB_FAIL,"数据库更新信息失败");165 }166 if(result){167 oldUser.token=user.token;168 return ClientUser.fromUser(oldUser);169 }170 }else{171 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"用户不存在");172 }173 }174 /**175 * 检查用户token是否登陆176 * @param token177 * @returns {*}178 */179 *checkLogin(token){180 var userId = yield this._cache.$get(token);181 if(!userId){182 throw new ServiceError(this._errorCode.ERROR_CODE_CLIENT_PARAMS_ERROR,"token失效");183 }else{184 yield this._cache.$set(token ,userId,this._constant.TWO_HOURS);185 }186 return userId;187 }...

Full Screen

Full Screen

AddUser.js

Source:AddUser.js Github

copy

Full Screen

1import React from 'react'2import './AddUser.css'3function AddUser(props){4 return(5 <div className='adduser-div'>6 <div className='adduser-field-div'>7 <div className='adduser-field-container'>8 <div className='adduser-field-tag'><p>First name</p></div>9 <div className='adduser-field-input'><input value={props.userData.firstname} onChange={(e)=>props.setUserData((prev)=>{return {...prev,firstname: e.target.value}})}></input></div>10 </div>11 </div>12 <div className='adduser-field-div'>13 <div className='adduser-field-container'>14 <div className='adduser-field-tag'><p>Last name</p></div>15 <div className='adduser-field-input'><input value={props.userData.lastname} onChange={(e)=>props.setUserData((prev)=>{return {...prev,lastname: e.target.value}})}></input></div>16 </div>17 </div>18 <div className='adduser-field-div'>19 <div className='adduser-field-container'>20 <div className='adduser-field-tag'><p>Email</p></div>21 <div className='adduser-field-input'><input value={props.userData.email} onChange={(e)=>props.setUserData((prev)=>{return {...prev,email: e.target.value}})}></input></div>22 </div>23 </div>24 <div className='adduser-field-div'>25 <div className='adduser-field-container'>26 <div className='adduser-field-tag'><p>Phone</p></div>27 <div className='adduser-field-input'><input value={props.userData.phone} onChange={(e)=>props.setUserData((prev)=>{return {...prev,phone: e.target.value}})}></input></div>28 </div>29 </div>30 <div className='adduser-field-div'>31 <div className='adduser-field-container'>32 <div className='adduser-field-tag'><p>Address</p></div>33 <div className='adduser-field-input'><input value={props.userData.address} onChange={(e)=>props.setUserData((prev)=>{return {...prev,address: e.target.value}})}></input></div>34 </div>35 </div>36 <div className='adduser-field-div'>37 <div className='adduser-field-container'>38 <div className='adduser-field-tag'><p>Password</p></div>39 <div className='adduser-field-input'><input value={props.userData.password} onChange={(e)=>props.setUserData((prev)=>{return {...prev,password: e.target.value}})}></input></div>40 </div>41 </div>42 <div className='adduser-button-div'>43 <div className='adduser-button-container'>44 <div className='adduser-button' onClick={props.submitForm}>45 <p>Submit</p>46 </div>47 </div>48 </div>49 </div>50 )51}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run devicefarmer-stf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful