Silent 6 years ago
parent
commit
e27d9bf50b

+ 31 - 0
app/Http/Controllers/Teacher/StudentController.php

xqd xqd xqd
@@ -10,6 +10,7 @@ use App\Models\Teacher;
 use App\Models\TeacherStudent;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Validator;
 
 class StudentController extends Controller
 {
@@ -62,6 +63,17 @@ class StudentController extends Controller
             return $this->showWarning('数据错误');
         }
 
+        $validator = Validator::make($request->input('data'), [
+            'phone' => 'required|unique:students'
+        ], [
+            'phone.required' => '手机必填',
+            'phone.unique' => '手机已存在',
+        ]);
+
+        if($validator->fails()) {
+            return back()->withErrors($validator)->withInput();
+        }
+
         $res = $this->model->create($request->input('data'));
 
         if(!$res) {
@@ -90,6 +102,25 @@ class StudentController extends Controller
             return $this->showWarning('数据错误');
         }
 
+        $validator = Validator::make($request->input('data'), [
+            'phone' => 'required'
+        ], [
+            'phone.required' => '手机必填'
+        ]);
+
+        if($validator->fails()) {
+            return back()->withErrors($validator)->withInput();
+        }
+
+        $tmp = $this->model->where([
+            ['id', '<>', $request->input('id')],
+            ['phone', '=', $request->input('data')['phone']],
+        ])->first();
+        if(!empty($tmp)) {
+            $validator->errors()->add('phone', '手机已存在!');
+            return back()->withErrors($validator)->withInput();
+        }
+
         $res = $this->model->where('id', $request->input('id'))->update($request->input('data'));
 
         if(!$res) {

+ 48 - 24
app/Http/Controllers/WeChat/ApiController.php

xqd xqd
@@ -12,6 +12,7 @@ use App\Models\Setting;
 use App\Models\Student;
 use App\Models\StudentCourse;
 use App\Models\StudentCourseTeacher;
+use App\Models\WeChatUser;
 use Carbon\Carbon;
 use GuzzleHttp\Client;
 use Illuminate\Http\Request;
@@ -24,41 +25,64 @@ class ApiController extends Controller
 {
     public function login(Request $request)
     {
-        if(empty($request->input('code')) || empty($request->input('iv')) || empty($request->input('encryptedData'))) {
+        if(empty($request->input('code'))) {
             return response()->json(['status' => 'error', 'info' => '参数错误']);
         }
-        $code = $request->input('code');
-        $iv = $request->input('iv');
-        $encryptedData = $request->input('encryptedData');
         $app = app('wechat.mini_program');
-        $res = $app->auth->session($code);
+        $res = $app->auth->session($request->input('code'));
 
         if(!isset($res['session_key'])) {
             return response()->json(['status' => 'error', 'info' => '接口错误']);
         }
 
-        $info = $app->encryptor->decryptData($res['session_key'], $iv, $encryptedData);
-
-        if(!isset($info['openId'])) {
-            return response()->json(['status' => 'error', 'info' => '接口错误']);
-        }
-
-        $student = Student::firstOrCreate([
-            'open_id' => $info['openId']
+        $res = WeChatUser::firstOrCreate([
+            'open_id' => $res['open_id'],
         ], [
-            'nickname' => $info['nickName'],
-            'gender' => $info['gender'],
-            'city' => $info['city'],
-            'province' => $info['province'],
-            'country' => $info['country'],
-            'avatar_url' => $info['avatarUrl'],
-            'name' => $info['nickName'],
-            'short_leave_times' => 0,
-            'long_leave_times' => 0,
+            'code' => $request->input('code'),
+            'session_key' => $res['session_key'],
         ]);
 
-        $data = ['id' => $student->id, 'nickname' => $student->nickname, 'avatar_url' => $student->avatar_url];
-        return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
+        if(empty($res)) {
+            return response()->json(['status' => 'error', 'info' => '数据库错误']);
+        }
+
+        return response()->json(['status' => 'success', 'id' => $res->id]);
+
+//        if(empty($request->input('code')) || empty($request->input('iv')) || empty($request->input('encryptedData'))) {
+//            return response()->json(['status' => 'error', 'info' => '参数错误']);
+//        }
+//        $code = $request->input('code');
+//        $iv = $request->input('iv');
+//        $encryptedData = $request->input('encryptedData');
+//        $app = app('wechat.mini_program');
+//        $res = $app->auth->session($code);
+//
+//        if(!isset($res['session_key'])) {
+//            return response()->json(['status' => 'error', 'info' => '接口错误']);
+//        }
+//
+//        $info = $app->encryptor->decryptData($res['session_key'], $iv, $encryptedData);
+//
+//        if(!isset($info['openId'])) {
+//            return response()->json(['status' => 'error', 'info' => '接口错误']);
+//        }
+//
+//        $student = Student::firstOrCreate([
+//            'open_id' => $info['openId']
+//        ], [
+//            'nickname' => $info['nickName'],
+//            'gender' => $info['gender'],
+//            'city' => $info['city'],
+//            'province' => $info['province'],
+//            'country' => $info['country'],
+//            'avatar_url' => $info['avatarUrl'],
+//            'name' => $info['nickName'],
+//            'short_leave_times' => 0,
+//            'long_leave_times' => 0,
+//        ]);
+//
+//        $data = ['id' => $student->id, 'nickname' => $student->nickname, 'avatar_url' => $student->avatar_url];
+//        return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
     }
 
     public function checkPosition(Request $request)

+ 12 - 0
app/Models/WeChatUser.php

xqd
@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class WeChatUser extends Model
+{
+    protected $table = 'we_chat_users';
+
+    protected $guarded = [];
+}

+ 35 - 0
database/migrations/2018_08_07_172746_create_we_chat_users_table.php

xqd
@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateWeChatUsersTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('we_chat_users', function (Blueprint $table) {
+            $table->increments('id');
+            $table->unsignedInteger('student_id')->nullable()->comment('学员ID');
+            $table->string('open_id', 200)->nullable()->comment('open_id');
+            $table->string('code', 200)->nullable()->comment('code');
+            $table->string('session_key ', 200)->nullable()->comment('session_key');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('we_chat_users');
+    }
+}

+ 1 - 1
resources/views/admin/students/create.blade.php

xqd
@@ -41,7 +41,7 @@
                             <div class="form-group row {{ $errors->has('phone') ? 'has-error' : '' }}">
                                 <label class="col-sm-2 col-sm-offset-1 control-label">手机</label>
                                 <div class="col-sm-8">
-                                    <input type="text" name="data[phone]" class="form-control" placeholder="请输入学员姓名" value="{{ isset(old('data')['phone']) ? old('data')['phone'] : '' }}" required>
+                                    <input type="text" name="data[phone]" class="form-control" placeholder="请输入学员手机" value="{{ isset(old('data')['phone']) ? old('data')['phone'] : '' }}" required>
                                     @if($errors->has('phone'))
                                         <span class="help-block">{{ $errors->first('phone') }}</span>
                                     @endif

+ 1 - 1
resources/views/admin/students/edit.blade.php

xqd
@@ -42,7 +42,7 @@
                             <div class="form-group row {{ $errors->has('phone') ? 'has-error' : '' }}">
                                 <label class="col-sm-2 col-sm-offset-1 control-label">手机</label>
                                 <div class="col-sm-8">
-                                    <input type="text" name="data[phone]" class="form-control" placeholder="请输入学员姓名" value="{{ $item->phone }}" required>
+                                    <input type="text" name="data[phone]" class="form-control" placeholder="请输入学员手机" value="{{ $item->phone }}" required>
                                     @if($errors->has('phone'))
                                         <span class="help-block">{{ $errors->first('phone') }}</span>
                                     @endif

+ 10 - 0
resources/views/teacher/students/create.blade.php

xqd
@@ -38,6 +38,16 @@
                                 @endif
                             </div>
 
+                            <div class="form-group row {{ $errors->has('phone') ? 'has-error' : '' }}">
+                                <label class="col-sm-2 col-sm-offset-1 control-label">手机</label>
+                                <div class="col-sm-8">
+                                    <input type="text" name="data[phone]" class="form-control" placeholder="请输入学员手机" value="{{ isset(old('data')['phone']) ? old('data')['phone'] : '' }}" required>
+                                    @if($errors->has('phone'))
+                                        <span class="help-block">{{ $errors->first('phone') }}</span>
+                                    @endif
+                                </div>
+                            </div>
+
                             <div class="form-group row">
                                 <label class="col-sm-2 col-sm-offset-1 control-label">年龄</label>
                                 <div class="col-sm-8">

+ 10 - 0
resources/views/teacher/students/edit.blade.php

xqd
@@ -39,6 +39,16 @@
                                 @endif
                             </div>
 
+                            <div class="form-group row {{ $errors->has('phone') ? 'has-error' : '' }}">
+                                <label class="col-sm-2 col-sm-offset-1 control-label">手机</label>
+                                <div class="col-sm-8">
+                                    <input type="text" name="data[phone]" class="form-control" placeholder="请输入学员手机" value="{{ $item->phone }}" required>
+                                    @if($errors->has('phone'))
+                                        <span class="help-block">{{ $errors->first('phone') }}</span>
+                                    @endif
+                                </div>
+                            </div>
+
                             <div class="form-group row">
                                 <label class="col-sm-2 col-sm-offset-1 control-label">年龄</label>
                                 <div class="col-sm-8">

+ 1 - 0
routes/wechat.php

xqd
@@ -2,6 +2,7 @@
 use \Illuminate\Support\Facades\Route;
 
 Route::post('login', 'ApiController@login');
+Route::get('login', 'ApiController@login');
 Route::get('checkPosition', 'ApiController@checkPosition');
 Route::get('startCheckCard', 'ApiController@startCheckCard');
 Route::get('endCheckCard', 'ApiController@endCheckCard');

+ 26 - 2
wechat/app.js

xqd xqd xqd
@@ -6,6 +6,30 @@ App({
     var logs = wx.getStorageSync('logs') || []
     logs.unshift(Date.now())
     wx.setStorageSync('logs', logs)
+
+    wx.checkSession({
+      success: function() {
+
+      },
+      fail: function() {
+        wx.login({
+          success: res => {
+            wx.request({
+              url: api.loginUrl,
+              method: 'GET',
+              data: {
+                code: res.code
+              },
+              success: res => {
+                if(res.data.status == 'success') {
+                  wx.setStorageSync('we_chat_user_id', res.data.id);
+                }
+              }
+            })
+          }
+        })
+      }
+    })
   },
 
   getUserInfo: function(cb) {
@@ -28,7 +52,7 @@ App({
     userInfo: null,
     ptStudent: null
   },
-  login: function (info) {
+  login: function(info) {
     wx.login({
       success: res => {
         console.log(info, res)
@@ -54,4 +78,4 @@ App({
       }
     });
   }
-})
+})

+ 7 - 0
wechat/pages/login/index.js

xqd xqd
@@ -16,6 +16,10 @@ Page({
     
   },
 
+  getPhoneInput: function (res) {
+    console.log(res)
+  },
+
   bindGetUserInfo: function (e) {
     if(e.detail.errMsg == 'getUserInfo:ok') {
       wx.showLoading({
@@ -24,6 +28,9 @@ Page({
       app.login(e.detail)
     }
   },
+  getPhoneNumber: function(e) {
+    console.log(e)
+  },
 
   /**
    * 生命周期函数--监听页面初次渲染完成

+ 2 - 1
wechat/pages/login/index.json

xqd
@@ -7,6 +7,7 @@
     "zan-button-group": "/bower_components/zanui-weapp/dist/btn-group/index",
     "zan-row": "/bower_components/zanui-weapp/dist/row/index",
     "zan-col": "/bower_components/zanui-weapp/dist/col/index",
-    "zan-popup": "../../bower_components/zanui-weapp/dist/popup/index"
+    "zan-popup": "../../bower_components/zanui-weapp/dist/popup/index",
+    "zan-field": "../../bower_components/zanui-weapp/dist/field/index"
   }
 }

+ 27 - 1
wechat/pages/login/index.wxml

xqd xqd
@@ -1,5 +1,5 @@
 <!--pages/login/index.wxml-->
-<view id='login-container' class='{{ loginDialogClass }}'>
+<!-- <view id='login-container' class='{{ loginDialogClass }}'>
   <view class='login-wrapper'>
     <view class='login-dialog'>
       <view class='login-body'>
@@ -17,4 +17,30 @@
       </zan-row>
     </view>
   </view>
+</view> -->
+<view class='login-container'>
+  <form bindsubmit="formSubmit">
+    <zan-row>
+      <view class='sg-row'>
+        <view class='login-avatar-container'>
+          <view class='login-avatar-wrapper'>
+            <open-data type='userAvatarUrl' class='wxAvatarUrl'></open-data>
+          </view>
+        </view>
+      </view>
+    </zan-row>
+    <zan-row>
+      <view class='sg-row'>
+        <zan-col col="16" col-class="custom-zan-col">
+          <zan-field placeholder="输入手机号" focus="true" name='phone' bind:blur="getPhoneInput"></zan-field>
+        </zan-col>
+        <zan-col col="8" col-class="custom-zan-col">
+          <zan-button type='warn' openType="getPhoneNumber" class='get-phone-btn'>获取</zan-button>
+        </zan-col>
+      </view>
+    </zan-row>
+    <view class="btn-area">
+      <button formType="submit" type='primary' class='login-btn'>登录</button>
+    </view>
+  </form>
 </view>

+ 29 - 1
wechat/pages/login/index.wxss

xqd xqd
@@ -1,6 +1,6 @@
 /* pages/login/index.wxss */
 
-#login-container {
+/* #login-container {
   position: absolute;
   top: 0;
   left: 0;
@@ -32,4 +32,32 @@
   padding-top: 20px;
   background-color: white;
   border-radius: 5px;
+} */
+.sg-row {
+  margin: 10px 0;
+}
+.btn-area {
+  margin: 20px 0;
+}
+.login-avatar-wrapper {
+  width: 65px;
+  height: 65px;
+  border-radius: 50px;
+  overflow:hidden;
+  border:3px #e3e3e3 solid;
+}
+.login-avatar-container {
+  display: flex;
+  justify-content: center;
+}
+.login-dialog {
+  padding-top: 20px;
+  background-color: white;
+  border-radius: 5px;
+}
+.login-btn {
+  border-radius: 0;
+}
+.get-phone-btn button {
+  border-radius: 0;
 }