Customer.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Customer extends Model
  5. {
  6. protected $table = 'customer';
  7. // 时间由控制器直接写入 datetime 字符串,避免自动写成 Unix 时间戳
  8. protected $autoWriteTimestamp = false;
  9. protected $createTime = false;
  10. protected $updateTime = false;
  11. protected $deleteTime = false;
  12. protected $append = [
  13. 'status_text',
  14. ];
  15. public function getStatusList()
  16. {
  17. return [
  18. '1' => __('Status 1'),
  19. '0' => __('Status 0'),
  20. ];
  21. }
  22. public function getStatusTextAttr($value, $data)
  23. {
  24. $value = $value !== '' && $value !== null ? $value : (isset($data['status']) ? $data['status'] : '');
  25. $list = $this->getStatusList();
  26. return isset($list[(string)$value]) ? $list[(string)$value] : '';
  27. }
  28. /**
  29. * 与手机端登录一致:md5(md5(明文))
  30. */
  31. public static function hashPassword(string $plain): string
  32. {
  33. return md5(md5($plain));
  34. }
  35. /**
  36. * 邮箱、手机号仅保留单个值(去掉逗号等多值写法)
  37. */
  38. public static function normalizeSingleContact(string $value): string
  39. {
  40. $value = trim($value);
  41. if ($value === '') {
  42. return '';
  43. }
  44. $value = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $value);
  45. $parts = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
  46. if (!$parts) {
  47. return $value;
  48. }
  49. return trim((string)$parts[0]);
  50. }
  51. protected static function init()
  52. {
  53. self::beforeWrite(function ($model) {
  54. $data = $model->getData();
  55. foreach (['phone', 'email'] as $field) {
  56. if (!array_key_exists($field, $data)) {
  57. continue;
  58. }
  59. $model->setAttr($field, self::normalizeSingleContact((string)$data[$field]));
  60. }
  61. if (array_key_exists('account', $data)) {
  62. $model->setAttr('account', trim((string)$data['account']));
  63. }
  64. });
  65. }
  66. }