const app = getApp(); const commonUtil = require("../../utils/commonUtil"); import { customRequest } from '../../utils/request.js'; Page({ data: { disabled: 0, //0不可点 getmsg: "获取验证码", pop: 0, tel: "", userId: -1, msgCode: "", userUniqueCheck: true, //用户是否唯一 tapFlag: true, //点击手机号倒计时变红 regionMgrId: "", reg_type: "a", wxCode: "", btnColor: false, formPath: "", loginType: "tel", // 登录类型 password: null, showPSW: false }, wxLogin () { var that = this; // #if MP wx.login({ success (res) { console.log('login', res); if (res.code) { that.setData({ wxCode: res.code, }); } else { console.log("获取code失败!" + res.errMsg); } }, }); // #elif ANDROID console.log('ANDROID'); wx.getMiniProgramCode({ success (res) { console.log('getMiniProgramCode', res); if (res.code) { that.setData({ wxCode: res.code, }); } else { console.log("获取code失败!" + res.errMsg); } }, }) // #endif }, onLoad: function (options) { var that = this; console.log(options); if (options.type) { this.setData({ loginType: options.type, }); } this.setData({ formPath: options.path, }); if (app.isNotEmptyCheck(options.scene)) { console.log("XXXXXXXXXXXX") //扫小程序码携带参数 var sceneStr = decodeURIComponent(options.scene); var sceneJson = commonUtil.sceneToJson(sceneStr); console.log("sceneJson===", sceneJson); app.globalData.loginKey = sceneJson.loginKey debugger app.getLoginUserTokenInfo().then(res => { console.log(res) if (app.isNotEmptyCheck(app.globalData.loginKey)) { app.globalData.loginKey = ""; if (app.globalData.loginUserInfo.agencyStatus == 1) { wx.navigateTo({ url: '/pages/bocaiTV/index', }) } } }) } }, onShow: function () { // app.dialog("通知","请注意","知道了") this.wxLogin(); }, changeBtn: function (e) { var myreg = /^[1][3456789][0-9]{9}$/; if (myreg.test(e.detail.value)) { console.log("验证码变色"); this.setData({ disabled: "1", btnColor: true, tel: e.detail.value, }); } else { this.setData({ btnColor: false, disabled: 0, }); } }, showCha: function (e) { var myreg = /^[1][3456789][0-9]{9}$/; console.log(e.detail.value); if (e.target.dataset.tel == 1) { // && this.data.tapFlag if (myreg.test(e.detail.value)) { console.log("验证码变色"); this.setData({ disabled: 1, btnColor: true, tel: e.detail.value, }); } else { this.setData({ btnColor: false, disabled: 0, tel: e.detail.value, }); } } else { this.setData({ msgCode: e.detail.value, }); } }, /** * 改变登录类型 * * */ changeLoginType () { this.setData({ loginType: this.data.loginType == "psw" ? "tel" : "psw", }); }, sendMsg: function () { console.log(this.data.tel); var that = this; if (that.data.disabled == 1) { that.getCode(); } that.setData({ btnColor: false, disabled: 0, }); var timer = 1; if (timer == 1) { timer = 0; var time = 60; var inter = setInterval(function () { that.setData({ getmsg: time + "s", btnColor: false, disabled: 0, tapFlag: false, }); time--; if (time < 0) { timer = 1; clearInterval(inter); that.setData({ getmsg: "重新获取", btnColor: true, disabled: 1, tapFlag: true, }); } }, 1000); } }, getCode: function () { var that = this; customRequest("/commons/sendMsgCode", { header: 'headers', method: 'GET', data: { tel: that.data.tel, } }).then((res) => { if (res.data.status == 200) { } else if (res.data.status == 9999) { app.dialogNotLogin(); } else { } }) // wx.request({ // url: app.globalData.ip + "/commons/sendMsgCode", // data: { // tel: that.data.tel, // }, // header: app.globalData.headers, // method: "GET", // success: function (res) { // console.log("发送短信验证码"); // console.log(res); // if (res.data.status == 200) { // } else if (res.data.status == 9999) { // app.dialogNotLogin(); // } else { // } // }, // }); }, getPhoneNumber (e) { var that = this; console.log(e); console.log(e.detail.errMsg); if ("getPhoneNumber:ok" == e.detail.errMsg || "phonenumber:ok" == e.detail.errMsg) { //同意 wx.showLoading({ title: "登录中...", }); var iv = e.detail.iv; var encryptedData = e.detail.encryptedData; // #if MP wx.checkSession({ success () { console.log('checkSession success'); //session_key 未过期,并且在本生命周期一直有效 customRequest("/getWechatTel", { header: 'headers', method: 'GET', data: { code: that.data.wxCode, iv: iv, encryptedData: encryptedData, appId: 'wxb0dea4d11428c6a5', } }).then((res) => { app.globalData.openId = res.data.data.openId; app.globalData.unionid = res.data.data.unionid; app.globalData.isLogin = 1; let promise = app.setLoginUserTokenInfo(res.data.data.phoneNumber, null); promise.then((res) => { try { var comeFrom = wx.getStorageSync("comeFromPage"); console.log(comeFrom); // if (app.isNotEmptyCheck(that.globalData.loginKey)) { // wx.redirectTo({ // url: 'pages/bocaiTV/index', // }) // } // var comeFromPageParam = wx.getStorageSync('comeFromPageParam'); console.log(comeFrom); if (comeFrom == "me") { wx.reLaunch({ url: "/pages/mine/index", }); } else if (comeFrom == "set") { wx.reLaunch({ url: "../workBench/index", }); } else if (comeFrom == "collect") { wx.reLaunch({ url: "/pages/collect/index", }); } else if (comeFrom == "detail") { var pages = getCurrentPages(); var prevPage = pages[pages.length - 2]; // 上一个页面 // prevPage.data.searchForm.storeJobId = comeFromPageParam.storeJobId prevPage.setData({ isSharePage: true, }); wx.navigateBack({ delta: 1, }); // wx.reLaunch({ // url: "/pages/detail/index?storeJobId=" + comeFromPageParam.storeJobId // }) } else if (comeFrom == "firstBill") { wx.reLaunch({ url: "/pages/firstBill/index", }); } else if (comeFrom == "BillInfo") { // console.log(wx.getStorageSync('QRBillInfo')); // console.log(JSON.stringify(wx.getStorageSync('QRBillInfo'))); wx.reLaunch({ url: "/pages/wodeBillDetail/index?info=" + JSON.stringify(wx.getStorageSync("QRBillInfo")), }); wx.removeStorageSync("QRBillInfo"); } else if (comeFrom == "shareList") { console.log("shareList"); wx.navigateBack({ detail: 1 }) wx.removeStorageSync("comeFromPage"); } else { wx.setStorage({ key: "comeFrom", data: "login", }); wx.reLaunch({ url: "/pages/firstBill/index", }); } } catch (e) { console.log(e); wx.setStorage({ key: "comeFrom", data: "login", }); wx.reLaunch({ url: "/pages/firstBill/index", }); } }); }) // wx.request({ // url: app.globalData.ip + "/getWechatTel", // data: { // code: that.data.wxCode, // iv: iv, // encryptedData: encryptedData, // type: "yishoudan", // }, // success: function (res) { // console.log(res); // app.globalData.openId = res.data.data.openId; // app.globalData.unionid = res.data.data.unionid; // app.globalData.isLogin = 1; // let promise = app.setLoginUserTokenInfo(res.data.data.phoneNumber, null); // promise.then((res) => { // try { // var comeFrom = wx.getStorageSync("comeFromPage"); // console.log(comeFrom); // // if (app.isNotEmptyCheck(that.globalData.loginKey)) { // // wx.redirectTo({ // // url: 'pages/bocaiTV/index', // // }) // // } // // var comeFromPageParam = wx.getStorageSync('comeFromPageParam'); // console.log(comeFrom); // if (comeFrom == "me") { // wx.reLaunch({ // url: "/pages/mine/index", // }); // } else if (comeFrom == "set") { // wx.reLaunch({ // url: "../workBench/index", // }); // } else if (comeFrom == "collect") { // wx.reLaunch({ // url: "/pages/collect/index", // }); // } else if (comeFrom == "detail") { // var pages = getCurrentPages(); // var prevPage = pages[pages.length - 2]; // 上一个页面 // // prevPage.data.searchForm.storeJobId = comeFromPageParam.storeJobId // prevPage.setData({ // isSharePage: true, // }); // wx.navigateBack({ // delta: 1, // }); // // wx.reLaunch({ // // url: "/pages/detail/index?storeJobId=" + comeFromPageParam.storeJobId // // }) // } else if (comeFrom == "firstBill") { // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // } else if (comeFrom == "BillInfo") { // // console.log(wx.getStorageSync('QRBillInfo')); // // console.log(JSON.stringify(wx.getStorageSync('QRBillInfo'))); // wx.reLaunch({ // url: "/pages/wodeBillDetail/index?info=" + JSON.stringify(wx.getStorageSync("QRBillInfo")), // }); // wx.removeStorageSync("QRBillInfo"); // } else if (comeFrom == "shareList") { // console.log("shareList"); // wx.navigateBack({ // detail: 1 // }) // wx.removeStorageSync("comeFromPage"); // } else { // wx.setStorage({ // key: "comeFrom", // data: "login", // }); // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // } // } catch (e) { // console.log(e); // wx.setStorage({ // key: "comeFrom", // data: "login", // }); // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // } // }); // }, // fail: function (res) { // console.log("获取用户手机号错误"); // }, // }); }, fail () { console.log('checkSession fail'); console.log(wx.login); // session_key 已经失效,需要重新执行登录流程 wx.login({ success (res) { console.log('login res', res); if (res.code) { console.log(res.code); //发起网络请求 customRequest("/getWechatTel", { header: 'headers', method: 'GET', data: { code: res.code, iv: iv, encryptedData: encryptedData, type: "dtdl", } }).then((res) => { let promise = app.setLoginUserTokenInfo(res.data.data.phoneNumber, null); promise.then((res) => { wx.reLaunch({ url: "/pages/firstBill/index", }); }); }) // wx.request({ // url: app.globalData.ip + "/getWechatTel", // data: { // code: res.code, // iv: iv, // encryptedData: encryptedData, // type: "dtdl", // }, // success: function (res) { // console.log(res); // let promise = app.setLoginUserTokenInfo(res.data.data.phoneNumber, null); // promise.then((res) => { // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // }); // }, // fail: function (res) { // console.log("获取用户手机号错误"); // }, // }); } else { console.log("获取手机号失败!" + res.errMsg); } }, }); }, }); // // #elif ANDROID // wx.checkSession({ // success (res) { // console.log('checkIdentitySession success', res); // wx.request({x // url: app.globalData.ip + "/getWechatTel", // data: { // code: that.data.wxCode, // iv: iv, // encryptedData: encryptedData, // type: "yishoudan", // }, // success: function (res) { // console.log(res); // app.globalData.openId = res.data.data.openId; // app.globalData.unionid = res.data.data.unionid; // app.globalData.isLogin = 1; // let promise = app.setLoginUserTokenInfo(res.data.data.phoneNumber, null); // promise.then((res) => { // try { // var comeFrom = wx.getStorageSync("comeFromPage"); // console.log(comeFrom); // // if (app.isNotEmptyCheck(that.globalData.loginKey)) { // // wx.redirectTo({ // // url: 'pages/bocaiTV/index', // // }) // // } // // var comeFromPageParam = wx.getStorageSync('comeFromPageParam'); // console.log(comeFrom); // if (comeFrom == "me") { // wx.reLaunch({ // url: "/pages/mine/index", // }); // } else if (comeFrom == "set") { // wx.reLaunch({ // url: "../workBench/index", // }); // } else if (comeFrom == "collect") { // wx.reLaunch({ // url: "/pages/collect/index", // }); // } else if (comeFrom == "detail") { // var pages = getCurrentPages(); // var prevPage = pages[pages.length - 2]; // 上一个页面 // // prevPage.data.searchForm.storeJobId = comeFromPageParam.storeJobId // prevPage.setData({ // isSharePage: true, // }); // wx.navigateBack({ // delta: 1, // }); // // wx.reLaunch({ // // url: "/pages/detail/index?storeJobId=" + comeFromPageParam.storeJobId // // }) // } else if (comeFrom == "firstBill") { // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // } else if (comeFrom == "BillInfo") { // // console.log(wx.getStorageSync('QRBillInfo')); // // console.log(JSON.stringify(wx.getStorageSync('QRBillInfo'))); // wx.reLaunch({ // url: "/pages/wodeBillDetail/index?info=" + JSON.stringify(wx.getStorageSync("QRBillInfo")), // }); // wx.removeStorageSync("QRBillInfo"); // } else if (comeFrom == "shareList") { // console.log("shareList"); // wx.navigateBack({ // detail: 1 // }) // wx.removeStorageSync("comeFromPage"); // } else { // wx.setStorage({ // key: "comeFrom", // data: "login", // }); // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // } // } catch (e) { // console.log(e); // wx.setStorage({ // key: "comeFrom", // data: "login", // }); // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // } // }); // }, // fail: function (res) { // console.log("获取用户手机号错误"); // }, // }); // }, // fail (err) { // console.log('checkIdentitySession err', err); // } // }) // #endif } else { //拒绝 try { wx.setStorageSync("comeFromPage", "me"); } catch (e) { console.log("0-页面跳转,设置参数错误:", e); } // wx.navigateTo({ // url: "/pages/login/index", // }); } return false; }, /** * app用微信登录 */ appLogin () { wx.weixinAppLogin({ success (res) { console.log('weixinAppLogin res', res); if (res.code) { console.log(res.code); //发起网络请求 customRequest("/getWechatTel", { header: 'headers', method: 'GET', data: { code: res.code, iv: iv, encryptedData: encryptedData, type: "dtdl", } }).then((res) => { let promise = app.setLoginUserTokenInfo(res.data.data.phoneNumber, null); promise.then((res) => { wx.reLaunch({ url: "/pages/firstBill/index", }); }); }) // wx.request({ // url: app.globalData.ip + "/getWechatTel", // data: { // code: res.code, // iv: iv, // encryptedData: encryptedData, // type: "dtdl", // }, // success: function (res) { // console.log(res); // let promise = app.setLoginUserTokenInfo(res.data.data.phoneNumber, null); // promise.then((res) => { // wx.reLaunch({ // url: "/pages/firstBill/index", // }); // }); // }, // fail: function (res) { // console.log("获取用户手机号错误"); // }, // }); } else { console.log("获取手机号失败!" + res.errMsg); } }, }); }, /** * 登录信息提交前校验 * * */ beforeFormSubmit (e) { console.log(e); let that = this; if (that.data.loginType == "tel") { this.formSubmit(e.detail.value); } else { this.pswLogin(); } }, /** * 账号密码登录 * * */ pswLogin () { let that = this; app.load("登录中..."); console.log(that.data.tel); console.log(that.data.password); if (that.data.tel == "") { app.hideLoad(); app.showTips(that, "请输入正确手机号"); return; } if (that.data.password == "") { app.hideLoad(); app.showTips(that, "密码不能为空"); return; } var channelContactId = ""; try { var value = wx.getStorageSync("fromQrCodeChannelContactId"); if (value) { console.log("fromQrCodeChannelContactId:======", value); channelContactId = value; } } catch (e) { console.log("获取缓存设置的参数错误:", e); } var agencyUserId = ""; try { var value = wx.getStorageSync("storageSyncAgencyUserId"); if (value) { console.log("storageSyncAgencyUserId======", value); agencyUserId = value; } } catch (e) { console.log("获取缓存设置的参数错误:", e); } customRequest("/yishoudan/login", { header: 'headers', method: 'post', data: { tel: that.data.tel, password: that.data.password, autoLoginTag: 0, loginType: 0, channelContactId: channelContactId, agencyUserId: agencyUserId, isScanQRCodes: app.globalData.isScanQRCodes, wxCode: that.data.wxCode, } }).then((res) => { if (res.data.status == 200) { that.setLoginData(res); customRequest("/costumer/manager", { header: 'headers', method: 'GET', data: {} }).then((res) => { if (res.data.status == 200) { app.globalData.serviceInfo = res.data.data; console.log(app.globalData.serviceInfo); if (app.globalData.serviceInfo.workPhone) { let tel = app.globalData.serviceInfo.workPhone; let num1 = tel.slice(0, 3); let num2 = tel.slice(3, 7); let num3 = tel.slice(7, 11); app.globalData.serviceInfo.tel = num1 + "-" + num2 + "-" + num3; console.log(app.globalData.serviceInfo.tel); } } }) // wx.request({ // url: app.globalData.ip + "/costumer/manager", // header: app.globalData.headers, // success (res) { // console.log(res); // if (res.data.status == 200) { // app.globalData.serviceInfo = res.data.data; // console.log(app.globalData.serviceInfo); // if (app.globalData.serviceInfo.workPhone) { // let tel = app.globalData.serviceInfo.workPhone; // let num1 = tel.slice(0, 3); // let num2 = tel.slice(3, 7); // let num3 = tel.slice(7, 11); // app.globalData.serviceInfo.tel = num1 + "-" + num2 + "-" + num3; // console.log(app.globalData.serviceInfo.tel); // } // } // }, // }); console.log(222); } else { wx.showToast({ icon: "none", title: res.data.msg, duration: 2000, }); } }) // wx.request({ // url: app.globalData.ip + "/yishoudan/login", // data: { // tel: that.data.tel, // password: that.data.password, // autoLoginTag: 0, // loginType: 0, // channelContactId: channelContactId, // agencyUserId: agencyUserId, // isScanQRCodes: app.globalData.isScanQRCodes, // wxCode: that.data.wxCode, // }, // method: "post", // header: { // "content-type": "application/x-www-form-urlencoded", // }, // success: function (res) { // app.hideLoad(); // console.log(res.data); // if (res.data.status == 200) { // that.setLoginData(res); // wx.request({ // url: app.globalData.ip + "/costumer/manager", // header: app.globalData.headers, // success (res) { // console.log(res); // if (res.data.status == 200) { // app.globalData.serviceInfo = res.data.data; // console.log(app.globalData.serviceInfo); // if (app.globalData.serviceInfo.workPhone) { // let tel = app.globalData.serviceInfo.workPhone; // let num1 = tel.slice(0, 3); // let num2 = tel.slice(3, 7); // let num3 = tel.slice(7, 11); // app.globalData.serviceInfo.tel = num1 + "-" + num2 + "-" + num3; // console.log(app.globalData.serviceInfo.tel); // } // } // }, // }); // console.log(222); // } else { // wx.showToast({ // icon: "none", // title: res.data.msg, // duration: 2000, // }); // } // }, // }); }, /** * 手机验证登录 * * */ formSubmit: function (data) { app.load("登录中..."); var that = this; if (that.data.tel == "") { app.hideLoad(); app.showTips(that, "请输入正确手机号"); return; } if (that.data.msgCode == "") { app.hideLoad(); app.showTips(that, "验证码不能为空"); return; } var channelContactId = ""; try { var value = wx.getStorageSync("fromQrCodeChannelContactId"); if (value) { console.log("fromQrCodeChannelContactId:======", value); channelContactId = value; } } catch (e) { console.log("获取缓存设置的参数错误:", e); } var agencyUserId = ""; try { var value = wx.getStorageSync("storageSyncAgencyUserId"); if (value) { console.log("storageSyncAgencyUserId======", value); agencyUserId = value; } } catch (e) { console.log("获取缓存设置的参数错误:", e); } console.log(that.data.tel); customRequest("/appLoginByTelYiShouDan", { header: 'headers', method: 'GET', data: { tel: that.data.tel, code: that.data.msgCode, autoLoginTag: 0, loginType: "login", channelContactId: channelContactId, agencyUserId: agencyUserId, isScanQRCodes: app.globalData.isScanQRCodes, wxCode: that.data.wxCode, } }).then((res) => { app.hideLoad(); console.log(res.data); if (res.data.status == 200) { that.setLoginData(res); console.log(222); } else { wx.showToast({ icon: "none", title: res.data.msg, duration: 2000, }); } }) }, /** * 将传回的登录信息存储以及页面的跳转 * * */ setLoginData (res) { console.log(res); app.globalData.user = res.data.data.user; app.globalData.sessionId = res.data.data.sessionId; let token = res.data.data.token; // app.globalData.header.Authorization = "Bearer " + token; app.globalData.headers2.Authorization = "Bearer " + token; app.globalData.headers1.Authorization = "Bearer " + token; app.globalData.headers.Authorization = "Bearer " + token; app.globalData.isLogin = true; app.globalData.agencyStatus = res.data.data.agencyStatus; app.globalData.loginUserInfo = res.data.data; //登录设置缓存 app.globalData.userLoginTokenInfo.tel = res.data.data.tel; // app.globalData.userLoginTokenInfo.token = null; wx.setStorageSync("loginUserTokenInfo", app.globalData.userLoginTokenInfo); console.log(111); try { var comeFrom = wx.getStorageSync("comeFromPage"); // var comeFromPageParam = wx.getStorageSync('comeFromPageParam'); console.log(comeFrom); // if (app.isNotEmptyCheck(that.globalData.loginKey)) { // wx.redirectTo({ // url: 'pages/bocaiTV/index', // }) // } if (comeFrom == "me") { wx.reLaunch({ url: "/pages/firstBill/index", }); } else if (comeFrom == "index") { wx.reLaunch({ url: "/pages/firstBill/index", }); } else if (comeFrom == "detail") { var pages = getCurrentPages(); var prevPage = pages[pages.length - 2]; // 上一个页面 // prevPage.data.searchForm.storeJobId = comeFromPageParam.storeJobId prevPage.setData({ isSharePage: true, }); wx.navigateBack({ delta: 1, }); // wx.reLaunch({ // url: "/pages/detail/index?storeJobId=" + comeFromPageParam.storeJobId // }) } else if (comeFrom == "collect") { wx.reLaunch({ url: "/pages/collect/index", }); } else if (comeFrom == "BillInfo") { // console.log(wx.getStorageSync('QRBillInfo')); // console.log(JSON.stringify(wx.getStorageSync('QRBillInfo'))); wx.reLaunch({ url: "/pages/wodeBillDetail/index?info=" + JSON.stringify(wx.getStorageSync("QRBillInfo")), }); wx.removeStorageSync("QRBillInfo"); } else if (comeFrom == "shareList") { console.log("shareList"); wx.navigateBack({ detail: 1 }) wx.removeStorageSync("comeFromPage"); } else { wx.setStorage({ key: "comeFrom", data: "login", }); wx.reLaunch({ url: "/pages/workBench/index", }); } } catch (e) { wx.setStorage({ key: "comeFrom", data: "login", }); wx.reLaunch({ url: "/pages/firstBill/index", }); } }, ohShitfadeOut () { var fadeOutTimeout = setTimeout(() => { this.setData({ popErrorMsg: "", pop: 0 }); clearTimeout(fadeOutTimeout); }, 3000); }, clearInput: function () { this.setData({ tel: "", }); }, showPSW () { this.setData({ showPSW: !this.data.showPSW, }); } });