Przeglądaj źródła

用户资产接口

shish 6 lat temu
rodzic
commit
2b1dea6450

+ 27 - 10
app/mobile/controllers/BaseController.php

@@ -1,17 +1,10 @@
 <?php
+
 namespace mobile\controllers;
 
-use biz\goods\services\CategoryRelateService;
-use biz\stat\services\VisitByDayService;
 use biz\user\services\UserService;
-use common\components\error;
-use Yii;
-use common\services\xhMerchantExtendService;
-use common\services\xhUserService;
-use common\components\jsSDK;
-use common\components\weixinUtil;
+use common\components\jwt;
 use common\components\util;
-use common\services\xhMerchantService;
 
 /**
  * Created by PhpStorm.
@@ -21,5 +14,29 @@ use common\services\xhMerchantService;
  */
 class BaseController extends PublicController
 {
-
+	
+	public $validate = true;
+	public $withoutLogin = [];//不需要登陆可以访问的方法
+	public $userId, $user;
+	
+	public function beforeAction($action)
+	{
+		//token验证
+		$userId = jwt::checkLogin();
+		if (empty($userId)) {
+			$this->validate = false;
+		} else {
+			$this->validate = true;
+			$this->userId = $userId;
+			$this->user = UserService::getById($userId);
+		}
+		//游客可以访问的方法
+		if (in_array($action->id, $this->withoutLogin)) {
+			$this->validate = true;
+		}
+		if ($this->validate == false) {
+			util::notLogin();
+		}
+		return parent::beforeAction($action);
+	}
 }

+ 1 - 0
app/mobile/controllers/MobileController.php

@@ -5,6 +5,7 @@ namespace mobile\controllers;
 use biz\goods\services\CategoryRelateService;
 use biz\goods\services\GoodsUsageService;
 use biz\goods\services\UsageRelationService;
+use common\components\jwt;
 use Yii;
 use common\components\configDict;
 use common\components\util;

+ 24 - 0
app/mobile/controllers/UserAssetController.php

@@ -0,0 +1,24 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: ouyangyz <ouyangyanzhong@163.com>
+ * Date: 2019/11/22
+ * Time: 19:09
+ */
+
+namespace mobile\controllers;
+
+use biz\user\services\UserAssetService;
+use common\components\util;
+
+class UserAssetController extends BaseController
+{
+
+	//获取客户资产 shish 2019.11.22
+	public function actionGetAsset()
+	{
+		$asset = UserAssetService::getByUserId($this->userId);
+		util::success($asset);
+	}
+
+}

+ 1 - 1
app/mobile/views/main/index.php

@@ -33,7 +33,7 @@ http://m.huaml.com/#main/index?account=12358&shopId=15680
     }
 
     var args = { suffixUrl: "#main/index?account=12358",token: "<?= $token ?>"};
-    StandardPost("<?= $host ?>/auth/prepare",args)
+    StandardPost("<?= $host ?>/mobile/index?account=12358",args)
     <?php } ?>
 </script>
 </body>

+ 17 - 4
common/components/jwt.php

@@ -70,20 +70,33 @@ class jwt
 	{
 		$token = (new Parser())->parse($token);
 		//数据校验
-		$data = new ValidationData(); // 使用当前时间来校验数据
+		$data = new ValidationData();//使用当前时间来校验数据
 		if (!$token->validate($data)) {
-			return ['code' => 1];
+			Yii::info('token时间验证没有通过。token:' . $token);
+			return 0;
 		}
 		//token校验
 		$signer = new Sha256();//生成JWT时使用的加密方式
 		$salt = Yii::$app->params['secretKey'];
 		if (!$token->verify($signer, new Key($salt))) {
-			return ['code' => 1];
+			Yii::info('token解密没有通过。token:' . $token);
+			return 0;
 		}
 		$token->getHeaders(); // 获取JWT的Header(头部)信息
 		$token->getClaims(); // 获取JWT的PayLoad(负载)信息
 		$userId = $token->getClaim('userId');
-		return ['code' => 0, 'data' => ['userId' => $userId]];
+		return $userId;
+	}
+	
+	//判断有没登陆并返回userId shish 2019.11.22
+	public static function checkLogin()
+	{
+		$header = httpUtil::getHeader();
+		$token = isset($header['token']) ? $header['token'] : '';
+		if (empty($token)) {
+			return 0;
+		}
+		return self::validate($token);
 	}
 	
 }

+ 6 - 1
common/components/util.php

@@ -461,7 +461,12 @@ class util
 	{
 		self::encode(['code' => 1, 'msg' => '操作成功', 'data' => []]);
 	}
-	
+
+	public static function notLogin()
+	{
+		self::encode(['code' => -1, 'msg' => '没有登陆', 'data' => []]);
+	}
+
 	//shisq
 	public static function echoStatus($num, $key, $status = 'status')
 	{

+ 5 - 0
sql.txt

@@ -0,0 +1,5 @@
+shish 2019.11.22
+!!注意要将成积分移到成长值里
+
+shish 2019.11.22
+ALTER TABLE xhUserAsset ADD growth INT NOT NULL DEFAULT 0 COMMENT '成长值' AFTER `point`;