CopyCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\common\repositories\store\product\ProductAssistSetRepository;
  13. use app\common\repositories\store\product\ProductGroupBuyingRepository;
  14. use app\common\repositories\store\product\ProductGroupRepository;
  15. use app\common\repositories\store\product\ProductPresellRepository;
  16. use app\common\repositories\store\product\ProductRepository;
  17. use app\common\repositories\store\product\SpuRepository;
  18. use think\exception\ValidateException;
  19. use think\helper\Str;
  20. class CopyCommand
  21. {
  22. protected $type = [
  23. '0' => 'p/:', //普通商品
  24. '1' => 's/:', //秒杀商品
  25. '2' => 'r/:', //预售商品
  26. '4' => 'g/:', //拼团商品
  27. '30' => 'sa/:', //助力活动
  28. '40' => 'gb/:', //拼团活动
  29. ];
  30. protected $productType = [
  31. 'p' => 0,
  32. 's' => 1,
  33. 'r' => 2,
  34. 'g' => 4,
  35. 'sa' => 30,
  36. 'gb' => 40,
  37. ];
  38. /**
  39. * 创建口令
  40. * @param int $id
  41. * @param string $type
  42. * @param $userInfo
  43. * @return string
  44. * @author Qinii
  45. * @day 9/8/21
  46. */
  47. public function create(int $id, string $type, $userInfo)
  48. {
  49. $data = $this->getTitle($id, $type);
  50. $str = $this->setNumberToStr($data['id'], $type);
  51. if ($userInfo->uid) $str .= '嗯'.dechex($userInfo->uid??0);
  52. return $str. '*/ '.$data['title'];
  53. }
  54. /**
  55. * 创建口令ID
  56. * @param $id
  57. * @param $type
  58. * @return string
  59. * @author Qinii
  60. * @day 9/8/21
  61. */
  62. public function setNumberToStr($id, $type)
  63. {
  64. $count = 10;
  65. $id = dechex($id);
  66. $strlen = strlen($id);
  67. $str = '/@'. $strlen.$this->type[$type];
  68. $str .= Str::random(($count-$strlen),null);
  69. $str .= $id;
  70. return $str;
  71. }
  72. /**
  73. * 商品信息
  74. * @param $id
  75. * @param $type
  76. * @return array
  77. * @author Qinii
  78. * @day 9/8/21
  79. */
  80. public function getTitle($id, $type)
  81. {
  82. switch ($type){
  83. case 0:
  84. $ret = app()->make(ProductRepository::class)->get($id);
  85. $title = $ret->store_name;
  86. break;
  87. case 1:
  88. $ret = app()->make(ProductRepository::class)->get($id);
  89. $title = $ret->store_name;
  90. break;
  91. case 2:
  92. $ret = app()->make(ProductPresellRepository::class)->get($id);
  93. $title = $ret->store_name;
  94. break;
  95. case 4:
  96. $ret = app()->make(ProductGroupRepository::class)->get($id);
  97. $title = $ret->product->store_name;
  98. break;
  99. case 30:
  100. $ret = app()->make(ProductAssistSetRepository::class)->get($id);
  101. if (!$ret) throw new ValidateException('数据不存在');
  102. $title = $ret->assist->store_name;
  103. break;
  104. case 40:
  105. $ret = app()->make(ProductGroupBuyingRepository::class)->get($id);
  106. if (!$ret) throw new ValidateException('数据不存在');
  107. $title = $ret->productGroup->product->store_name;
  108. break;
  109. default:
  110. return ;
  111. break;
  112. }
  113. return compact('title','id');
  114. }
  115. /**
  116. * 解析口令
  117. * @param string $key
  118. * @return array
  119. * @author Qinii
  120. * @day 9/8/21
  121. */
  122. public function getMassage(string $key)
  123. {
  124. $key = rtrim(ltrim($key));
  125. try{
  126. $com = explode('*/',$key)[0].'*/';
  127. $key = str_replace('/@','',$key);
  128. $keyArray = explode('/:',$key);
  129. $num = substr($keyArray[0],0,1);
  130. $idArray = explode('嗯',$keyArray[1]);
  131. $id = substr($idArray[0],-$num);
  132. $id = hexdec($id);
  133. $type_ = substr($keyArray[0],1);
  134. $uidArray = explode('*/',$idArray[1]);
  135. $uid = hexdec($uidArray[0]);
  136. $type = $this->productType[$type_];
  137. return compact('type','id','uid','com');
  138. } catch (\Exception $exception){
  139. return [];
  140. }
  141. }
  142. }