Toutiao.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Toutiao.php UTF-8
  4. * 头条api
  5. *
  6. * @date : 2018/6/21 14:29
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : ouzhongfu <ozf@huosdk.com>
  10. * @version : HUOSDK 7.2
  11. */
  12. namespace huolib\promotion;
  13. use think\Db;
  14. use think\Log;
  15. class Toutiao {
  16. protected $platform_id = 4; /*4为今日头条*/
  17. public $toutiao_conf
  18. = array(
  19. 'toutiao_key' => '', /* 今日头条key */
  20. );
  21. /**
  22. * 自定义错误处理
  23. *
  24. * @param string $msg
  25. * @param string $level
  26. *
  27. */
  28. private function _error($msg, $level = 'error') {
  29. $_info = 'toutiao\Api Error:'.$msg;
  30. Log::record($_info, $level);
  31. }
  32. public function __construct() {
  33. }
  34. /**验证是否配置
  35. *
  36. * @param array $params
  37. *
  38. * @return bool
  39. */
  40. public function check($params = array()) {
  41. $_app_id = isset($params['app_id']) ? $params['app_id'] : 0;
  42. $_agent_id = isset($params['agent_id']) ? $params['agent_id'] : 0;
  43. if (empty($_app_id) || empty($_agent_id)) {
  44. return false;
  45. }
  46. $_map['app_id'] = $_app_id;
  47. $_map['agent_id'] = $_agent_id;
  48. /*投放平台验证*/
  49. $_check_platform = $this->checkPlatform($_app_id, $_agent_id);
  50. if (!$_check_platform) {
  51. return false;
  52. }
  53. $_toutiao_key = Db::name('promotion_detail')->where($_map)->value('toutiao_key');
  54. if (empty($_toutiao_key)) {
  55. return false;
  56. }
  57. $this->toutiao_conf['toutiao_key'] = $_toutiao_key;
  58. return true;
  59. }
  60. /**投放平台验证
  61. *
  62. * @param $app_id
  63. * @param $agent_id
  64. *
  65. * @return bool
  66. */
  67. public function checkPlatform($app_id, $agent_id) {
  68. $_map['app_id'] = $app_id;
  69. $_map['agent_id'] = $agent_id;
  70. $_plan_id = Db::name('promotion_detail')->where($_map)->value('promotion_plan_id');
  71. $_pp_map['id'] = $_plan_id;
  72. $_platform_id = Db::name('promotion_plan')->where($_pp_map)->value('platform_id');
  73. if ($this->platform_id == $_platform_id) {
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * 验证
  81. *
  82. * @param array $params
  83. * @param int $type_id
  84. *
  85. * @return bool|int|string
  86. */
  87. public function varify($params = array(), $type_id = 0) {
  88. if (empty($params)) {
  89. \think\Log::write(array('头条记录:验证参数空', $params), 'error');
  90. return false;
  91. }
  92. //$_check_map["agent_id"] = $params['agent_id'];
  93. $_check_map["app_id"] = $params['app_id'];
  94. if (!empty($params['from']) && \huolib\promotion\Base::FROM_ANDROID == $params['from']) {
  95. $_check_map["imei"] = md5($params['device_id']);
  96. } else {
  97. $_check_map["idfa"] = $params['idfa'];
  98. }
  99. /*是否推广白名单*/
  100. $_is_while = $this->isWhile($params['from'], $params['device_id'], $params['idfa']);
  101. if ($_is_while) {
  102. $_check_map["is_test"] = 1;
  103. } else {
  104. $_check_map["is_test"] = 2;
  105. }
  106. $_check_data = DB::name('promotion_toutiao_user')->where($_check_map)->order("id desc")->find();
  107. if (empty($_check_data)) {
  108. \think\Log::write(array('头条记录:不是通过头条过来数据', $_check_map), 'error');
  109. return false;
  110. }
  111. if (2 == $_check_data['status'] && !$_is_while) {
  112. \think\Log::write(array('头条记录:设备已存在', $_check_map), 'error');
  113. return false;
  114. }
  115. $_base_url = $_check_data['callback_url'];
  116. $_check_url = $_base_url."&event_type=".$type_id;
  117. $_str = hash_hmac("sha1", $_check_url, $this->toutiao_conf['toutiao_key']);
  118. $_signature = $this->base64urlEncode($_str);
  119. $_vare_url = $_check_url."&signature=".$_signature;
  120. $_result = file_get_contents($_vare_url);
  121. $_result = json_decode($_result, true);
  122. if (0 == $_result["ret"]) {
  123. $_set_data['status'] = 2;
  124. $_set_data['update_time'] = time();
  125. $_rs = DB::name('promotion_toutiao_user')->where($_check_map)->update($_set_data);
  126. if ($_rs) {
  127. $_base_class = new \huolib\promotion\Base();
  128. return $_base_class->entDownLog($params['agent_id'], $params['app_id']);
  129. }
  130. } else {
  131. \think\Log::write(array('头条记录:回传信息ret不为0', $_result), 'error');
  132. return false;
  133. }
  134. }
  135. /**检查是否为调试设备
  136. *
  137. * @param $from 来源 3为安卓,4为ios
  138. * @param $device_id
  139. * @param $idfa
  140. * @param int $is_md5 是否已经md5
  141. *
  142. * @return bool
  143. */
  144. public function isWhile($from, $device_id, $idfa, $is_md5 = false) {
  145. if (!empty($from) && \huolib\promotion\Base::FROM_ANDROID == $from) {
  146. if ($is_md5) {
  147. $_device_id = $device_id;
  148. } else {
  149. $_device_id = md5($device_id);
  150. }
  151. } else {
  152. $_device_id = $idfa;
  153. }
  154. $_map['platform_id'] = $this->platform_id;
  155. $_map['is_delete'] = 2;
  156. $_map['status'] = 2;
  157. $_lists = DB::name("promotion_whitelist")->where($_map)->column('device_id');
  158. if (!empty($_lists) && in_array($_device_id, $_lists)) {
  159. return true;
  160. } else {
  161. return false;
  162. }
  163. }
  164. public function base64urlEncode($data) {
  165. return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  166. }
  167. /**
  168. * 激活
  169. *
  170. * @param array $params 请求参数
  171. *
  172. * @return bool
  173. */
  174. public function install($params = array()) {
  175. /*1 查看参数是否配置*/
  176. $_rs = $this->check($params);
  177. if (false === $_rs) {
  178. return false;
  179. }
  180. /*2 是否本地有记录及回调验证*/
  181. $_type = 0; /*0为激活,1为注册,2为充值*/
  182. $_check = $this->varify($params, $_type);
  183. return $_check;
  184. }
  185. /**注册
  186. *
  187. * @param array $params
  188. *
  189. * @return bool
  190. */
  191. public function register($params = array()) {
  192. /*1 查看参数是否配置*/
  193. $_rs = $this->check($params);
  194. if (false === $_rs) {
  195. return false;
  196. }
  197. /*2 是否本地有记录及回调验证*/
  198. $_type = 1; /*0为激活,1为注册,2为充值*/
  199. $_check = $this->varify($params, $_type);
  200. return $_check;
  201. }
  202. /**充值
  203. *
  204. * @param array $params
  205. *
  206. * @return bool
  207. */
  208. public function payment($params = array()) {
  209. /*1 查看参数是否配置*/
  210. $_rs = $this->check($params);
  211. if (false === $_rs) {
  212. return false;
  213. }
  214. /*2 是否本地有记录及回调验证*/
  215. $_type = 2; /*0为激活,1为注册,2为充值*/
  216. $_check = $this->varify($params, $_type);
  217. return $_check;
  218. }
  219. }