@cloudbase/lowcode-builder 1.9.13 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,293 +1,558 @@
1
- import { app, $app } from '../../../../app/weapps-api';
1
+ import {
2
+ app,
3
+ $app
4
+ } from '../../../../app/weapps-api';
2
5
  import loginByPassword from './methods/loginByPassword';
3
6
  import loginByWXPhone from './methods/loginByWXPhone';
4
- import { parseSmsError } from './methods/errorHandler';
7
+ import {
8
+ parseSmsError
9
+ } from './methods/errorHandler';
5
10
  import loginBySms from './methods/loginBySms';
6
- import { getLoginConfig } from '../../../../common/util';
7
- import { phoneNumberPattern, phoneVerifyInfoKey, verifyDelay, loginOnly } from './methods/contants';
11
+ import loginByOpenId from './methods/loginByOpenId';
12
+ import loginByUnionId from './methods/loginByUnionId';
13
+ import {
14
+ getLoginConfig
15
+ } from '../../../../common/util';
16
+ import {
17
+ phoneNumberPattern,
18
+ phoneVerifyInfoKey,
19
+ verifyDelay,
20
+ loginOnly
21
+ } from './methods/contants';
22
+ import loginSuccessCallBack from './methods/loginSuccessCallBack'
23
+
24
+ const AUTO_LOGIN_TYPE = ['openid_login', 'unionid_login']
25
+ const AUTO_LOGIN_METHOD = {
26
+ 'openid_login': loginByOpenId,
27
+ 'unionid_login': loginByUnionId
28
+ }
8
29
 
9
30
  function decodePageQuery(query) {
10
- return Object.keys(query).reduce((decoded, key) => {
11
- decoded[key] = decodeURIComponent(query[key]);
12
- return decoded;
13
- }, {});
31
+ return Object.keys(query).reduce((decoded, key) => {
32
+ decoded[key] = decodeURIComponent(query[key]);
33
+ return decoded;
34
+ }, {});
14
35
  }
15
36
 
16
37
  const BUTTON_CLASSNAME = 'weda-ui weda-button weui-btn weui-btn_primary wd-event-tap';
17
38
 
18
39
  function setStorage(data) {
19
- const current = wx.getStorageSync(phoneVerifyInfoKey);
20
- wx.setStorageSync(phoneVerifyInfoKey, { ...current, ...data });
40
+ const current = wx.getStorageSync(phoneVerifyInfoKey);
41
+ wx.setStorageSync(phoneVerifyInfoKey, {
42
+ ...current,
43
+ ...data
44
+ });
21
45
  }
22
46
 
