VipUser.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 创客管理
  7. */
  8. namespace app\allwin\controller;
  9. use app\common\facade\Inform;
  10. use app\common\model\SystemUser;
  11. use app\allwin\model\AllwinUser;
  12. use app\allwin\model\VipCard;
  13. use app\allwin\model\Vip;
  14. use think\facade\Request;
  15. use think\facade\Validate;
  16. use think\helper\Time;
  17. class VipUser extends Common{
  18. public function initialize() {
  19. parent::initialize();
  20. $this->assign('pathMaps',[['name'=>'创客设置','url'=>url("vip/index")]]);
  21. }
  22. /**
  23. *
  24. * @param int $types
  25. * @param int $vip_id
  26. * @return type
  27. */
  28. public function index(int $types = 0,int $vip_id = 0){
  29. $vip_class = [];
  30. $vip = [];
  31. $condition = [];
  32. $vip_class = VipCard::where($this->mini_program)->order('sort desc,id desc')->select();
  33. if($vip_id > 0){
  34. $condition[] = ['vipcard_id','=',$vip_id];
  35. }
  36. $time = Request::param('time/d',0);
  37. $starttime = Request::param('starttime/s');
  38. $endtime = Request::param('endtime/s');
  39. if($time){
  40. switch ($time) {
  41. case 2:
  42. list($start, $end) = Time::yesterday();
  43. break;
  44. case 30:
  45. list($start, $end) = Time::month();
  46. break;
  47. case 60:
  48. list($start, $end) = Time::lastMonth();
  49. break;
  50. default:
  51. list($start, $end) = Time::today();
  52. break;
  53. }
  54. $condition[] = ['update_time','>=',$start];
  55. $condition[] = ['update_time','<=',$end];
  56. }else{
  57. if($starttime){
  58. $condition[] = ['update_time','>=',strtotime($starttime)];
  59. }
  60. if($endtime){
  61. $condition[] = ['update_time','<=',strtotime($endtime)];
  62. }
  63. }
  64. $view['store_vipnum'] = Vip::where($this->mini_program)->where($condition)->where('store_id','>',0)->count(); //好店会员数量
  65. $view['platform_vipnum'] = Vip::where($this->mini_program)->where($condition)->where('store_id','=',0)->count(); //平台会员数量
  66. $condition[] = ['state','=',1];
  67. $store_id = Request::param('store_id/d','');
  68. $store_name = Request::param('store_name/s','');
  69. if($store_id){
  70. $condition[] = ['store_id','=',$store_id];
  71. }
  72. $view['vipnum'] = Vip::where($this->mini_program)->where($condition)->count(); //会员数量
  73. $view['vipamount'] = Vip::where($this->mini_program)->where($condition)->sum('amount'); //支付金额
  74. //用户精准查询
  75. $uid = Request::param('uid/d','');
  76. if($uid){
  77. $condition[] = ['user_id','=',$uid];
  78. }
  79. //锁定或正常
  80. $condition[] = ['is_lock','=',$types ? 1 : 0];
  81. $view['lists'] = Vip::where($this->mini_program)->where($condition)->order('id desc')->paginate(20,false,['query'=> ['types' => $types,'vip_id' => $vip_id]]);
  82. //模板赋值
  83. $view['pathMaps'][] = ['name'=>' 会员会员','url'=>url('vip_user/index')];
  84. if ($vip_id > 0) {
  85. $vip = VipCard::where(['id' => $vip_id])->field('name,id')->find();
  86. $view['pathMaps'][] = ['name' => $vip->name,'url'=>url('vip_user/index',['vip_id' => $vip->id])];
  87. }
  88. $view['pathMaps'][] = ['name'=>$types ? '<span class="red fn-fb">锁定<span>' : '<span class="blue fn-fb">正常<span>','url'=>'javascript:;'];
  89. $view['vip'] = $vip;
  90. $view['vip_class'] = $vip_class;
  91. $view['types'] = $types;
  92. $view['vip_id'] = $vip_id;
  93. $view['time'] = $time;
  94. $view['starttime'] = $starttime;
  95. $view['endtime'] = $endtime;
  96. $view['store_id'] = $store_id;
  97. $view['store_name'] = $store_name;
  98. $view['uid'] = $uid;
  99. return view()->assign($view);
  100. }
  101. /**
  102. * 添加创客会员
  103. * @return type
  104. */
  105. public function winUser(){
  106. if(request()->isAjax()){
  107. $uids = ids(input('post.ids/s'),true);
  108. if(empty($uids)){
  109. return json(['code'=>0,'msg'=>'请选择要关联的用户']);
  110. }
  111. $vip_id = input('get.vip_id/d');
  112. $vipcard = VipCard::where($this->mini_program)->where(['id' => $vip_id])->find();
  113. if(empty($vipcard)){
  114. return json(['code'=>0,'msg'=>'未找到对应会员']);
  115. }
  116. //读取并判断是否VIP
  117. $vipdata = [];
  118. $userdata = [];
  119. foreach ($uids as $key => $uid) {
  120. $vipdata[$key]['order_no'] = 'Platform'.date('YmdhHis');
  121. $vipdata[$key]['state'] = 1;
  122. $vipdata[$key]['coupon_ids'] = json_encode([]);
  123. $vipdata[$key]['amount'] = 0;
  124. $vipdata[$key]['is_up'] = $vipcard->is_up;
  125. $vipdata[$key]['vipcard_id'] = $vipcard->id;
  126. $vipdata[$key]['update_time'] = time();
  127. $vipdata[$key]['is_lock'] = 0;
  128. $vipdata[$key]['is_diy'] = 1;
  129. $vipdata[$key]['store_id'] = 0; //邀请参数的店铺ID
  130. $vipdata[$key]['user_id'] = $uid;
  131. $vipdata[$key]['member_miniapp_id'] = $this->member_miniapp_id;
  132. $is_user = AllwinUser::where(['uid'=> $uid])->find();
  133. if(empty($is_user)){
  134. $userdata[] = ['uid' =>$uid,'store_id' => 0];
  135. }
  136. //通知到申请者微信
  137. Inform::sms($uid,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为'.$vipcard['name']]);
  138. }
  139. //锁定以前的会员
  140. Vip::where(['user_id' => $uids,'state' => 1])->update(['is_lock' => 1]);
  141. Vip::insertAll($vipdata);
  142. if(!empty($userdata)){
  143. AllwinUser::insertAll($userdata);
  144. }
  145. return json(['code'=>200,'msg'=>'会员用户新增成功','data' => ['url'=>'']]);
  146. }else{
  147. $vip_id = input('get.vip_id/d');
  148. $view['vip'] = VipCard::where($this->mini_program)->where(['id' => $vip_id])->find();
  149. if(empty($view['vip'])){
  150. $this->error('未找到会员类型');
  151. }
  152. $keyword = trim(input('get.keyword','','htmlspecialchars'));
  153. $condition = [] ;
  154. if(!empty($keyword)){
  155. if(Validate::isMobile($keyword)){
  156. $condition[] = ['phone_uid','=',$keyword] ;
  157. }else{
  158. $condition[] = ['nickname','like','%'.$keyword.'%'];
  159. }
  160. }
  161. $view['list'] = SystemUser::where($this->mini_program)->where($condition)->where('id', 'NOT IN', function ($query) use($vip_id){
  162. $query->table('ai_allwin_vip')->where(['state' => 1,'vipcard_id' => $vip_id])->field('user_id');
  163. })->order('id desc')->paginate(20,false,['query' => ['keyword'=>$keyword,'vip_id' => $vip_id]]);
  164. $view['keyword'] = $keyword;
  165. $view['vip_id'] = $vip_id;
  166. return view()->assign($view);
  167. }
  168. }
  169. /**
  170. * 删除用户的优惠券
  171. * @return void
  172. */
  173. public function deleteUser(){
  174. $uid = input('get.uid/d');
  175. $result = Vip::where($this->mini_program)->where(['user_id' => $uid])->delete();
  176. if($result){
  177. return json(['code'=>200,'msg'=>'操作成功']);
  178. }else{
  179. return json(['code'=>403,'msg'=>'删除失败']);
  180. }
  181. }
  182. /**
  183. * 开通会员的社交关系
  184. * @return void
  185. */
  186. public function level(int $id){
  187. $agent = model('AllwinUser')->where(['uid' => $id])->find();
  188. if(!$agent){
  189. return $this->error("404 NOT FOUND");
  190. }
  191. $data['nickname'] = $agent->user->nickname;
  192. $data['invite_code'] = $agent->user->invite_code;
  193. $data['phone_uid'] = $agent->user->phone_uid;
  194. $data['face'] = $agent->user->face;
  195. $data['store_name'] = empty($agent->store->name) ? '[无店铺]会员' : $agent->store->name;
  196. $data['level'] = 0;
  197. $view['user'] = $data;
  198. $view['level'] = model('Levels')->children_user($id);
  199. return view()->assign($view);
  200. }
  201. }