| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- import { login,loginByWeChat,setPhone,bindWechat,updatePushId } from '@/api/user'
- import { qconfig } from '@/utils/config.js';
- import { navBack, registerIdCode,updateMsgDot,setSource } from '@/utils/common.js';
- import {isLogin} from '@/utils/common.js'
- let univerifyManager=null;
- let registrationID=uni.getStorageSync("registrationID");
- let userPhone="";
- let unionid="";
- let loginType=3;//1账号密码 2:微信登录 3:手机号一键登录
- let isMobileLoginFst=false; //标识是否先手机号登录且没有绑定微信;(该手机号用户没有微信unionId)
- let pages = '';
- let url = ''; //当前页页面实例
- // #ifdef APP-PLUS
- let source = 'app';
- // #endif
- // #ifndef APP-PLUS
- let source = '';
- // #endif
- export function showLoginPage(){
- pages = getCurrentPages();
- url = pages[ pages.length - 1];
- // #ifdef APP-PLUS
- if(qconfig!=undefined && qconfig.applyLogin){
- return;
- }
- if(!plus.runtime.isAgreePrivacy()) {
- if(url&&url.route=="pages/auth/login"){
- return;
- }
- uni.navigateTo({
- url: "/pages/auth/login"
- });
- return;
- }
- uni.showLoading({title:""});
- getRegistrationID();
- let that=this;
- console.log("----qxj 一键登录调用1-----");
- univerifyManager = uni.getUniverifyManager();
-
- //手机号一键登录时
- univerifyLogin(this);
-
- const loginCallBack = (res) => {
- // 获取一键登录弹框协议勾选状态
- univerifyManager.getCheckBoxState({success(res) {
- if(qconfig!=undefined){
- qconfig.applyLogin=false;
- }
- uni.hideLoading();
- if(!res.state){
- uni.showToast({title: "请同意服务条款和隐私政策",icon: 'none',position:'bottom'});
- return;
- }
- quickWechatLogin(false);
- // 关闭一键登录弹框
- univerifyManager.close();
- }
- });
- }
-
- // 订阅自定义按钮点击事件
- univerifyManager.onButtonsClick(loginCallBack);
- // 取消订阅自定义按钮点击事件
- //univerifyManager.offButtonsClick(loginCallBack);
- if(qconfig!=undefined){
- qconfig.applyLogin=true;
- }
- uni.hideLoading();
- // #endif
-
- // #ifdef H5
-
- if(String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"){
- let url=window.location.href;
- //let urlPaths=url.split("/h5");
- let urlPaths=url.split("/pages");
- let pagePath="/pages"+urlPaths[1];
- uni.setStorageSync('beforLoginPage',pagePath);
- uni.navigateTo({
- url: "/pages/auth/h5WxLogin"
- });
- }
- else{
- if(url&&url.route=="pages/auth/login"){
- return;
- }
- uni.navigateTo({
- url: "/pages/auth/login"
- });
- }
-
- // #endif
-
- }
-
- const univerifyLogin=function(that){
- univerifyManager.login({
- provider: 'univerify',
- univerifyStyle: { // 自定义登录框样式
- fullScreen: true,
- authButton: {
- "title": "本机号码一键登录",
- "normalColor": "#FF5C03",
- "highlightColor": "#FF5C03",
- },
- otherLoginButton: {
- "title": "微信快捷登录",
- "visible": "false",
- },
- closeIcon: {
- "path": "static/image/icon_close.png",
- "width": "160px", //图标宽度 默认值:60px (HBuilderX 4.0支持)
- "height": "160px" //图标高度 默认值:60px (HBuilderX 4.0支持)
- },
- buttons: {
- "iconWidth": "60px", // 图标宽度(高度等比例缩放) 默认值:45px
- "list": [
- // {
- // "provider": "apple",
- // "iconPath": "/static/apple.png" // 图标路径仅支持本地图片
- // },
- {
- "provider": "weixin",
- "iconPath": "/static/image/login/weixin.png" // 图标路径仅支持本地图片
- }
- ]
- },
- privacyTerms: {
- "defaultCheckBoxState":false, // 条款勾选框初始状态 默认值: true
- "isCenterHint":false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持)
- "uncheckedImage":"", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持)
- "checkedImage":"", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持)
- "checkBoxSize":18, // 可选 条款勾选框大小
- "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB
- "termsColor": "#FF5C03", // 协议文字颜色 默认值: #5496E3
- "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
- "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
- "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
- {
- "url": "https://userapp.his.cdwjyyh.com/web/userAgreement", // 点击跳转的协议详情页面
- "title": "用户服务协议" // 协议名称
- },
- {
- "url": "https://userapp.his.cdwjyyh.com/web/privacyPolicy", // 点击跳转的协议详情页面
- "title": "隐私保护" // 协议名称
- }
- ]
- },
- },
- success(res){ // 登录成功
- // console.log("qxj univerify login:"+JSON.stringify(res.authResult)); // {openid:'登录授权唯一标识',access_token:'接口返回的 token'}
- uni.hideLoading();
- if(qconfig!=undefined){
- qconfig.applyLogin=false;
- }
- loginType=3;
- loginByUniverify(res.authResult,0);
- },
- fail(res){
- uni.hideLoading();
- if(qconfig!=undefined){
- qconfig.applyLogin=false;
- }
- if(res.code==30008){ //用户点击了自定义按钮
- uni.getCheckBoxState({
- success(res){
- if(!res.state){
- uni.showToast({title: "请同意服务条款和隐私政策",icon: 'none',position:'bottom'});
- return;
- }
- quickWechatLogin(false);
- },
- 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'});
- if(url&&url.route=="pages/auth/login"){
- return;
- }
- setTimeout(function() {
- uni.navigateTo({url: "/pages/auth/login"});
- }, 400);
- }
- }
- });
- };
-
-
- export function quickWechatLogin(isMobileLogin){
- isMobileLoginFst=isMobileLogin;
- loginType=2;
- uni.login({
- provider: "weixin",
- onlyAuthorize:true,
- success: async (res) => {
- let that=this;
- uni.showLoading({
- title:"处理中..."
- });
- if(isMobileLogin){
- bindWechatAct(userPhone,res.code);
- }
- else{
- const params={"code":res.code,"loginDevice":getLoginDevice(),"jpushId":registrationID,"source":plus.runtime.channel || source};
- 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 == '30005') {
- uni.showModal({
- showCancel: false,
- title: '预登录失败',
- content: err.errMsg
- });
- return;
- }
- // 一键登录用户关闭验证界面
- if (err.code != '30003') {
- uni.showModal({
- showCancel: false,
- title: '登录失败',
- content: JSON.stringify(err)
- });
- }
- },
- complete: () => {
-
- }
- });
-
- };
-
- 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.$emit('refreshUserInfo',{ "isFirst":res.isFirst });
- uni.$emit('showHealthButler');
- uni.closeAuthView();
- if(res.isFirst){ //未完成新手任务
- uni.navigateTo({ url: '/pages/user/integral/points' });
- }
- else {
- goPage()
- }
- }
- else{
- unionid=res.unionid;
- if(isMobileLoginFst){ //若先点击手机号登录且没有绑定微信
- bindWechatAct(userPhone,unionid);
- }else{
- if(res.isNew){ //是否绑定手机号
- bindMobile();
- }
- }
- }
- }
- else{
- uni.hideLoading();
- 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": "#FF5C03",
- "highlightColor": "#FF5C03",
- },
- 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": "#FF5C03", // 协议文字颜色 默认值: #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'});
- if(url&&url.route=="pages/auth/login"){
- return;
- }
- //setTimeout(function() {
- uni.navigateTo({url: "/pages/auth/login"});
- //}, 1800);
- }
- console.log("qxj failRes:"+JSON.stringify(res));
-
- }
- });
- console.log("----qxj 一键登录调用3-----");
-
- // #endif
-
- // #ifdef H5
- if(url&&url.route=="pages/auth/login"){
- return;
- }
- uni.navigateTo({
- url: "/pages/auth/login"
- });
- // #endif
- };
-
- export function loginByUniverify(authResult,type){
- uniCloud.callFunction({
- name: 'loginByMobile', // 你的云函数名称
- data: {
- access_token: authResult.access_token, // 客户端一键登录接口返回的access_token
- openid:authResult.openid // 客户端一键登录接口返回的openid
- }
- }).then(res => {
- if(res.result.errCode==0){
- userPhone=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,
- loginDevice:getLoginDevice(),
- source:plus.runtime.channel || source
- };
- var that=this;
- uni.showLoading({
- title:"处理中..."
- });
- login(data).then(res => {
- console.log("qxj res:"+JSON.stringify(res));
- uni.hideLoading();
- if(res.code==200){
- if(res.isNew){
- userPhone=res.phone;
- quickWechatLogin(true);
- return;
- }
- uni.setStorageSync('AppToken',res.token);
- uni.setStorageSync('userInfo',JSON.stringify(res.user));
- uni.$emit('showHealthButler');
- uni.closeAuthView();
- if(res.isFirst){
- // #ifdef APP-PLUS
- if(plus.runtime.channel=="baidu"){ //获取百度渠道标识
- let bdCmdType=uni.getStorageSync("bdCmdType");
- if(bdCmdType!=null && parseInt(bdCmdType)<1){
- registerIdCode("register",1,0,res.user.userId); //注册
- }
- }
- // #endif
- uni.navigateTo({ url: '/pages/user/integral/points' });
- } else {
- goPage()
- }
- }
- else{
- uni.showToast({title: res.msg,icon: 'none'});
- }
- },
- rej => {}
- );
- }
-
- export function setPhoneAct(phone,unionId,){
- let params={"phone":phone,"unionId":unionId,"jpushId":registrationID,"source":plus.runtime.channel || source};
- setPhone(params).then(res => {
- uni.hideLoading();
- if(res.code==200){
- uni.setStorageSync('AppToken',res.token);
- uni.setStorageSync('userInfo',JSON.stringify(res.user));
- uni.$emit('showHealthButler');
- uni.closeAuthView();
- goPage()
- // #ifdef APP-PLUS
- // const jyJPush = uni.requireNativePlugin('JY-JPush');
- // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> {
- // console.log("qxj userAlias "+JSON.stringify(result));
- // });
- // #endif
- }
- else{
- uni.showToast({title: res.msg,icon: 'none'});
- }
- },
- rej => {}
- );
- }
-
-
- export function bindWechatAct(phone,code){
- let params={"phone":phone,"code":code,"jpushId":registrationID,"source":plus.runtime.channel || source};
- bindWechat(params).then(res => {
- uni.hideLoading();
- console.log("qxj bindWeChat res:"+JSON.stringify(res));
- if(res.code==200){
- uni.setStorageSync('AppToken',res.token);
- uni.setStorageSync('userInfo',JSON.stringify(res.user));
- uni.$emit('showHealthButler');
- uni.closeAuthView();
- goPage()
- // #ifdef APP-PLUS
- const jyJPush = uni.requireNativePlugin('JY-JPush');
- // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> {
- // console.log("qxj userAlias "+JSON.stringify(result));
- // });
- // #endif
- }
- else{
- uni.showToast({title: res.msg,icon: 'none'});
- setTimeout(function() {
- uni.closeAuthView();
- }, 1800);
-
- }
- },
- rej => {}
- );
- }
-
- export function getRegistrationID(type) {
- // #ifdef APP-PLUS
- registrationID = uni.getStorageSync("registrationID");
- const needUpdate = type === 'update' || !registrationID;
- if (needUpdate&&plus.runtime.isAgreePrivacy()) {
- uni.getPushClientId({success: res => {
- registrationID = res.cid
- uni.setStorageSync("registrationID",res.cid);
- if(type == 'update'&&isLogin()) {
- upPushClientId(res.cid)
- }
- },
- fail:(err)=> {
- console.log("getRegistrationID error:",err)
- }
- });
- }
- // #endif
- }
- function getLoginDevice(){
- let loginDevice = "";
- // #ifdef APP-PLUS
- if(plus.runtime.isAgreePrivacy()) {
- let devinfo=uni.getDeviceInfo();
- if(devinfo!=null){
- loginDevice += devinfo.deviceBrand ? devinfo.deviceBrand : "";
- loginDevice += devinfo.deviceModel ? " " + devinfo.deviceModel : "";
- loginDevice += devinfo.system ? " " + devinfo.system : "";
- }
- }
-
- // #endif
-
- return loginDevice;
- }
- function goPage() {
- pages = getCurrentPages();
- url = pages[ pages.length - 1];
- if(univerifyManager) {
- univerifyManager.close();
- uni.closeAuthView();
- univerifyManager = null
- }
- const openUrl = uni.getStorageSync("openUrl")
- if(openUrl) {
- uni.navigateTo({
- url: openUrl,
- success: function(res) {
- uni.removeStorageSync("openUrl")
- }
- })
- } else {
- if(pages.length == 1) {
- uni.reLaunch({
- url: '../course/index',
- //url: '../course/video/living-app',
- animationType: 'none',
- animationDuration: 2000
- })
- } else {
- navBack()
- }
- }
- updateMsgDot()
- setSource()
- }
-
- export function upPushClientId(pushId) {
- updatePushId({pushId:pushId})
- }
-
|