23
47
  Component({
24
- data: {
25
- params: {},
26
- initing: true,
27
- error: null,
28
- settingData: {
29
- logo: '',
30
- agreement: {
31
- items: [],
32
- enable: false,
33
- },
34
- miniprogram: [],
35
- },
36
- pageStyle: '',
37
- agreement: false,
38
- wxLoginStatus: '',
39
- passwordVisible: false,
40
- enablePassword: false,
41
- enableMpPhone: false,
42
- enbaleSms: false,
43
- disableLoginSubmit: false,
44
- loginButtonClass: BUTTON_CLASSNAME,
45
- phoneValidateMessage: '手机号为空',
46
- phoneNum: '',
47
- defaultPhoneNumber: '',
48
- smsVerificationInfo: {
49
- id: '',
50
- isUser: false,
51
- },
52
- },
53
- methods: {
54
- async getPhoneCodeNumber(e) {
55
- const agree = this.checkAgreement();
56
- if (!agree) {
57
- return;
58
- }
59
- if (e?.detail?.errMsg?.includes('fail') || e?.detail?.errno || !e?.detail?.code) {
60
- console.warn('获取手机号授权令牌失败:', !e?.detail.code ? { errMsg: '可能基础库版本过低' } : e.detail);
61
-
62
- $app.showModal({
63
- cancelColor: '#000000',
64
- cancelText: '取消',
65
- confirmColor: '#576B95',
66
- confirmText: '确认',
67
- showCancel: true,
68
- title: '获取手机号授权令牌失败',
69
- content: `获取手机号授权令牌失败: ${e.detail.errno || e.detail?.errMsg || '可能基础库版本过低'}`,
70
- });
71
-
72
- return;
73
- }
74
-
75
- return loginByWXPhone(this, { event: e });
76
- },
77
- async onLoad(options) {
78
- try {
79
- const cache = wx.getStorageSync(phoneVerifyInfoKey);
80
- if (cache.phoneNum) {
81
- this.setData({ defaultPhoneNumber: cache.phoneNum, phoneNum: cache.phoneNum, phoneValidateMessage: '' });
82
- }
83
-
84
- const config = await getLoginConfig().catch(() => {
85
- return getLoginConfig();
86
- });
87
- const query = decodePageQuery(options || {});
88
- let settingData = {
89
- logo:
90
- app.__internal__.resolveStaticResourceUrl(config.logo) ||
91
- 'https://sso-1303824488.cos.ap-shanghai.myqcloud.com/logo.svg',
92
- agreement: {
93
- items: [],
94
- enable: false,
95
- ...config.agreement,
96
- },
97
- miniprogram: config.miniprogram || [],
98
- };
99
-
100
- const enableSms = this._getEnableSms(settingData);
101
- const smsDelay =
102
- enableSms && !cache.phoneNum ? Math.max(0, Math.ceil(((cache?.smsDelay || 0) - +new Date()) / 1000)) : 0;
103
-
104
- this.setData({
105
- params: query,
106
- settingData,
107
- enablePassword: this._getEnablePassword(settingData),
108
- enableMpPhone: this._getEnableMpPhone(settingData),
109
- enableSms,
110
- smsDelay,
111
- smsVerificationInfo: cache,
112
- pageStyle: `${this.data.pageStyle}; background-color: ${config.backgroundColor || '#fff'};`,
113
- initing: false,
114
- });
115
-
116
- if (smsDelay > 0) {
117
- this._intervalSender(smsDelay);
118
- }
119
- } catch (e) {
120
- console.error('获取登录配置失败:', e);
121
- this.setData({
122
- initing: false,
123
- error: {
124
- message: '获取登录配置失败,' + (e.message || ''),
125
- },
126
- });
127
- }
128
- },
129
- togglePasswordVisible() {
130
- this.setData({
131
- passwordVisible: !this.data.passwordVisible,
132
- });
133
- },
134
- loginByPassword(e) {
135
- if (!this.checkAgreement()) {
136
- return;
137
- }
138
-
139
- const values = e.detail.value;
140
-
141
- return loginByPassword(this, {
142
- phone: values?.username?.value,
143
- sms: values?.password?.value,
144
- });
145
- },
146
- loginBySms(e) {
147
- if (!this.checkAgreement()) {
148
- return;
149
- }
150
-
151
- const values = e.detail.value;
152
-
153
- return loginBySms(this, {
154
- phoneNum: values?.phoneNum,
155
- verificationCode: values?.verificationCode,
156
- verificationInfo: this.data.smsVerificationInfo,
157
- callback: (err, data) => {
158
- if (err) {
159
- const { usedCount = 0 } = wx.getStorageSync(phoneVerifyInfoKey) || {};
160
- setStorage({ usedCount: usedCount + 1 });
161
- } else {
162
- setStorage({ usedCount: 0, smsDelay: 0 });
163
- }
164
- },
165
- });
166
- },
167
- showAgreement(e) {
168
- const index = e.currentTarget?.dataset?.index;
169
- const agreementContent = this.data.settingData.agreement.items[index]?.value;
170
- this.setData({
171
- agreementContent,
172
- isShowAgreement: !!agreementContent,
173
- });
174
- },
175
- checkAgreement() {
176
- if (this.data.settingData?.agreement?.enable && !this.data.agreement) {
177
- app.showToast({
178
- title: '请阅读勾选协议',
179
- icon: 'error',
180
- duartion: 1500,
181
- });
182
-
183
- return false;
184
- }
185
- return true;
186
- },
187
- onAgreementChange(e) {
188
- this.setData({
189
- agreement: e.detail.value.length > 0,
190
- });
191
- },
192
- onPhoneChange(e) {
193
- const { value } = e.detail;
194
- if (!phoneNumberPattern.test(value)) {
195
- this.setData({ phoneValidateMessage: '手机号格式非法', phoneNum: value });
196
- } else {
197
- this.setData({ phoneValidateMessage: '', phoneNum: value });
198
- }
199
- },
200
- getCaptcha(e) {
201
- if (this.data.phoneValidateMessage) {
202
- wx.showToast({
203
- icon: 'error',
204
- title: this.data.phoneValidateMessage,
205
- });
206
- return;
207
- }
208
- if (this.data.smsDelay > 0) {
209
- return;
210
- }
211
- this._intervalSender(verifyDelay);
212
- const target = loginOnly ? 'USER' : 'ANY';
213
- const phoneNum = this.data.phoneNum;
214
- app.cloud
215
- .getCloudInstance()
216
- .then((cloudbase) => {
217
- return cloudbase.authInstance || cloudbase.auth;
218
- })
219
- .then((auth) => {
220
- setStorage({ phoneNum });
221
- return auth.getVerification({
222
- target,
223
- phone_number: `+86 ${phoneNum}`,
224
- });
225
- })
226
- .then((data) => {
227
- const verifyInfo = {
228
- id: data.verification_id,
229
- isUser: data.is_user,
230
- };
231
- this.setData({
232
- smsVerificationInfo: verifyInfo,
233
- });
234
- setStorage({ ...verifyInfo, usedCount: 0 });
235
- wx.showToast({
236
- title: '验证码已发送',
237
- });
238
- })
239
- .catch((e) => {
240
- wx.hideLoading()
241
- clearInterval(this.timerInterval);
242
- this.setData({ smsDelay: 0 });
243
- setStorage({ smsDelay: 0 });
244
- wx.showModal({
245
- title: '获取验证码失败',
246
- content: parseSmsError(e, target, loginOnly),
247
- showCancel: false,
248
- });
249
- });
250
- },
251
- _getEnablePassword(settingData) {
252
- const config = settingData || this.data.settingData;
253
- return config.miniprogram?.find?.((item) => item.type === 'password')?.enable ?? false;
254
- },
255
- _getEnableMpPhone(settingData) {
256
- const config = settingData || this.data.settingData;
257
- return !!config.miniprogram.find(function (item) {
258
- return item.type === 'miniprogram_phone';
259
- })?.enable;
260
- },
261
- _getEnableSms(settingData) {
262
- const config = settingData || this.data.settingData;
263
- return !!config.miniprogram.find(function (item) {
264
- return item.type === 'sms';
265
- })?.enable;
266
- },
267
- _intervalSender(delay) {
268
- this.setData({ smsDelay: delay });
269
- const timerInterval = setInterval(() => {
270
- delay = delay - 1;
271
- if (delay <= 0) {
272
- clearInterval(timerInterval);
273
- }
274
- this.setData({ smsDelay: delay });
275
- setStorage({ smsDelay: +new Date() + delay * 1000 });
276
- }, 1000);
277
- this.timerInterval = timerInterval;
278
- },
279
- },
280
- observers: {
281
- 'settingData,agreement': function (settingData, agreement) {
282
- const disabled = settingData?.agreement?.enable && !agreement;
283
- if (!!this.data.disableLoginSubmit !== disabled) {
284
- this.setData({ disableLoginSubmit: disabled });
285
- }
286
- },
287
- disableLoginSubmit: function (disableLoginSubmit) {
288
- this.setData({
289
- loginButtonClass: `${BUTTON_CLASSNAME} ${disableLoginSubmit ? 'is-disabled' : ''}`,
290
- });
291
- },
292
- },
48
+ data: {
49
+ params: {},
50
+ initing: true,
51
+ error: null,
52
+ settingData: {
53
+ logo: '',
54
+ agreement: {
55
+ items: [],
56
+ enable: false,
57
+ },
58
+ miniprogram: [],
59
+ },
60
+ pageStyle: '',
61
+ agreement: false,
62
+ wxLoginStatus: '',
63
+ passwordVisible: false,
64
+ enablePassword: false,
65
+ enableMpPhone: false,
66
+ enbaleSms: false,
67
+ disableLoginSubmit: false,
68
+ loginButtonClass: BUTTON_CLASSNAME,
69
+ phoneValidateMessage: '手机号为空',
70
+ phoneNum: '',
71
+ defaultPhoneNumber: '',
72
+ smsVerificationInfo: {
73
+ id: '',
74
+ isUser: false,
75
+ },
76
+
77
+ currentLoginType: '',
78
+ currentLoginList: [],
79
+ baseInfoShow: false,
80
+ avatarUrl: '',
81
+ avatarRealUrl: '',
82
+ nickName: '',
83
+ },
84
+ methods: {
85
+ async getPhoneCodeNumber(e) {
86
+ const agree = this.checkAgreement();
87
+ if (!agree) {
88
+ return;
89
+ }
90
+ if (e?.detail?.errMsg?.includes('fail') || e?.detail?.errno || !e?.detail?.code) {
91
+ console.warn('获取手机号授权令牌失败:', !e?.detail.code ? {
92
+ errMsg: '可能基础库版本过低'
93
+ } : e.detail);
94
+
95
+ $app.showModal({
96
+ cancelColor: '#000000',
97
+ cancelText: '取消',
98
+ confirmColor: '#576B95',
99
+ confirmText: '确认',
100
+ showCancel: true,
101
+ title: '获取手机号授权令牌失败',
102
+ content: `获取手机号授权令牌失败: ${e.detail.errno || e.detail?.errMsg || '可能基础库版本过低'}`,
103
+ });
104
+
105
+ return;
106
+ }
107
+
108
+ return loginByWXPhone(this, {
109
+ event: e
110
+ });
111
+ },
112
+ async onLoad(options) {
113
+ try {
114
+ const cache = wx.getStorageSync(phoneVerifyInfoKey);
115
+ if (cache.phoneNum) {
116
+ this.setData({
117
+ defaultPhoneNumber: cache.phoneNum,
118
+ phoneNum: cache.phoneNum,
119
+ phoneValidateMessage: ''
120
+ });
121
+ }
122
+
123
+ const config = await getLoginConfig().catch(() => {
124
+ return getLoginConfig();
125
+ });
126
+ const query = decodePageQuery(options || {});
127
+ let settingData = {
128
+ logo: app.__internal__.resolveStaticResourceUrl(config.logo) ||
129
+ 'https://sso-1303824488.cos.ap-shanghai.myqcloud.com/logo.svg',
130
+ agreement: {
131
+ items: [],
132
+ enable: false,
133
+ ...config.agreement,
134
+ },
135
+ miniprogram: (config.miniprogram || []).map(v => ({
136
+ ...v,
137
+ label: v.label?.replace(/^微信小程序/, '')
138
+ })),
139
+ };
140
+
141
+ const enableSms = this._getEnableSms(settingData);
142
+ const smsDelay =
143
+ enableSms && !cache.phoneNum ? Math.max(0, Math.ceil(((cache?.smsDelay || 0) - +new Date()) / 1000)) : 0;
144
+ const baseInfoShow = query.baseInfoShow === 'true'
145
+ const currentLoginType = query.currentLoginType || settingData.miniprogram.filter(v => !AUTO_LOGIN_TYPE.includes(v.type))?.[0]?.type || settingData.miniprogram[0]?.type;
146
+ const currentLoginList = query.currentLoginType ? [] : settingData.miniprogram.filter(v => v.type !== currentLoginType)
147
+
148
+ const {
149
+ avatarUrl,
150
+ nickName
151
+ } = app.auth.currentUser
152
+ const avatarRealUrl = await this.getAvatarUrl(avatarUrl)
153
+
154
+ this.setData({
155
+ params: query,
156
+ settingData,
157
+ enablePassword: this._getEnablePassword(settingData),
158
+ enableMpPhone: this._getEnableMpPhone(settingData),
159
+ enableSms,
160
+ smsDelay,
161
+ smsVerificationInfo: cache,
162
+ pageStyle: `${this.data.pageStyle}; background-color: ${config.backgroundColor || '#fff'};`,
163
+ initing: false,
164
+ currentLoginType,
165
+ currentLoginList,
166
+ baseInfoShow,
167
+ avatarRealUrl,
168
+ avatarUrl,
169
+ nickName,
170
+ });
171
+
172
+ if (smsDelay > 0) {
173
+ this._intervalSender(smsDelay);
174
+ }
175
+
176
+ if (settingData.miniprogram.length === 1 && AUTO_LOGIN_TYPE.includes(currentLoginType) && !baseInfoShow) {
177
+ await this.dealOpenIdOrUnionIdLogin(currentLoginType);
178
+ }
179
+ } catch (e) {
180
+ console.error('获取登录配置失败:', e);
181
+ this.setData({
182
+ initing: false,
183
+ error: {
184
+ message: '获取登录配置失败,' + (e.message || ''),
185
+ },
186
+ });
187
+ }
188
+ },
189
+ togglePasswordVisible() {
190
+ this.setData({
191
+ passwordVisible: !this.data.passwordVisible,
192
+ });
193
+ },
194
+ loginByPassword(e) {
195
+ if (!this.checkAgreement()) {
196
+ return;
197
+ }
198
+
199
+ const values = e.detail.value;
200
+
201
+ return loginByPassword(this, {
202
+ phone: values?.username?.value,
203
+ sms: values?.password?.value,
204
+ });
205
+ },
206
+ loginBySms(e) {
207
+ if (!this.checkAgreement()) {
208
+ return;
209
+ }
210
+
211
+ const values = e.detail.value;
212
+
213
+ return loginBySms(this, {
214
+ phoneNum: values?.phoneNum,
215
+ verificationCode: values?.verificationCode,
216
+ verificationInfo: this.data.smsVerificationInfo,
217
+ callback: (err, data) => {
218
+ if (err) {
219
+ const {
220
+ usedCount = 0
221
+ } = wx.getStorageSync(phoneVerifyInfoKey) || {};
222
+ setStorage({
223
+ usedCount: usedCount + 1
224
+ });
225
+ } else {
226
+ setStorage({
227
+ usedCount: 0,
228
+ smsDelay: 0
229
+ });
230
+ }
231
+ },
232
+ });
233
+ },
234
+ showAgreement(e) {
235
+ const index = e.currentTarget?.dataset?.index;
236
+ const agreementContent = this.data.settingData.agreement.items[index]?.value;
237
+ this.setData({
238
+ agreementContent,
239
+ isShowAgreement: !!agreementContent,
240
+ });
241
+ },
242
+ checkAgreement() {
243
+ if (this.data.settingData?.agreement?.enable && !this.data.agreement) {
244
+ app.showToast({
245
+ title: '请阅读勾选协议',
246
+ icon: 'error',
247
+ duartion: 1500,
248
+ });
249
+
250
+ return false;
251
+ }
252
+ return true;
253
+ },
254
+ onAgreementChange(e) {
255
+ this.setData({
256
+ agreement: e.detail.value.length > 0,
257
+ });
258
+ },
259
+ onPhoneChange(e) {
260
+ const {
261
+ value
262
+ } = e.detail;
263
+ if (!phoneNumberPattern.test(value)) {
264
+ this.setData({
265
+ phoneValidateMessage: '手机号格式非法',
266
+ phoneNum: value
267
+ });
268
+ } else {
269
+ this.setData({
270
+ phoneValidateMessage: '',
271
+ phoneNum: value
272
+ });
273
+ }
274
+ },
275
+ getCaptcha(e) {
276
+ if (this.data.phoneValidateMessage) {
277
+ wx.showToast({
278
+ icon: 'error',
279
+ title: this.data.phoneValidateMessage,
280
+ });
281
+ return;
282
+ }
283
+ if (this.data.smsDelay > 0) {
284
+ return;
285
+ }
286
+ this._intervalSender(verifyDelay);
287
+ const target = loginOnly ? 'USER' : 'ANY';
288
+ const phoneNum = this.data.phoneNum;
289
+ app.cloud
290
+ .getCloudInstance()
291
+ .then((cloudbase) => {
292
+ return cloudbase.authInstance || cloudbase.auth;
293
+ })
294
+ .then((auth) => {
295
+ setStorage({
296
+ phoneNum
297
+ });
298
+ return auth.getVerification({
299
+ target,
300
+ phone_number: `+86 ${phoneNum}`,
301
+ });
302
+ })
303
+ .then((data) => {
304
+ const verifyInfo = {
305
+ id: data.verification_id,
306
+ isUser: data.is_user,
307
+ };
308
+ this.setData({
309
+ smsVerificationInfo: verifyInfo,
310
+ });
311
+ setStorage({
312
+ ...verifyInfo,
313
+ usedCount: 0
314
+ });
315
+ wx.showToast({
316
+ title: '验证码已发送',
317
+ });
318
+ })
319
+ .catch((e) => {
320
+ wx.hideLoading()
321
+ clearInterval(this.timerInterval);
322
+ this.setData({
323
+ smsDelay: 0
324
+ });
325
+ setStorage({
326
+ smsDelay: 0
327
+ });
328
+ wx.showModal({
329
+ title: '获取验证码失败',
330
+ content: parseSmsError(e, target, loginOnly),
331
+ showCancel: false,
332
+ });
333
+ });
334
+ },
335
+ _getEnablePassword(settingData) {
336
+ const config = settingData || this.data.settingData;
337
+ return config.miniprogram?.find?.((item) => item.type === 'password')?.enable ?? false;
338
+ },
339
+ _getEnableMpPhone(settingData) {
340
+ const config = settingData || this.data.settingData;
341
+ return !!config.miniprogram.find(function (item) {
342
+ return item.type === 'miniprogram_phone';
343
+ })?.enable;
344
+ },
345
+ _getEnableSms(settingData) {
346
+ const config = settingData || this.data.settingData;
347
+ return !!config.miniprogram.find(function (item) {
348
+ return item.type === 'sms';
349
+ })?.enable;
350
+ },
351
+ _intervalSender(delay) {
352
+ this.setData({
353
+ smsDelay: delay
354
+ });
355
+ const timerInterval = setInterval(() => {
356
+ delay = delay - 1;
357
+ if (delay <= 0) {
358
+ clearInterval(timerInterval);
359
+ }
360
+ this.setData({
361
+ smsDelay: delay
362
+ });
363
+ setStorage({
364
+ smsDelay: +new Date() + delay * 1000
365
+ });
366
+ }, 1000);
367
+ this.timerInterval = timerInterval;
368
+ },
369
+ compareVersion(version1, version2) {
370
+ // 将版本号字符串分割成数组
371
+ const v1 = version1.split('.');
372
+ const v2 = version2.split('.');
373
+
374
+ // 获取两个版本号数组的最大长度
375
+ const maxLength = Math.max(v1.length, v2.length);
376
+
377
+ // 遍历版本号数组并比较每个部分
378
+ for (let i = 0; i < maxLength; i++) {
379
+ // 如果某个版本号的长度较短,则将其补0
380
+ const num1 = parseInt(v1[i] || 0, 10);
381
+ const num2 = parseInt(v2[i] || 0, 10);
382
+
383
+ // 比较当前位置的数字大小
384
+ if (num1 > num2) {
385
+ return 1;
386
+ } else if (num1 < num2) {
387
+ return -1;
388
+ }
389
+ }
390
+
391
+ // 如果所有位置的数字都相同,则两个版本号相等
392
+ return 0;
393
+ },
394
+ async getAvatarUrl(avatarUrl) {
395
+ let avatarRealUrl = avatarUrl
396
+ if (avatarUrl.startsWith('cloud://')) {
397
+ const tcb = await app.cloud.getCloudInstance()
398
+ const pic = await tcb.getTempFileURL({
399
+ fileList: [avatarUrl]
400
+ })
401
+ avatarRealUrl = pic?.fileList?.[0]?.tempFileURL || ''
402
+ }
403
+
404
+ return avatarRealUrl;
405
+ },
406
+ async dealOpenIdOrUnionIdLogin(type) {
407
+ try {
408
+ const res = await (AUTO_LOGIN_METHOD[type])(this);
409
+ if (res) {
410
+ const {
411
+ avatarUrl,
412
+ nickName
413
+ } = app.auth.currentUser
414
+ const {
415
+ SDKVersion
416
+ } = wx.getSystemInfoSync();
417
+ // 2.21.2以下版本不支持获取用户头像/昵称,暂时跳过
418
+ if ((!avatarUrl || !nickName) && this.compareVersion(SDKVersion, '2.21.2') >= 0) {
419
+ const avatarRealUrl = await this.getAvatarUrl(avatarUrl)
420
+ this.setData({
421
+ avatarRealUrl,
422
+ avatarUrl,
423
+ nickName,
424
+ baseInfoShow: true
425
+ })
426
+ } else {
427
+ loginSuccessCallBack(this);
428
+ }
429
+ }
430
+ } catch (error) {
431
+
432
+ }
433
+ },
434
+ jumpBaseInfoConfig() {
435
+ loginSuccessCallBack(this);
436
+ },
437
+ methodClick(e) {
438
+ const {
439
+ type
440
+ } = e.currentTarget.dataset.item || {};
441
+
442
+ const currentLoginType = type || this.data.currentLoginType;
443
+
444
+ this.setData({
445
+ currentLoginType,
446
+ currentLoginList: this.data.settingData.miniprogram.filter(v => v.type !== currentLoginType)
447
+ })
448
+
449
+ if (AUTO_LOGIN_TYPE.includes(currentLoginType)) {
450
+ if (this.data.disableLoginSubmit) {
451
+ wx.showToast({
452
+ title: '请先阅读并同意协议',
453
+ icon: 'none'
454
+ })
455
+ return;
456
+ };
457
+ this.dealOpenIdOrUnionIdLogin(currentLoginType)
458
+ return;
459
+ }
460
+ },
461
+ onChooseAvatar(e) {
462
+ this.setData({
463
+ avatarUrl: e.detail.avatarUrl,
464
+ avatarRealUrl: '',
465
+ })
466
+ },
467
+ nickNameChange(e) {
468
+ this.setData({
469
+ nickName: e.detail.value
470
+ })
471
+ },
472
+ randomStr(len = 32) {
473
+ const s = [];
474
+ const hexDigits = '0123456789abcdef';
475
+ const dictLen = hexDigits.length;
476
+ for (var i = 0; i < len; i++) {
477
+ const index = Math.floor(Math.random() * dictLen);
478
+ s[i] = hexDigits[index];
479
+ }
480
+ const result = s.join('');
481
+ return result;
482
+ },
483
+ async uploadFile(file) {
484
+ return new Promise(async (resolve) => {
485
+ const uploadPath = 'weda-uploader';
486
+ const fileType = file.split('.')?.slice(-1)?.[0];
487
+ const cloudPath = `${uploadPath}/${this.randomStr()}-${Date.now()}.${fileType}`;
488
+ try {
489
+ const tcb = await app.cloud.getCloudInstance();
490
+ const res = await tcb.uploadFile({
491
+ cloudPath: cloudPath,
492
+ filePath: file,
493
+ });
494
+ if (res.fileID) {
495
+ resolve(res)
496
+ } else {
497
+ throw res
498
+ }
499
+ } catch (e) {
500
+ wx.showModal({
501
+ title: '上传失败,请重试',
502
+ content: e.message,
503
+ showCancel: false,
504
+ });
505
+ resolve({});;
506
+ }
507
+ })
508
+ },
509
+ async baseInfoConfirm() {
510
+ let {
511
+ avatarUrl,
512
+ nickName,
513
+ currentLoginType,
514
+ } = this.data
515
+
516
+ wx.showLoading()
517
+
518
+ if (avatarUrl && !avatarUrl.startsWith('cloud://')) {
519
+ const res = await this.uploadFile(avatarUrl)
520
+ avatarUrl = res.fileID || ''
521
+ }
522
+
523
+ try {
524
+ const tcb = await app.cloud.getCloudInstance();
525
+ await tcb.auth.updateUserBasicInfo({
526
+ avatar_url: avatarUrl,
527
+ nickname: nickName,
528
+ user_id: app.auth.currentUser.userId
529
+ });
530
+ await (AUTO_LOGIN_METHOD[currentLoginType])(this);
531
+ loginSuccessCallBack(this);
532
+ } catch (error) {
533
+ wx.showModal({
534
+ title: '更新用户信息出错',
535
+ content: error.message,
536
+ showCancel: false,
537
+ });
538
+ }
539
+
540
+ wx.hideLoading()
541
+ }
542
+ },
543
+ observers: {
544
+ 'settingData,agreement': function (settingData, agreement) {
545
+ const disabled = settingData?.agreement?.enable && !agreement;
546
+ if (!!this.data.disableLoginSubmit !== disabled) {
547
+ this.setData({
548
+ disableLoginSubmit: disabled
549
+ });
550
+ }
551
+ },
552
+ disableLoginSubmit: function (disableLoginSubmit) {
553
+ this.setData({
554
+ loginButtonClass: `${BUTTON_CLASSNAME} ${disableLoginSubmit ? 'is-disabled' : ''}`,
555
+ });
556
+ },
557
+ },
293
558
  });