# 微信小程序转 uniapps

  1. 利用 minprogram-to-uniapp 迁移代码
  2. 处理一些常见错误,例如:this.data.xxx -> this.xxx
  3. 重写页面和组件的一些东西
  4. 封装请求

# 重新封装页面和组件

  • 为了更好地快速适配小程序,将页面和组件做了一定的封装。
export const MaiPage = function (config) {
  const vueProperties = [
    'data',
    'created',
    'methods',
    'preLoad',
    'preUnload',
    'install',
    'onLoad',
    'onUnload',
    'onShow',
    'onHide',
    'onShareAppMessage',
  ];

  config.preLoad = [];
  config.preUnload = [];
  config.components = {};
  const { onLoad = () => {}, onUnload = () => {}, onShow = () => {}, onHide = () => {} } = config;

  for (const module of modules) {
    if (module.install) {
      module.install({
        page: config,
        app: appCtx,
      });
    }
  }

  const methods = {};
  for (const property in config) {
    if (vueProperties.includes(property)) {
      continue;
    }
    const propertyValue = config[property];
    if (typeof propertyValue === 'function') {
      methods[property] = propertyValue;
    }
  }
  config.methods = methods;

  const dataObject = config.data;
  const dataFunction = function () {
    const allData = {
      navigationBarHeight,
      statusBarHeight,
      productImageMode,
      themeVars: '',
      pageColorStyle: '',
      themeColor: '',
      mainColor: 'main-color',
      mainBorderColor: 'main-border-color',
      mainBgColor: 'main-bg-color',
      mainCaretColor: 'main-caret-color',
      mainBgColorOpacity1: 'main-bg-color-opacity-1',
      mainBgColorOpacity2: 'main-bg-color-opacity-2',
      mainBgColorOpacity5: 'main-bg-color-opacity-5',
      subColor: 'sub-color',
      subBgColor: 'sub-bg-color',
    };
    Object.assign(allData, dataObject);
    return allData;
  };
  config.data = dataFunction;

  config.onLoad = async function (options) {
    // 记录扫码进入的页面
    if (options.scene) {
      const routes = getCurrentPages();
      const { route } = routes[routes.length - 1];
      Setting._options.scanQrcodePage = route;
    } else {
      delete Setting._options.scanQrcodePage;
    }

    this.options = options;
    config.hasLoaded = false;
    const splashPath = Engine.getSplashPage();

    // #ifndef MP_TOUTIAO || MP-WEIXIN
    // if (appCtx.pageScene !== 1154 && this.route !== splashPath && !mai.member?.getOpenId()) {
    if (this.route !== splashPath && !mai.member.getOpenId()) {
      const redirectUrl = `/${this.route}?${qs.stringify(options)}`; // eslint-disable-next-line no-console

      // eslint-disable-next-line no-console
      console.debug(`no oauth url, redirect to: ${redirectUrl}`);
      MaiUNI.redirectTo({
        url: `/${splashPath}?redirectUrl=${encodeURIComponent(redirectUrl)}`,
      });
      return;
    } // 统一处理扫码逻辑
    // #endif

    const qrCodeDetail = await Setting.getOptions(options.scene);
    const qrCodeOptions = qrCodeDetail.options; // 将 B 类码携带的 UTM 参数设置到 maijs

    mai.memberEvent.setUtmProperties(qrCodeOptions);

    // eslint-disable-next-line no-param-reassign
    options = { ...qrCodeOptions, ...options }; // 从不同的门店进来时需要将缓存的门店清除

    // if (options.storeId && Stores.getStoreId() !== options.storeId) {
    //   Stores.setCurrentStore({});
    // }
    // deleted

    const themeColors = await getThemeColors();
    if (themeColors) {
      this.setData({ ...themeColors });
    }

    this.setData({
      currency: Setting.getCurrency(),
      i18n: Setting.replaceSpecificText(this.i18n),
    });
    if (typeof this.getTabBar === 'function') {
    // eslint-disable-next-line no-unused-expressions
    this.getTabBar()?.refresh();
    }

    for (const key of Object.keys(options)) {
      options[key] = decodeURIComponent(options[key]);
    }

    for (const preLoad of config.preLoad) {
      preLoad.call(this, options);
    }

    const params = MaiUNI._routeData[this.__route__]; // this.route 没有,改为 this.__route__。如后续该属性变更导致无法获取到参数,再做更改

    const newOptions = _.merge(options, params);

    this.options = newOptions;

    // eslint-disable-next-line no-console
    console.log('----------Options', newOptions);
    config.hasLoaded = true;
    onLoad.call(this, newOptions);

    // eslint-disable-next-line no-console
    console.debug('Page.Load', {
      routeData: MaiUNI._routeData,
      params,
      path: this.__route__,
      options: newOptions,
    });
  };

  config.onShow = function (options) {
    const params = MaiUNI._routeData[this.__route__];

    const newOptions = _.merge(options, params);

    let timer = null; // 由于 onLoad 中有异步请求,确保 onShow 在 onLoad 之后执行

    const polling = () => {
      if (!config.hasLoaded) {
        timer = setTimeout(() => {
          polling();
        }, 100);
        return;
      }

      onShow.call(this, newOptions);
      clearTimeout(timer);
    };

    polling();

    // eslint-disable-next-line no-console
    console.debug('Page.onShow', {
      routeData: MaiUNI._routeData,
      params,
      path: this.__route__,
      options: newOptions,
    });
  };

  //   config.onHide = function (options) {
  //     delete MaiUNI._routeData[this.__route__];
  //     onHide.call(this, options);
  //   };
  config.onHide = function (options) {
    if (this.__route__ !== 'mall/pages/bind-phone/index') {
      Setting._phoneBindStatus = '';
    }
    if (_.isNumber(config._maiLastViewedAt)) {
      mai.memberEvent.traffic.logPageLeave({
        durationInSeconds: Math.round((moment().valueOf() - config._maiLastViewedAt) / 1000),
      });
      config._maiLastViewedAt = null;
    }
    delete MaiUNI._routeData[this.route];
    onHide.call(this, options);
  };

  config.onUnload = function (options) {
    if (this.__route__ !== 'mall/pages/bind-phone/index') {
      Setting._phoneBindStatus = '';
    } // new
    config._maiLastViewedAt = null; // new
    for (const preUnload of config.preUnload) {
      preUnload.call(this, options);
    }

    if (onUnload) {
      onUnload.call(this, options);
    }
  };

  config.onLoad.bind(this); // 判断上滑/下滑,swipeUp: true 为上滑

  config.methods.onTouchStart = function ({ changedTouches }) {
    this.lastClientY = changedTouches[0].clientY;
  };

  config.methods.onTouchEnd = function ({ changedTouches }) {
    const { lastClientY } = this;
    const { clientY } = changedTouches[0];

    if (Math.abs(clientY - lastClientY) > 20) {
      this.setData({
        swipeUp: clientY < lastClientY,
      });
    }

    clearTimeout(this.swipeTimer); // 1s 没有操作则认为静止状态,恢复默认值

    this.swipeTimer = setTimeout(() => {
      this.setData({
        swipeUp: false,
      });
    }, 1000);
  };

  if (config.onShareAppMessage) {
    const { onShareAppMessage } = config;

    config.onShareAppMessage = function (options) {
      const { path, title, query = {}, imageUrl } = onShareAppMessage.call(this, options);
      let result = null; // 如果页面中的 onShareAppMessage 返回空对象,则分享小程序首页

      if (!path && !title && !imageUrl) {
        result = Setting.getDefaultShareConf(defaultShareQuery);
      } else {
        query.inviterMemberId = mai.member.getMemberId();
        query.inviterOpenId = mai.member.getOpenId();
        query.inviterName = mai.member.getNickname();
        query.promoterMemberId = mai.member.getMemberId();
        query.accountId = Engine.getMaiAccountId();

        const newQuery = _.merge(query, defaultShareQuery);

        for (const key of Object.keys(newQuery)) {
          if (newQuery[key] === undefined || newQuery[key] === null) {
            delete newQuery[key];
          }
        }

        const queryString = qs.stringify(newQuery, `${path}?`);
        result = {
          title,
          imageUrl,
          path: queryString,
        };
      }

      result.path = getShareCustomizePath(result.path);

      // eslint-disable-next-line no-console
      console.debug('onShareAppMessage result', result);
      logPageShare({ ...result, ...options });
      return result;
    };
  } else {
    config.onShareAppMessage = function (options) {
      const result = Setting.getDefaultShareConf(defaultShareQuery);
      result.path = getShareCustomizePath(result.path);

      // eslint-disable-next-line no-console
      console.debug('onShareAppMessage result', result);
      logPageShare({ ...result, ...options });
      return result;
    };
  }

  return config;
};
export const MaiComponent = function (config) {
  config.preAttached = [];
  config.preDetached = [];
  config.props = config.properties;

  const dataObject = config.data;
  const dataFunction = function () {
    const allData = {
      navigationBarHeight,
      statusBarHeight,
      productImageMode,
      themeVars: '',
      pageColorStyle: '',
      themeColor: '',
      mainColor: 'main-color',
      mainBorderColor: 'main-border-color',
      mainBgColor: 'main-bg-color',
      mainCaretColor: 'main-caret-color',
      mainBgColorOpacity1: 'main-bg-color-opacity-1',
      mainBgColorOpacity2: 'main-bg-color-opacity-2',
      mainBgColorOpacity5: 'main-bg-color-opacity-5',
      subColor: 'sub-color',
      subBgColor: 'sub-bg-color',
    };
    Object.assign(allData, dataObject);
    return allData;
  };
  config.data = dataFunction;

  const props = {};
  const properties = config.properties || {};
  Object.keys(properties).forEach((_) => {
    props[_] = {
      ...properties[_],
      default: properties[_].value,
    };
    const { observer } = properties[_];
    if (observer && typeof observer === 'function') {
      config.watch = config.watch || {};
      config.watch[_] = {
        handler: properties[_].observer,
        deep: properties[_].deep || false,
        immediate: true,
      };
    }
  });
  config.props = props;

  const { attached, detached } = config;

  for (const module of modules) {
    if (module.install) {
      module.install({
        component: config,
        app: appCtx,
      });
    }
  }

  config.mounted = async function () {
    const themeColors = await getThemeColors();
    if (themeColors) {
      this.setData({ ...themeColors });
    }
    console.log('=====================>config.preAttached', config.preAttached);
    for (const preAttached of config.preAttached) {
      preAttached.call(this);
    }

    if (attached) {
      attached.call(this);
    }
  };

  config.unmounted = function () {
    for (const preDetached of config.preDetached) {
      preDetached.call(this);
    }

    if (detached) {
      detached.call(this);
    }
  };

  config.methods = config.methods || {};
  config.methods.triggerEvent = function (eventName, eventParams, event) {
    // eslint-disable-next-line no-console
    console.log('========= positioning triggerEvent', eventName);
    this.$emit(eventName, { detail: eventParams, event });
  };

  return config;
};
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
更新时间: 2022年8月2日星期二 18:20