绿满眶商城微信小程序-uniapp
xiangpei
2025-05-26 86cbf346204232ba9a4ffea6fbcb4d9d2b521916
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<template>
    <view class="container">
        <u-navbar :custom-back="back" title="小程序登录"></u-navbar>
        <u-modal v-model="phoneAuthPopup" :mask-close-able="true" :title="projectName+'商城'"
            :show-confirm-button="false">
            <div class="tips">
                为了更好地用户体验,需要您授权手机号
            </div>
            <button class="register" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
                去授权
            </button>
        </u-modal>
        <view class="wx-auth-container">
            <div class="box">
                <view class="logo-info">
                    <text class="title">欢迎进入{{ projectName }}</text>
                </view>
                <view class="small-tips">
                    <view>为您提供优质服务,{{ projectName }}需要获取以下信息</view>
                    <view>您的公开信息(昵称、头像)</view>
                </view>
                <view class="btns">
                    <button type="primary" :disabled="logingFlag" bindtap="getUserProfile" @click="getUserProfile()"
                        class="btn-auth">登录</button>
                    <div @click="backToHome" class="btn-callback">暂不登录</div>
                </view>
                <div class="privacy">
            <u-checkbox shape="circle" v-model="checked" :active-color="lightColor">
              <div class="flex">
                阅读并同意<navigator class="light-color" url="/pages/mine/help/tips?type=PRIVACY_POLICY">《隐私协议》</navigator>
                <navigator class="light-color" url="/pages/mine/help/tips?type=USER_AGREEMENT">《用户协议》</navigator>
              </div>
            </u-checkbox>
        </div>
            </div>
        </view>
    </view>
</template>
 
<script>
    import UCheckBox from '@/uview-components/uview-ui/components/u-checkbox/u-checkbox.vue';
    import UNavbar from '@/uview-components/uview-ui/components/u-navbar/u-navbar.vue';
    import UModel from '@/uview-components/uview-ui/components/u-modal/u-modal.vue';
    import {
        mpAutoLogin
    } from "@/api/connect.js";
 
    import {
        whetherNavigate
    } from "@/utils/Foundation"; //登录跳转
    import {
        getUserInfo
    } from "@/api/members";
    import storage from "@/utils/storage.js";
    import config from '@/config/config'
    export default {
        components: {UCheckBox, UModel,UNavbar},
        data() {
            return {
                lightColor:this.$lightColor,
                checked:false,
        configs:config,
                // 是否展示手机号码授权弹窗,默认第一步不展示,要先获取用户基础信息
                phoneAuthPopup: false,
                // 授权信息展示,商城名称
                projectName: config.name,
                //微信返回信息,用于揭秘信息,获取sessionkey
                code: "",
                //微信昵称
                nickName: "",
        logingFlag: false,
                //微信头像
                image: "",
            };
        },
 
        //微信小程序进入页面,先获取code,否则几率出现code和后续交互数据不对应情况
        mounted() {
            // 小程序默认分享
            uni.showShareMenu({
                withShareTicket: true
            });
 
 
      //获取code
      uni.login({
        success: (res) => {
          if(res.errMsg === "login:ok") {
            this.code = res.code
          } else {
            uni.showToast({
              title: "系统异常,请联系管理员!"
            })
          }
        },
      });
            
        },
        methods: {
            /**
             * TODO 此方法不一定是最优解,如果有更好的办法请在  https://gitee.com/beijing_hongye_huicheng/lilishop/issues 中提出
             * 小程序返回bug
             * 1.介于微信登录是在login.vue的基础上作为判断跳转来
             * 所以在页面栈中会自动记录回退路径,所以导致每次微信小程序点击回退就会自动返回login页面
             * 当然login页面的判断就是 没有登录就会跳转到微信小程序页面 导致了无法回退到之前页面
             * 2.解决方法: 尝试在回退的时候判断地址,让回退多一级这样就避免了
             */
 
            back() {
                whetherNavigate("wx");
            },
            backToHome() {
                uni.switchTab({
                    url: `/pages/tabbar/home/index`,
                });
            },
 
 
 
 
            //获取用户信息
      getUserProfile(e) {
                if(!this.checked){
                    uni.showToast({
                        title:"请勾选协议",
                        icon:'none'
                    })
                    return
                }
        this.logingFlag = true;
 
        if (this.code) {
          // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
          uni.getUserProfile({
            desc: "用于完善会员资料", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
            success: (res) => {
              console.log("success", res)
              this.nickName = res.userInfo.nickName;
              this.image = res.userInfo.avatarUrl;
 
              /**
               * 根据公有的配置设置登录方式
               */
              if(this.configs.enableFetchMobileLogin){
                this.phoneAuthPopup = true;
                return false
              }
              let iv = res.iv;
              let encryptedData = res.encryptedData;
 
              let code = this.code;
              let image = this.image;
              let nickName = this.nickName;
              mpAutoLogin({
                encryptedData,
                iv,
                code,
                image,
                nickName,
              }).then((apiRes) => {
                storage.setAccessToken(apiRes.data.result.accessToken);
                storage.setRefreshToken(apiRes.data.result.refreshToken);
                // 登录成功
                uni.showToast({
                  title: "登录成功!",
                  icon: "none",
                });
                //获取用户信息
                getUserInfo().then((user) => {
                  storage.setUserInfo(user.data.result);
                  storage.setHasLogin(true);
 
                  uni.navigateBack({
                    delta: 1,
                  });
                });
              });
 
            },
            fail: (res) => {
              console.log("fail", res)
            },
          });
 
          this.logingFlag = false;
        }
            },
 
            //获取手机号授权
            getPhoneNumber(e) {
              let iv = e.detail.iv;
              let encryptedData = e.detail.encryptedData;
              if (!e.detail.encryptedData) {
                uni.showToast({
                  title: "请授予手机号码权限,手机号码会和会员系统用户绑定!",
                  icon: "none",
                });
                return;
              }
 
              let code = this.code;
              let image = this.image;
              let nickName = this.nickName;
              mpAutoLogin({
                encryptedData,
                iv,
                code,
                image,
                nickName,
              }).then((res) => {
                storage.setAccessToken(res.data.result.accessToken);
                storage.setRefreshToken(res.data.result.refreshToken);
                // 登录成功
                uni.showToast({
                  title: "登录成功!",
                  icon: "none",
                });
                //获取用户信息
                getUserInfo().then((user) => {
                  storage.setUserInfo(user.data.result);
                  storage.setHasLogin(true);
 
                  uni.navigateBack({
                    delta: 1,
                  });
                });
              });
            },
 
 
 
 
        },
    };
