import { login,loginByWeChat,setPhone,getUserInfo } from '@/api/user' let univerifyManager=null; let registrationID=uni.getStorageSync("registrationID"); let userPhone=""; let unionid=""; let loginType=3;//1账号密码 2:微信登录 3:手机号一键登录 export function showLoginPage(provider){ // #ifdef APP-PLUS // if(this.$qconfig.applyLogin){ // return; // } // if(registrationID==null || registrationID==""){ // getRegistrationID(); // } // 微信一键登录 if(provider == "weixin") { weixinLogin() } else { // 手机号一键登录 let that=this; univerifyManager = uni.getUniverifyManager(); univerifyLogin(that); const loginCallBack = (res) => { // 获取一键登录弹框协议勾选状态 univerifyManager.getCheckBoxState({ success(res) { // that.$qconfig.applyLogin=false; if(!res.state){ uni.showToast({title: "请同意服务条款和隐私政策",icon: 'none',position:'bottom'}); return; } loginType=2; uni.login({ provider: "weixin", onlyAuthorize:false, success: async (res) => { console.log('login success:成功', JSON.stringify(res)); let that=this; uni.showLoading({title:"处理中..."}); const params={code:res.code,jpushId:registrationID}; loginByWeChatAct(111,params); }, fail: (err) => { console.log('login fail:', JSON.stringify(err)); // 未开通 if (err.code == 1000) { uni.showModal({ title: '登录失败', content: `${err.errMsg}\n,错误码:${err.code}`, confirmText: '开通指南', cancelText: '确定', success: (res) => { if (res.confirm) { setTimeout(() => { plus.runtime.openWeb('https://ask.dcloud.net.cn/article/37965') }, 500) } } }); return; } if (err.code == '-100') { uni.showToast({ title: "授权拒绝", icon: "none" }); return; } // 一键登录预登陆失败 if (err.code == '30005') { uni.showModal({ showCancel: false, title: '预登录失败', content: err.errMsg }); return; } if (err.code == 30006) { uni.showToast({ title: "登录已取消,请重新操作", icon: "none" }); return; } // 一键登录用户关闭验证界面 if (err.code != '30003') { uni.showModal({ showCancel: false, title: '登录失败', content: JSON.stringify(err) }); } }, complete: () => { } }); // 关闭一键登录弹框 univerifyManager.close(); } }); } // 订阅自定义按钮点击事件 univerifyManager.onButtonsClick(loginCallBack); // 取消订阅自定义按钮点击事件 //univerifyManager.offButtonsClick(loginCallBack); // this.$qconfig.applyLogin=true; } // #endif // #ifdef H5 if(String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"){ let url=window.location.href; let urlPaths=url.split("/h5"); uni.setStorageSync('beforLoginPage',urlPaths[1]); uni.navigateTo({ url: "/pages/auth/h5WxLogin" }); } else{ uni.navigateTo({ url: "/pages/auth/passwordLogin" }); } // #endif } const univerifyLogin=function(that){ univerifyManager.login({ provider: 'univerify', univerifyStyle: { // 自定义登录框样式 fullScreen: true, icon: { "path": "static/logo.png", // 自定义显示在授权框中的logo,仅支持本地图片 默认显示App logo "width": "80px", //图标宽度 默认值:60px "height": "80px" ,//图标高度 默认值:60px }, authButton: { "title": "本机号码一键登录", "normalColor": "#018C39", "highlightColor": "#018C39", }, otherLoginButton: { // "title": "微信快捷登录", "visible": "true", "borderRadius": "24px", "borderColor": "#ECECEC", }, closeIcon: { "path": "static/images/icon_close.png", "width": "160px", //图标宽度 默认值:60px (HBuilderX 4.0支持) "height": "160px" //图标高度 默认值:60px (HBuilderX 4.0支持) }, buttons: { "iconWidth": "55px", // 图标宽度(高度等比例缩放) 默认值:45px "list": [ // { // "provider": "apple", // "iconPath": "/static/apple.png" // 图标路径仅支持本地图片 // }, { "provider": "weixin", "iconPath": "/static/images/wechat.png" // 图标路径仅支持本地图片 } ] }, privacyTerms: { "defaultCheckBoxState":false, // 条款勾选框初始状态 默认值: true "isCenterHint":false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持) "uncheckedImage":"/static/icons/radio_default_icon.png", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持) "checkedImage":"/static/icons/radio_choose_icon.png", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持) "checkBoxSize":13, // 可选 条款勾选框大小 "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB "termsColor": "#018C39", // 协议文字颜色 默认值: #5496E3 "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意” "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录” "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效 { "url": "https://userapp.zhengzefeng.com/h5/userAgreement", // 点击跳转的协议详情页面 "title": "用户服务协议" // 协议名称 }, { "url": "https://userapp.zhengzefeng.com/h5/privacyPolicy", // 点击跳转的协议详情页面 "title": "隐私保护" // 协议名称 } // { // "url": "https://userapp.his.cdwjyyh.com/h5/healthCustomerService", // 点击跳转的协议详情页面 // "title": "健康客服协议" // 协议名称 // } ] }, }, success(res){ // 登录成功 console.log("qxj univerify login:"+JSON.stringify(res.authResult)); // {openid:'登录授权唯一标识',access_token:'接口返回的 token'} //uni.hideLoading(); // that.$qconfig.applyLogin=false; loginType=3; loginByUniverify(res.authResult,0); }, fail(res){ //uni.hideLoading(); // that.$qconfig.applyLogin=false; if (res.code == 30002) { // uni.closeAuthView() uni.navigateTo({ url: "/pages/auth/passwordLogin" }) }else if(res.code==30008){ //用户点击了自定义按钮 uni.getCheckBoxState({ success(res){ console.log(res.state) // Boolean 用户是否勾选了选框 console.log(res.errMsg) if(!res.state){ uni.showToast({title: "请同意服务条款和隐私政策",icon: 'none',position:'bottom'}); return; } loginType=2; uni.login({ provider: "weixin", onlyAuthorize:false, success: async (res) => { console.log('login success:', JSON.stringify(res)); let that=this; uni.showLoading({ title:"处理中..." }); const params={code:res.code}; console.log('微信',params) loginByWeChatAct(params); }, fail: (err) => { console.log('login fail:', JSON.stringify(err)); // 未开通 if (err.code == 1000) { uni.showModal({ title: '登录失败', content: `${err.errMsg}\n,错误码:${err.code}`, confirmText: '开通指南', cancelText: '确定', success: (res) => { if (res.confirm) { setTimeout(() => { plus.runtime.openWeb('https://ask.dcloud.net.cn/article/37965') }, 500) } } }); return; } if (err.code == '-100') { uni.showToast({ title: "授权拒绝", icon: "none" }); return; } // 一键登录预登陆失败 if (err.code == '30005') { uni.showModal({ showCancel: false, title: '预登录失败', content: err.errMsg }); return; } // 一键登录用户关闭验证界面 if (err.code != '30003') { uni.showModal({ showCancel: false, title: '登录失败', content: JSON.stringify(err) }); } }, complete: () => { } }); }, fail(res){ console.log(res.errCode) console.log(res.errMsg) } }); console.log("----qxj 一键登录调用2-----"); } else if(res.code==30003){ //关闭验证界面 return false; } else if(res.code==30005){ //预登录失败 //uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'}); setTimeout(function() { uni.navigateTo({url: "/pages/auth/passwordLogin"}); }, 1800); } else{ } //登录失败 console.log("微信一键登录failRes:"+JSON.stringify(res)); } }); }; export function loginByWeChatAct(params){ loginByWeChat(params).then(res => { if(res.code==200){ if(res.user!=null){ uni.hideLoading(); uni.setStorageSync('AppToken',res.token); uni.setStorageSync('userInfo',JSON.stringify(res.user)); uni.closeAuthView(); uni.$emit('refreshUserInfo',{}); // // #ifdef APP-PLUS // const jyJPush = uni.requireNativePlugin('JY-JPush'); // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> { // // 设置成功或者失败,都会通过这个result回调返回数据;数据格式保持极光返回的安卓/iOS数据一致 // // 注:若没有返回任何数据,考虑是否初始化完成 // console.log("qxj userAlias "+JSON.stringify(result)); // }); // // #endif uni.reLaunch({ url: "/pages/home/index" }) }else{ unionid=res.unionid; if(res.isNew){ bindMobile(); } } } else{ uni.hideLoading(); console.log(res) uni.showToast({title: res.msg,icon: 'none'}); } }, rej => {} ); } export function bindMobile(){ // #ifdef APP-PLUS //uni.showLoading({title:""}); uni.login({ provider: 'univerify', univerifyStyle: { // 自定义登录框样式 fullScreen: false, authButton: { "title": "绑定手机号", "normalColor": "#018C39", "highlightColor": "#018C39", }, privacyTerms: { "defaultCheckBoxState":true, // 条款勾选框初始状态 默认值: true "isCenterHint":false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持) "uncheckedImage":"", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持) "checkedImage":"", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持) "checkBoxSize":18, // 可选 条款勾选框大小 "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB "termsColor": "#018C39", // 协议文字颜色 默认值: #5496E3 "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意” "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录” "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效 ] }, }, success(res){ // console.log("qxj univerify login:"+JSON.stringify(res.authResult)); // {openid:'登录授权唯一标识',access_token:'接口返回的 token'} loginByUniverify(res.authResult,1); }, fail(res){ uni.hideLoading(); uni.closeAuthView(); if(res.errCode==30005){ //预登录失败 //uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'}); //setTimeout(function() { uni.navigateTo({url: "/pages/auth/passwordLogin"}); //}, 1800); } console.log("qxj failRes:"+JSON.stringify(res)); } }); console.log("----qxj 一键登录调用3-----"); // #endif // #ifdef H5 uni.navigateTo({ url: "/pages/auth/passwordLogin" }); // #endif }; export function loginByUniverify(authResult,type){ uniCloud.callFunction({ name: 'loginByMobile', // 你的云函数名称 data: { access_token: authResult.access_token, // 客户端一键登录接口返回的access_token openid:authResult.openid // 客户端一键登录接口返回的openid } }).then(res => { console.log("qxj doUniverify:",JSON.stringify(res)); if(res.result.errCode==0){ userPhone=res.result.phoneNumber; console.log("电话:",res.result.phoneNumber); if(type==0){ loginActionAct(); }else{ setPhoneAct(userPhone,unionid); } }else{ uni.showToast({title:res.result.message,icon: 'none' }); } }).catch(err=>{ // 处理错误 console.error('调用云函数失败:',err); // 详细记录错误信息 console.error('错误信息:', err.errMsg); }) } export function loginActionAct(){ if (loginType==3 && userPhone==null) { uni.showToast({ title: "请授权手机号登录", icon: 'none' }); return } var data = { phone:userPhone, // jpushId:registrationID, loginType:loginType }; var that=this; uni.showLoading({ title:"处理中..." }); login(data).then(res => { uni.hideLoading(); if(res.code==200){ uni.setStorageSync('AppToken',res.token); uni.setStorageSync('userInfo',JSON.stringify(res.user)); uni.closeAuthView(); // // #ifdef APP-PLUS // const jyJPush = uni.requireNativePlugin('JY-JPush'); // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> { // // 设置成功或者失败,都会通过这个result回调返回数据;数据格式保持极光返回的安卓/iOS数据一致 // // 注:若没有返回任何数据,考虑是否初始化完成 // console.log("qxj userAlias "+JSON.stringify(result)); // }); // // #endif uni.reLaunch({ url: "/pages/home/index" }) } else{ console.log("loginActionAct error",res) uni.showToast({title: res.msg,icon: 'none'}); } }, rej => {} ); } export function setPhoneAct(phone,unionId){ let params={phone:phone,unionId:unionId}; setPhone(params).then(res => { uni.hideLoading(); if(res.code==200){ uni.setStorageSync('AppToken',res.token); uni.setStorageSync('userInfo',JSON.stringify(res.user)); uni.closeAuthView(); // // #ifdef APP-PLUS // const jyJPush = uni.requireNativePlugin('JY-JPush'); // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> { // // 设置成功或者失败,都会通过这个result回调返回数据;数据格式保持极光返回的安卓/iOS数据一致 // // 注:若没有返回任何数据,考虑是否初始化完成 // console.log("qxj userAlias "+JSON.stringify(result)); // }); // // #endif } else{ uni.showToast({title: res.msg,icon: 'none'}); } }, rej => {} ); } export function getRegistrationID(){ // #ifdef APP-PLUS const jyJPush = uni.requireNativePlugin('JY-JPush'); //获取registrationID jyJPush.getRegistrationID(result => { console.log("qxj loginJs getRegistrationID result:"+JSON.stringify(result)); registrationID=result.registrationID; uni.setStorageSync("registrationID",result.registrationID) }); // #endif } export function weixinLogin() { loginType=2; uni.login({ provider: "weixin", onlyAuthorize:false, success: async (res) => { console.log('login success:', JSON.stringify(res)); let that=this; uni.showLoading({ title:"处理中..." }); // const params={code:res.code,jpushId:registrationID}; const params={code:res.code}; console.log('login微信:', params); loginByWeChatAct(params); }, fail: (err) => { console.log('login fail:', JSON.stringify(err)); // 未开通 if (err.code == 1000) { uni.showModal({ title: '登录失败', content: `${err.errMsg}\n,错误码:${err.code}`, confirmText: '开通指南', cancelText: '确定', success: (res) => { if (res.confirm) { setTimeout(() => { plus.runtime.openWeb('https://ask.dcloud.net.cn/article/37965') }, 500) } } }); return; } // 授权拒绝 if (err.code == '-100') { uni.showToast({ title: "授权拒绝", icon: "none" }); return; } // 一键登录预登陆失败 if (err.code == '30005') { uni.showModal({ showCancel: false, title: '预登录失败', content: err.errMsg }); return; } if (err.code == 30006) { uni.showToast({ title: "登录已取消,请重新操作", icon: "none" }); return; } // 一键登录用户关闭验证界面 if (err.code != '30003') { uni.showModal({ showCancel: false, title: '登录失败', content: JSON.stringify(err) }); } }, complete: () => { } }); }