Baidu.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Baidu.php UTF-8
  4. * 百度api
  5. *
  6. * @date : 2018/7/6 17:23
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : ouzhongfu <ozf@huosdk.com>
  10. * @version : HUOSDK 7.2
  11. */
  12. namespace huolib\promotion;
  13. use huolib\promotion\Base;
  14. use think\Config;
  15. use think\Db;
  16. use think\Log;
  17. class Baidu {
  18. protected $usertable = 'promotion_baidu_user';
  19. protected $platform_id = 1; /*1为百度*/
  20. public $baidu_conf
  21. = array(
  22. 'baidu_key' => '', /* key */
  23. );
  24. /**
  25. * 自定义错误处理
  26. *
  27. * @param string $msg
  28. * @param string $level
  29. *
  30. */
  31. private function _error($msg, $level = 'error') {
  32. $_info = 'promotion\Baidu Error:'.$msg;
  33. Log::record($_info, $level);
  34. }
  35. public function __construct() {
  36. }
  37. /**验证是否配置
  38. *
  39. * @param int $app_id
  40. * @param int $agent_id
  41. *
  42. * @return bool
  43. * @internal param array $params
  44. *
  45. */
  46. public function check($app_id = 0, $agent_id = 0) {
  47. if (empty($app_id) || empty($agent_id)) {
  48. return false;
  49. }
  50. $_map['app_id'] = $app_id;
  51. $_map['agent_id'] = $agent_id;
  52. /*投放平台验证*/
  53. $_check_platform = $this->checkPlatform($app_id, $agent_id);
  54. if (!$_check_platform) {
  55. return false;
  56. }
  57. $_key = Db::name('promotion_detail')->where($_map)->value('toutiao_key');
  58. if (empty($_key)) {
  59. return false;
  60. }
  61. /*数据库表检测*/
  62. $_rs = $this->checkUserTable();
  63. if (!$_rs) {
  64. return false;
  65. }
  66. $this->baidu_conf['baidu_key'] = $_key;
  67. return true;
  68. }
  69. /**投放平台验证
  70. *
  71. * @param $app_id
  72. * @param $agent_id
  73. *
  74. * @return bool
  75. */
  76. public function checkPlatform($app_id, $agent_id) {
  77. $_map['app_id'] = $app_id;
  78. $_map['agent_id'] = $agent_id;
  79. $_plan_id = Db::name('promotion_detail')->where($_map)->value('promotion_plan_id');
  80. $_pp_map['id'] = $_plan_id;
  81. $_platform_id = Db::name('promotion_plan')->where($_pp_map)->value('platform_id');
  82. if ($this->platform_id == $_platform_id) {
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. }
  88. /**
  89. * 验证
  90. *
  91. * @param array $params
  92. * @param string $type
  93. *
  94. * @return bool|int|string
  95. */
  96. public function varify($params = array(), $type = '') {
  97. if (empty($params)) {
  98. \think\Log::write(array('百度信息流记录:验证参数空', $params), 'error');
  99. return false;
  100. }
  101. if (empty($type)) {
  102. \think\Log::write(array('百度信息流记录:type为空', $params), 'error');
  103. return false;
  104. }
  105. //$_check_map["agent_id"] = $params['agent_id'];
  106. $_check_map["app_id"] = $params['app_id'];
  107. if (!empty($params['from']) && Base::FROM_ANDROID == $params['from']) {
  108. $_check_map["imei_md5"] = md5($params['device_id']);
  109. } else {
  110. $_check_map["idfa"] = $params['idfa'];
  111. }
  112. /*是否推广白名单*/
  113. $_is_while = $this->isWhile($params['from'], $params['device_id'], $params['idfa']);
  114. if ($_is_while) {
  115. $_check_map["is_test"] = 1;
  116. } else {
  117. $_check_map["is_test"] = 2;
  118. }
  119. $_check_data = DB::name($this->usertable)->where($_check_map)->order('id desc')->find();
  120. if (empty($_check_data)) {
  121. \think\Log::write(array('百度信息流记录:不是通过百度信息流过来数据', $_check_map), 'error');
  122. return false;
  123. }
  124. if (2 == $_check_data['status'] && !$_is_while) {
  125. \think\Log::write(array('百度信息流记录:设备已存在', $_check_map), 'error');
  126. return false;
  127. }
  128. $_base_url = $_check_data['callback_url'];
  129. $_a_value = !empty($params['amount']) ? $params['amount'] : 0;
  130. $_base_url = str_replace("{{ATYPE}}", $type, $_base_url);
  131. $_check_url = str_replace("{{AVALUE}}", $_a_value, $_base_url);
  132. $_md5_string = $_check_url.$this->baidu_conf['baidu_key'];
  133. $_signature = md5($_md5_string);
  134. $_vare_url = $_check_url."&sign=".$_signature;
  135. $_result = file_get_contents($_vare_url);
  136. $_result = json_decode($_result, true);
  137. if (0 == $_result["error_code"]) {
  138. $_set_data['status'] = 2;
  139. $_set_data['update_time'] = time();
  140. $_rs = DB::name($this->usertable)->where($_check_map)->update($_set_data);
  141. /*下载统计记录表c_promotion_day*/
  142. if ($_rs) {
  143. $_base_class = new Base();
  144. $_re = $_base_class->entDownLog($params['agent_id'], $params['app_id']);
  145. if ($_re) {
  146. return $_re;
  147. }
  148. } else {
  149. return $_rs;
  150. }
  151. } else {
  152. \think\Log::write(array('百度信息流记录:回传信息error_code不为0', $_result), 'error');
  153. return false;
  154. }
  155. return true;
  156. }
  157. /**检查是否为调试设备
  158. *
  159. * @param $from 来源 3为安卓,4为ios
  160. * @param $device_id
  161. * @param $idfa
  162. * @param bool|int $is_md5 是否已经md5
  163. *
  164. * @return bool
  165. */
  166. public function isWhile($from, $device_id, $idfa, $is_md5 = false) {
  167. if (!empty($from) && Base::FROM_ANDROID == $from) {
  168. if ($is_md5) {
  169. $_device_id = $device_id;
  170. } else {
  171. $_device_id = md5($device_id);
  172. }
  173. } else {
  174. $_device_id = $idfa;
  175. }
  176. if (empty($_device_id)) {
  177. return false;
  178. }
  179. $_map['platform_id'] = $this->platform_id;
  180. $_map['is_delete'] = 2;
  181. $_map['status'] = 2;
  182. $_lists = DB::name("promotion_whitelist")->where($_map)->column('device_id');
  183. if (!empty($_lists) && in_array($_device_id, $_lists)) {
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. /**
  190. * 激活
  191. *
  192. * @param array $params 请求参数
  193. *
  194. * @return bool
  195. */
  196. public function install($params = array()) {
  197. \think\Log::write(array('百度信息流记录:kaishi', $params), 'error');
  198. /*1 查看参数是否配置*/
  199. $_rs = $this->check($params['app_id'], $params['agent_id']);
  200. if (false === $_rs) {
  201. \think\Log::write(array('百度信息流记录:未配置参数', $params), 'error');
  202. return false;
  203. }
  204. /*2 是否本地有记录及回调验证*/
  205. $_type = 'activate'; /*wake_up:唤醒;download:下载;activate:激活;register:注册;new_customers:新客;orders:成单*/
  206. $_check = $this->varify($params, $_type);
  207. return $_check;
  208. }
  209. /**注册
  210. *
  211. * @param array $params
  212. *
  213. * @return bool
  214. */
  215. public function register($params = array()) {
  216. /*1 查看参数是否配置*/
  217. $_rs = $this->check($params);
  218. if (false === $_rs) {
  219. return false;
  220. }
  221. /*2 是否本地有记录及回调验证*/
  222. $_type = 1; /*0为激活,1为注册,2为充值*/
  223. $_check = $this->varify($params, $_type);
  224. return $_check;
  225. }
  226. /**充值
  227. *
  228. * @param array $params
  229. *
  230. * @return bool
  231. */
  232. public function payment($params = array()) {
  233. /*1 查看参数是否配置*/
  234. $_rs = $this->check($params);
  235. if (false === $_rs) {
  236. return false;
  237. }
  238. /*2 是否本地有记录及回调验证*/
  239. $_type = 2; /*0为激活,1为注册,2为充值*/
  240. $_check = $this->varify($params, $_type);
  241. return $_check;
  242. }
  243. /**
  244. * @return bool
  245. */
  246. private function checkUserTable() {
  247. $_table_name = Config::get('database.prefix').$this->usertable;
  248. $_sql = '';
  249. $_sql .= "CREATE TABLE IF NOT EXISTS `".$_table_name."` (";
  250. $_sql .= " `id` int(11) NOT NULL AUTO_INCREMENT,";
  251. $_sql .= " `app_id` INT(11) NOT NULL DEFAULT 0 COMMENT '游戏id',";
  252. $_sql .= " `agent_id` INT(11) NOT NULL DEFAULT 0 COMMENT '子渠道id',";
  253. $_sql .= " `imei_md5` VARCHAR(64) NOT NULL COMMENT '用户终端的IMEI码md5后的值',";
  254. $_sql .= " `idfa` VARCHAR(64) NOT NULL COMMENT 'iOS 手机广告唯一标识',";
  255. $_sql .= " `os` VARCHAR(10) NOT NULL DEFAULT '' COMMENT '客户端操作系统的类型,1–iOS;2–Android',";
  256. $_sql .= " `callback_url` VARCHAR(1000) NOT NULL DEFAULT '' COMMENT '回调地址',";
  257. $_sql .= " `create_time` INT(11) NOT NULL DEFAULT 0 COMMENT '创建时间戳',";
  258. $_sql .= " `update_time` INT(11) NOT NULL DEFAULT 0 COMMENT '更新时间戳',";
  259. $_sql .= " `status` TINYINT(2) NOT NULL DEFAULT 1 COMMENT '状态,1为待激活,2为已激活',";
  260. $_sql .= " PRIMARY KEY (`id`)";
  261. $_sql .= " ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='百度点击用户';";
  262. try {
  263. $_rs = db()->execute($_sql);
  264. if (false === $_rs) {
  265. Log::write("baidu_log create table ".$_table_name." failed1", 'error');
  266. return false;
  267. }
  268. return true;
  269. } catch (Exception $_e) {
  270. Log::write("baidu_log create table ".$_table_name." failed2", 'error');
  271. return false;
  272. }
  273. }
  274. }