增加微信小程序的登陆的测试代码

This commit is contained in:
YunaiV 2021-10-30 10:09:31 +08:00
parent 038c0b87b9
commit 8dbd6143bf
26 changed files with 381 additions and 20 deletions

31
mini-program-test/.eslintrc.js Executable file
View File

@ -0,0 +1,31 @@
/*
* Eslint config file
* Documentation: https://eslint.org/docs/user-guide/configuring/
* Install the Eslint extension before using this feature.
*/
module.exports = {
env: {
es6: true,
browser: true,
node: true,
},
ecmaFeatures: {
modules: true,
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
globals: {
wx: true,
App: true,
Page: true,
getCurrentPages: true,
getApp: true,
Component: true,
requirePlugin: true,
requireMiniProgram: true,
},
// extends: 'eslint:recommended',
rules: {},
}

View File

@ -0,0 +1 @@
临时项目,作为测试微信小程序登陆之用

19
mini-program-test/app.js Executable file
View File

@ -0,0 +1,19 @@
// app.js
App({
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
},
globalData: {
userInfo: null
}
})

14
mini-program-test/app.json Executable file
View File

@ -0,0 +1,14 @@
{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin",
"navigationBarTextStyle":"black"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}

10
mini-program-test/app.wxss Executable file
View File

@ -0,0 +1,10 @@
/**app.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}

View File

@ -0,0 +1,91 @@
// index.js
const common=require('../../utils/common.js')
// 获取应用实例
const app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
canIUseGetUserProfile: false,
canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
holderText: 'to be auth'
},
// 事件处理函数
bindViewTap() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息开发者每次通过该接口获取用户个人信息均需用户确认开发者妥善保管用户快速填写的头像昵称避免重复弹窗
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
console.log(res)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
},
getUserInfo(e) {
// 不推荐使用getUserInfo获取用户信息预计自2021年4月13日起getUserInfo将不再弹出弹窗并直接返回匿名的用户个人信息
console.log(e)
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
// 小程序登录 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
wxLogin(e){
let page=this;
wx.login({
success (res) {
console.log("res:")
console.log(res)
if (res.code) {
//发起网络请求
console.log('发起网络请求'+common.baseurl)
wx.request({
url: common.baseurl+'/api/social-login2',
method: "POST",
data: {
code: res.code,
state: 'empty',
type: 33,
username: '15601691300',
password: 'admin123'
},
header: {
'content-type': 'application/json' // 默认值
},
success: function(res) {
console.log(res.data)
let holder="auth success, token:"+res.data.data.token
page.setData({holderText: holder})
},
fail: function(data){
console.error("请求出错");
console.error(data)
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
}
})

View File

@ -0,0 +1,3 @@
{
"usingComponents": {}
}

View File

@ -0,0 +1,29 @@
<!--index.wxml-->
<view class="container">
<view class="userinfo">
<block wx:if="{{canIUseOpenData}}">
</block>
<block wx:elif="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
<button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
<view wx:else> 请使用1.4.4及以上版本基础库 </view>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
</view>
<text style="position: relative; left: 1rpx; top: -476rpx">授权登录测试1024</text>
<button style="position: relative; left: 0rpx; top: -361rpx" type="primary" id="login-button" bindtap="wxLogin">点击授权登录</button>
<text style="position: relative; left: 1rpx; top: -272rpx" id="login-user-id">{{holderText}}</text>
</view>

View File

@ -0,0 +1,19 @@
/**index.wxss**/
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
color: #aaa;
}
.userinfo-avatar {
overflow: hidden;
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}
.usermotto {
margin-top: 200px;
}

View File

@ -0,0 +1,18 @@
// logs.js
const util = require('../../utils/util.js')
Page({
data: {
logs: []
},
onLoad() {
this.setData({
logs: (wx.getStorageSync('logs') || []).map(log => {
return {
date: util.formatTime(new Date(log)),
timeStamp: log
}
})
})
}
})

View File

@ -0,0 +1,4 @@
{
"navigationBarTitleText": "查看启动日志",
"usingComponents": {}
}

