CrmGuideFansController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. private $crm_one_day_exposure_limit;
  18. private $crm_one_time_exposure_limit;
  19. private $now_personal_account_info;
  20. private $crm_now_exposure_uv;
  21. /**
  22. * 主动导粉
  23. * @param Request $request
  24. * @param $channel_id
  25. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View}
  26. */
  27. public function activeGuidePersonalAccountV2(Request $request){
  28. $uid = $this->uid;
  29. $cookie_crm_img = Cookie::get('crm_person_img');
  30. $cookie_crm_name = Cookie::get('crm_person_name','');
  31. $fee = $request->get('fee',200);
  32. $group = $request->get('group','ACTIVE');
  33. $page = 'jump.guidePersonalAccountOurs';
  34. if(in_array($group,['ACTIVE_YEAR','ACTIVE_MONTH'])){
  35. $page = 'crm.vipguidePersonalAccount';
  36. }
  37. if($cookie_crm_img){
  38. return view($page,['img'=>$cookie_crm_img,'name'=>$cookie_crm_name,'fee'=>$fee]);
  39. }
  40. list($img,$max,$now_id,$one_loop_max) = redisEnvMulti(strtoupper($group).'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  41. strtoupper($group).'_GUIDE_PERSONAL_ACCOUNT_MAX_UV',strtoupper($group).'_GUIDE_PERSONAL_ACCOUNT_ID',
  42. strtoupper($group).'_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  43. if(empty($img) || empty($now_id)){
  44. return back();
  45. }
  46. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('id','name','count','status','url','group')->first();
  47. if(!$personal_info){
  48. return back();
  49. }
  50. $this->now_personal_account_info = $personal_info;
  51. //单日曝光上线
  52. $max = (int)(empty($max)?100:$max);
  53. $this->crm_one_day_exposure_limit = $max;
  54. //单次曝光上线
  55. $one_loop_max = empty($one_loop_max)?10:$one_loop_max;
  56. $this->crm_one_time_exposure_limit = $one_loop_max;
  57. $uv_key = strtolower($group).'_guide_personal_uv';
  58. $uv = Redis::scard($uv_key);
  59. $this->crm_now_exposure_uv = $uv;
  60. $this->crmGuideProcess();
  61. if(!$this->now_personal_account_info){
  62. return back();
  63. }
  64. if($group == 'OUT_ACTIVE'){
  65. $user = UserService::getById($uid);
  66. Redis::hset('crm:out_guide_exposure_user',$user->openid,$uid);
  67. }
  68. $name = $this->now_personal_account_info->name;
  69. $img = $this->now_personal_account_info->url;
  70. $time = strtotime(date('Y-m-d',time()+86400))-time();
  71. Cookie::queue('crm_person_img', $img,$time);
  72. Cookie::queue('crm_person_name', $name,$time);
  73. return view($page,['img'=>$img,'name'=>$name,'fee'=>$fee]);
  74. }
  75. /**
  76. * 强制导粉
  77. * @param Request $request
  78. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
  79. */
  80. public function forceGuidePersonalAccountV2(Request $request)
  81. {
  82. $bid = $request->get('bid');
  83. $cid = $request->get('cid');
  84. $prev_cid = $request->get('prev_cid');
  85. $book_name = $request->get('book_name');
  86. $bid_no = Hashids::decode($bid)[0];
  87. $group = $request->get('group','FORCE');
  88. /*if (Redis::SISMEMBER('crm:out_channel_sites', $this->distribution_channel_id)) {
  89. $group = 'OUT_FORCE';
  90. } else {
  91. $group = 'FORCE';
  92. }*/
  93. list($img,$max,$now_id,$one_loop_max) = redisEnvMulti($group . '_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE', $group . '_GUIDE_PERSONAL_ACCOUNT_MAX_UV',
  94. $group . '_GUIDE_PERSONAL_ACCOUNT_ID', $group . '_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  95. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('id','name','count','status','url','group')->first();
  96. if(!$personal_info){
  97. return back();
  98. }
  99. $this->now_personal_account_info = $personal_info;
  100. //单日曝光上线
  101. $max = (int)(empty($max)?100:$max);
  102. $this->crm_one_day_exposure_limit = $max;
  103. //单次曝光上线
  104. $one_loop_max = empty($one_loop_max)?10:$one_loop_max;
  105. $this->crm_one_time_exposure_limit = $one_loop_max;
  106. $uv_key = strtolower($group).'_guide_personal_uv';
  107. $uv = Redis::scard($uv_key);
  108. $this->crm_now_exposure_uv = $uv;
  109. $this->crmGuideProcess();
  110. if ($group == 'OUT_FORCE') {
  111. $data = UserService::getById($this->uid);
  112. Redis::hset('crm:out_guide_exposure_user', $data->openid, $this->uid);
  113. }
  114. $img = $this->now_personal_account_info->url;
  115. ForceGuidePersonAccountService::create($this->uid, $bid_no, $cid);
  116. $link['next'] = sprintf('/reader?bid=%s&cid=%s', $bid, $cid);
  117. $link['prev'] = sprintf('/reader?bid=%s&cid=%s', $bid, $prev_cid);
  118. $link['catalog'] = sprintf('/catalog?id=%s', $bid);
  119. return view('jump.forceGuidePersonalAccountV2', ['img' => $img, 'link' => $link, 'title' => $book_name, 'name' => $this->now_personal_account_info->name]);
  120. }
  121. private function crmGuideProcess(){
  122. $group = strtoupper($this->now_personal_account_info->group);
  123. $total_max = $this->crm_now_exposure_uv+$this->now_personal_account_info->count;
  124. if($this->crm_now_exposure_uv >= $this->crm_one_time_exposure_limit || $total_max >= $this->crm_one_day_exposure_limit){
  125. if($total_max>= $this->crm_one_day_exposure_limit){
  126. DB::table('personal_account_list')->where('id',$this->now_personal_account_info->id)->update([
  127. 'status'=>2,
  128. 'count'=>$total_max,
  129. 'updated_at'=>date('Y-m-d H:i:s')
  130. ]);
  131. }else{
  132. DB::table('personal_account_list')->where('id',$this->now_personal_account_info->id)->increment('count',$this->crm_now_exposure_uv,[
  133. 'updated_at'=>date('Y-m-d H:i:s')
  134. ]);
  135. }
  136. $account = DB::table('personal_account_list')
  137. ->where('is_enable',1)
  138. ->whereIn('status',[1,0])
  139. ->select('id','name','count','status','url','group')
  140. ->where('group',$this->now_personal_account_info->group)
  141. ->where('count','<',$this->crm_one_day_exposure_limit)
  142. ->orderBy('count','asc')
  143. ->orderBy('id')
  144. ->first();
  145. Redis::del(strtolower($group) . '_guide_personal_uv');
  146. if($account){
  147. DB::table('personal_account_list')->where('id',$account->id)->update([
  148. 'status'=>1,
  149. 'updated_at'=>date('Y-m-d H:i:s')
  150. ]);
  151. $this->now_personal_account_info = $account;
  152. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,$group.'_GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
  153. }else{
  154. Redis::Hmset('env',$group.'_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','',$group.'_GUIDE_PERSONAL_ACCOUNT_ID','');
  155. $this->now_personal_account_info= null;
  156. }
  157. }
  158. if($this->now_personal_account_info){
  159. Redis::sadd(strtolower($group) . '_guide_personal_uv',$this->uid);
  160. DB::table('ad_pdd')->insert([
  161. 'uid'=>$this->uid,
  162. 'img'=>strtoupper($group).'_PERSONAL_ACCOUNT_'.$this->now_personal_account_info->id,
  163. 'date'=>date('Y-m-d'),
  164. 'created_at'=>date('Y-m-d H:i:s'),
  165. 'updated_at'=>date('Y-m-d H:i:s')
  166. ]);
  167. }
  168. }
  169. public function crmPushView(Request $request)
  170. {
  171. $crm_config = redisEnvMulti('ACTIVE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',
  172. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_UV', 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_ID',
  173. 'ACTIVE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  174. $img = $crm_config[0];
  175. $max = empty($crm_config[1]) ? 100 : $crm_config[1];
  176. $max = (int)$max;
  177. $one_loop_max = empty($crm_config[3]) ? 10 : $crm_config[3];
  178. $now_id = (int)$crm_config[2];
  179. $uv_key = 'active_guide_personal_uv';
  180. $uv = Redis::scard($uv_key);
  181. $personal_info = DB::table('personal_account_list')->where('id', $now_id)->select('id','count','name')->first();
  182. $total_max = (int)($personal_info->count + $uv);
  183. if (empty($img) || empty($now_id) || $total_max >= $max) {
  184. //没有名额
  185. $data = ['code' => -1, 'read_url' => '/sign'];
  186. return view('crm.crmPush', $data);
  187. }
  188. $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';
  189. $friend_link_uid_bind = DB::select(sprintf($sql, $this->uid));
  190. //$get_info = UserBindHkWelfareService::isHasGet($this->uid);
  191. if ($friend_link_uid_bind) {
  192. $info = DB::table('ad_pdd')->where('uid', $friend_link_uid_bind[0]->uid)->orderBy('id', 'desc')->first();
  193. if ($info) {
  194. $personal_account_id = preg_replace('/\D+/', '', $info->img);
  195. if ($info) {
  196. $personal_info = DB::table('personal_account_list')
  197. ->where('id', $personal_account_id)
  198. ->where('is_enable', 1)
  199. ->select('name', 'url', 'status', 'is_enable')->first();
  200. if ($personal_info) {
  201. $data = ['code' => -2, 'name' => $personal_info->name];
  202. return view('crm.crmPush', $data);
  203. }
  204. }
  205. }
  206. $user_info = UserService::getById($friend_link_uid_bind[0]->uid);
  207. $link = 'https://site'.encodeDistributionChannelId($user_info->distribution_channel_id).'.leyuee.com/sign';
  208. $data = ['code' => -3, 'read_url' => $link];
  209. return view('crm.crmPush', $data);
  210. }
  211. $property = UserDivisionCpcPropertyService::getUserPropertyV2($this->uid);
  212. $charge = false;
  213. if (in_array($property, ['medium', 'high'])) {
  214. $charge = true;
  215. } else {
  216. if ($this->property == 'undefined' && OrderService::getChargeNum($this->uid) >= 10) {
  217. $charge = true;
  218. }
  219. }
  220. if (!$charge) {
  221. //不符合条件
  222. $data = ['code' => -1, 'read_url' => '/sign'];
  223. return view('crm.crmPush', $data);
  224. }
  225. Redis::sadd($uv_key, $this->uid);
  226. DB::table('ad_pdd')->insert([
  227. 'uid' => $this->uid,
  228. 'img' => 'GUIDE_PERSONAL_ACCOUNT_' . $now_id,
  229. 'date' => date('Y-m-d'),
  230. 'created_at' => date('Y-m-d H:i:s'),
  231. 'updated_at' => date('Y-m-d H:i:s')
  232. ]);
  233. return view('crm.crmPush2', ['name' => $personal_info->name, 'img' => $img]);
  234. }
  235. }