CrmGuideFansController.php 11 KB

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