PromotionShortLink.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. * 推广落地页短链表
  5. * @author Administrator
  6. */
  7. class PromotionShortLink extends \think\Model
  8. {
  9. protected $pk = 'id';
  10. protected $table = 'nw_promotion_short_link';
  11. protected $dateFormat = false; //时间戳格式不自动转换
  12. /**
  13. * 生成推广的短链
  14. *
  15. * @param string $game_id 游戏ID
  16. * @param string $channel_id 渠道ID
  17. * @param string $package_type
  18. * @param string $type 类型:推广/推广页
  19. *
  20. * @return bool
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function insertData($game_id,$channel_id,$package_type, $type=1)
  26. {
  27. if(!$this->where(['game_id'=>$game_id,'channel_id'=>$channel_id,'package_type'=>$package_type, 'type' => $type])->find()){
  28. $this->insert(['game_id'=> $game_id,'channel_id'=> $channel_id,'package_type'=>$package_type,'create_time'=>time(),'short_link'=>$this->createShortLink(), 'type' => $type]);
  29. return true;
  30. }
  31. else{
  32. return false;
  33. }
  34. }
  35. /**
  36. * 生成推广落地页短链接参数字段值
  37. */
  38. private function createShortLink()
  39. {
  40. $shortLink = strtolower(random(7));
  41. while ($this->field('id')->where(['short_link' => $shortLink])->find()) {
  42. $shortLink = strtolower(random(7));
  43. if (preg_match('/^\d+$/', $shortLink)) {
  44. $shortLink = strtolower(random(7));
  45. }
  46. }
  47. return $shortLink;
  48. }
  49. }