| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace app\admin\model;
- use think\Model;
- class Customer extends Model
- {
- protected $table = 'customer';
- // 时间由控制器直接写入 datetime 字符串,避免自动写成 Unix 时间戳
- protected $autoWriteTimestamp = false;
- protected $createTime = false;
- protected $updateTime = false;
- protected $deleteTime = false;
- protected $append = [
- 'status_text',
- ];
- public function getStatusList()
- {
- return [
- '1' => __('Status 1'),
- '0' => __('Status 0'),
- ];
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value !== '' && $value !== null ? $value : (isset($data['status']) ? $data['status'] : '');
- $list = $this->getStatusList();
- return isset($list[(string)$value]) ? $list[(string)$value] : '';
- }
- /**
- * 与手机端登录一致:md5(md5(明文))
- */
- public static function hashPassword(string $plain): string
- {
- return md5(md5($plain));
- }
- /**
- * 邮箱、手机号仅保留单个值(去掉逗号等多值写法)
- */
- public static function normalizeSingleContact(string $value): string
- {
- $value = trim($value);
- if ($value === '') {
- return '';
- }
- $value = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $value);
- $parts = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
- if (!$parts) {
- return $value;
- }
- return trim((string)$parts[0]);
- }
- protected static function init()
- {
- self::beforeWrite(function ($model) {
- $data = $model->getData();
- foreach (['phone', 'email'] as $field) {
- if (!array_key_exists($field, $data)) {
- continue;
- }
- $model->setAttr($field, self::normalizeSingleContact((string)$data[$field]));
- }
- if (array_key_exists('account', $data)) {
- $model->setAttr('account', trim((string)$data['account']));
- }
- });
- }
- }
|