瀏覽代碼

给好友设置备注

lizhen_gitee 2 月之前
父節點
當前提交
4dbab32def
共有 2 個文件被更改,包括 104 次插入0 次删除
  1. 40 0
      application/api/controller/UserInfo.php
  2. 64 0
      application/common/model/UserRemark.php

+ 40 - 0
application/api/controller/UserInfo.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use app\common\model\UserRemark;
+use app\common\library\Keyworld;
+use think\Db;
+
+/**
+ *
+ */
+class UserInfo extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+    // 设置备注
+    public function setRemark()
+    {
+        $params  = $this->request->param();
+        $user_id = $this->auth->id;
+        if (empty($params['to_user_id'])) {
+            return $this->error('参数缺失');
+        }
+        $model = new UserRemark();
+        if (!empty($params['remark'])) {
+            [$res, $msg] = $model->setRemark($user_id, $params['to_user_id'], Keyworld::sensitive($params['remark']));
+        } else {
+            [$res, $msg] = $model->delRemark($user_id, $params['to_user_id']);
+        }
+
+        if (!$res) {
+            return $this->error($msg);
+        }
+        return $this->success($msg);
+    }
+
+
+}

+ 64 - 0
application/common/model/UserRemark.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 系统消息模型
+ */
+class UserRemark extends Model
+{
+
+    // 表名
+    protected $name = 'user_remark';
+
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = false;
+
+    // 定义时间戳字段名
+    protected $createTime = false;
+    protected $updateTime = false;
+    protected $deleteTime = false;
+
+    /**
+     * 设置备注
+     * @param int $user_id
+     * @param int $to_user_id
+     * @param string $remark
+     * @return array
+     */
+    public function setRemark(int $user_id, int $to_user_id, string $remark = '')
+    {
+        if ($user_id === $to_user_id) {
+            return [false, '不可以给自己设置备注'];
+        }
+
+        if ($info = $this->where(['user_id' => $user_id, 'to_user_id' => $to_user_id])->find()) {
+            $res = $this->where('id', $info['id'])->update(['remark' => $remark, 'updated_time' => time()]);
+        } else {
+            $res = $this->insert(['user_id' => $user_id, 'to_user_id' => $to_user_id, 'remark' => $remark, 'updated_time' => time()]);
+        }
+        if (!$res) {
+            return [false, '设置失败'];
+        }
+        return [true, '设置成功'];
+    }
+
+    /**
+     * 删除备注
+     * @param int $user_id
+     * @param int $to_user_id
+     * @return array
+     */
+    public function delRemark(int $user_id, int $to_user_id)
+    {
+        if ($info = $this->where(['user_id' => $user_id, 'to_user_id' => $to_user_id])->find()) {
+            $res = $this->where('id', $info['id'])->update(['remark' => '', 'updated_time' => time()]);
+            if (!$res) {
+                return [false, '设置失败'];
+            }
+        }
+        return [true, '设置成功'];
+    }
+}