SmsRecord.php 3.2 KB

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