zhengyiming
2025-03-27 55c03f35a31979aefd46aead13a145c9b293e6d8
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
import {
  BlLifeRechargeServices,
  PhoneMesssageCodeLoginInput,
  RequestConfig,
} from './lifeRechargeServices';
import { BlLifeRechargeOptions } from './types';
import { LifeRechargeConstants } from './lifeRechargeConstants';
import { BlLifeRechargeAccountModel } from './lifeRechargeAccountModel';
 
export class BlLifeRecharge<TResponse = any, TRequestOptions = any> {
  services: BlLifeRechargeServices<TResponse, TRequestOptions>;
  accountModel: BlLifeRechargeAccountModel;
 
  static constants = LifeRechargeConstants;
  constants = LifeRechargeConstants;
 
  constructor(options: BlLifeRechargeOptions<TResponse, TRequestOptions>) {
    this.services = new BlLifeRechargeServices(this, options);
    this.accountModel = new BlLifeRechargeAccountModel({
      userId: options.userId,
      phoneNumber: options.phoneNumber,
      channlesNum: options.channlesNum,
    });
  }
 
  async login(body: PhoneMesssageCodeLoginInput, options?: RequestConfig) {
    try {
      let res = await this.services.lifePayPhoneMesssageCodeLogin(body, options);
      this.accountModel.setUserId(res.userId);
      this.accountModel.setPhoneNumber(body.phoneNumber);
      return res;
    } catch (error) {
      throw new Error(error);
    }
  }
 
  loginout() {
    this.accountModel.setUserId('');
    this.accountModel.setPhoneNumber('');
  }
 
  isLogin() {
    return !!this.accountModel.userId;
  }
 
  getRechargeParValue(amount: number | string, rate: number) {
    return ((Number(amount) * rate) / 100).toFixed(2);
  }
 
  MaxParValue = 300;
 
  filterParValueList(parValueList: string[]) {
    return parValueList.filter((x) => Number(x) <= this.MaxParValue);
  }
 
  getCarrierByPhoneNumber(phoneNumber: string): LifeRechargeConstants.IspCode {
    // 定义号段和对应的运营商
    const carrierSegments = {
      [LifeRechargeConstants.IspCode.yidong]: [
        '134',
        '135',
        '136',
        '137',
        '138',
        '139',
        '150',
        '151',
        '152',
        '157',
        '158',
        '159',
        '182',
        '183',
        '184',
        '187',
        '188',
        '198',
        '147',
        '178',
        '165',
      ],
      [LifeRechargeConstants.IspCode.liantong]: [
        '130',
        '131',
        '132',
        '155',
        '156',
        '185',
        '186',
        '145',
        '176',
        '166',
      ],
      [LifeRechargeConstants.IspCode.dianxin]: [
        '133',
        '153',
        '180',
        '181',
        '189',
        '177',
        '199',
        '191',
      ],
    };
 
    // 检查手机号是否有效(这里只简单检查长度)
    if (!/^\d{11}$/.test(phoneNumber)) {
      return '' as any;
    }
 
    let prefix = phoneNumber.substr(0, 3);
    for (let carrier in carrierSegments) {
      if (carrierSegments[carrier].includes(prefix)) {
        return carrier as LifeRechargeConstants.IspCode;
      }
    }
 
    // 如果没有找到匹配的号段,则返回未知
    return '' as any;
  }
}