123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace App\Http\Controllers\Miniprogram;
- use App\Http\Controllers\Controller;
- use App\Models\InviteRelationModel;
- use App\Models\User\UserModel;
- class Invite extends Controller
- {
- /**
- * 创建uid专属链接
- * @return array
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function getLink()
- {
- $uid = Auth::auth();
- return array(
- 'code' => 200,
- 'message' => 'success',
- 'data' => [
- 'link' => $uid
- ],
- );
- }
- /**
- * 通过邀请进入
- * (sign时间小于一天且未被建立邀请关系的为新用户)
- * @param string $link
- * @return array
- * @throws \ApiException
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function intoLink(string $link)
- {
- $uid = Auth::auth();
- $inviteModel = new InviteRelationModel();
- $auth = \DB::table('kdgx_user_auth_key')->where(array(
- ['uid', $uid],
- ['auth_type', config('miniprogram.public_id')]
- ))->first();
- if ($auth->created_at < mktime(0, 0, 0) - 3600) {
- return array(
- 'code' => 201,
- 'message' => '新用户专享福利'
- );
- }
- if ($inviteModel->where('is_invite_uid', $uid)->exists()) {
- return array(
- 'code' => 201,
- 'message' => '新用户专享福利'
- );
- }
- $invite = $inviteModel->fill([
- 'uid' => (int)$link,
- 'is_invite_uid' => $uid,
- 'state' => 1
- ]);
- if ($invite->save()) {
- UserModel::where('uid', $uid)->increment('red_flower', 1);
- \DB::table('kdgx_partner_charge_pay_logs')->insert([
- 'uid' => $uid,
- 'create_at' => time(),
- 'type' => 10,
- 'gold_flower' => 0,
- 'red_flower' => 1,
- 'jsk' => 0,
- 'type_id' => 0,
- 'remark' => "邀请新人奖励"
- ]);
- return array(
- 'code' => 200,
- 'message' => 'success'
- );
- } else {
- throw new \ApiException("数据库异常", 505);
- }
- }
- /**
- * 新用户完成注册任务奖励
- * @param int $uid
- * @return bool
- */
- public function task(int $uid)
- {
- $rela = InviteRelationModel::where('is_invite_uid', $uid)->first();
- if (collect($rela)->isEmpty()) {
- return false;
- } else {
- $rela->state = 2;
- $rela->save();
- $ticketObj = new Ticket();
- $ticketObj->checkTicket(5, $rela->uid);
- \DB::table('kdgx_partner_charge_pay_logs')->insert([
- 'uid' => $uid,
- 'create_at' => time(),
- 'type' => 10,
- 'remark' => '邀请新人奖励',
- 'jsk' => 1,
- ]);
- return true;
- }
- }
- /**
- * 获取邀请统计
- * @return array
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function inviteList()
- {
- $uid = Auth::auth();
- $relas = InviteRelationModel::where('uid', $uid)->get();
- $cnt = $relas->count();
- $users = UserModel::whereIn('uid', $relas->pluck('is_invite_uid'))->get(['headimgurl', 'uid']);
- $jsk_relas = $relas->where('state', 2);
- $jsk_cnt = $jsk_relas->count();
- $jsk_headimgurls = $users->whereIn('uid', $jsk_relas->pluck('is_invite_uid'));
- return array(
- 'code' => 200,
- 'message' => 'success',
- 'data' => [
- 'flower' => [
- 'headimgurl' => $users->pluck('headimgurl'),
- 'cnt' => $cnt
- ],
- 'jsk' => [
- 'headimgurl' => $jsk_headimgurls,
- 'cnt' => $jsk_cnt
- ],
- ],
- );
- }
- }
|