123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <template>
- <mobile-frame>
- <view class="main">
- <view class="two">
- <uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
- <uni-forms-item label="账号" name="phone">
- <uni-easyinput type="text" v-model="form.phone" placeholder="请输入账号" />
- </uni-forms-item>
- <uni-forms-item label="密码" name="password">
- <uni-easyinput type="password" v-model="form.password" placeholder="请输入密码" />
- </uni-forms-item>
- <view class="btn">
- <button type="default" size="mini" @click="toSubmit('form')">登录</button>
- <button type="default" size="mini" @click="wxLogin()">微信信任登录</button>
- </view>
- </uni-forms>
- </view>
- </view>
- </mobile-frame>
- </template>
- <script>
- export default {
- data() {
- return {
- // 平台信息
- platform: {},
- openid: '',
- logoUrl: this.$config.logoUrl,
- form: {},
- rules: {
- phone: {
- rules: [{
- required: true,
- errorMessage: '请输入账号',
- },
- {
- minLength: 11,
- maxLength: 11,
- errorMessage: '账号长度在{maxLength}个字符',
- }
- ]
- },
- password: {
- rules: [{
- required: true,
- errorMessage: '请输入密码',
- }, ]
- },
- }
- };
- },
- onShow: function() {
- const that = this;
- that.search();
- },
- methods: {
- search() {
- const that = this;
- uni.getStorage({
- key: 'system',
- success: function(res) {
- if (res.data) that.$set(that, `platform`, res.data);
- // 获取openid
- that.searchOpenid()
- }
- })
- },
- // 查询openid
- searchOpenid() {
- const that = this;
- if (that.platform.uniPlatform == 'mp-weixin') {
- uni.login({
- provider: 'weixin',
- success: async function(res) {
- const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
- js_code: res.code,
- config: 'pointApp'
- })
- if (aee.errcode == '0') {
- that.$set(that, `openid`, aee.data.openid)
- }
- },
- fail: function(err) {
- console.log(err);
- }
- })
- } else if (that.platform.uniPlatform == 'app') {
- console.log('to do');
- }
- },
- // 账号登录
- toSubmit(ref) {
- const that = this;
- that.$refs[ref].validate().then(async params => {
- const res = await that.$api(`/user/login`, 'POST', {
- ...params
- })
- if (res.errcode == '0') {
- uni.showToast({
- title: '登录成功',
- icon: 'success'
- });
- uni.setStorage({
- key: 'token',
- data: res.data,
- success: function() {
- uni.navigateBack({
- delta: 1
- })
- }
- });
- } else {
- if (res.errcode == '-5') {
- // 账号无,注册账号
- that.createUser(params);
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- });
- }
- }
- }).catch(err => {
- console.log('表单错误信息:', err);
- })
- },
- // 微信信任登录
- wxLogin() {
- const that = this;
- if (that.platform.uniPlatform == 'mp-weixin') {
- that.userLogin({
- openid: that.openid
- }, '2');
- } else if (that.platform.uniPlatform == 'app') {
- console.log('app');
- }
- },
- // 账号登录,无账号,注册账号
- async createUser(params) {
- const that = this;
- const openid = that.openid;
- if (that.platform.uniPlatform == 'mp-weixin') params.openid = openid;
- const res = await that.$api(`/user`, 'POST', {
- ...params
- })
- if (res.errcode == '0') {
- that.userLogin(params, '1')
- } else {
- uni.showToast({
- title: res.errmsg,
- });
- }
- },
- // 微信登录,无账号,微信注册账号
- async createwxUser() {
- const that = this;
- const res = await that.$api(`/user`, 'POST', {
- openid: that.openid
- })
- if (res.errcode == '0') {
- that.userLogin({
- openid: that.openid
- }, '2')
- } else {
- uni.showToast({
- title: res.errmsg,
- });
- }
- },
- // 用户登录
- async userLogin(params, type) {
- const that = this;
- let res;
- if (type == '1') {
- res = await that.$api(`/user/login`, 'POST', {
- ...params
- })
- } else if (type == '2') {
- res = await that.$api(`/user/wxLogin`, 'POST', {
- openid: params.openid
- })
- }
- if (res.errcode == '0') {
- uni.showToast({
- title: '登录成功',
- icon: 'success'
- });
- uni.setStorage({
- key: 'token',
- data: res.data,
- success: function() {
- uni.navigateBack({
- delta: 1
- })
- }
- });
- } else {
- if (res.errcode == '-5') {
- // 账号无,注册账号
- that.createwxUser(params);
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- });
- }
- }
- },
- }
- }
- </script>
- <style lang="scss">
- .main {
- .two {
- padding: 2vw;
- .btn {
- text-align: center;
- margin: 0 0 2vw 0;
- button {
- margin: 0 2vw;
- background-color: var(--f35BColor);
- color: var(--fffColor);
- }
- }
- }
- }
- </style>
|