| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace app\api\controller;
- use think\Controller;
- use think\Db;
- use think\Env;
- use think\Request;
- use think\response\Json;
- /**
- * 176平台对接
- */
- class YqlData extends Controller
- {
- protected $api_url;
- protected $api_sign_key;
- public function __construct(Request $request = null)
- {
- parent::__construct($request);
- $this->api_url = Env::get("yql.api_url");
- $this->api_sign_key = Env::get("yql.api_sign_key");
- log_message('__construct.input:' . json_encode(input()), 'log', LOG_PATH . 'yql_log/');
- }
- /**
- * sign
- *
- * @param $data
- *
- * @return string
- */
- private function sign($data)
- {
- unset($data['sign']);
- $data['key'] = $this->api_sign_key;
- $string = '';
- foreach ($data as $k => $v) {
- if ($string) {
- $string .= '&' . $k . "=" . $v;
- } else {
- $string = $k . "=" . $v;
- }
- }
- // dump(md5($string), $string);
- return md5($string);
- }
- /**
- * 查询玩家账号列表 - 176请求
- *
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGameAccount()
- {
- $data = request()->only(['mobile', 'username', 'password', 'sign']);
- $validateResult = $this->validate($data, [
- 'mobile' => 'alphaNum', // 玩家绑定相关的手机号
- 'username' => 'alphaNum', // 玩家账号
- 'password' => 'alphaDash', // 玩家密码
- 'sign' => 'require', // sign
- ], [
- 'mobile' => 'mobile 数据有误!',
- 'username' => 'username 数据有误!',
- 'password' => 'password 数据有误!',
- 'sign.require' => '缺少必要参数: sign',
- ]);
- if ($validateResult !== true) {
- return json(["code" => 0, "message" => $validateResult, "data" => []]);
- }
- if (!empty($data['mobile']) && !preg_match("/^1[345789]\d{9}$/", $data['mobile'])) {
- return json(["code" => 0, "message" => "手机号有误!", "data" => []]);
- }
- if ($this->sign($data) != $data['sign']) {
- return json(["code" => 0, "message" => "签名有误!", "data" => []]);
- }
- $where = [];
- if (isset($data['username']) && isset($data['password'])) {
- $password = auth_code($data['password'], "ENCODE", Env::get('auth_key'));
- $info = model('Members')->where(['username' => $data['username'], 'password' => $password, 'flag' => 0])->find();
- if (!$info) {
- return json(["code" => 0, "message" => "账号不存在或者密码不正确!", "data" => []]);
- }
- $where['s.member_id'] = $info['id'];
- }
- if (isset($data['mobile'])) {
- $ids = model('Members')->where(['mobile' => $data['mobile'], 'flag' => 0])->column('id');
- $where['s.member_id'] = ['in', $ids];
- }
- if (empty($where)) {
- return json(["code" => 1, "message" => "数据为空!", "data" => []]);
- }
- $where['s.flag'] = 0;
- $list = model('Subaccount')->alias('s')
- ->join('cy_members m', 's.member_id = m.id')
- ->join('cy_game g', 's.game_id = g.id', 'LEFT')
- ->join('nw_channel c', 's.channel_id = c.id', 'LEFT')
- ->where($where)
- ->field("g.id as gid, m.username, c.name as channel_name")
- ->order("s.create_time desc")
- ->select();
- foreach($list as &$value){
- $value['gid'] = (string)$value['gid'];
- }
- return json(["code" => 1, "message" => "success", "data" => $list]);
- }
- /**
- * 代金券领取 - 176请求
- * - 领取时根据代金卷条件,没有则创建,同时绑定176的代金卷ID。 - 176 API - 提git 供
- *
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getCouponReceive()
- {
- $data = request()->only(['coupon_id', 'coupon_name', 'gid', 'max', 'min', 'username', 'sign']);
- $validateResult = $this->validate($data, [
- 'coupon_id' => 'require', // 176用户代金卷ID
- 'coupon_name' => 'require', // 名字
- 'gid' => 'require', // 指定游戏ID(0=不限)
- 'max' => 'require', // 满
- 'min' => 'require', // 减
- 'username' => 'require', // 用户名(祈盟唯一)
- 'sign' => 'require', // sign
- ], [
- 'coupon_id.require' => '缺少必要参数: coupon_id',
- 'coupon_name.require' => '缺少必要参数: coupon_name',
- 'max.require' => '缺少必要参数: max',
- 'min.require' => '缺少必要参数: min',
- 'gid.require' => '缺少必要参数: gid',
- 'username.require' => '缺少必要参数: username',
- 'sign.require' => '缺少必要参数: sign',
- ]);
- if ($validateResult !== true) {
- return json(["code" => 0, "message" => $validateResult, "data" => []]);
- }
- if ($this->sign($data) != $data['sign']) {
- return json(["code" => 0, "message" => "签名有误!", "data" => []]);
- }
- // 判断用户是否存在
- $memberInfo = model('Members')->where('username', $data['username'])->find();
- if (!$memberInfo) {
- return json(["code" => 0, "message" => "用户不存在!", "data" => []]);
- }
- // 判断游戏是否存在
- $gameInfo = model('Game')->where('id', $data['gid'])->find();
- if (!$gameInfo) {
- return json(["code" => 0, "message" => "游戏不存在!", "data" => []]);
- }
- $couponInfo = model('Common/Coupon')->where([
- "name" => $data['coupon_name'],
- "game_id" => $data['gid'],
- "money" => $data['min'],
- "min_money" => $data['max']
- ])->find();
- $couponDay = 7; // 代金卷有效期
- // 判断代金卷是否存在,没有则新建
- if (!$couponInfo) {
- $couponData = [
- "name" => $data['coupon_name'], // 名称
- "game_id" => $data['gid'], // 游戏id:0=全部游戏
- "money" => $data['min'], // 面值
- "min_money" => $data['max'], // 最低使用金额
- "type_id" => 2, // 类型:1=有效时间,2=领取后多少天内有效
- "day" => $couponDay, // 天数
- "start_time" => 0, // 有效开始时间
- "end_time" => 0, // 有效结束时间
- "state" => 1, // 状态:1=有效, 2=无效
- "is_examine" => 1, // 发放是否需要审核:1=不需要、2=需要
- "create_time" => time(),
- "update_time" => time(),
- "is_show" => 1, // sdk是否显示:1=不显示、2=显示
- "content" => '176 的代金卷', // 使用规则
- "is_yql" => 1, // 176代金卷
- ];
- $couponInfo = model('Common/Coupon')->create($couponData);
- if (!$couponInfo) {
- return json(["code" => 0, "message" => "代金卷创建失败!", "data" => []]);
- }
- }
- // 限制一个用户只能领取一个代金卷一次
- // $info = model('Common/CouponMember')->where(["coupon_id" => $couponInfo['id'], "yql_id" => $data['coupon_id']])->find();
- // if ($info) {
- // return json(["code" => 0, "message" => "请勿重复领取!", "data" => []]);
- // }
- // 发放代金卷
- $memberData = [
- "code" => makeUniqueid('YQL') . 'Q',
- "member_id" => $memberInfo['id'], // 用户id
- "coupon_id" => $couponInfo['id'], // 代金券id
- "examine" => 2, // 审核状态:1=未审核, 2=已审核
- "is_use" => 1, // 状态:1=未使用、2=使用中、3=已使用
- "state" => 1, // 有效状态:1=有效、2=冻结
- "start_time" => strtotime(date("Y-m-d H:i:s", time())), // 开始时间
- "end_time" => strtotime(date("Y-m-d 23:59:59", time() + (86400 * $couponDay-1))), // 到期时间
- "remarks" => "176 玩家的代金卷", // xxx
- "yql_id" => $data['coupon_id'], // 176用户代金卷ID
- ];
- $res = model('Common/CouponMember')->create($memberData);
- if ($res) {
- Db::table('cy_coupon')->where(['id' => $couponInfo['id']])->update([
- 'grant_num' => Db::raw('grant_num+1'),
- 'receive_num' => Db::raw('receive_num+1'),
- ]);
- return json(["code" => 1, "message" => "代金卷领取成功!", "data" => ['code' => $res['code'], "code_id" => $res['id']]]);
- }
- return json(["code" => 0, "message" => "代金卷领取失败!", "data" => []]);
- }
- /**
- * 代金卷使用通知 - 请求176 + 使用计数
- *
- * @param $coupon_member_id 祈盟平台的用户代金卷ID
- *
- * @return Json
- */
- public function notifyCoupon($coupon_member_id)
- {
- if (!$coupon_member_id) {
- return json(["code" => 0, "message" => "非法操作:代金卷不存在!", "data" => []]);
- }
- // 176 平台的用户代金卷ID
- $couponInfo = model('common/CouponMember')->where(['id' => $coupon_member_id])->field('coupon_id,yql_id')->find();
- if(empty($couponInfo['coupon_id'])){
- return json(["code" => 0, "message" => "代金卷有误!", "data" => []]);
- }
- Db::table('cy_coupon')->where(['id' => $couponInfo['coupon_id']])->setInc('use_num');
- if (empty($couponInfo['yql_id'])) {
- return json(["code" => 0, "message" => "代金卷不存在!", "data" => []]);
- }
- $data = ['id' => $couponInfo['yql_id']];
- $sign = $this->sign($data);
- $res = getHttpPost($this->api_url . '/serve/v1/notify/vouchers?id=' . $couponInfo['yql_id'] . '&sign=' . $sign);
- log_message('## notifyCoupon:' . $this->api_url . '/serve/v1/notify/vouchers?id=' . $couponInfo['yql_id'] . '&sign=' . $sign . ' - result: ' . $res, 'log', LOG_PATH . 'yql_log/');
- if ($res) {
- return json(["code" => 1, "message" => "通知成功!", "data" => []]);
- }
- return json(["code" => 0, "message" => "通知失败!", "data" => []]);
- }
- // 176通知祈盟渠道绑定接口 - 176请求
- public function notifyChannel()
- {
- $data = request()->only(['channel_id', 'uid', 'sign']);
- $validateResult = $this->validate($data, [
- 'channel_id' => 'require', // 祈盟SDK的渠道ID
- 'uid' => 'require', // 176的用户ID
- 'sign' => 'require', // sign
- ], [
- 'channel_id.require' => '缺少必要参数: channel_id',
- 'uid.require' => '缺少必要参数: uid',
- 'sign.require' => '缺少必要参数: sign',
- ]);
- if ($validateResult !== true) {
- return json(["code" => 0, "message" => $validateResult, "data" => []]);
- }
- if ($this->sign($data) != $data['sign']) {
- return json(["code" => 0, "message" => "签名有误!", "data" => []]);
- }
- $channelInfo = model('Channel')->where('id', $data['channel_id'])->find();
- if (!$channelInfo) {
- return json(["code" => 0, "message" => "渠道不存在!", "data" => []]);
- }
- $result = model('Channel')->where('id', $data['channel_id'])->update(['yql_id' => $data['uid']]);
- if ($result) {
- return json(["code" => 1, "message" => "success", "data" => []]);
- }
- return json(["code" => 0, "message" => "修改失败!", "data" => []]);
- }
- // 176渠道推广接口:根据渠道ID查询176的渠道推广数据。 - 聚合平台 - 接入
- public function getChannelData(){
- // TODO: 可以不用写,提供到推广地址,祈盟SDK根据 推广地址+渠道ID 去跳转。
- }
- }
|