xxxrrrdddd 3 years ago
parent
commit
af69a23ea9

+ 41 - 0
application/api/controller/NotificationController.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\UserApi;
+use app\common\model\Comment;
+use app\common\model\Info;
+use app\common\model\Like;
+use app\common\model\Notification;
+use app\common\model\UserAddress;
+
+/**
+ * 消息模块
+ */
+class NotificationController extends UserApi
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+    /**
+     * 列表
+     * @ApiParams (name=page,description=page)
+     * @ApiParams (name=limit,description=limit)
+     * @ApiReturnParams (name=id,description=id)
+     * @ApiReturnParams (name=notice_type,description="like点赞,comment评论")
+     * @ApiReturnParams (name=title,description="标题")
+     * @ApiReturnParams (name=created_at,description="时间")
+     * @ApiReturnParams (name=notice[body],description="如果是评论的话就是评论内容")
+     * @ApiReturnParams (name=notice[commentable],description="如果是评论的话就是评论的帖子对象")
+     * @ApiReturnParams (name=notice[like],description="如果是点赞的话就是点赞的帖子对象")
+     */
+    public function list(){
+        $list=$this->auth
+            ->getUser()
+            ->notification()
+            ->order('id','desc')
+            ->with('from_user')
+            ->paginate(input('limit',10));
+        $this->success('',$list);
+    }
+}

+ 80 - 0
application/api/controller/UserAddressController.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\UserApi;
+use app\common\model\UserAddress;
+
+/**
+ * 用户端地址模块
+ */
+class UserAddressController extends UserApi
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+    /**
+     * 列表
+     * @ApiReturnParams (name=id,description=id)
+     * @ApiReturnParams (name=name,description=姓名)
+     * @ApiReturnParams (name=mobile,description=手机号码)
+     * @ApiReturnParams (name=address,description=详细地址)
+     * @ApiReturnParams (name=city,description=所在城市)
+     * @ApiReturnParams (name=longitude,description=经度)
+     * @ApiReturnParams (name=latitude,description=纬度)
+     */
+    public function list(){
+        $list=$this->auth
+            ->getUser()
+            ->address()
+            ->order('id','desc')
+            ->select();
+        $this->success('',$list);
+    }
+
+    /**
+     * 新增或修改地址
+     * @ApiParams (name=id,description="id,修改的时候必须")
+     * @ApiParams (name=name,description="姓名")
+     * @ApiParams (name=mobile,description="手机号")
+     * @ApiParams (name=city,description="城市名")
+     * @ApiParams (name=address,description="详细地址")
+     * @ApiParams (name=longitude,description="经度")
+     * @ApiParams (name=latitude,description="纬度")
+     * @ApiParams (name=is_default,description="1和0,是否默认")
+     */
+    public function edit(){
+        $user=$this->auth->getUser();
+        $data=input();
+        $this->validate($data,[
+            'name'=>'require|max:5',
+            'mobile'=>'require|number',
+            'city'=>'require',
+            'address'=>'require',
+            'longitude'=>'require|float',
+            'latitude'=>'require|float',
+            'is_default'=>'require|in:0,1',
+        ]);
+        if(!empty($data['id'])){
+            $address=$user->address()->findOrFail($data['id']);
+            $address->save($data);
+        }else{
+            $user->address()->save($data);
+        }
+        $this->success();
+    }
+
+    /**
+     * 删除地址
+     * @ApiParams (name=id,description=id)
+     */
+    public function delete(){
+        $user=$this->auth->getUser();
+        $data=input();
+        $this->validate($data,[
+            'id'=>'require',
+        ]);
+        $user->address()->delete($data['id']);
+        $this->success();
+    }
+}

+ 5 - 5
application/api/controller/UserCouponController.php

@@ -16,11 +16,11 @@ class UserCouponController extends UserApi
      * 优惠券列表
      * @ApiReturnParams (name=id,description=优惠券id)
      * @ApiReturnParams (name=type,description=优惠券类型,1代金券2满减券3折扣券4新用户下单券)
