Best JavaScript code snippet using devicefarmer-stf
user.service.js
Source:user.service.js
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 }...
AddUser.js
Source:AddUser.js
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}...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!