ReminderUsers.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\admin\model\equipment;
  3. use addons\qcloudsms\library\SmsMultiSender;
  4. use app\admin\model\User;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. class ReminderUsers extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'equipment_reminder_users';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'integer';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append = [
  20. 'status_text'
  21. ];
  22. public function getStatusList()
  23. {
  24. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  25. }
  26. public function getStatusTextAttr($value, $data)
  27. {
  28. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  29. $list = $this->getStatusList();
  30. return isset($list[$value]) ? $list[$value] : '';
  31. }
  32. public function multiStaff()
  33. {
  34. return $this->belongsTo(Staff::class, 'staff_id');
  35. }
  36. public function toRemindSMS($type, $id = '')
  37. {
  38. $result = true;
  39. $equipmentConfig = get_addon_config('equipment');
  40. if ($type == 'newRepair') {
  41. if ($equipmentConfig['remind_assign_repair'] == 1) {
  42. $staffIds = $this->where(['type' => 'assign_repair', 'status' => 'normal'])->column('staff_id');
  43. $userIds = Staff::where(['id' => ['in', $staffIds], 'status' => 'normal'])->column('user_id');
  44. $mobiles = User::where(['id' => ['in', $userIds]])->column('mobile');
  45. if (!empty($mobiles)) {
  46. $result = $this->sendSMS($mobiles, 'remindAssignRepair');
  47. }
  48. }
  49. if ($equipmentConfig['remind_receive_repair'] == 1) {
  50. $departmentIds = Department::where(['equipment_manage' => 1, 'status' => 'normal'])->column('id');
  51. $userIds = Staff::where(['department_id' => ['in', $departmentIds], 'status' => 'normal'])->column('user_id');
  52. $mobiles = User::where(['id' => ['in', $userIds]])->column('mobile');
  53. if (!empty($mobiles)) {
  54. $result = $this->sendSMS($mobiles, 'remindReceiveRepair');
  55. }
  56. }
  57. }
  58. if ($type == 'assignRepair' && $equipmentConfig['remind_deal_repair'] == 1) {
  59. $repairUid = Repair::where('id', $id)->value('repair_uid');
  60. $mobile = User::where('id', $repairUid)->value('mobile');
  61. if ($mobile) {
  62. $result = $this->sendSMS($mobile, 'remindDealRepair');
  63. }
  64. }
  65. return $result;
  66. }
  67. private function sendSMS($mobiles, $type)
  68. {
  69. if (is_string($mobiles)) {
  70. $mobiles = explode(',', $mobiles);
  71. }
  72. $params = [];
  73. $smsConfig = get_addon_config('qcloudsms');
  74. switch ($type) {
  75. case 'remindAssignRepair':
  76. $templateID = $smsConfig['template']['repair'];
  77. $params = ["待指派"];
  78. break;
  79. case 'remindReceiveRepair':
  80. $templateID = $smsConfig['template']['repair'];
  81. $params = ["可认领"];
  82. break;
  83. case 'remindDealRepair':
  84. $templateID = $smsConfig['template']['repair'];
  85. $params = ["被指派"];
  86. break;
  87. default:
  88. break;
  89. }
  90. $sender = new SmsMultiSender($smsConfig['appid'], $smsConfig['appkey']);
  91. $result = $sender->sendWithParam("86", $mobiles, $templateID, $params, $smsConfig['sign'], "", "");
  92. $rsp = json_decode($result, true);
  93. if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. }