From 689843d7b7c74d248aef0a7694a198b2fb7744a1 Mon Sep 17 00:00:00 2001 From: lwh <2679599887@qq.com> Date: Sun, 4 Jun 2023 22:06:52 +0800 Subject: [PATCH] init --- .husky/commit-msg | 4 -- .husky/pre-commit | 4 -- .husky/prepare-commit-msg | 4 -- .vscode/settings.json | 11 +-- app.json | 4 ++ pages/home/home.js | 24 +++++-- pages/login/login.js | 141 +++++++++++++++++++++++++++++++++++++ pages/login/login.json | 4 ++ pages/login/login.wxml | 8 +++ pages/login/login.wxss | 44 ++++++++++++ services/_utils/request.js | 65 +++++++++++++++++ services/home/home.js | 6 +- services/login/login.js | 1 + utils/storage.js | 58 +++++++++++++++ 14 files changed, 352 insertions(+), 26 deletions(-) delete mode 100644 .husky/commit-msg delete mode 100644 .husky/pre-commit delete mode 100644 .husky/prepare-commit-msg create mode 100644 pages/login/login.js create mode 100644 pages/login/login.json create mode 100644 pages/login/login.wxml create mode 100644 pages/login/login.wxss create mode 100644 services/_utils/request.js create mode 100644 services/login/login.js create mode 100644 utils/storage.js diff --git a/.husky/commit-msg b/.husky/commit-msg deleted file mode 100644 index a8650bc..0000000 --- a/.husky/commit-msg +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - -npx --no-install commitlint --edit "" diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100644 index 36af219..0000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -npx lint-staged diff --git a/.husky/prepare-commit-msg b/.husky/prepare-commit-msg deleted file mode 100644 index 71fe5b3..0000000 --- a/.husky/prepare-commit-msg +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -exec < /dev/tty && npx git-cz --hook || true \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 4c9c889..3e631c8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,14 +1,14 @@ { "editor.defaultFormatter": "esbenp.prettier-vscode", "eslint.enable": true, - "editor.formatOnSave": true, + "editor.formatOnSave": false, "editor.codeActionsOnSave": { "source.fixAll.tslint": true, "source.fixAll.eslint": true }, "[javascript]": { - "editor.formatOnSave": true, - "editor.defaultFormatter": "esbenp.prettier-vscode" + "editor.formatOnSave": false, + "editor.defaultFormatter": "HookyQR.beautify" }, "[json]": { "editor.defaultFormatter": "vscode.json-language-features" @@ -30,5 +30,8 @@ "wrap_attributes_count": 4, "wrap_attributes_indent_size": 2 }, - "editor.tabSize": 2 + "editor.tabSize": 2, + "[wxml]": { + "editor.defaultFormatter": "wechat.miniprogram.wxml-language-features" + } } diff --git a/app.json b/app.json index b55e23c..bb07cc2 100644 --- a/app.json +++ b/app.json @@ -1,6 +1,7 @@ { "pages": [ "pages/home/home", + "pages/login/login", "pages/usercenter/index", "pages/usercenter/person-info/index", "pages/usercenter/address/list/index", @@ -69,5 +70,8 @@ "scope.userLocation": { "desc": "你的位置信息将用于小程序位置接口的效果展示" } + }, + "resolveAlias": { + "~/*": "/*" } } diff --git a/pages/home/home.js b/pages/home/home.js index 903558f..0937265 100644 --- a/pages/home/home.js +++ b/pages/home/home.js @@ -1,5 +1,5 @@ -import { fetchHome } from '../../services/home/home'; -import { fetchGoodsList } from '../../services/good/fetchGoods'; +import { fetchHome } from '~/services/home/home'; +import { fetchGoodsList } from '~/services/good/fetchGoods'; import Toast from 'tdesign-miniprogram/toast/index'; Page({ @@ -13,8 +13,12 @@ Page({ autoplay: true, duration: '500', interval: 5000, - navigation: { type: 'dots' }, - swiperImageProps: { mode: 'scaleToFill' }, + navigation: { + type: 'dots', + }, + swiperImageProps: { + mode: 'scaleToFill', + }, }, goodListPagination: { @@ -80,7 +84,9 @@ Page({ }); } - this.setData({ goodsListLoadStatus: 1 }); + this.setData({ + goodsListLoadStatus: 1, + }); const pageSize = this.goodListPagination.num; let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1; @@ -98,7 +104,9 @@ Page({ this.goodListPagination.index = pageIndex; this.goodListPagination.num = pageSize; } catch (err) { - this.setData({ goodsListLoadStatus: 3 }); + this.setData({ + goodsListLoadStatus: 3, + }); } }, @@ -119,7 +127,9 @@ Page({ }, navToSearchPage() { - wx.navigateTo({ url: '/pages/goods/search/index' }); + wx.navigateTo({ + url: '/pages/goods/search/index', + }); }, navToActivityDetail({ detail }) { diff --git a/pages/login/login.js b/pages/login/login.js new file mode 100644 index 0000000..458784e --- /dev/null +++ b/pages/login/login.js @@ -0,0 +1,141 @@ +// pages/login/login.js +const app = getApp(); + +Page({ + /** + * 页面的初始数据 + */ + data: { + back: true, + code: '', + }, + login() { + var that = this; + wx.getUserProfile({ + //获取用户信息。页面产生点击事件(例如 button 上 bindtap 的回调中)后才可调用,每次请求都会弹出授权窗口,用户同意后返回 userInfo,用于替换 wx.getUserInfo + lang: 'zh_CN', + desc: '用于在后台更好的识别您的身份', //声明获取用户个人信息后的用途 + success(user) { + //接口调用成功的回调函数 + + const data = { + encryptedData: user.encryptedData, + iv: user.iv, + // signature: user.signature, + // rawData: user.rawData, + code: that.data.code, + }; + // console.log(data); + + app.request({ + url: `login`, + method: 'POST', + data: data, + success: async function (res) { + // wx.setStorageSync('userInfo', res); + app.put('userInfo', res, 86400); + const userInfo = app.get('userInfo'); + + // 如果第一次登录就注册一个客户账号 + app.request({ + url: `login/addCustomer`, + method: 'POST', + data: { + CustomerId: 0, + CustomerGuid: 0, + CustomerXcxOpenid: userInfo.openId, + CustomerXcxName: userInfo.nickName, + CustomerXcxImg: userInfo.avatarUrl, + CustomerSex: userInfo.gender, + }, + success(res) { + app.put('Authorization', res.data, 86400); + + const pages = getCurrentPages(); //获取小程序页面栈 + const beforePage = pages[pages.length - 2]; //获取上个页面的实例对象 -3是上上一个页面 + that.setData({ + back: false, + }); + wx.navigateBack({ + delta: 2, + }); + beforePage.onLoad(); + // console.log(wx.getStorageSync('userInfo')); + wx.showToast({ + icon: 'success', + title: '登录成功', + }); + }, + }); + }, + }); + }, + fail() { + wx.switchTab({ + url: '/pages/home/home', + }); + }, + }); + }, + /** + * 生命周期函数--监听页面加载 + */ + onLoad() { + var that = this; + + wx.login({ + success(res) { + that.setData({ + code: res.code, + }); + }, + }); + }, + + /** + * 生命周期函数--监听页面初次渲染完成 + */ + onReady() {}, + + /** + * 生命周期函数--监听页面显示 + */ + onShow() {}, + + /** + * 生命周期函数--监听页面隐藏 + */ + onHide() {}, + + /** + * 生命周期函数--监听页面卸载 + */ + onUnload() { + var that = this; + //设置缓存允许下次跳转登录页 + wx.setStorage({ + key: 'isshowLogin', + data: false, + }); + if (that.data.back === true) { + wx.reLaunch({ + url: '/pages/home/home', + }); + } + }, + + /** + * 页面相关事件处理函数--监听用户下拉动作 + */ + onPullDownRefresh() {}, + + /** + * 页面上拉触底事件的处理函数 + */ + onReachBottom() {}, + + /** + * 用户点击右上角分享 + */ + onShareAppMessage() {}, +}); diff --git a/pages/login/login.json b/pages/login/login.json new file mode 100644 index 0000000..c6b900b --- /dev/null +++ b/pages/login/login.json @@ -0,0 +1,4 @@ +{ + "usingComponents": {}, + "navigationBarTitleText": "授权登录" +} diff --git a/pages/login/login.wxml b/pages/login/login.wxml new file mode 100644 index 0000000..e2b1368 --- /dev/null +++ b/pages/login/login.wxml @@ -0,0 +1,8 @@ + + + + 授权登录 + 快转铺请求授权以下消息 + 获取您的公开信息(名称、头像等) + 授权名称/头像 + diff --git a/pages/login/login.wxss b/pages/login/login.wxss new file mode 100644 index 0000000..6f21075 --- /dev/null +++ b/pages/login/login.wxss @@ -0,0 +1,44 @@ +/* pages/login/login.wxss */ +page{ + background-color: black; +} +.loginBox{ + width: 75%; + display: flex; + flex-wrap: wrap; + margin: 0 auto; + padding: 30rpx; + margin-top: 50%; + background-color: white; +} +.loginBox .title{ + width: 100%; + text-align: center; + font-size: 38rpx; + font-weight: 600; + margin-bottom: 5rpx; +} + +.loginBox .NeedLabel{ + display: flex; + align-items: center; + color: rgb(190, 190, 190); + font-size: 30rpx; + margin: 15rpx 0; +} +.NeedLabel view{ + width: 15rpx; + height: 15rpx; + border-radius: 50%; + margin-right: 15rpx; + background-color: rgb(190, 190, 190); +} +.loginBox .button{ + width: 100%; + text-align: center; + padding: 25rpx 0; + border-radius: 15rpx; + color: white; + font-size: 36rpx; + background-color: rgb(30, 153, 30); +} \ No newline at end of file diff --git a/services/_utils/request.js b/services/_utils/request.js new file mode 100644 index 0000000..f8485b0 --- /dev/null +++ b/services/_utils/request.js @@ -0,0 +1,65 @@ +const ServerBasePath = 'https://localhost:8888/api/'; + +// 网络请求封装 +export function request(option) { + const that = this; + const { url } = option; + const { data } = option; + const { fail } = option; + const { success } = option; + const { method } = option; + + wx.showLoading({ + title: '数据加载中', + mask: true, + }); + wx.request( + (option, + { + header: { + Authorization: `Bearer ${that.get('Authorization')}`, + }, + timeout: 30000, //设置超时时间 + url: ServerBasePath + url, + data: data, + method: method, + success: function (res) { + // var pages = getCurrentPages(); // 获取页面指针数组 + // var currentPage = pages[pages.length - 2]; // 获取当前页 + if (res.data.Code === 401) { + const userInfo = that.get('userInfo'); + //初次跳转登录页 + if (!userInfo) { + wx.navigateTo({ + url: '/pages/login/login', + }); + return; + } + } + + if (typeof success === 'function') { + if (res.statusCode === 200) { + success(res.data, res); + wx.hideLoading(); + } else { + wx.showToast({ + icon: 'error', + title: '网络异常,请重试', + }); + wx.hideLoading(); + } + } + }, + fail: function (res) { + if (typeof fail === 'function') { + fail(res); + } + wx.showToast({ + icon: 'error', + title: '网络异常,请重试', + }); + console.log(`请求:【${url}】fail`, res); + }, + }), + ); +} diff --git a/services/home/home.js b/services/home/home.js index 7d4469d..9aef66f 100644 --- a/services/home/home.js +++ b/services/home/home.js @@ -1,9 +1,9 @@ -import { config, cdnBase } from '../../config/index'; +import { config, cdnBase } from '~/config/index'; /** 获取首页数据 */ function mockFetchHome() { - const { delay } = require('../_utils/delay'); - const { genSwiperImageList } = require('../../model/swiper'); + const { delay } = require('~/services/_utils/delay'); + const { genSwiperImageList } = require('~/model/swiper'); return delay().then(() => { return { swiper: genSwiperImageList(), diff --git a/services/login/login.js b/services/login/login.js new file mode 100644 index 0000000..0b36685 --- /dev/null +++ b/services/login/login.js @@ -0,0 +1 @@ +// import { request } from '../_utils/request'; diff --git a/utils/storage.js b/utils/storage.js new file mode 100644 index 0000000..9c3155a --- /dev/null +++ b/utils/storage.js @@ -0,0 +1,58 @@ +/** + * 设置存储 + * k 键key + * v 值value + * t 秒 + */ +function setStorage(k, v, t) { + wx.setStorageSync(k, v); + var seconds = parseInt(t); + if (seconds > 0) { + var newtime = Date.parse(new Date()); + newtime = newtime / 1000 + seconds; + wx.setStorageSync(k + redis, `${newtime}`); + } else { + wx.removeStorageSync(k + redis); + } +} + +/** + * 获取存储 + * k 键key + */ +function getStorage(k) { + var deadtime = parseInt(wx.getStorageSync(k + redis)); + if (deadtime) { + if (parseInt(deadtime) < Date.parse(new Date()) / 1000) { + wx.removeStorageSync(k); + return null; + } + } + var res = wx.getStorageSync(k); + if (res) { + return res; + } + return null; +} + +/** + * 删除存储 + */ +function removeStorage(k) { + wx.removeStorageSync(k); + wx.removeStorageSync(k + redis); +} + +/** + * 清除所有存储 + */ +function clear() { + wx.clearStorageSync(); +} + +module.exports = { + setStorage, + getStorage, + removeStorage, + clear, +};