123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846 |
- <template>
- <mobile-frame>
- <view class="main">
- <!-- <view class="one">
- <text>{{type==0?'短信验证码登录':'手机号密码登录'}}</text>
- </view>
- <view class="two">
- <uni-forms ref="form" :model="form" :rules="rules" label-width="auto">
- <uni-forms-item label="手机号" name="phone">
- <uni-easyinput type="number" v-model="form.phone" />
- </uni-forms-item>
- <uni-forms-item label="验证码" name="code" v-if="type==0">
- <view class="yzm">
- <view class="l">
- <uni-easyinput type="number" v-model="form.code" placeholder="请输入验证码" />
- </view>
- <view class="r">
- <button type="default" size="mini" @tap="oneSms">{{sms_1==0?'验证码':sms_1}}</button>
- </view>
- </view>
- </uni-forms-item>
- <uni-forms-item label="登录密码" name="password" v-if="type==1">
- <uni-easyinput type="text" v-model="form.password" placeholder="请输入登录密码" />
- </uni-forms-item>
- <view class="type">
- <button type="default" size="mini" @tap="passLogin">{{type==0?'密码登录':'短信登录'}}</button>
- </view>
- <view class="agree">
- <checkbox-group @change="changeAgree">
- <label>
- <checkbox :checked="agree" />
- <text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
- </label>
- </checkbox-group>
- </view>
- <view class="btn">
- <button type="default" size="mini" @click="onSubmit('form')">登录</button>
- </view>
- </uni-forms>
- </view>
- <view class="thr">
- <view class="thr_1">
- <text>其他登录方式</text>
- </view>
- <view class="thr_2">
- <view class="thr_2_1" @tap="otherLogin('wx')">
- <text class="iconfont icon-weixin"></text>
- </view>
- </view>
- </view> -->
- <view class="wx">
- <view class="wx_1">
- <text class="iconfont icon-weixin"></text>
- </view>
- <view class="wx_2">
- <button size="mini" @tap="otherLogin('wx')">微信信任登录</button>
- </view>
- <view class="wx_3">
- <checkbox-group @change="changeAgree">
- <label>
- <checkbox :checked="agree" />
- <text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
- </label>
- </checkbox-group>
- </view>
- </view>
- </view>
- <uni-popup ref="dialogShow" type="center" :mask-click="false" background-color="#ffffff">
- <view class="popup">
- <view class="title">
- <text>绑定手机号</text>
- </view>
- <view class="wx_1">
- <text>确定获取微信绑定手机号吗?</text>
- </view>
- <view class="wx_2">
- <button size="mini" @tap="diaClose">取消</button>
- <button size="mini" open-type="getPhoneNumber" @getphonenumber="getUserPhone">确定</button>
- </view>
- </view>
- </uni-popup>
- <!-- <uni-popup ref="dialogShow" type="dialog">
- <uni-popup-dialog :title="dialog.title" mode="base" :before-close="true" @confirm="diaSubmit(dialog)" @close="diaReset(dialog)">
- <view class="dialog dialog_1" v-if="dialog.type=='1'">
- <uni-forms ref="phoneForm" :model="phoneForm" label-width="auto">
- <uni-forms-item label="方式">
- <uni-data-checkbox v-model="phoneForm.type" :localdata="typeList" />
- </uni-forms-item>
- <uni-forms-item label="微信" v-if="phoneForm.type==0">
- <button type="default" size="mini" open-type="getPhoneNumber" @getphonenumber="getUserPhone">获取微信绑定手机</button>
- </uni-forms-item>
- <uni-forms-item label="手机号" v-if="phoneForm.type==0">
- <uni-easyinput type="number" v-model="phoneForm.phone" disabled />
- </uni-forms-item>
- <uni-forms-item label="手机号" v-if="phoneForm.type==1">
- <view class="yzm">
- <view class="l">
- <uni-easyinput type="number" v-model="phoneForm.phone" placeholder="请输入手机号" />
- </view>
- <view class="r">
- <button type="default" size="mini" @tap="twoSms">{{sms_1==0?'验证码':sms_1}}</button>
- </view>
- </view>
- </uni-forms-item>
- <uni-forms-item label="验证码" v-if="phoneForm.type==1">
- <uni-easyinput type="number" v-model="phoneForm.code" placeholder="请输入验证码" />
- </uni-forms-item>
- </uni-forms>
- </view>
- </uni-popup-dialog>
- </uni-popup> -->
- </mobile-frame>
- </template>
- <script>
- export default {
- data() {
- return {
- // 平台信息
- system: {},
- openid: '',
- // 登录类型
- type: 0,
- form: {},
- rules: {
- phone: {
- rules: [ //
- {
- required: true,
- errorMessage: '请输入手机号',
- },
- {
- minLength: 11,
- maxLength: 11,
- errorMessage: '账号长度在{maxLength}个字符',
- }
- ]
- }
- },
- sms_1: 0,
- // 用戶协议
- agree: true,
- // 注册账号信息
- user: {},
- // 弹框
- dialog: {
- title: '绑定手机号',
- type: '1'
- },
- // 绑定手机号
- phoneForm: {
- type: 0,
- },
- typeList: [ //
- {
- text: '微信',
- value: 0
- }
- // {
- // text: '短信验证',
- // value: 1
- // }
- ],
- };
- },
- onShow: function() {
- const that = this;
- // 查询平台信息
- that.searchSystem();
- },
- methods: {
- // 查询平台信息
- searchSystem() {
- const that = this;
- uni.getStorage({
- key: 'system',
- success: function(res) {
- that.$set(that, `system`, res.data);
- that.searchOpenid();
- },
- fail: function(err) {
- uni.showToast({
- title: err.errMsg,
- icon: 'none'
- })
- }
- })
- },
- // 查询openid
- searchOpenid() {
- const that = this;
- let system = that.system;
- if (system.uniPlatform == "mp-weixin") {
- uni.login({
- provider: 'weixin',
- success: async function(res) {
- const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
- config: that.$config.wx_projectkey,
- js_code: res.code
- })
- if (aee.errcode == '0') {
- that.$set(that, `openid`, aee.data.openid);
- }
- }
- })
- } else if (system.uniPlatform == "app") {
- console.log('app');
- }
- },
- // 选择登录类型
- typeChange(e) {
- const that = this;
- that.$set(that, `type`, e.detail.value);
- },
- // 短信登录/密码登录
- passLogin() {
- const that = this;
- let type = 0;
- if (that.type == 0) type = 1;
- that.$set(that, `type`, type)
- },
- // 短信登录-发送验证码
- async oneSms() {
- const that = this;
- let form = that.form;
- if (form && form.phone) {
- let res = await that.$api(`/user/toLoginByCode`, 'POST', {
- phone: form.phone
- })
- if (res.errcode == '0') {
- uni.showToast({
- title: '发送成功',
- icon: 'none'
- })
- that.$set(that, `sms_1`, 60);
- that.timeDown();
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- } else {
- uni.showToast({
- title: '输入错误,请重新输入!',
- icon: 'none'
- })
- }
- },
- // 登录
- onSubmit(ref) {
- const that = this;
- let agree = that.agree;
- let system = that.system;
- let type = that.type;
- that.$refs[ref].validate().then(async params => {
- if (agree) {
- if (type == 0 && system.uniPlatform == 'app') {
- // app,短信验证码登录
- that.appSmsLogin(params);
- } else if (type == 1 && system.uniPlatform == 'app') {
- // app,密码登录
- that.appPassLogin(params);
- } else if (type == 0 && system.uniPlatform == 'mp-weixin') {
- // wx,短信验证码登录
- that.wxSmsLogin(params)
- } else if (type == 1 && system.uniPlatform == 'mp-weixin') {
- // wx,密码登录
- that.wxPassLogin(params)
- }
- } else {
- uni.showToast({
- title: '请阅读并同意用户协议和隐私政策',
- icon: 'none'
- })
- }
- }).catch(err => {
- console.log(err);
- })
- },
- // app,短信验证码登录
- async appSmsLogin(e) {
- const that = this;
- if (e.code) {
- const res = await that.$api(`/user/checkLoginCode`, 'POST', e);
- if (res.errcode == '0') {
- uni.setStorage({
- key: 'token',
- data: res.data,
- success: function() {
- uni.navigateBack({
- delta: 1
- })
- }
- });
- } else {
- if (res.errcode == '-5') {
- uni.showToast({
- title: '用户不存在',
- icon: 'none'
- })
- // todo:验证码登录用户不存在可自动注册账号,并跳转
- } else {
- uni.showToast({
- title: res.errmsg || '信息错误',
- icon: 'none'
- })
- }
- }
- } else {
- uni.showToast({
- title: '请输入验证码',
- icon: 'none'
- })
- }
- },
- // app,密码登录
- async appPassLogin(e) {
- const that = this;
- if (e.password) {
- const res = await that.$api(`/user/login`, 'POST', e);
- if (res.errcode == '0') {
- uni.setStorage({
- key: 'token',
- data: res.data,
- success: function() {
- uni.navigateBack({
- delta: 1
- })
- }
- });
- } else {
- uni.showToast({
- title: res.errmsg || '信息错误',
- icon: 'none'
- })
- }
- } else {
- uni.showToast({
- title: '请输入登录密码',
- icon: 'none'
- })
- }
- },
- // wx,短信验证码登录
- async wxSmsLogin(e) {
- const that = this;
- if (e.code) {
- const res = await that.$api(`/user/checkLoginCode`, 'POST', e);
- if (res.errcode == '0') {
- uni.setStorage({
- key: 'token',
- data: res.data,
- success: function() {
- uni.navigateBack({
- delta: 1
- })
- }
- });
- } else {
- if (res.errcode == '-5') {
- uni.showModal({
- title: '提示',
- content: '用户不存在,是否注册新账号?',
- success: function(res) {
- if (res.confirm) {
- // 无账号,注册账号
- that.wxCreate();
- }
- }
- });
- } else {
- uni.showToast({
- title: res.errmsg || '信息错误',
- icon: 'none'
- })
- }
- }
- } else {
- uni.showToast({
- title: '请输入验证码',
- icon: 'none'
- })
- }
- },
- // wx,密码登录
- async wxPassLogin(e) {
- const that = this;
- if (e.password) {
- const res = await that.$api(`/user/login`, 'POST', e);
- if (res.errcode == '0') {
- uni.setStorage({
- key: 'token',
- data: res.data,
- success: function() {
- uni.navigateBack({
- delta: 1
- })
- }
- });
- } else {
- if (res.errcode == '-5') {
- uni.showModal({
- title: '提示',
- content: '用户不存在,是否注册新账号?',
- success: function(res) {
- if (res.confirm) {
- // 无账号,注册账号
- that.wxCreate();
- }
- }
- });
- } else {
- uni.showToast({
- title: res.errmsg || '信息错误',
- icon: 'none'
- })
- }
- }
- } else {
- uni.showToast({
- title: '请输入登录密码',
- icon: 'none'
- })
- }
- },
- // 其他登录方式
- async otherLogin(e) {
- const that = this;
- let system = that.system;
- let agree = that.agree;
- if (agree) {
- if (system.uniPlatform == 'app') {
- uni.showToast({
- title: '暂无开通',
- icon: 'none'
- })
- // todo:app端,微信一键登录
- } else if (system.uniPlatform == 'mp-weixin') {
- if (e == 'wx') {
- const res = await that.$api(`/user/wxLogin`, 'POST', {
- openid: that.openid
- })
- if (res.errcode == '0') {
- uni.setStorage({
- key: 'token',
- data: res.data,
- success: function() {
- uni.navigateBack({
- delta: 1
- })
- }
- });
- } else {
- if (res.errcode == '-5') {
- uni.showModal({
- title: '提示',
- content: '用户不存在,是否注册新账号?',
- success: function(res) {
- if (res.confirm) {
- // 无账号,注册账号
- that.wxCreate();
- }
- }
- });
- } else {
- uni.showToast({
- title: res.errmsg || '信息错误',
- icon: 'none'
- })
- }
- }
- }
- }
- } else {
- uni.showToast({
- title: '请阅读并同意用户协议和隐私政策',
- icon: 'none'
- })
- }
- },
- // wx,注册账号
- wxCreate() {
- const that = this;
- uni.getUserProfile({
- desc: '用于展示',
- success: async function(res) {
- let parmas = {
- openid: that.openid,
- icon: [{
- url: res.userInfo.avatarUrl
- }],
- name: res.userInfo.nickName,
- }
- console.log(parmas);
- const arr = await that.$api(`/user`, 'POST', parmas);
- if (arr.errcode == '0') {
- that.$set(that, `user`, arr.data);
- // 打开弹框
- that.diaShow();
- } else {
- uni.showToast({
- title: arr.errmsg,
- icon: 'none'
- });
- }
- },
- fail: function(err) {
- console.log(err);
- }
- })
- },
- // 打开弹框,绑定手机号
- diaShow() {
- const that = this;
- that.$refs.dialogShow.open();
- },
- async getUserPhone(e) {
- const that = this;
- let user = that.user;
- let detail = e.detail;
- let res = await that.$api(`/wechat/api/login/getPhone`, 'GET', {
- config: that.$config.wx_projectkey,
- code: detail.code
- })
- if (res.errcode == '0') {
- let phone = res.data && res.data.phone_info && res.data.phone_info.purePhoneNumber || '';
- that.updatePhone(phone, 'wx')
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- },
- // 弹框关闭
- diaClose() {
- const that = this;
- that.$refs.dialogShow.close();
- that.otherLogin('wx');
- },
- async twoSms() {
- const that = this;
- let user = this.user;
- let form = that.phoneForm;
- if (form && form.phone) {
- let res = await that.$api(`/user/toBindPhone`, 'POST', {
- id: user._id,
- phone: form.phone
- })
- if (res.errcode == '0') {
- uni.showToast({
- title: '发送成功',
- icon: 'none'
- })
- that.$set(that, `sms_1`, 60);
- that.timeDown();
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- } else {
- uni.showToast({
- title: '请输入手机号!',
- icon: 'none'
- })
- }
- },
- // 弹框确定
- async diaSubmit(e) {
- const that = this;
- let user = this.user;
- let form = that.phoneForm;
- if (e.type == '1') {
- if (form.type == 0) {
- if (form && form.phone) that.updatePhone(form.phone, 'wx');
- } else if (form.type == 1) {
- const res = await that.$api(`/user/checkBindPhone`, 'POST', {
- id: user._id,
- code: form.code,
- phone: form.phone
- });
- if (res.errcode == '0') {
- that.updatePhone(form.phone, 'wx');
- } else {
- uni.showToast({
- title: arr.errmsg,
- icon: 'none'
- })
- }
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- }
- },
- // 弹框取消(关闭)
- diaReset(e) {
- const that = this;
- if (e.type == '1') {
- that.otherLogin('wx');
- }
- },
- async updatePhone(e, type) {
- const that = this;
- let user = that.user;
- let arr = await that.$api(`/user/${user._id}`, 'POST', {
- phone: e
- })
- if (arr.errcode == '0') {
- that.otherLogin(type);
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- },
- // 倒计时
- timeDown() {
- const that = this;
- var times = setInterval(() => {
- that.sms_1--;
- if (that.sms_1 <= 0) {
- clearInterval(times);
- }
- }, 1000);
- },
- // 同意隐私协议
- changeAgree() {
- const that = this;
- let agree = true;
- if (that.agree) agree = false;
- that.$set(that, `agree`, agree);
- },
- // 查看隐私协议
- toAgree() {
- const that = this;
- uni.navigateTo({
- url: `/pages/other/agree`
- })
- },
- },
- }
- </script>
- <style lang="scss">
- .main {
- display: flex;
- flex-direction: column;
- width: 100vw;
- height: 100vh;
- .one {
- position: relative;
- flex-grow: 0.01;
- padding: 0 5vw;
- margin: 10vw 0;
- text {
- font-size: 30px;
- font-family: cursive;
- font-weight: bold;
- }
- }
- .two {
- position: relative;
- flex-grow: 0.3;
- padding: 0 5vw;
- .type {
- text-align: center;
- margin: 0 0 2vw 0;
- }
- .agree {
- text-align: center;
- font-size: 12px;
- margin: 0 0 2vw 0;
- }
- .btn {
- text-align: center;
- button {
- width: 80%;
- border-radius: 25px;
- font-size: 16px;
- color: #fff;
- background-color: green;
- }
- }
- }
- .thr {
- padding: 0 2vw;
- text-align: center;
- .thr_1 {
- font-size: 14px;
- margin: 0 0 2vw 0;
- color: #858585;
- }
- .thr_2 {
- display: flex;
- justify-content: center;
- .thr_2_1 {
- margin: 0 2vw;
- text {
- font-size: 40px;
- }
- }
- }
- }
- .wx {
- text-align: center;
- margin: 25vw 0 0 0;
- .wx_1 {
- margin: 0 0 5vw 0;
- text {
- font-size: 50px;
- }
- }
- .wx_2 {
- button {
- background: var(--f35BColor);
- color: var(--fffColor);
- font-size: var(--font16Size);
- }
- }
- .wx_3 {
- position: absolute;
- bottom: 10vw;
- width: 100vw;
- text-align: center;
- font-size: 12px;
- }
- }
- }
- .yzm {
- display: flex;
- flex-direction: row;
- justify-content: space-between;
- .l {
- flex-grow: 1;
- }
- .r {
- width: 18vw;
- button {
- width: 100%;
- line-height: 2.8;
- padding: 0 1vw;
- }
- }
- }
- .dialog {
- width: 100vw;
- }
- .dialog_1 {
- .uni-forms {
- display: flex;
- form {
- width: 100%;
- }
- }
- .uni-data-checklist {
- border: 1px solid #E5E5E5;
- padding: 1vw;
- border-radius: 5px;
- }
- .uni-forms-item {
- margin-bottom: 2vw !important;
- }
- }
- .uni-popup__wrapper {
- border-radius: 5px;
- }
- .popup {
- width: 86vw;
- padding: 2vw;
- .title {
- text-align: center;
- margin: 0 0 2vw 0;
- text {
- color: var(--ff0Color);
- font-size: var(--font16Size);
- }
- }
- .wx_1 {
- text-align: center;
- margin: 9vw 0;
- text {
- font-size: var(--font18Size);
- }
- }
- .wx_2 {
- text-align: center;
- button {
- margin: 0 2vw;
- font-size: var(--font16Size);
- padding: 0 10vw;
- color: var(--fffColor);
- background-color: var(--f35BColor);
- }
- button:nth-child(1) {
- background-color: var(--ff0Color);
- }
- }
- }
- </style>
|