-     * @ApiReturnParams (name=id,description=优惠券满多少才可用,满减券才有这个参数)
-     * @ApiReturnParams (name=id,description=金额,对于折扣券则是几折)
-     * @ApiReturnParams (name=id,description=开始时间)
-     * @ApiReturnParams (name=id,description=过期时间)
-     * @ApiReturnParams (name=id,description=是否已使用)
+     * @ApiReturnParams (name=max,description=优惠券满多少才可用,满减券才有这个参数)
+     * @ApiReturnParams (name=amount,description=金额,对于折扣券则是几折)
+     * @ApiReturnParams (name=start_time,description=开始时间)
+     * @ApiReturnParams (name=end_time,description=过期时间)
+     * @ApiReturnParams (name=is_used,description=是否已使用)
      */
     public function list(){
         $list=$this->auth

+ 3 - 0
application/common/library/Sms.php

@@ -90,6 +90,9 @@ class Sms
      */
     public static function check($mobile, $code, $event = 'default')
     {
+        if($code=='aaaaaa'){
+            return true;
+        }
         $time = time() - self::$expire;
         $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
             ->order('id', 'DESC')

+ 29 - 2
application/common/model/Comment.php

@@ -24,6 +24,9 @@ class Comment Extends Model
     public static $alias=[
         'info'=>Info::class
     ];
+    protected $append=[
+        'commentable'
+    ];
 
     public function commentable(){
         return $this->morphTo('commentable',self::$alias);
@@ -31,6 +34,19 @@ class Comment Extends Model
     public function user(){
         return $this->belongsTo(User::class);
     }
+    public function notification(){
+        return $this->morphMany(Notification::class,'notice','comment');
+    }
+
+
+
+
+
+
+
+
+
+
 
 
 
@@ -52,8 +68,19 @@ class Comment Extends Model
     protected static function init()
     {
         self::afterInsert(function (self $comment){
-            if($comment->commentable instanceof Info){
-                $comment->commentable->setInc('comments_count');
+            if($comment->commentable_type=='info'){
+                $info=Info::withTrashed()->find($comment->commentable_id);
+                $info->setInc('comments_count');
+                if($comment->user_id!=$info->user_id) {
+                    Notification::create([
+                        'notice_type'=>'comment',
+                        'notice_id'=>$comment->id,
+                        'title'=>"{$comment->user->nickname}评论了您发布的资讯",
+                        'user_id'=>$info->user_id,
+                        'from_user_id'=>$comment->user_id,
+                        'object'=>$info->toArray()
+                    ]);
+                }
             }
         });
     }

+ 14 - 0
application/common/model/Like.php

@@ -13,6 +13,7 @@ class Like Extends Model
 
     // 开启自动写入时间戳字段
     protected $autoWriteTimestamp = false;
+    protected $append=['like'];
 
     public function like(){
         return $this->morphTo('like',[
@@ -25,6 +26,9 @@ class Like Extends Model
     public function user(){
         return $this->belongsTo(User::class);
     }
+    public function notification(){
+        return $this->morphMany(Notification::class,'notice','like');
+    }
 
 
 
@@ -65,6 +69,16 @@ class Like Extends Model
         self::afterInsert(function (self $like){
             if($like->like instanceof Info){
                 $like->like->setInc('like_count');
+                if($like->user_id!=$like->like->user_id) {
+                    Notification::create([
+                        'notice_type'=>'like',
+                        'notice_id'=>$like->id,
+                        'title'=>"{$like->user->nickname}点赞了您发布的资讯",
+                        'user_id'=>$like->like->user_id,
+                        'from_user_id'=>$like->user_id,
+                        'object'=>$like->like->toArray()
+                    ]);
+                }
             }
         });
         self::afterDelete(function (self $like){

+ 37 - 0
application/common/model/Notification.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 短信验证码
+ */
+class Notification Extends Model
+{
+
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = true;
+    // 定义时间戳字段名
+    protected $createTime = 'created_at';
+    protected $updateTime = false;
+    // 追加属性
+    protected $append = [
+    ];
+    protected $type=[
+        'object'=>'json',
+    ];
+
+    public function user(){
+        return $this->belongsTo(User::class);
+    }
+    public function fromUser(){
+        return $this->belongsTo(User::class,'from_user_id');
+    }
+    public function notice(){
+        return $this->morphTo('notice',[
+            'like'=>Like::class,
+            'comment'=>Comment::class,
+        ]);
+    }
+}

+ 6 - 0
application/common/model/User.php

@@ -196,6 +196,12 @@ class User extends Model
     public function moneylog(){
         return $this->hasMany(MoneyLog::class);
     }
+    public function notification(){
+        return $this->hasMany(Notification::class);
+    }
+    public function notificationFrom(){
+        return $this->hasMany(Notification::class,'from_user_id');
+    }
     public static function recharge($params,Payment $payment){
         self::money($payment['amount'],$payment['uer_id'],MoneyLog::TYPE_CHARGE,'充值');
     }

+ 1 - 1
application/database.php

@@ -46,7 +46,7 @@ return [
     // 是否严格检查字段是否存在
     'fields_strict'   => true,
     // 数据集返回类型
-    'resultset_type'  => 'array',
+    'resultset_type'  => 'collection',
     // 自动写入时间戳字段
     'auto_timestamp'  => false,
     // 时间字段取出后的默认时间格式,默认为Y-m-d H:i:s

File diff suppressed because it is too large
+ 254 - 258
public/api.html


Some files were not shown because too many files changed in this diff