123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace app\service\byte_dance;
- use app\common\model\Mobile;
- use app\common\model\MobileOrder;
- use app\common\model\Payment;
- use app\common\model\User;
- class ByteDanceOrderPush implements ByteDanceInterface {
- /** @var MobileOrder */
- protected $order;
- /** @var Payment */
- protected $payment;
- /**
- * @param MobileOrder $order
- */
- public function setOrder(MobileOrder $order): void
- {
- $this->order = $order;
- }
- /**
- * @param Payment $payment
- */
- public function setPayment($payment): void
- {
- if(!$payment){
- $payment=$this->order->payment;
- }
- if(!$payment){
- $payment=$this->order->payment()->order('id','desc')->find();
- }
- $this->payment = $payment;
- }
- public function get()
- {
- $openid=$this->getOpenid();
- if(!$openid){
- return false;
- }
- if(!$this->payment){
- return false;
- }
- $params=[
- 'access_token'=>(new ByteDanceAccessToken())->get(),
- 'app_name'=>'douyin',
- 'open_id'=>$openid,
- 'order_detail'=>json_encode([
- 'order_id'=>$this->payment['order_no'],
- 'create_time'=>$this->order['create_time']*1000,
- 'status'=>$this->order->getOrderStatusText(),
- 'amount'=>1,
- 'total_price'=>$this->order['amount']*100,
- //'detail_url'=>"pages/payment/payment?id={$this->order->id}",
- 'detail_url'=>"pages/index/index",
- 'item_list'=>[
- [
- 'item_code'=>(string)$this->order['mobile_id'],
- 'img'=>$this->getImg(),
- 'title'=>$this->getTitle(),
- 'price'=>$this->order['amount']*100,
- ]
- ]
- ],256),
- 'order_status'=>$this->getOrderStatus(),
- 'order_type'=>0,
- 'update_time'=>time()*1000,
- ];
- try {
- $data=ByteDance::httpPost('https://developer.toutiao.com/api/apps/order/v2/push',$params,[],false);
- if($data['err_code']!=0){
- unset($params['access_token']);
- throw_user($data['err_msg'],$params);
- }
- }catch (\Exception $e){
- user_log('ByteDanceOrderPush',[
- 'e'=>$e->getMessage(),
- 'file'=>$e->getFile(),
- 'line'=>$e->getLine(),
- ]);
- }
- }
- public function getWangka()
- {
- $openid=$this->getOpenid();
- if(!$openid){
- return false;
- }
- if(!$this->payment){
- return false;
- }
- $params=[
- 'access_token'=>(new ByteDanceAccessToken())->getWangKa(),
- 'app_name'=>'douyin',
- 'open_id'=>$openid,
- 'order_detail'=>json_encode([
- 'order_id'=>$this->payment['order_no'],
- 'create_time'=>$this->order['create_time']*1000,
- 'status'=>$this->order->getOrderStatusText(),
- 'amount'=>1,
- 'total_price'=>$this->order['amount']*100,
- //'detail_url'=>"pages/payment/payment?id={$this->order->id}",
- 'detail_url'=>"pages/index/index",
- 'item_list'=>[
- [
- 'item_code'=>(string)$this->order['mobile_id'],
- 'img'=>$this->getImg(),
- 'title'=>$this->getTitle(),
- 'price'=>$this->order['amount']*100,
- ]
- ]
- ],256),
- 'order_status'=>$this->getOrderStatus(),
- 'order_type'=>0,
- 'update_time'=>time()*1000,
- ];
- try {
- $data=ByteDance::httpPost('https://developer.toutiao.com/api/apps/order/v2/push',$params,[],false);
- if($data['err_code']!=0){
- unset($params['access_token']);
- throw_user($data['err_msg'],$params);
- }
- }catch (\Exception $e){
- user_log('ByteDanceOrderPush',[
- 'e'=>$e->getMessage(),
- 'file'=>$e->getFile(),
- 'line'=>$e->getLine(),
- ]);
- }
- }
- public function getHaoYuan()
- {
- $openid=$this->getOpenid();
- if(!$openid){
- return false;
- }
- if(!$this->payment){
- return false;
- }
- $params=[
- 'access_token'=>(new ByteDanceAccessToken())->getHaoYuan(),
- 'app_name'=>'douyin',
- 'open_id'=>$openid,
- 'order_detail'=>json_encode([
- 'order_id'=>$this->payment['order_no'],
- 'create_time'=>$this->order['create_time']*1000,
- 'status'=>$this->order->getOrderStatusText(),
- 'amount'=>1,
- 'total_price'=>$this->order['amount']*100,
- //'detail_url'=>"pages/payment/payment?id={$this->order->id}",
- 'detail_url'=>"pages/index/index",
- 'item_list'=>[
- [
- 'item_code'=>(string)$this->order['mobile_id'],
- 'img'=>$this->getImg(),
- 'title'=>$this->getTitle(),
- 'price'=>$this->order['amount']*100,
- ]
- ]
- ],256),
- 'order_status'=>$this->getOrderStatus(),
- 'order_type'=>0,
- 'update_time'=>time()*1000,
- ];
- try {
- $data=ByteDance::httpPost('https://developer.toutiao.com/api/apps/order/v2/push',$params,[],false);
- if($data['err_code']!=0){
- unset($params['access_token']);
- throw_user($data['err_msg'],$params);
- }
- }catch (\Exception $e){
- user_log('ByteDanceOrderPush',[
- 'e'=>$e->getMessage(),
- 'file'=>$e->getFile(),
- 'line'=>$e->getLine(),
- ]);
- }
- }
- protected function getOpenid(){
- $user_id=$this->order['user_id'];
- if($user_id){
- return User::where('id',$user_id)->value('openid');
- }
- return false;
- }
- protected function getImg(){
- if($this->order['type']==Mobile::FLOW){
- $info=$this->order->info;
- return $info['mobile']['logo'];
- }
- return request()->domain().'/assets/img/20220917163638.png';
- }
- protected function getTitle(){
- if($this->order['type']==Mobile::FLOW){
- return $this->order['brand'].'流量卡';
- }
- //return $this->order['brand']."【{$this->order['no']}】";
- return "靓号【{$this->order['no']}】";
- }
- protected function getOrderStatus(){
- if($this->order['status']==0){
- return 0;
- }elseif ($this->order['status']==50){
- return 2;
- }else{
- return 1;
- }
- }
- }
|