ByteDanceOrderPush.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\service\byte_dance;
  3. use app\common\model\Mobile;
  4. use app\common\model\MobileOrder;
  5. use app\common\model\Payment;
  6. use app\common\model\User;
  7. class ByteDanceOrderPush implements ByteDanceInterface {
  8. /** @var MobileOrder */
  9. protected $order;
  10. /** @var Payment */
  11. protected $payment;
  12. /**
  13. * @param MobileOrder $order
  14. */
  15. public function setOrder(MobileOrder $order): void
  16. {
  17. $this->order = $order;
  18. }
  19. /**
  20. * @param Payment $payment
  21. */
  22. public function setPayment($payment): void
  23. {
  24. if(!$payment){
  25. $payment=$this->order->payment;
  26. }
  27. if(!$payment){
  28. $payment=$this->order->payment()->order('id','desc')->find();
  29. }
  30. $this->payment = $payment;
  31. }
  32. public function get()
  33. {
  34. $openid=$this->getOpenid();
  35. if(!$openid){
  36. return false;
  37. }
  38. if(!$this->payment){
  39. return false;
  40. }
  41. $params=[
  42. 'access_token'=>(new ByteDanceAccessToken())->get(),
  43. 'app_name'=>'douyin',
  44. 'open_id'=>$openid,
  45. 'order_detail'=>json_encode([
  46. 'order_id'=>$this->payment['order_no'],
  47. 'create_time'=>$this->order['create_time']*1000,
  48. 'status'=>$this->order->getOrderStatusText(),
  49. 'amount'=>1,
  50. 'total_price'=>$this->order['amount']*100,
  51. //'detail_url'=>"pages/payment/payment?id={$this->order->id}",
  52. 'detail_url'=>"pages/index/index",
  53. 'item_list'=>[
  54. [
  55. 'item_code'=>(string)$this->order['mobile_id'],
  56. 'img'=>$this->getImg(),
  57. 'title'=>$this->getTitle(),
  58. 'price'=>$this->order['amount']*100,
  59. ]
  60. ]
  61. ],256),
  62. 'order_status'=>$this->getOrderStatus(),
  63. 'order_type'=>0,
  64. 'update_time'=>time()*1000,
  65. ];
  66. try {
  67. $data=ByteDance::httpPost('https://developer.toutiao.com/api/apps/order/v2/push',$params,[],false);
  68. if($data['err_code']!=0){
  69. unset($params['access_token']);
  70. throw_user($data['err_msg'],$params);
  71. }
  72. }catch (\Exception $e){
  73. user_log('ByteDanceOrderPush',[
  74. 'e'=>$e->getMessage(),
  75. 'file'=>$e->getFile(),
  76. 'line'=>$e->getLine(),
  77. ]);
  78. }
  79. }
  80. public function getWangka()
  81. {
  82. $openid=$this->getOpenid();
  83. if(!$openid){
  84. return false;
  85. }
  86. if(!$this->payment){
  87. return false;
  88. }
  89. $params=[
  90. 'access_token'=>(new ByteDanceAccessToken())->getWangKa(),
  91. 'app_name'=>'douyin',
  92. 'open_id'=>$openid,
  93. 'order_detail'=>json_encode([
  94. 'order_id'=>$this->payment['order_no'],
  95. 'create_time'=>$this->order['create_time']*1000,
  96. 'status'=>$this->order->getOrderStatusText(),
  97. 'amount'=>1,
  98. 'total_price'=>$this->order['amount']*100,
  99. //'detail_url'=>"pages/payment/payment?id={$this->order->id}",
  100. 'detail_url'=>"pages/index/index",
  101. 'item_list'=>[
  102. [
  103. 'item_code'=>(string)$this->order['mobile_id'],
  104. 'img'=>$this->getImg(),
  105. 'title'=>$this->getTitle(),
  106. 'price'=>$this->order['amount']*100,
  107. ]
  108. ]
  109. ],256),
  110. 'order_status'=>$this->getOrderStatus(),
  111. 'order_type'=>0,
  112. 'update_time'=>time()*1000,
  113. ];
  114. try {
  115. $data=ByteDance::httpPost('https://developer.toutiao.com/api/apps/order/v2/push',$params,[],false);
  116. if($data['err_code']!=0){
  117. unset($params['access_token']);
  118. throw_user($data['err_msg'],$params);
  119. }
  120. }catch (\Exception $e){
  121. user_log('ByteDanceOrderPush',[
  122. 'e'=>$e->getMessage(),
  123. 'file'=>$e->getFile(),
  124. 'line'=>$e->getLine(),
  125. ]);
  126. }
  127. }
  128. public function getHaoYuan()
  129. {
  130. $openid=$this->getOpenid();
  131. if(!$openid){
  132. return false;
  133. }
  134. if(!$this->payment){
  135. return false;
  136. }
  137. $params=[
  138. 'access_token'=>(new ByteDanceAccessToken())->getHaoYuan(),
  139. 'app_name'=>'douyin',
  140. 'open_id'=>$openid,
  141. 'order_detail'=>json_encode([
  142. 'order_id'=>$this->payment['order_no'],
  143. 'create_time'=>$this->order['create_time']*1000,
  144. 'status'=>$this->order->getOrderStatusText(),
  145. 'amount'=>1,
  146. 'total_price'=>$this->order['amount']*100,
  147. //'detail_url'=>"pages/payment/payment?id={$this->order->id}",
  148. 'detail_url'=>"pages/index/index",
  149. 'item_list'=>[
  150. [
  151. 'item_code'=>(string)$this->order['mobile_id'],
  152. 'img'=>$this->getImg(),
  153. 'title'=>$this->getTitle(),
  154. 'price'=>$this->order['amount']*100,
  155. ]
  156. ]
  157. ],256),
  158. 'order_status'=>$this->getOrderStatus(),
  159. 'order_type'=>0,
  160. 'update_time'=>time()*1000,
  161. ];
  162. try {
  163. $data=ByteDance::httpPost('https://developer.toutiao.com/api/apps/order/v2/push',$params,[],false);
  164. if($data['err_code']!=0){
  165. unset($params['access_token']);
  166. throw_user($data['err_msg'],$params);
  167. }
  168. }catch (\Exception $e){
  169. user_log('ByteDanceOrderPush',[
  170. 'e'=>$e->getMessage(),
  171. 'file'=>$e->getFile(),
  172. 'line'=>$e->getLine(),
  173. ]);
  174. }
  175. }
  176. protected function getOpenid(){
  177. $user_id=$this->order['user_id'];
  178. if($user_id){
  179. return User::where('id',$user_id)->value('openid');
  180. }
  181. return false;
  182. }
  183. protected function getImg(){
  184. if($this->order['type']==Mobile::FLOW){
  185. $info=$this->order->info;
  186. return $info['mobile']['logo'];
  187. }
  188. return request()->domain().'/assets/img/20220917163638.png';
  189. }
  190. protected function getTitle(){
  191. if($this->order['type']==Mobile::FLOW){
  192. return $this->order['brand'].'流量卡';
  193. }
  194. //return $this->order['brand']."【{$this->order['no']}】";
  195. return "靓号【{$this->order['no']}】";
  196. }
  197. protected function getOrderStatus(){
  198. if($this->order['status']==0){
  199. return 0;
  200. }elseif ($this->order['status']==50){
  201. return 2;
  202. }else{
  203. return 1;
  204. }
  205. }
  206. }