dyjh 6 years ago
parent
commit
6c47ba7372

+ 5 - 0
app/Http/Controllers/Api/V1/Controller.php

xqd
@@ -49,6 +49,11 @@ class Controller extends BaseController
 //        }
 //    }
 
+    public function getUserId()
+    {
+        return session('user_id');
+    }
+
     public function rawPostData() {
         $request = Request::instance();
         $data = $request->getContent();

+ 97 - 4
app/Http/Controllers/Api/V1/IndexController.php

xqd
@@ -9,24 +9,117 @@
 namespace App\Http\Controllers\Api\V1;
 
 
+use App\Models\MessagesFollowerModel;
 use App\Models\MessagesInfoModel;
 use App\Models\MessagesTagModel;
+use App\Models\PaymentInfoModel;
+use App\Models\UserInfoModel;
+use App\Models\WechatAppModel;
+use EasyWeChat\Factory;
 use Illuminate\Http\Request;
 
 class IndexController extends Controller
 {
-    public function indexMenu(Request $request) {
+    private $pay_config;
+
+    public function __construct()
+    {
+        $wechat_app = WechatAppModel::find(1);
+        $this->pay_config = [
+            'app_id' => $wechat_app->appId,
+            'mch_id' => $wechat_app->mchId,
+            'key'    => $wechat_app->key,
+
+            'cert_path'  => public_path().'/pem/cert.pem',
+            'key_path'   => public_path().'/pen/key.pem',
+            'notify_url' => '/api/index/notify',
+        ];
+    }
+
+    public function indexMenu(Request $request)
+    {
         $data = $request->input();
         $menu = MessagesTagModel::orderBy('sort','Desc')->take(3)->get();
         if($data['tag_id'] == 0){
-            $message = MessagesInfoModel::orderBy('id','Desc')->paginate(1);
+            $message = MessagesInfoModel::where('state',0)->orderBy('id','Desc')->paginate(1);
         } else {;
-            $message = MessagesTagModel::find($data['tag_id'])->messgaes()->orderBy('id','Desc')->paginate(1);
+            $message = MessagesTagModel::find($data['tag_id'])->messgaes()->where('state',0)->orderBy('id','Desc')->paginate(1);
             //dd($message);
         }
-
+        foreach ($message as $item){
+            $user = UserInfoModel::find($item->user_id);
+            $message->owner = $user->nickname;
+        }
         return $this->api(compact('menu','message'));
     }
 
 
+
+    public function indexMessage(Request $request)
+    {
+        $id = $request->input('id');
+
+        $message = MessagesInfoModel::where([['state',0],['id',$id]])->first();
+        if(!$message){
+            $data = [
+                'msg' => '该信息不存在或者已完成'
+            ];
+            return $this->api($data);
+        }
+
+        if($message->type == 1){
+            $check_message = MessagesFollowerModel::where([['message_id',$message->id],['user_id',$this->getUserId()]])->first();
+            if(!$check_message){
+                $message->is_pay = 0;
+            } else {
+                $message->is_pay = 1;
+            }
+        } else {
+            $message->is_pay = '';
+        }
+        return $this->api(compact('message'));
+    }
+
+    public function indexGetMessage(Request $request)
+    {
+        $id = $request->input('id');
+        $message = MessagesInfoModel::where([['id',$id],['type',1]])->first();
+        if(!$message){
+            $data = [
+                'msg' => '该信息不存在或者已完成'
+            ];
+            return $this->api($data);
+        }
+        $out_trade_no = 'We'.date('YmdHis').rand(1000,9999);
+        $user = UserInfoModel::find($this->getUserId());
+        $user->money -= $message->price;
+        if($user->money<0){
+            $data = [
+                'msg' => '您的余额已不足,请先充值'
+            ];
+            return $this->api($data);
+        }
+        $user->save();
+        $save['openid'] = $user->openid;
+        $save['out_trade_no'] = $out_trade_no;
+        $save['user_id'] = $this->getUserId();
+        $save['price'] = $message->price;
+        $save['type'] = 2;
+        $res = PaymentInfoModel::create($save);
+        if($res){
+
+            $data = [
+                'msg' => '购买成功',
+            ];
+            return $this->api($data);
+        } else {
+            $data = [
+                'msg' => '购买失败',
+            ];
+            return $this->api($data);
+        }
+    }
+
+
+
 }

+ 6 - 2
app/Http/Controllers/Api/V1/LoginController.php

xqd xqd xqd xqd
@@ -9,6 +9,7 @@
 namespace App\Http\Controllers\Api\V1;
 
 use App\Models\UserInfoModel;
+use App\Models\WechatAppModel;
 use EasyWeChat\Factory;
 use Illuminate\Http\Request;
 
@@ -18,9 +19,10 @@ class LoginController extends Controller
 
     public function __construct()
     {
+        $wechat_app = WechatAppModel::find(1);
         $this->config = [
-            'app_id' => env('APP_ID'),
-            'secret' => env('APP_SECRET'),
+            'app_id' => $wechat_app->appId,
+            'secret' => $wechat_app->appSecret,
             'response_type' => 'array',
         ];
     }
@@ -88,6 +90,7 @@ class LoginController extends Controller
             $res = UserInfoModel::create($add);
             if($res){
                 $data_user = UserInfoModel::where('wechat_open_id',$res['openid'])->first();
+                session(['user_id'=>$check_user->id]);
                 $user = [
                     'user_id'=>$data_user['id'],
                     'avatar'=>$data_user['avatar'],
@@ -106,6 +109,7 @@ class LoginController extends Controller
             $res = UserInfoModel::where('wechat_open_id',$res['openid'])->update($save);
             //echo 111;
             if($res){
+                session(['user_id'=>$check_user->id]);
                 $user = [
                     'user_id'=>$check_user->id,
                     'avatar'=>$data['avatar'],

+ 2 - 1
app/Models/PaymentInfoModel.php

xqd
@@ -40,7 +40,8 @@ class PaymentInfoModel extends BaseModel
         'user_id',
         'openid',
         'out_trade_no',
-        'price'
+        'price',
+        'type'
     ];
 
 }

+ 3 - 2
app/Models/UserInfoModel.php

xqd xqd
@@ -22,7 +22,7 @@ class UserInfoModel extends Authenticatable
      *
      */
     protected $table = 'user_info';
-    /**
+    /**W
      * 主键
      */
     protected $primaryKey = 'id';
@@ -41,7 +41,8 @@ class UserInfoModel extends Authenticatable
         'mobile',
         'avatar',
         'lost_login_time',
-        'remember_token'
+        'remember_token',
+        'money'
     ];
 
 }

+ 41 - 0
app/Models/WechatAppModel.php

xqd
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: 思维定制
+ * Date: 2018/7/13
+ * Time: 10:45
+ */
+
+namespace App\Models;
+
+
+class WechatAppModel extends BaseModel
+{
+    /**
+     * 数据表名
+     *
+     * @var string
+     *
+     */
+    protected $table = 'wechat_app';
+    /**
+     * 主键
+     */
+    protected $primaryKey = 'id';
+
+    //分页
+    protected $perPage = PAGE_NUMS;
+
+    /**
+     * 可以被集体附值的表的字段
+     *
+     * @var string
+     */
+    protected $fillable = [
+        'appId',
+        'appSecret',
+        'mchId',
+        'key',
+        'poundage'
+    ];
+}

+ 33 - 0
database/migrations/2018_07_13_015446_add_state_to_messages_info.php

xqd
@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddStateToMessagesInfo extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('messages_info', function (Blueprint $table) {
+            //
+            $table->unsignedInteger('state')->comment('完成状态:0 未完成 1完成');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('messages_info', function (Blueprint $table) {
+            //
+        });
+    }
+}

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

xqd
@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateTableWechatApp extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('wechat_app', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('appId',255)->comment('appid');
+            $table->string('appSecret',255)->comment('appsecret');
+            $table->string('mchId',255)->comment('商户id');
+            $table->string('key',255)->comment('商户key');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('wechat_app');
+    }
+}

+ 33 - 0
database/migrations/2018_07_13_053627_add_money_to_table_user_info.php

xqd
@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddMoneyToTableUserInfo extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('user_info', function (Blueprint $table) {
+            //
+            $table->decimal('money')->comment('余额');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('user_info', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 33 - 0
database/migrations/2018_07_13_053753_add_poundage_to_table_wechat_app.php

xqd
@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddPoundageToTableWechatApp extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('wechat_app', function (Blueprint $table) {
+            //
+            $table->decimal('poundage')->comment('手续费');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('wechat_app', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 33 - 0
database/migrations/2018_07_13_054306_add_type_to_table_payment_info.php

xqd
@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddTypeToTablePaymentInfo extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('payment_info', function (Blueprint $table) {
+            //
+            $table->unsignedInteger('type')->comment('交易类型 0 充值 1 提现 2 付费只是 3 收费知识');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('payment_info', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 33 - 0
database/migrations/2018_07_13_060004_add_type_to_table_messages_follower.php

xqd
@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddTypeToTableMessagesFollower extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('messages_followers', function (Blueprint $table) {
+            //
+            $table->unsignedInteger('type')->comment('类型 0 付费 1收费');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('messages_followers', function (Blueprint $table) {
+            //
+        });
+    }
+}