SpecialRechargeUrl.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class SpecialRechargeUrl extends Model
  6. {
  7. // 表名
  8. protected $table = 'special_recharge_url';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'order_status_text',
  17. 'status_text'
  18. ];
  19. public function getOrderStatusList()
  20. {
  21. return ['0' => __('Order_status 0'),'1' => __('Order_status 1')];
  22. }
  23. public function getStatusList()
  24. {
  25. return ['normal' => __('Status normal'),'hidden' => __('Status hidden')];
  26. }
  27. public function getOrderStatusTextAttr($value, $data)
  28. {
  29. $value = $value ? $value : $data['order_status'];
  30. $list = $this->getOrderStatusList();
  31. return isset($list[$value]) ? $list[$value] : '';
  32. }
  33. public function getStatusTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : $data['status'];
  36. $list = $this->getStatusList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. /**
  40. * 检测链接
  41. * @param $id
  42. * @return array|mixed
  43. * @throws \think\Exception
  44. * @throws \think\exception\DbException
  45. */
  46. public function checkUrl($id)
  47. {
  48. $result = [];
  49. //链接已支付 存一次redis
  50. $redisKey = 'RUL:'.$id;
  51. if (Redis::instance()->exists($redisKey)) {
  52. $result = json_decode(Redis::instance()->get($redisKey), true);
  53. return $result;
  54. }
  55. $row = $this->get($id);
  56. if (!$row) {
  57. return ['code' => 201, 'msg' => '记录不存在', 'data' => []];
  58. }
  59. $row = $row->toArray();
  60. if ($row['status'] == 'hidden') {
  61. $result = ['code' => 202, 'msg' => '链接已关闭', 'data' => $row];
  62. }
  63. if ($row['order_status'] == '1') {
  64. $result = ['code' => 203, 'msg' => '链接已支付', 'data' => $row];
  65. }
  66. if ($result) {
  67. Redis::instance()->set($redisKey, json_encode($result, 256),86400);
  68. return $result;
  69. }
  70. $code = 200;
  71. $msg = '';
  72. if ($row['out_trade_no'] && $row['order_status'] == '0') {
  73. $order = model("Orders")->where("out_trade_no", 'EQ', $row['out_trade_no'])->find();
  74. if ($order['state'] == '1') {
  75. $this->where('id', 'eq', $id)->update(['order_status' => '1']);
  76. $row['order_status'] = '1';
  77. $code = 203;
  78. $msg = '链接已支付';
  79. }
  80. }
  81. return ['code' => $code, 'msg' => $msg, 'data' => $row];
  82. }
  83. }