import http from "./http"; function wxloginmethod(data) { let wxloginurl = '/wx/auth/wxlogin'; return new Promise((resolve, reject) => { uni.login({ provider: 'weixin', success: function(loginRes) { uni.showLoading('正在登录中...'); console.log('loginRes--',loginRes) data.code=loginRes.code console.log('data组织传给后台接口--',data) // 获取 sessionKey openid unionid // return; http.quickPost(`${wxloginurl}`, data).then(res => { console.log('登录成功1',res) uni.hideLoading(); if(res.data.code === 0) { console.log('登录成功',res.data) let {sessionKey, openid, unionid} = res.data.data; resolve({sessionKey, openid, unionid}) uni.setStorageSync('sessionKey', sessionKey); uni.setStorageSync('openid', openid) uni.setStorageSync('unionid', unionid) } else { reject(res.data) } }).catch((err)=>{ reject(err) }); }, fail(res) { reject(res) } }); }) } function saveWxUserInfoLocal (userInfo) { uni.setStorageSync('wxUserInfo', userInfo) } //获取用户信息info function getWxUserInfoServer (sessionKey, signature, rawData, encryptedData, iv) { let wxinfourl = '/equestrianinfo/wxma/info/wx9d9f429ba1775063'; return new Promise((resolve, reject) => { http.quickPost(`${wxinfourl}`, {sessionKey, signature, rawData, encryptedData, iv}) .then(res => { if (res.data.code === 0) { uni.setStorageSync('wxUserInfo', res.data.data) resolve(res.data.data) } else { reject(res.data) } }).catch((err)=>{ reject(err) }) }) } //获取用户手机号信息 function getWxUserPhoneServer (sessionKey, signature, encryptedData, iv) { let wxphoneurl = '/equestrianinfo/wxma/phone/wx9d9f429ba1775063'; return new Promise((resolve, reject) => { http.quickPost(`${wxphoneurl}`, {sessionKey, signature, encryptedData, iv}) .then(res => { if (res.data.code === 0) { uni.setStorageSync('wxPhoneInfo', res.data.data) resolve(res.data.data) } else { reject(res.data) } }).catch((err)=>{ reject(err) }) }) } function registerUserInfo() { let regurl = '/equestrianinfo/wxma/register'; if (http.devStatus) { regurl = http.devUrl + regurl } let wxUserInfo = uni.getStorageSync('wxUserInfo'); let wxPhoneInfo = uni.getStorageSync('wxPhoneInfo'); let {avatarUrl,city, country, gender,language,nickName, openId,province,unionId} = wxUserInfo let {phoneNumber,countryCode, purePhoneNumber} = wxPhoneInfo; return new Promise((resolve, reject) => { http.quickPost(regurl, {avatarUrl,city, country, gender,language,nickName, openId,province,unionId, phoneNumber,countryCode, purePhoneNumber}).then((res)=>{ console.log('registerUserInfo:', res.data); if(res.data.code === 0) { resolve(res.data.data) } else { reject(res.data)} }).catch((err)=>{ reject(err); }) }) } /*function refreshToken(){ let rturl = '/auth/oauth/token'; if (http.devStatus) { rturl = http.devUrl + rturl } let refresh_token = uni.getStorageSync('refreshToken') return new Promise((resolve, reject) => { http.post(rturl, {refresh_token, grant_type:'refresh_token', scope:'server' }).then((res)=>{ if(res.data.code === 0) { let {access_token,refresh_token,expires_in,username} = res.data.data; console.log('access_token:', access_token,'refresh_token:', refresh_token, 'expires_in:', expires_in); uni.setStorageSync('accessToken', access_token) uni.setStorageSync('refreshToken', refresh_token) uni.setStorageSync('expiresIn', expires_in) // uni.setStorageSync('username', user_name) resolve() } else { reject(res.data) } }).catch((err)=>{ reject(err) }) }) }*/ function refreshToken(){ let rturl = '/auth/oauth/token'; if (http.devStatus) { rturl = http.devUrl + rturl } let refresh_token = uni.getStorageSync('refreshToken'); /*http.post(rturl, {refresh_token, grant_type:'refresh_token', scope:'server' }).then((res)=>{ if(res.data.code === 0) { let {access_token,refresh_token,expires_in,username} = res.data.data; console.log('access_token:', access_token,'refresh_token:', refresh_token, 'expires_in:', expires_in); uni.setStorageSync('accessToken', access_token) uni.setStorageSync('refreshToken', refresh_token) uni.setStorageSync('expiresIn', expires_in) // uni.setStorageSync('username', user_name) resolve() } else { reject(res.data) } }).catch((err)=>{ reject(err) })*/ return new Promise((resolve, reject) => { uni.request({ url: rturl, data: {refresh_token, grant_type:'refresh_token', scope:'server' }, method:'POST', header:{isToken: false}, success(res) { // resolve(res) if(res.data.code === 0) { let {access_token,refresh_token,expires_in,username} = res.data.data; console.log('access_token:', access_token,'refresh_token:', refresh_token, 'expires_in:', expires_in); uni.setStorageSync('accessToken', access_token) uni.setStorageSync('refreshToken', refresh_token) uni.setStorageSync('expiresIn', expires_in) // uni.setStorageSync('username', user_name) resolve() } else { reject(res.data) } }, fail(err) { reject(err) }, complete() { //不管请求成没成功都会走这一步 然后隐藏 加载中的效果 } }) }) } //退出登录 清空信息 function systemLogOut() { uni.setStorageSync('accessToken', '') uni.setStorageSync('refreshToken', '') uni.setStorageSync('expiresIn', '') uni.setStorageSync('personalInfo', null) uni.setStorageSync('organInfo', null) uni.setStorageSync('wxUserInfo', null) } function systemLogin() { let adlogurl = '/admin/register/mobile'; let adsmsurl = '/auth/mobile/token/sms'; if (http.devStatus) { adlogurl = http.devUrl + adlogurl adsmsurl = http.devUrl + adsmsurl } return new Promise((resolve, reject) => { let wxPhoneInfo = uni.getStorageSync('wxPhoneInfo'); if (!wxPhoneInfo) (reject({ msg:'手机号未授权'})) let mobile = wxPhoneInfo.purePhoneNumber; if (!mobile)(reject({msg:'未授权手机号'})) http.get(`${adlogurl}/${mobile}`,{force:false,debug:true}).then((codeRes)=>{ let codeData = codeRes.data; let data = {mobile: 'SMS@' + mobile, code: codeData.msg, grant_type:'mobile'}; if (codeData.data) { http.post(adsmsurl, data, null).then((logRes)=>{ let logResData = logRes.data if(logResData) { // logResData = typeof logResData === 'string'? JSON.parse(logResData) : logResData; let {access_token,refresh_token,expires_in,username} = logResData; console.log('access_token:', access_token,'refresh_token:', refresh_token, 'expires_in:', expires_in); uni.setStorageSync('accessToken', access_token) uni.setStorageSync('refreshToken', refresh_token) uni.setStorageSync('expiresIn', expires_in) uni.setStorageSync('username', username) uni.setStorageSync('expiresTimeStamp', expires_in * 1000 + new Date().getTime()); // console.log(access_token,refresh_token,expires_in,'信息:',logResData); resolve(logResData) } else { reject(logRes) } }).catch((err)=>{ reject(err) }) } else { reject({msg:'验证码错误'}) } }) }); } function getSystemUserInfo() { let sysurl = '/equestrianinfo/wxma/userinfo'; if (http.devStatus) { sysurl = http.devUrl + sysurl } return new Promise((resolve, reject) => { http.quickPost(sysurl,null).then((res)=>{ console.log('logRes', res.data); if(res.data.code === 0) { uni.setStorageSync('organInfo', res.data.data.organInfo) let personalInfo = res.data.data.personalInfo; uni.setStorageSync('personalInfo', personalInfo) // console.log('sync:',personalInfo) // uni.setStorageSync('personalInfo', res.data.data.personalInfo) resolve(res.data.data) } else { reject(res.data); } }).catch((err)=>{ reject(err); }) }) } function checkSystemLogin() { return uni.getStorageSync('accessToken'); } function checkusernameLogin() { return uni.getStorageSync('username'); } function checkPersonalInfo() { return uni.getStorageSync('personalInfo'); } function checkOrganInfo() { return uni.getStorageSync('organInfo'); } function checkWxUserInfo() { return uni.getStorageSync('wxUserInfo'); } function checkPersonalBaseInfo() { let personalInfo = uni.getStorageSync('personalInfo'); if (personalInfo) { let {id,card,phone,email,fullName, familyPy,namePy,birthday,sex,accountOwner, certificateType,certificateNumber, idPhoto,province,city,county,postal,addr,country, certificateImg,otherImg,resumeImg} = personalInfo // console.log('个人信息:',personalInfo) let idPhotoList = []; if (idPhoto && idPhoto.indexOf('http') ===0) { idPhotoList.push({url: idPhoto}) } if(certificateImg&&certificateImg.length>0) { if(certificateImg[0] && certificateImg[0].path) { certificateImg = []} } else { certificateImg = []} if(otherImg&&otherImg.length>0) { if(otherImg[0] && otherImg[0].path) { otherImg = []} } else { otherImg = []} if(resumeImg&&resumeImg.length>0) { if(resumeImg[0] && resumeImg[0].path) { resumeImg = []} } else { resumeImg = []} return {id,card,phone,email,fullName, familyPy,namePy,birthday,sex,accountOwner, certificateType,certificateNumber, idPhoto,province,city,county,postal,addr,country, certificateImg,otherImg,resumeImg, idPhotoList}; } return personalInfo; } function checkPersonalRoleList() { let personalInfo = uni.getStorageSync('personalInfo'); if (personalInfo) { return personalInfo.roleList; } return []; } export default { wxloginmethod, getWxUserInfoServer, saveWxUserInfoLocal, getWxUserPhoneServer, registerUserInfo, systemLogin, refreshToken, systemLogOut, getSystemUserInfo, checkSystemLogin, checkOrganInfo, checkPersonalInfo, checkWxUserInfo, checkPersonalBaseInfo, checkPersonalRoleList, checkusernameLogin, }