WxuserController.php 3.08 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * File UserController.php
 *
 * @author Tuan Duong <bacduong@gmail.com>
 * @package Laravue
 * @version 1.0
 */

namespace App\Http\Controllers;

Administrator's avatar
Administrator committed
12
use App\Laravue\Models\WxUser;
冯超鹏's avatar
冯超鹏 committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
use Validator;

class WxuserController extends Controller
{
    //获取wx用户列表
    public function wxlist(Request $request){
        /*
         * get请求返回wx用户列表
         * */
冯超鹏's avatar
冯超鹏 committed
27

Administrator's avatar
Administrator committed
28 29
        $limit = $request->input('limit');
        $pagenNum = $limit * ($request->input('page')-1);//页数
冯超鹏's avatar
冯超鹏 committed
30 31 32
        if($pagenNum === '' || $limit == ''){
            return $this->jsonErrorData(105,'页数或limit不能为空');
        }
Administrator's avatar
Administrator committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        $where = [];
        $keys = $request->all();
        if(isset($keys['sex'])) {
            $keys['sex'] = $keys['sex'] == 3 ? 0 : $keys['sex'];
            $where[] = ['sex', '=', $keys['sex']];
        }
        if(isset($keys['state'])) {
            $where[] = ['state', '=', $keys['state']];
        }
        if(isset($keys['mobile'])) {
            $where[] = ['mobile', 'like', "%$keys[mobile]%"];
        }
        $city = '';
        if(isset($keys['city'])) {
            $city = $keys['city'];
        }
        $cont =  WxUser::where($where)
            ->whereRaw("concat(country, province, city) like '%{$city}%'")->count();
Administrator's avatar
Administrator committed
51
        $Wxuser = WxUser::select('nickname','id','openid','sex','province','city','mobile','country','headimgurl','created_at','state')
Administrator's avatar
Administrator committed
52 53
            ->where($where)
            ->whereRaw("concat(country, province, city) like '%{$city}%'")
冯超鹏's avatar
冯超鹏 committed
54 55 56 57
            ->offset($pagenNum)
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->get();
冯超鹏's avatar
冯超鹏 committed
58
        $data = ['Wxuser'=>$Wxuser,'cont'=>$cont];
冯超鹏's avatar
冯超鹏 committed
59
        if ($Wxuser){
冯超鹏's avatar
冯超鹏 committed
60
            return $this->jsonSuccessData($data);
冯超鹏's avatar
冯超鹏 committed
61 62 63 64
        }else{
            return $this->jsonErrorData('105','获取数据失败');
        }
    }
冯超鹏's avatar
冯超鹏 committed
65 66 67
    //用户搜索
    public  function seek (Request $request){
        $keys = $request->all();
Administrator's avatar
Administrator committed
68 69 70 71 72
        $where = [];

        if(isset($keys['sex'])) {
            $keys['sex'] = $keys['sex'] == 3 ? 0 : $keys['sex'];
            $where[] = ['sex', '=', $keys['sex']];
冯超鹏's avatar
冯超鹏 committed
73
        }
Administrator's avatar
Administrator committed
74 75
        if(isset($keys['state'])) {
            $where[] = ['state', '=', $keys['state']];
冯超鹏's avatar
冯超鹏 committed
76
        }
Administrator's avatar
Administrator committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        if(isset($keys['mobile'])) {
            $where[] = ['mobile', 'like', "%$keys[mobile]%"];
        }
        $city = '';
        if(isset($keys['city'])) {
           $city = $keys['city'];
        }
        $datas = DB::table('wx_user')
            ->where($where)
            ->whereRaw("concat(country, province, city) like '%{$city}%'")
            ->limit($keys['limit'])
            ->offset($keys['limit'] * ($keys['page'] - 1))
            ->orderBy('id', 'DESC')
            ->get();
        $cont = DB::table('wx_user')->where($where)->whereRaw("concat(country, province, city) like '%{$city}%'")->count();
冯超鹏's avatar
冯超鹏 committed
92 93 94
        $data = ['Wxuser'=>$datas,'cont'=>$cont];
        return $this->jsonSuccessData($data);
    }
Administrator's avatar
Administrator committed
95
}