SmsRecord.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\models\sms;
  3. use app\models\system\SystemConfig;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\services\sms\Sms;
  6. /**
  7. * @mixin think\Model
  8. */
  9. class SmsRecord extends BaseModel
  10. {
  11. /**
  12. * 短信状态
  13. * @var array
  14. */
  15. protected static $resultcode = ['100' => '成功', '130' => '失败', '131' => '空号', '132' => '停机', '133' => '关机', '134' => '无状态'];
  16. protected function getAddTimeAttr($value)
  17. {
  18. return $value ? date('Y-m-d H:i:s', $value) : '';
  19. }
  20. public static function vaildWhere($where)
  21. {
  22. $model = new static();
  23. if ($where['type']) $model = $model->where('resultcode', $where['type']);
  24. return $model;
  25. }
  26. /**
  27. * 获取短信记录列表
  28. * @param $where
  29. * @return array
  30. */
  31. public static function getRecordList($where)
  32. {
  33. $data = self::vaildWhere($where)->page((int)$where['page'], (int)$where['limit'])->select();
  34. $recordIds = [];
  35. foreach ($data as $k => $item) {
  36. if (!$item['resultcode']) {
  37. $recordIds[] = $item['record_id'];
  38. } else {
  39. $data[$k]['_resultcode'] = self::$resultcode[$item['resultcode']] ?? '无状态';
  40. }
  41. }
  42. unset($item);
  43. if (count($recordIds)) {
  44. $smsHandle = new Sms('yunxin', [
  45. 'sms_account' => sys_config('sms_account'),
  46. 'sms_token' => sys_config('sms_token'),
  47. 'site_url' => sys_config('site_url')
  48. ]);
  49. $codeLists = $smsHandle->getStatus($recordIds);
  50. if ($codeLists && isset($codeLists['status']) && $codeLists['status'] == 200 && isset($codeLists['data']) && is_array($codeLists['data'])) {
  51. foreach ($codeLists['data'] as $item) {
  52. if (isset($item['id']) && isset($item['resultcode'])) {
  53. self::where('record_id', $item['id'])->update(['resultcode' => $item['resultcode']]);
  54. foreach ($data as $key => $value) {
  55. if ($item['id'] == $value['record_id']) {
  56. $data[$key]['_resultcode'] = $item['_resultcode'];
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. $count = self::vaildWhere($where)->count();
  64. return compact('count', 'data');
  65. }
  66. /**
  67. * 发送记录
  68. * @param $phone
  69. * @param $content
  70. * @param $template
  71. * @param $record_id
  72. * @return bool
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public static function sendRecord($phone, $content, $template, $record_id)
  77. {
  78. $map = [
  79. 'uid' => SystemConfig::getConfigValue('sms_account'),
  80. 'phone' => $phone,
  81. 'content' => $content,
  82. 'add_time' => time(),
  83. 'template' => $template,
  84. 'record_id' => $record_id
  85. ];
  86. $msg = SmsRecord::create($map);
  87. if ($msg)
  88. return true;
  89. else
  90. return false;
  91. }
  92. }