View File

@ -0,0 +1,6 @@
<!--logs.wxml-->
<view class="container log-list">
<block wx:for="{{logs}}" wx:key="timeStamp" wx:for-item="log">
<text class="log-item">{{index + 1}}. {{log.date}}</text>
</block>
</view>

View File

@ -0,0 +1,8 @@
.log-list {
display: flex;
flex-direction: column;
padding: 40rpx;
}
.log-item {
margin: 10rpx;
}

View File

@ -0,0 +1,75 @@
{
"description": "项目配置文件",
"packOptions": {
"ignore": [
{
"type": "file",
"value": ".eslintrc.js"
}
]
},
"setting": {
"bundle": false,
"userConfirmedBundleSwitch": false,
"urlCheck": true,
"scopeDataCheck": false,
"coverView": true,
"es6": true,
"postcss": true,
"compileHotReLoad": false,
"lazyloadPlaceholderEnable": false,
"preloadBackgroundData": false,
"minified": true,
"autoAudits": false,
"newFeature": false,
"uglifyFileName": false,
"uploadWithSourceMap": true,
"useIsolateContext": true,
"nodeModules": false,
"enhance": true,
"useMultiFrameRuntime": true,
"useApiHook": true,
"useApiHostProcess": true,
"showShadowRootInWxmlPanel": true,
"packNpmManually": false,
"enableEngineNative": false,
"packNpmRelationList": [],
"minifyWXSS": true,
"showES6CompileOption": false,
"minifyWXML": true
},
"compileType": "miniprogram",
"libVersion": "2.19.4",
"appid": "wx44d047d87e6284d8",
"appid1": "wx63c280fe3248a3e7",
"projectname": "mini-program-test",
"debugOptions": {
"hidedInDevtools": []
},
"scripts": {},
"staticServerOptions": {
"baseURL": "",
"servePath": ""
},
"isGameTourist": false,
"condition": {
"search": {
"list": []
},
"conversation": {
"list": []
},
"game": {
"list": []
},
"plugin": {
"list": []
},
"gamePlugin": {
"list": []
},
"miniprogram": {
"list": []
}
}
}

7
mini-program-test/sitemap.json Executable file
View File

@ -0,0 +1,7 @@
{
"desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html",
"rules": [{
"action": "allow",
"page": "*"
}]
}

View File

@ -0,0 +1,3 @@
module.exports = {
baseurl: "http://127.0.0.1:28080"
}

19
mini-program-test/utils/util.js Executable file
View File

@ -0,0 +1,19 @@
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
module.exports = {
formatTime
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth;
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

View File

@ -8,15 +8,16 @@ import cn.iocoder.yudao.adminserver.modules.system.convert.user.SysUserConvert;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysPostDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService;
import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysPostService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService;
import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import io.swagger.annotations.Api;
@ -77,7 +78,7 @@ public class SysUserProfileController {
resp.setPosts(SysUserConvert.INSTANCE.convertList02(posts));
}
// 获得社交用户信息
List<SysSocialUserDO> socialUsers = socialService.getSocialUserList(user.getId());
List<SysSocialUserDO> socialUsers = socialService.getSocialUserList(user.getId(), UserTypeEnum.ADMIN);
resp.setSocialUsers(SysUserConvert.INSTANCE.convertList03(socialUsers));
return success(resp);
}

View File

@ -7,7 +7,7 @@ import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.*;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysPostDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

View File

@ -7,11 +7,11 @@ import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEn
import cn.iocoder.yudao.adminserver.modules.system.service.auth.impl.SysAuthServiceImpl;
import cn.iocoder.yudao.adminserver.modules.system.service.common.SysCaptchaService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService;
import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.security.core.LoginUser;

View File

