Silent 6 سال پیش
والد
کامیت
80911dc051

+ 52 - 1
app/Http/Controllers/WeChat/ApiController.php

@@ -34,7 +34,7 @@ class ApiController extends Controller
         if(!isset($res['session_key'])) {
         if(!isset($res['session_key'])) {
             return response()->json(['status' => 'error', 'info' => '接口错误']);
             return response()->json(['status' => 'error', 'info' => '接口错误']);
         }
         }
-        $res = WeChatUser::firstOrCreate([
+        $res = WeChatUser::updateOrCreate([
             'open_id' => $res['openid'],
             'open_id' => $res['openid'],
         ], [
         ], [
             'code' => $request->input('code'),
             'code' => $request->input('code'),
@@ -84,6 +84,57 @@ class ApiController extends Controller
 //        return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
 //        return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
     }
     }
 
 
+    public function updateUserInfo(Request $request)
+    {
+        if(empty($request->input('id')) || empty($we_chat_user = WeChatUser::find($request->input('id'))) || empty($we_chat_user->session_key)) {
+            return response()->json(['status' => 'error', 'info' => '参数错误']);
+        }
+        if(empty($request->input('iv')) || empty($request->input('encryptedData'))) {
+            return response()->json(['status' => 'error', 'info' => '参数错误']);
+        }
+        $iv = $request->input('iv');
+        $encryptedData = $request->input('encryptedData');
+        $session_key = $we_chat_user->session_key;
+        $app = app('wechat.mini_program');
+
+
+        $info = $app->encryptor->decryptData($session_key, $iv, $encryptedData);
+
+        if(!isset($info['openId'])) {
+            return response()->json(['status' => 'error', 'info' => '接口错误']);
+        }
+
+        $res = $we_chat_user->update([
+            'nickname' => $info['nickName'],
+            'gender' => $info['gender'],
+            'city' => $info['city'],
+            'province' => $info['province'],
+            'country' => $info['country'],
+            'avatar_url' => $info['avatarUrl']
+        ]);
+
+        if(empty($res)) {
+            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' => $we_chat_user->id, 'nickname' => $we_chat_user->nickname, 'avatar_url' => $we_chat_user->avatar_url];
+        return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
+    }
+
     public function checkPosition(Request $request)
     public function checkPosition(Request $request)
     {
     {
         if(empty($request->input('latitude')) || empty($request->input('longitude'))) {
         if(empty($request->input('latitude')) || empty($request->input('longitude'))) {

+ 36 - 0
database/migrations/2018_08_08_151946_add_wechat_info_to_we_chat_user.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddWechatInfoToWeChatUser extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('we_chat_users', function (Blueprint $table) {
+            $table->string('avatar_url', 200)->nullable()->after('id')->comment('avatar_url');
+            $table->string('country', 200)->nullable()->after('id')->comment('country');
+            $table->string('province', 200)->nullable()->after('id')->comment('province');
+            $table->string('city', 200)->nullable()->after('id')->comment('city');
+            $table->string('gender', 200)->nullable()->after('id')->comment('gender');
+            $table->string('nickname', 200)->nullable()->after('id')->comment('nickname');
+            $table->string('open_id', 200)->nullable()->after('id')->comment('open_id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        //
+    }
+}

+ 2 - 1
routes/wechat.php

@@ -14,4 +14,5 @@ Route::get('applyLeave', 'ApiController@applyLeave');
 Route::get('getRemarkTitles', 'ApiController@getRemarkTitles');
 Route::get('getRemarkTitles', 'ApiController@getRemarkTitles');
 Route::get('remarkTeacher', 'ApiController@remarkTeacher');
 Route::get('remarkTeacher', 'ApiController@remarkTeacher');
 Route::get('getArticleContent', 'ApiController@getArticleContent');
 Route::get('getArticleContent', 'ApiController@getArticleContent');
-Route::get('getAnnounces', 'ApiController@getAnnounces');
+Route::get('getAnnounces', 'ApiController@getAnnounces');
+Route::post('updateUserInfo', 'ApiController@updateUserInfo');

+ 12 - 13
wechat/app.js

@@ -14,19 +14,18 @@ App({
       fail: function() {
       fail: function() {
         wx.login({
         wx.login({
           success: res => {
           success: res => {
-            console.log(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);
-            //     }
-            //   }
-            // })
+            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);
+                }
+              }
+            })
           }
           }
         })
         })
       }
       }

+ 2 - 1
wechat/app.json

@@ -11,7 +11,8 @@
     "pages/rate-review/rate-review",
     "pages/rate-review/rate-review",
     "pages/my-log/my-log",
     "pages/my-log/my-log",
     "pages/apply-leave/apply-leave",
     "pages/apply-leave/apply-leave",
-    "pages/login/index"
+    "pages/login/index",
+    "pages/bind-phone/index"
   ],
   ],
   "window": {
   "window": {
     "backgroundTextStyle": "light",
     "backgroundTextStyle": "light",

+ 66 - 0
wechat/pages/bind-phone/index.js

@@ -0,0 +1,66 @@
+// pages/bind-phone/index.js
+Page({
+
+  /**
+   * 页面的初始数据
+   */
+  data: {
+  
+  },
+
+  /**
+   * 生命周期函数--监听页面加载
+   */
+  onLoad: function (options) {
+  
+  },
+
+  /**
+   * 生命周期函数--监听页面初次渲染完成
+   */
+  onReady: function () {
+  
+  },
+
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow: function () {
+  
+  },
+
+  /**
+   * 生命周期函数--监听页面隐藏
+   */
+  onHide: function () {
+  
+  },
+
+  /**
+   * 生命周期函数--监听页面卸载
+   */
+  onUnload: function () {
+  
+  },
+
+  /**
+   * 页面相关事件处理函数--监听用户下拉动作
+   */
+  onPullDownRefresh: function () {
+  
+  },
+
+  /**
+   * 页面上拉触底事件的处理函数
+   */
+  onReachBottom: function () {
+  
+  },
+
+  /**
+   * 用户点击右上角分享
+   */
+  onShareAppMessage: function () {
+  
+  }
+})

+ 13 - 0
wechat/pages/bind-phone/index.json

@@ -0,0 +1,13 @@
+{
+  "navigationBarBackgroundColor": "#000",
+  "navigationBarTextStyle": "white",
+  "navigationBarTitleText": "钢琴时间",
+  "usingComponents": {
+    "zan-button": "/bower_components/zanui-weapp/dist/btn/index",
+    "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-field": "../../bower_components/zanui-weapp/dist/field/index"
+  }
+}

+ 27 - 0
wechat/pages/bind-phone/index.wxml

@@ -0,0 +1,27 @@
+<!--pages/bind-phone/index.wxml-->
+<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' bindgetphonenumber='getPhoneNumber'>获取</zan-button>
+        </zan-col>
+      </view>
+    </zan-row>
+    <view class="btn-area">
+      <button formType="submit" type='primary' class='login-btn'>登录</button>
+    </view>
+  </form>
+</view>

+ 29 - 0
wechat/pages/bind-phone/index.wxss

@@ -0,0 +1,29 @@
+/* pages/bind-phone/index.wxss */
+.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;
+}

+ 24 - 8
wechat/pages/login/index.js

@@ -1,5 +1,6 @@
 // pages/login/index.js
 // pages/login/index.js
 var app = getApp();
 var app = getApp();
+var api = require('../../utils/api.js');
 Page({
 Page({
 
 
   /**
   /**
@@ -16,21 +17,36 @@ Page({
     
     
   },
   },
 
 
-  getPhoneInput: function (res) {
-    console.log(res)
-  },
-
   bindGetUserInfo: function (e) {
   bindGetUserInfo: function (e) {
     if(e.detail.errMsg == 'getUserInfo:ok') {
     if(e.detail.errMsg == 'getUserInfo:ok') {
       wx.showLoading({
       wx.showLoading({
         title: '登录中',
         title: '登录中',
       })
       })
-      app.login(e.detail)
+      console.log(wx.getStorageSync('we_chat_user_id'))
+      console.log(e.detail.iv)
+      console.log(e.detail.encryptedData)
+      wx.request({
+        url: api.updateUserInfoUrl,
+        method: 'POST',
+        data: {
+          id: wx.getStorageSync('we_chat_user_id'),
+          iv: e.detail.iv,
+          encryptedData: e.detail.encryptedData
+        },
+        success: info => {
+          if (info.data.status == 'success') {
+            wx.setStorageSync('we_chat_user', info.data.data);
+            wx.redirectTo({
+              url: 'pages/bind-phone/index',
+            })
+          }
+        },
+        complete: res => {
+          wx.hideLoading()
+        }
+      });
     }
     }
   },
   },
-  getPhoneNumber: function(e) {
-    console.log(e)
-  },
 
 
   /**
   /**
    * 生命周期函数--监听页面初次渲染完成
    * 生命周期函数--监听页面初次渲染完成

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

@@ -1,5 +1,5 @@
 <!--pages/login/index.wxml-->
 <!--pages/login/index.wxml-->
-<!-- <view id='login-container' class='{{ loginDialogClass }}'>
+<view id='login-container' class='{{ loginDialogClass }}'>
   <view class='login-wrapper'>
   <view class='login-wrapper'>
     <view class='login-dialog'>
     <view class='login-dialog'>
       <view class='login-body'>
       <view class='login-body'>
@@ -17,30 +17,4 @@
       </zan-row>
       </zan-row>
     </view>
     </view>
   </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>
 </view>

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

@@ -1,6 +1,6 @@
 /* pages/login/index.wxss */
 /* pages/login/index.wxss */
 
 
-/* #login-container {
+#login-container {
   position: absolute;
   position: absolute;
   top: 0;
   top: 0;
   left: 0;
   left: 0;
@@ -32,32 +32,4 @@
   padding-top: 20px;
   padding-top: 20px;
   background-color: white;
   background-color: white;
   border-radius: 5px;
   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;
 }
 }

+ 1 - 0
wechat/utils/api.js

@@ -15,4 +15,5 @@ module.exports = {
   remarkTeacherUrl: headUrl + 'remarkTeacher',
   remarkTeacherUrl: headUrl + 'remarkTeacher',
   getArticleContentUrl: headUrl + 'getArticleContent',
   getArticleContentUrl: headUrl + 'getArticleContent',
   getAnnouncesUrl: headUrl + 'getAnnounces',
   getAnnouncesUrl: headUrl + 'getAnnounces',
+  updateUserInfoUrl: headUrl + 'updateUserInfo',
 }
 }