Uc.php 9.2 KB

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