YqlData.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\api\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Env;
  6. use think\Request;
  7. use think\response\Json;
  8. /**
  9. * 176平台对接
  10. */
  11. class YqlData extends Controller
  12. {
  13. protected $api_url;
  14. protected $api_sign_key;
  15. public function __construct(Request $request = null)
  16. {
  17. parent::__construct($request);
  18. $this->api_url = Env::get("yql.api_url");
  19. $this->api_sign_key = Env::get("yql.api_sign_key");
  20. log_message('__construct.input:' . json_encode(input()), 'log', LOG_PATH . 'yql_log/');
  21. }
  22. /**
  23. * sign
  24. *
  25. * @param $data
  26. *
  27. * @return string
  28. */
  29. private function sign($data)
  30. {
  31. unset($data['sign']);
  32. $data['key'] = $this->api_sign_key;
  33. $string = '';
  34. foreach ($data as $k => $v) {
  35. if ($string) {
  36. $string .= '&' . $k . "=" . $v;
  37. } else {
  38. $string = $k . "=" . $v;
  39. }
  40. }
  41. // dump(md5($string), $string);
  42. return md5($string);
  43. }
  44. /**
  45. * 查询玩家账号列表 - 176请求
  46. *
  47. * @return Json
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public function getGameAccount()
  53. {
  54. $data = request()->only(['mobile', 'username', 'password', 'sign']);
  55. $validateResult = $this->validate($data, [
  56. 'mobile' => 'alphaNum', // 玩家绑定相关的手机号
  57. 'username' => 'alphaNum', // 玩家账号
  58. 'password' => 'alphaDash', // 玩家密码
  59. 'sign' => 'require', // sign
  60. ], [
  61. 'mobile' => 'mobile 数据有误!',
  62. 'username' => 'username 数据有误!',
  63. 'password' => 'password 数据有误!',
  64. 'sign.require' => '缺少必要参数: sign',
  65. ]);
  66. if ($validateResult !== true) {
  67. return json(["code" => 0, "message" => $validateResult, "data" => []]);
  68. }
  69. if (!empty($data['mobile']) && !preg_match("/^1[345789]\d{9}$/", $data['mobile'])) {
  70. return json(["code" => 0, "message" => "手机号有误!", "data" => []]);
  71. }
  72. if ($this->sign($data) != $data['sign']) {
  73. return json(["code" => 0, "message" => "签名有误!", "data" => []]);
  74. }
  75. $where = [];
  76. if (isset($data['username']) && isset($data['password'])) {
  77. $password = auth_code($data['password'], "ENCODE", Env::get('auth_key'));
  78. $info = model('Members')->where(['username' => $data['username'], 'password' => $password, 'flag' => 0])->find();
  79. if (!$info) {
  80. return json(["code" => 0, "message" => "账号不存在或者密码不正确!", "data" => []]);
  81. }
  82. $where['s.member_id'] = $info['id'];
  83. }
  84. if (isset($data['mobile'])) {
  85. $ids = model('Members')->where(['mobile' => $data['mobile'], 'flag' => 0])->column('id');
  86. $where['s.member_id'] = ['in', $ids];
  87. }
  88. if (empty($where)) {
  89. return json(["code" => 1, "message" => "数据为空!", "data" => []]);
  90. }
  91. $where['s.flag'] = 0;
  92. $list = model('Subaccount')->alias('s')
  93. ->join('cy_members m', 's.member_id = m.id')
  94. ->join('cy_game g', 's.game_id = g.id', 'LEFT')
  95. ->join('nw_channel c', 's.channel_id = c.id', 'LEFT')
  96. ->where($where)
  97. ->field("g.id as gid, m.username, c.name as channel_name")
  98. ->order("s.create_time desc")
  99. ->select();
  100. foreach($list as &$value){
  101. $value['gid'] = (string)$value['gid'];
  102. }
  103. return json(["code" => 1, "message" => "success", "data" => $list]);
  104. }
  105. /**
  106. * 代金券领取 - 176请求
  107. * - 领取时根据代金卷条件,没有则创建,同时绑定176的代金卷ID。 - 176 API - 提git 供
  108. *
  109. * @return Json
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. public function getCouponReceive()
  115. {
  116. $data = request()->only(['coupon_id', 'coupon_name', 'gid', 'max', 'min', 'username', 'sign']);
  117. $validateResult = $this->validate($data, [
  118. 'coupon_id' => 'require', // 176用户代金卷ID
  119. 'coupon_name' => 'require', // 名字
  120. 'gid' => 'require', // 指定游戏ID(0=不限)
  121. 'max' => 'require', // 满
  122. 'min' => 'require', // 减
  123. 'username' => 'require', // 用户名(祈盟唯一)
  124. 'sign' => 'require', // sign
  125. ], [
  126. 'coupon_id.require' => '缺少必要参数: coupon_id',
  127. 'coupon_name.require' => '缺少必要参数: coupon_name',
  128. 'max.require' => '缺少必要参数: max',
  129. 'min.require' => '缺少必要参数: min',
  130. 'gid.require' => '缺少必要参数: gid',
  131. 'username.require' => '缺少必要参数: username',
  132. 'sign.require' => '缺少必要参数: sign',
  133. ]);
  134. if ($validateResult !== true) {
  135. return json(["code" => 0, "message" => $validateResult, "data" => []]);
  136. }
  137. if ($this->sign($data) != $data['sign']) {
  138. return json(["code" => 0, "message" => "签名有误!", "data" => []]);
  139. }
  140. // 判断用户是否存在
  141. $memberInfo = model('Members')->where('username', $data['username'])->find();
  142. if (!$memberInfo) {
  143. return json(["code" => 0, "message" => "用户不存在!", "data" => []]);
  144. }
  145. // 判断游戏是否存在
  146. $gameInfo = model('Game')->where('id', $data['gid'])->find();
  147. if (!$gameInfo) {
  148. return json(["code" => 0, "message" => "游戏不存在!", "data" => []]);
  149. }
  150. $couponInfo = model('Common/Coupon')->where([
  151. "name" => $data['coupon_name'],
  152. "game_id" => $data['gid'],
  153. "money" => $data['min'],
  154. "min_money" => $data['max']
  155. ])->find();
  156. $couponDay = 7; // 代金卷有效期
  157. // 判断代金卷是否存在,没有则新建
  158. if (!$couponInfo) {
  159. $couponData = [
  160. "name" => $data['coupon_name'], // 名称
  161. "game_id" => $data['gid'], // 游戏id:0=全部游戏
  162. "money" => $data['min'], // 面值
  163. "min_money" => $data['max'], // 最低使用金额
  164. "type_id" => 2, // 类型:1=有效时间,2=领取后多少天内有效
  165. "day" => $couponDay, // 天数
  166. "start_time" => 0, // 有效开始时间
  167. "end_time" => 0, // 有效结束时间
  168. "state" => 1, // 状态:1=有效, 2=无效
  169. "is_examine" => 1, // 发放是否需要审核:1=不需要、2=需要
  170. "create_time" => time(),
  171. "update_time" => time(),
  172. "is_show" => 1, // sdk是否显示:1=不显示、2=显示
  173. "content" => '176 的代金卷', // 使用规则
  174. "is_yql" => 1, // 176代金卷
  175. ];
  176. $couponInfo = model('Common/Coupon')->create($couponData);
  177. if (!$couponInfo) {
  178. return json(["code" => 0, "message" => "代金卷创建失败!", "data" => []]);
  179. }
  180. }
  181. // 限制一个用户只能领取一个代金卷一次
  182. // $info = model('Common/CouponMember')->where(["coupon_id" => $couponInfo['id'], "yql_id" => $data['coupon_id']])->find();
  183. // if ($info) {
  184. // return json(["code" => 0, "message" => "请勿重复领取!", "data" => []]);
  185. // }
  186. // 发放代金卷
  187. $memberData = [
  188. "code" => makeUniqueid('YQL') . 'Q',
  189. "member_id" => $memberInfo['id'], // 用户id
  190. "coupon_id" => $couponInfo['id'], // 代金券id
  191. "examine" => 2, // 审核状态:1=未审核, 2=已审核
  192. "is_use" => 1, // 状态:1=未使用、2=使用中、3=已使用
  193. "state" => 1, // 有效状态:1=有效、2=冻结
  194. "start_time" => strtotime(date("Y-m-d H:i:s", time())), // 开始时间
  195. "end_time" => strtotime(date("Y-m-d 23:59:59", time() + (86400 * $couponDay-1))), // 到期时间
  196. "remarks" => "176 玩家的代金卷", // xxx
  197. "yql_id" => $data['coupon_id'], // 176用户代金卷ID
  198. ];
  199. $res = model('Common/CouponMember')->create($memberData);
  200. if ($res) {
  201. Db::table('cy_coupon')->where(['id' => $couponInfo['id']])->update([
  202. 'grant_num' => Db::raw('grant_num+1'),
  203. 'receive_num' => Db::raw('receive_num+1'),
  204. ]);
  205. return json(["code" => 1, "message" => "代金卷领取成功!", "data" => ['code' => $res['code'], "code_id" => $res['id']]]);
  206. }
  207. return json(["code" => 0, "message" => "代金卷领取失败!", "data" => []]);
  208. }
  209. /**
  210. * 代金卷使用通知 - 请求176 + 使用计数
  211. *
  212. * @param $coupon_member_id 祈盟平台的用户代金卷ID
  213. *
  214. * @return Json
  215. */
  216. public function notifyCoupon($coupon_member_id)
  217. {
  218. if (!$coupon_member_id) {
  219. return json(["code" => 0, "message" => "非法操作:代金卷不存在!", "data" => []]);
  220. }
  221. // 176 平台的用户代金卷ID
  222. $couponInfo = model('common/CouponMember')->where(['id' => $coupon_member_id])->field('coupon_id,yql_id')->find();
  223. if(empty($couponInfo['coupon_id'])){
  224. return json(["code" => 0, "message" => "代金卷有误!", "data" => []]);
  225. }
  226. Db::table('cy_coupon')->where(['id' => $couponInfo['coupon_id']])->setInc('use_num');
  227. if (empty($couponInfo['yql_id'])) {
  228. return json(["code" => 0, "message" => "代金卷不存在!", "data" => []]);
  229. }
  230. $data = ['id' => $couponInfo['yql_id']];
  231. $sign = $this->sign($data);
  232. $res = getHttpPost($this->api_url . '/serve/v1/notify/vouchers?id=' . $couponInfo['yql_id'] . '&sign=' . $sign);
  233. log_message('## notifyCoupon:' . $this->api_url . '/serve/v1/notify/vouchers?id=' . $couponInfo['yql_id'] . '&sign=' . $sign . ' - result: ' . $res, 'log', LOG_PATH . 'yql_log/');
  234. if ($res) {
  235. return json(["code" => 1, "message" => "通知成功!", "data" => []]);
  236. }
  237. return json(["code" => 0, "message" => "通知失败!", "data" => []]);
  238. }
  239. // 176通知祈盟渠道绑定接口 - 176请求
  240. public function notifyChannel()
  241. {
  242. $data = request()->only(['channel_id', 'uid', 'sign']);
  243. $validateResult = $this->validate($data, [
  244. 'channel_id' => 'require', // 祈盟SDK的渠道ID
  245. 'uid' => 'require', // 176的用户ID
  246. 'sign' => 'require', // sign
  247. ], [
  248. 'channel_id.require' => '缺少必要参数: channel_id',
  249. 'uid.require' => '缺少必要参数: uid',
  250. 'sign.require' => '缺少必要参数: sign',
  251. ]);
  252. if ($validateResult !== true) {
  253. return json(["code" => 0, "message" => $validateResult, "data" => []]);
  254. }
  255. if ($this->sign($data) != $data['sign']) {
  256. return json(["code" => 0, "message" => "签名有误!", "data" => []]);
  257. }
  258. $channelInfo = model('Channel')->where('id', $data['channel_id'])->find();
  259. if (!$channelInfo) {
  260. return json(["code" => 0, "message" => "渠道不存在!", "data" => []]);
  261. }
  262. $result = model('Channel')->where('id', $data['channel_id'])->update(['yql_id' => $data['uid']]);
  263. if ($result) {
  264. return json(["code" => 1, "message" => "success", "data" => []]);
  265. }
  266. return json(["code" => 0, "message" => "修改失败!", "data" => []]);
  267. }
  268. // 176渠道推广接口:根据渠道ID查询176的渠道推广数据。 - 聚合平台 - 接入
  269. public function getChannelData(){
  270. // TODO: 可以不用写,提供到推广地址,祈盟SDK根据 推广地址+渠道ID 去跳转。
  271. }
  272. }