CrmGuideFansController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace App\Http\Controllers\Wap\Crm;
  3. use Illuminate\Http\Request;
  4. use App\Modules\Subscribe\Services\OrderService;
  5. use App\Modules\User\Services\UserDivisionCpcPropertyService;
  6. use App\Modules\User\Services\ForceGuidePersonAccountService;
  7. use App\Http\Controllers\Wap\BaseController;
  8. use App\Modules\User\Services\UserService;
  9. use App\Modules\User\Services\UserBindHkWelfareService;
  10. use Log;
  11. use Redis;
  12. use Cookie;
  13. use DB;
  14. use Hashids;
  15. class CrmGuideFansController extends BaseController
  16. {
  17. /**
  18. * 主动导粉
  19. * @param Request $request
  20. * @param $channel_id
  21. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View}
  22. */
  23. public function activeGuidePersonalAccount(Request $request,$channel_id){
  24. $uid = $request->get('uid',0);
  25. //$user_cookie = Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  26. //$uid = $user_cookie ? decrypt($user_cookie) : null;
  27. $cookie_crm_img = Cookie::get('crm_person_img');
  28. $cookie_crm_name = Cookie::get('crm_person_name','');
  29. $request_img = $request->get('img');
  30. $fee = $request->get('fee',200);
  31. if($request_img){
  32. $name = $request_img = $request->get('name');
  33. return view('jump.guidePersonalAccountOurs',['img'=>$cookie_crm_img,'name'=>$name,'fee'=>$fee]);
  34. }
  35. $get_info = UserBindHkWelfareService::isHasGet($uid);
  36. if($get_info) return back();
  37. if($cookie_crm_img){
  38. return view('jump.guidePersonalAccountOurs',['img'=>$cookie_crm_img,'name'=>$cookie_crm_name,'fee'=>$fee]);
  39. }
  40. $distribution_channel_id = decodeDistributionChannelId($channel_id);
  41. //$distribution_channel_id = $channel_id;
  42. //if(in_array($distribution_channel_id,Redis::SISMEMBER('')));
  43. //$out = false;
  44. $group = 'ACTIVE';
  45. if(Redis::SISMEMBER('crm:out_channel_sites',$distribution_channel_id)){
  46. //$out = true;
  47. $group = 'OUT_ACTIVE';
  48. $env_config = redisEnvMulti('OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  49. 'OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV','OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  50. 'OUT_ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  51. }else{
  52. $env_config = redisEnvMulti('ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  53. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV','ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  54. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  55. }
  56. if(empty($env_config[0])){
  57. return back();
  58. }
  59. //$img = empty($env_config[0])?'https://cdn-novel.iycdm.com/h5/personal_account/chenchen.jpg':$env_config[0];
  60. $img =$env_config[0];
  61. $max = empty($env_config[1])?100:$env_config[1];
  62. $max = (int)$max;
  63. $one_loop_max = empty($env_config[3])?10:$env_config[3];
  64. $now_id = (int)$env_config[2];
  65. if($group == 'OUT_ACTIVE'){
  66. $uv_key = 'out_active_guide_personal_uv';
  67. }else{
  68. $uv_key = 'active_guide_personal_uv';
  69. }
  70. $uv = Redis::scard($uv_key);
  71. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('count')->first();
  72. if(!$personal_info){
  73. return back();
  74. }
  75. $total_max = (int)($personal_info->count+$uv);
  76. //Log::info('$total_max is: '.);
  77. if($uv >= $one_loop_max || $total_max>=$max){
  78. if($total_max>=$max){
  79. DB::table('personal_account_list')->where('id',$now_id)->update([
  80. 'status'=>2,
  81. 'count'=>$total_max,
  82. 'updated_at'=>date('Y-m-d H:i:s')
  83. ]);
  84. }else{
  85. DB::table('personal_account_list')->where('id',$now_id)->increment('count',$uv,[
  86. 'updated_at'=>date('Y-m-d H:i:s')
  87. ]);
  88. }
  89. $account = DB::table('personal_account_list')
  90. ->where('is_enable',1)
  91. ->whereIn('status',[1,0])
  92. ->select('id','url')
  93. ->where('group',$group)
  94. ->where('count','<',$max)
  95. ->orderBy('count','asc')
  96. ->orderBy('id')
  97. ->first();
  98. Redis::del($uv_key);
  99. if($account){
  100. DB::table('personal_account_list')->where('id',$account->id)->update([
  101. 'status'=>1,
  102. 'updated_at'=>date('Y-m-d H:i:s')
  103. ]);
  104. $img = $account->url;
  105. $now_id = $account->id;
  106. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,$group.'_GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
  107. }else{
  108. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','',$group.'_GUIDE_PERSONAL_ACCOUNT_ID','');
  109. return back();
  110. }
  111. }
  112. Redis::sadd($uv_key,$uid);
  113. if($uid){
  114. if($group == 'OUT_ACTIVE'){
  115. DB::table('ad_pdd')->insert([
  116. 'uid'=>$uid,
  117. 'img'=>'OUT_GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  118. 'date'=>date('Y-m-d'),
  119. 'created_at'=>date('Y-m-d H:i:s'),
  120. 'updated_at'=>date('Y-m-d H:i:s')
  121. ]);
  122. $user = UserService::getById($uid);
  123. Redis::hset('crm:out_guide_exposure_user',$user->openid,$uid);
  124. }else{
  125. DB::table('ad_pdd')->insert([
  126. 'uid'=>$uid,
  127. 'img'=>'GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  128. 'date'=>date('Y-m-d'),
  129. 'created_at'=>date('Y-m-d H:i:s'),
  130. 'updated_at'=>date('Y-m-d H:i:s')
  131. ]);
  132. }
  133. }
  134. $name = '';
  135. $name_info = DB::table('personal_account_list')->where('id',$now_id)->select('name')->first();
  136. if($name_info){
  137. $name = $name_info->name;
  138. }
  139. $page = 'jump.guidePersonalAccountOurs';
  140. $time = strtotime(date('Y-m-d',time()+86400))-time();
  141. Cookie::queue('crm_person_img', $img,$time);
  142. Cookie::queue('crm_person_name', $name,$time);
  143. return view($page,['img'=>$img,'name'=>$name,'fee'=>$fee]);
  144. }
  145. /**
  146. * 强制导粉
  147. * @param Request $request
  148. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
  149. */
  150. public function forceGuidePersonalAccount(Request $request)
  151. {
  152. $bid = $request->get('bid');
  153. $cid = $request->get('cid');
  154. $prev_cid = $request->get('prev_cid');
  155. $book_name = $request->get('book_name');
  156. $bid_no = Hashids::decode($bid)[0];
  157. if (Redis::SISMEMBER('crm:out_channel_sites', $this->distribution_channel_id)) {
  158. $group = 'OUT_FORCE';
  159. } else {
  160. $group = 'FORCE';
  161. }
  162. $env_config = redisEnvMulti($group . '_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE', $group . '_GUIDE_PERSONAL_ACCOUNT_MAX_UV',
  163. $group . '_GUIDE_PERSONAL_ACCOUNT_ID', $group . '_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  164. if (empty($env_config[0])) return back();
  165. $img = $env_config[0];
  166. $max = empty($env_config[1]) ? 100 : $env_config[1];
  167. $one_loop_max = empty($env_config[3]) ? 10 : $env_config[3];
  168. $now_id = (int)$env_config[2];
  169. $uv = Redis::scard(strtolower($group) . '_guide_personal_uv');
  170. $personal_info = DB::table('personal_account_list')->where('id', $now_id)->select('count')->first();
  171. $total_max = $personal_info->count + $uv;
  172. //Log::info('$total_max is: '.);
  173. if ($uv >= $one_loop_max || $total_max >= $max) {
  174. if ($total_max >= $max) {
  175. DB::table('personal_account_list')->where('id', $now_id)->update([
  176. 'status' => 2,
  177. 'count' => $total_max,
  178. 'updated_at' => date('Y-m-d H:i:s')
  179. ]);
  180. } else {
  181. DB::table('personal_account_list')->where('id', $now_id)->increment('count', $uv, [
  182. 'updated_at' => date('Y-m-d H:i:s')
  183. ]);
  184. }
  185. $account = DB::table('personal_account_list')
  186. ->where('is_enable', 1)
  187. ->whereIn('status', [1, 0])
  188. ->select('id', 'url')
  189. ->where('count', '<', $max)
  190. ->where('group', $group)
  191. ->orderBy('count', 'asc')
  192. ->orderBy('id')
  193. ->first();
  194. Redis::del(strtolower($group) . '_guide_personal_uv');
  195. if ($account) {
  196. DB::table('personal_account_list')->where('id', $account->id)->update([
  197. 'status' => 1,
  198. 'updated_at' => date('Y-m-d H:i:s')
  199. ]);
  200. $img = $account->url;
  201. $now_id = $account->id;
  202. Redis::Hmset('env', $group . '_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE', $account->url, $group . '_GUIDE_PERSONAL_ACCOUNT_ID', $account->id);
  203. } else {
  204. Redis::Hmset('env', $group . '_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE', '', $group . '_GUIDE_PERSONAL_ACCOUNT_ID', '');
  205. }
  206. }
  207. Redis::sadd(strtolower($group) . '_guide_personal_uv', $this->uid);
  208. if ($group == 'OUT_FORCE') {
  209. $data = UserService::getById($this->uid);
  210. Redis::hset('crm:out_guide_exposure_user', $data->openid, $this->uid);
  211. }
  212. $personal_info = DB::table('personal_account_list')->where('id', $now_id)->select('name')->first();
  213. DB::table('ad_pdd')->insert([
  214. 'uid' => $this->uid,
  215. 'img' => $group . '_GUIDE_PERSONAL_ACCOUNT_' . $now_id,
  216. 'date' => date('Y-m-d'),
  217. 'created_at' => date('Y-m-d H:i:s'),
  218. 'updated_at' => date('Y-m-d H:i:s')
  219. ]);
  220. ForceGuidePersonAccountService::create($this->uid, $bid_no, $cid);
  221. $link['next'] = sprintf('/reader?bid=%s&cid=%s', $bid, $cid);
  222. $link['prev'] = sprintf('/reader?bid=%s&cid=%s', $bid, $prev_cid);
  223. $link['catalog'] = sprintf('/catalog?id=%s', $bid);
  224. return view('jump.forceGuidePersonalAccountV2', ['img' => $img, 'link' => $link, 'title' => $book_name, 'name' => $personal_info->name]);
  225. }
  226. public function crmPushView(Request $request)
  227. {
  228. $crm_config = redisEnvMulti('ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  229. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV', 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  230. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  231. $img = $crm_config[0];
  232. $max = empty($crm_config[1]) ? 100 : $crm_config[1];
  233. $max = (int)$max;
  234. $one_loop_max = empty($crm_config[3]) ? 10 : $crm_config[3];
  235. $now_id = (int)$crm_config[2];
  236. $uv_key = 'active_guide_personal_uv';
  237. $uv = Redis::scard($uv_key);
  238. $personal_info = DB::table('personal_account_list')->where('id', $now_id)->select('id','count','name')->first();
  239. $total_max = (int)($personal_info->count + $uv);
  240. if (empty($img) || empty($now_id) || $total_max >= $max) {
  241. //没有名额
  242. $data = ['code' => -1, 'read_url' => '/sign'];
  243. return view('crm.crmPush', $data);
  244. }
  245. $sql = 'SELECT friend_link_uid_bind.uid FROM friend_link_uid_bind where openid = (SELECT openid FROM users WHERE id = %s) ORDER by id desc limit 1';
  246. $friend_link_uid_bind = DB::select(sprintf($sql, $this->uid));
  247. //$get_info = UserBindHkWelfareService::isHasGet($this->uid);
  248. if ($friend_link_uid_bind) {
  249. $info = DB::table('ad_pdd')->where('uid', $friend_link_uid_bind[0]->uid)->orderBy('id', 'desc')->first();
  250. if ($info) {
  251. $personal_account_id = preg_replace('/\D+/', '', $info->img);
  252. if ($info) {
  253. $personal_info = DB::table('personal_account_list')
  254. ->where('id', $personal_account_id)
  255. ->where('is_enable', 1)
  256. ->select('name', 'url', 'status', 'is_enable')->first();
  257. if ($personal_info) {
  258. $data = ['code' => -2, 'name' => $personal_info->name];
  259. return view('crm.crmPush', $data);
  260. }
  261. }
  262. }
  263. $user_info = UserService::getById($friend_link_uid_bind[0]->uid);
  264. $link = 'https://site'.encodeDistributionChannelId($user_info->distribution_channel_id).'.leyuee.com/sign';
  265. $data = ['code' => -3, 'read_url' => $link];
  266. return view('crm.crmPush', $data);
  267. }
  268. $property = UserDivisionCpcPropertyService::getUserPropertyV2($this->uid);
  269. $charge = false;
  270. if (in_array($property, ['medium', 'high'])) {
  271. $charge = true;
  272. } else {
  273. if ($this->property == 'undefined' && OrderService::getChargeNum($this->uid) >= 10) {
  274. $charge = true;
  275. }
  276. }
  277. if (!$charge) {
  278. //不符合条件
  279. $data = ['code' => -1, 'read_url' => '/sign'];
  280. return view('crm.crmPush', $data);
  281. }
  282. Redis::sadd($uv_key, $this->uid);
  283. DB::table('ad_pdd')->insert([
  284. 'uid' => $this->uid,
  285. 'img' => 'GUIDE_PERSONAL_ACCOUNT_' . $now_id,
  286. 'date' => date('Y-m-d'),
  287. 'created_at' => date('Y-m-d H:i:s'),
  288. 'updated_at' => date('Y-m-d H:i:s')
  289. ]);
  290. return view('crm.crmPush2', ['name' => $personal_info->name, 'img' => $img]);
  291. }
  292. }