123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <template>
- <view>
- <image :src="src" class="img"></image>
- <view class="btn">
- <view style="width: 80%;">
- <u-button v-if="canIUseGetUserProfile" shape="circle"
- color="linear-gradient(to right, #F54319, #FF6D20)" @click="getUserProfile">选择餐车</u-button>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'MyLogin',
- data() {
- return {
- src: '../../static/first.png',
- canIUseGetUserProfile: false,
- }
- },
- onLoad(e) {
- // 检查微信版本是否支持 getUserProfile
- if (wx.getUserProfile) {
- this.canIUseGetUserProfile = true;
- }
- // uni.setStorageSync('dinnerId', '1806258588536201217')
- // if(e.id){
- // uni.setStorageSync('dinnerId', e.id)
- // }
-
- let infoData = uni.getStorageSync('infoData');
- if(infoData){
- try{
- infoData = JSON.parse(infoData)
- // console.log('用户数据',infoData)
- if (infoData.expire_in && Date.now() / 1000 - infoData.time > infoData.expire_in) {
- uni.removeStorageSync('infoData')
- return null
- }
-
- if(infoData && infoData.access_token){
- uni.switchTab({
- url:'/pages/home/home'
- })
- }
- }catch(e){}
- }
- },
- methods: {
- getUserProfile() {
- wx.showModal({
- title: '温馨提示',
- content: '亲,授权微信登录后才能正常使用小程序功能',
- })
- let that = this;
- wx.getUserProfile({
- desc: '获取用户信息',
- success: (res) => {
- console.log('res', res);
- // 调用微信登录接口
- wx.login({
- success: async (loginRes) => {
- console.log('数据:', loginRes)
- // 发送code到开发者服务器换取用户信息
- const result = await this.$request('post', '/system/appUser/miniLogin?tenantId=000000&code='+loginRes.code)
- console.log('登录成功:', result);
- if (result && result.data !== false) {
- // 保存token
- uni.setStorageSync("token", result.data.access_token);
- uni.setStorageSync("openId", result.data.open_id);
- //uni.setStorageSync("tenantId", result.data.tenantId);
- uni.setStorageSync("appUserId", result.data.app_user_id);
-
- let d = result.data;
- d.time = Date.now() / 1000;
- uni.setStorageSync('infoData', JSON.stringify(d))
- that.initdining()
- } else {
- // 处理错误情况
- }
- // 更新用户信息
- const id = uni.getStorageSync('appUserId');
- // console.log('id', id)
- uni.setStorageSync("userName", res.userInfo.nickName);
- const userInfo = {
- "userId": id,
- "nickName": res.userInfo.nickName,
- "profilePhoto": res.userInfo.avatarUrl
- }
- console.log('userInfo', userInfo)
- that.$request('post', '/system/user/editSysUser',
- userInfo, true)
- },
- fail: (error) => {
- console.error('登录失败:', error);
- }
- })
- },
- fail: (error) => {
- console.error('获取用户信息失败:', error);
- }
- })
- },
- //获取餐车信息
- async initdining() {
- const response = await this.$request('post', '/sale/diningCar/queryDiningCar', {
- "isAsc": "desc",
- "orderByColumn": "createTime"});
- if(response.code == 200) {
- let current;
- if(response.rows.length > 0){
- for(const item of response.rows){
- if(item.defaultState == '2'){
- current = item;
- break
- }
- }
- }
- if(current) {
- const carl = {
- id: current.id,
- customerPhone: current.customerPhone,
- name: current.name,
- openState: current.openState
- }
- uni.setStorageSync("carl", carl)
- // 跳转到下一个页面
- uni.switchTab({
- url:'/pages/home/home'
- })
- }else{
- uni.navigateTo({
- url: `/pages/diningList/diningList`
- });
- }
- }else{
- wx.showModal({
- title: '温馨提示',
- content: '系统异常!',
- })
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .img {
- height: 375px;
- width: 100%;
- }
- .btn {
- display: flex;
- justify-content: center;
- align-items: center;
- margin-top: 20px;
- }
- </style>
|