@ -1,11 +1,11 @@
package cn.iocoder.yudao.adminserver.modules.system.service.social;
import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper;
import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO;
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO;
import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserMapper;
import cn.iocoder.yudao.coreservice.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO;
import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum;
import cn.iocoder.yudao.coreservice.modules.system.service.social.impl.SysSocialServiceImpl;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import com.xkcoding.justauth.AuthRequestFactory;
import me.zhyd.oauth.model.AuthUser;
@ -23,6 +23,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO @timfruit这个单元测试挪到 yudao-core-service
/**
* {@link SysSocialServiceImpl} 的单元测试类
*
@ -53,7 +54,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
// mock 方法
// 调用
socialService.bindSocialUser(userId, type, authUser);
socialService.bindSocialUser(userId, type, authUser, UserTypeEnum.ADMIN);
// 断言
List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
assertEquals(1, socialUsers.size());
@ -78,7 +79,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
// mock 方法
// 调用
socialService.bindSocialUser(userId, type, authUser);
socialService.bindSocialUser(userId, type, authUser, UserTypeEnum.ADMIN);
// 断言
List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
assertEquals(1, socialUsers.size());
@ -103,7 +104,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
// mock 方法
// 调用
socialService.bindSocialUser(userId, type, authUser);
socialService.bindSocialUser(userId, type, authUser, UserTypeEnum.ADMIN);
// 断言
List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
assertEquals(1, socialUsers.size());
@ -140,7 +141,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
String newUnionId = oldSocialUser.getUnionId();
// 调用
socialService.unbindOldSocialUser(userId, type, newUnionId);
socialService.unbindOldSocialUser(userId, type, newUnionId, UserTypeEnum.ADMIN);
// 断言
assertEquals(1L, socialUserMapper.selectCount(null).longValue());
}
@ -163,7 +164,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
String newUnionId = randomString(10);
// 调用
socialService.unbindOldSocialUser(userId, type, newUnionId);
socialService.unbindOldSocialUser(userId, type, newUnionId, UserTypeEnum.ADMIN);
// 断言
assertEquals(0L, socialUserMapper.selectCount(null).longValue());
}

View File

@ -59,7 +59,7 @@ public interface SysSocialService {
* @param userId 用户编号
* @return 社交用户列表
*/
List<SysSocialUserDO> getSocialUserList(Long userId,UserTypeEnum userTypeEnum);
List<SysSocialUserDO> getSocialUserList(Long userId, UserTypeEnum userTypeEnum);
/**
* 绑定社交用户

View File

@ -87,7 +87,7 @@ public class SysSocialServiceImpl implements SysSocialService {
@Override
@Transactional
public void bindSocialUser(Long userId, Integer type, AuthUser authUser,UserTypeEnum userTypeEnum) {
public void bindSocialUser(Long userId, Integer type, AuthUser authUser, UserTypeEnum userTypeEnum) {
// 获得 unionId 对应的 SysSocialUserDO 列表
String unionId = getAuthUserUnionId(authUser);
List<SysSocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId, userTypeEnum);

View File

@ -156,7 +156,6 @@ yudao:
pay-notify-url: http://niubi.natapp1.cc/api/pay/order/notify
refund-notify-url: http://niubi.natapp1.cc/api/pay/refund/notify
justauth:
enabled: true
type:
@ -169,10 +168,12 @@ justauth:
extend:
enum-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthExtendSource
config:
WECHAT_MINI_PROGRAM: # 微信小程序 扩展
WECHAT_MINI_PROGRAM: # 微信小程序 https://www.yuque.com/docs/share/88e3d30a-6830-45fc-8c25-dae485aef3aa?#%20%E3%80%8A%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%8E%88%E6%9D%83%E7%99%BB%E5%BD%95%E3%80%8B
request-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthWeChatMiniProgramRequest
client-id: wx44d047d87e6284d8
client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
# client-id: wx63c280fe3248a3e7 # TODO 芋艿:自己的测试,后续可以删除
# client-secret: 6f270509224a7ae1296bbf1c8cb97aed
ignore-check-redirect-uri: true
ignore-check-state: true
cache:

View File

@ -15,6 +15,7 @@ TODO
* 支付
* 用户前台的社交登陆
* 用户前台的修改手机、修改密码、忘记密码
## [v1.1.0] 2021.10.25