</script>
<style lang="scss" scoped>
    /*微信授权*/
    page {
        background-color: #ffffff;
    }
 
    .wx-auth-container {
        width: 100%;
        margin-top: 20%;
    }
 
    .logo-info {
        display: flex;
        flex-wrap: nowrap;
        justify-content: flex-start;
        flex-direction: row;
        align-items: flex-start;
        padding: 20rpx;
 
        flex-direction: column;
        font-weight: bold;
    }
 
    image {
        width: 100px;
        height: 100px;
        text-align: center;
        -webkit-transform: scale(2.5);
        transform: scale(2.5);
    }
 
    .logo-info-img {
        width: 80rpx;
        height: 80rpx;
        border-radius: 50%;
        border: none;
    }
 
    text.title,
    text.shop {
        display: inline-block;
        font-size: 60rpx;
        color: #333;
    }
 
    text.shop {
        display: inline-block;
        font-size: 55rpx;
        color: #333;
    }
 
    .box {
        margin: 0 32rpx;
    }
 
    /* 文字提示*/
    .small-tips {
        width: 94%;
        padding: 20rpx;
        font-size: 24rpx;
        margin: 0 0 20rpx;
        color: #999;
    }
 
    .auth-button {
        padding: 10px 20px;
        width: calc(100% - 20 * 4rpx);
    }
 
    .tips {
        width: 80%;
        text-align: left;
        margin: 6% 10%;
        margin-top: 48rpx;
        line-height: 1.75;
    }
 
    .register {
        color: $weChat-color !important;
        border: none !important;
        background: #fff !important;
    }
 
.btn-auth {
  width: 92%;
  margin: 0 auto 40rpx;
  border-radius: 100px;
  // animation: mymove 5s infinite;
  // -webkit-animation: mymove 5s infinite; /*Safari and Chrome*/
  // animation-direction: alternate; /*轮流反向播放动画。*/
  // animation-timing-function: ease-in-out; /*动画的速度曲线*/
  // /* Safari 和 Chrome */
  // -webkit-animation: mymove 5s infinite;
  // -webkit-animation-direction: alternate; /*轮流反向播放动画。*/
  // -webkit-animation-timing-function: ease-in-out; /*动画的速度曲线*/
}
.btn-callback {
  text-align: center;
  font-size: 30rpx;
  background: #ededed;
  height: 90rpx;
  line-height: 90rpx;
  border-radius: 100px;
  width: 92%;
  margin: 0 auto;
}
 
    .btn-callback {
        text-align: center;
        font-size: 30rpx;
        background: #ededed;
        height: 90rpx;
        line-height: 90rpx;
        border-radius: 100px;
        width: 92%;
        margin: 0 auto;
    }
 
    .btns {
        margin-top: 100rpx;
        display: flex;
        flex-direction: column;
        width: 100%;
        justify-content: center;
    }
 
    @keyframes mymove {
        0% {
            transform: scale(1);
            /*开始为原始大小*/
        }
 
        25% {
            transform: scale(1.1);
            /*放大1.1倍*/
        }
 
        50% {
            transform: scale(1);
        }
 
        75% {
            transform: scale(1.1);
        }
    }
 
.privacy{
  text-align: center;
  margin-top: 20rpx;
  width: 100%;
}
</style>