Sos.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\common\model;
  3. use liuniu\BaseModel;
  4. use liuniu\WechatTempleService;
  5. use think\Exception;
  6. use think\Model;
  7. class Sos extends BaseModel
  8. {
  9. // 表名
  10. protected $name = 'sos';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'status_text'
  20. ];
  21. public function getStatusList()
  22. {
  23. return ['0' => __('Status 0'), '1' => __('Status 1'), '-1' => __('Status -1')];
  24. }
  25. public function getStatusTextAttr($value, $data)
  26. {
  27. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  28. $list = $this->getStatusList();
  29. return isset($list[$value]) ? $list[$value] : '';
  30. }
  31. public static function sos_create($where)
  32. {
  33. self::beginTrans();
  34. try {
  35. if(SosBill::where('id', $where['user_id'])->where('rescuers_id',$where['rescuers_id'])->whereTime('createtime',"today")->find()) return self::setErrorInfo('已求救,不用重复发起!');
  36. $where1 = $where;
  37. unset($where1['from']);
  38. unset($where1['re_url']);
  39. $res = self::create($where1);
  40. $user_phone = User::where('id', $where['user_id'])->value('mobile');
  41. $rescue_phone = User::where('id', $where['rescuers_user_id'])->value('mobile');
  42. $rescuers_id = Rescue::where('user_id',$where['rescuers_user_id'])->value('id');
  43. $res1 = SosBill::income($where['cid'], $where['user_id'], $res['id'], $rescuers_id,$where['rescuers_user_id'], $where['latitude'], $where['longitude'], $user_phone, $rescue_phone,$where['address']);
  44. //tudo 模板信息
  45. if($where['from'] == "weixin") {
  46. $data['first'] = "有人向你发起求救";
  47. $data['keyword1'] = $user_phone;
  48. $data['keyword2'] = $where['address'];
  49. $data['keyword3'] = date("Y-m-d H:i:s");
  50. $data['keyword4'] = "";
  51. $data['remark'] ="详细请点信息";
  52. @file_put_contents("sos.txt",json_encode($data)."\r\n");
  53. $rs = WechatTempleService::sendTemplate($where['cid'], UserRelation::userIdToOpenId($where['rescuers_user_id']), WechatTempleService::EMERGANCY_RESCUE,$data,$where['re_url']);
  54. @file_put_contents("sos.txt",json_encode($rs)."\r\n",8);
  55. }
  56. if($res && $res1)
  57. {
  58. self::commitTrans();
  59. }
  60. }catch (Exception $e)
  61. {
  62. return self::setErrorInfo($e->getMessage(),true);
  63. }
  64. return $res;
  65. }
  66. }