瀏覽代碼

移植了userData文件函数到ctlShop

MoYuWang 9 月之前
父節點
當前提交
62d0dec4af

+ 3 - 3
Classes/IAP/Ctl/IAPProcess.cpp

@@ -12,7 +12,7 @@
 #include "RUReboltLayer.h"
 #include "IAPUtils.h"
 
-#include "IAPUserData.hpp"
+#include "IAPCtlShop.hpp"
 #include <cocos2d.h>
 
 IAPProcess* IAPProcess::_instance = nullptr;
@@ -117,9 +117,9 @@ void IAPProcess::onProductPurchaseSuccess(const std::string& pid) {
         _cbIfSuccess();
     }
     // 记录用户购买信息
-    iap::IAPUserData::getInstance()->addBuyInfo(pid);
+    iap::IAPCtlShop::createWith()->addUserBuyInfo(pid);
     printf("购买%s成功\n", pid.c_str());
-    printf("用户类型为: %d\n", iap::IAPUserData::getInstance()->getUserBuyType());
+    printf("用户类型为: %d\n", iap::IAPCtlShop::createWith()->getUserBuyType());
     
 //    // 判断当前是否在家装场景,如果在游戏场景,则等待
 //    if (bNeedAcceptNow) {

+ 93 - 0
Classes/IAP/IAPCtlShop.cpp

@@ -16,6 +16,10 @@
 #include "IAPCtlShopUI.hpp"
 #include "IAPUserData.hpp"
 
+#include <algorithm>
+#include <string>
+#include <regex>
+
 NS_IAP_BEGIN
 
 IAPCtlShop* IAPCtlShop::_instance = nullptr;
@@ -78,8 +82,97 @@ void IAPCtlShop::showPlacementsInNode(cocos2d::Node *pParent, const vector<std::
     
 }
 
+UserBuyType IAPCtlShop::getUserBuyType(){
+    // 如果没有购买记录
+    auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
+    if(buyInfos.size() == 0) return UserBuyType::NoShopping;
+    
+    auto conf = IAPConf::getInstance();
+    vector<GoodsInfo> goodInfos;
+    conf->getAllGoods(goodInfos);
+    
+    struct SortVec{
+        std::string id;
+        std::string price;
+        bool isSpecial;
+    };
+    vector<SortVec> vec;
+    
+    std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
+    for(const auto& goodInfo : goodInfos){
+        bool isSpecial = goodInfo.style == "mostValued" ? true : false;
+        std::smatch match;
+        if (std::regex_search(goodInfo.cost, match, re)) {
+            SortVec sv;
+            sv.id = goodInfo.id;
+            sv.price = match[0];    // 提取价格
+            sv.isSpecial = isSpecial;
+            vec.push_back(sv);
+        } else {
+            log("IAPUserData::getUserBuyType : 转换失败");
+        }
+    }
+    // 按照价格排序 , 价格相同按照是否为special商品排序
+    sort(vec.begin(), vec.end(), [](SortVec a,SortVec b){
+        if(a.price != b.price) return stof(a.price) < stof(b.price);
+        if(a.isSpecial)return true;
+        if(b.isSpecial)return false;
+        return true;
+    });
+    
+    bool onlySpecial = true;
+    bool isBuyExpensive = false;
+    float maxAmount = getMaxAmount();
+    for(const auto& buyInfo : buyInfos){
+        
+        for(size_t i = 0; i < vec.size(); i++){
+            if(buyInfo != vec[i].id)continue;
+            if(!vec[i].isSpecial) onlySpecial = false;
+            if(stof(vec[i].price) == maxAmount) isBuyExpensive = true;
+        }
+        
+    }
+    
+    // 购买过最贵商品
+    if(isBuyExpensive)return UserBuyType::LotShopping;
+    // 仅购买Special商品
+    if(onlySpecial)return UserBuyType::LittleShopping;
+    // 正常购物
+    return UserBuyType::NormalShopping;
+}
+
+std::vector<std::string> IAPUserData::getBuyInfos(){
+    return _buyInfos;
+}
+
+void IAPCtlShop::addUserBuyInfo(std::string commodityID){
+    iap::IAPUserData::getInstance()->addBuyInfo(commodityID);
+}
+
 void IAPCtlShop::clearUserBuyInfo(){
     iap::IAPUserData::getInstance()->clearBuyInfo();
 }
 
+
+float IAPCtlShop::getMaxAmount(){
+    float maxn = 0.0;
+    
+    auto conf = IAPConf::getInstance();
+    vector<GoodsInfo> goodInfos;
+    conf->getAllGoods(goodInfos);
+    
+    std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
+    for(const auto& goodInfo : goodInfos){
+        std::smatch match;
+        if (std::regex_search(goodInfo.cost, match, re)) {
+            maxn = std::max(maxn, stof(match[0]));    // 提取价格
+        } else {
+            log("IAPUserData::getUserBuyType : 转换失败");
+        }
+    }
+    
+    return maxn;
+}
+
+
 NS_IAP_END

+ 18 - 0
Classes/IAP/IAPCtlShop.hpp

@@ -19,6 +19,13 @@ class IAPCtlShopUI;
 
 NS_IAP_BEGIN
 
+enum UserBuyType{
+    NoShopping,         // 无购买行为
+    LittleShopping,     // 仅购买特惠
+    NormalShopping,     // 正常消费
+    LotShopping         // 土豪
+};
+
 class IAPCtlShop : public cocos2d::Ref {
 public:
     // 创建一个商店控制器
@@ -60,10 +67,21 @@ public:
     // plIds 需要的版位id
     void showPlacementsInNode(cocos2d::Node* pParent, const vector<std::string>& plIds);
     
+    
+    // 获取用户类型
+    UserBuyType getUserBuyType();
+    
+    // 添加用户购买信息
+    void addUserBuyInfo(std::string commodityID);
+    
     // 清除用户购买信息
     void clearUserBuyInfo();
     
 private:
+    // 获取用户购买信息中最贵的金额
+    float getMaxAmount();
+    
+private:
     static IAPCtlShop* _instance;
     
     IAPConf* _conf;

+ 1 - 2
Classes/IAP/Shop/IAPCtlShopUI.cpp

@@ -11,7 +11,6 @@
 #include "RUPlatform.h"
 
 #include "IAPConf.hpp"
-#include "IAPUserData.hpp"
 #include "IAPCtlShopUI.hpp"
 #include "IAPCtlShopItem.hpp"
 #include <regex>
@@ -132,7 +131,7 @@ void IAPCtlShopUI::constructShopItem(bool bShowAll) {
     // 重新获取
     if(bShowAll)IAPConf::getInstance()->getAllGoods(_goodsInfo);
     // 根据用户类型进行礼包排序
-    switch (iap::IAPUserData::getInstance()->getUserBuyType()) {
+    switch (iap::IAPCtlShop::createWith()->getUserBuyType()) {
         case iap::UserBuyType::NoShopping:
             sortGoods(true);
             break;

+ 2 - 88
Classes/IAP/User/IAPUserData.cpp

@@ -8,10 +8,6 @@
 #include "IAPUserData.hpp"
 #include "IAPConf.hpp"
 
-#include <algorithm>
-#include <string>
-#include <regex>
-
 NS_IAP_BEGIN
 
 IAPUserData* IAPUserData::_instance = nullptr;
@@ -25,96 +21,14 @@ IAPUserData* IAPUserData::getInstance(){
 
 void IAPUserData::init(){
     
-    
-    
 }
 
 void IAPUserData::addBuyInfo(std::string commodityID){
-    buyInfos.push_back(commodityID);
-}
-
-UserBuyType IAPUserData::getUserBuyType(){
-    // 如果没有购买记录
-    if(buyInfos.size() == 0) return UserBuyType::NoShopping;
-    
-    auto conf = IAPConf::getInstance();
-    vector<GoodsInfo> goodInfos;
-    conf->getAllGoods(goodInfos);
-    
-    struct SortVec{
-        std::string id;
-        std::string price;
-        bool isSpecial;
-    };
-    vector<SortVec> vec;
-    
-    std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
-    for(const auto& goodInfo : goodInfos){
-        bool isSpecial = goodInfo.style == "mostValued" ? true : false;
-        std::smatch match;
-        if (std::regex_search(goodInfo.cost, match, re)) {
-            SortVec sv;
-            sv.id = goodInfo.id;
-            sv.price = match[0];    // 提取价格
-            sv.isSpecial = isSpecial;
-            vec.push_back(sv);
-        } else {
-            log("IAPUserData::getUserBuyType : 转换失败");
-        }
-    }
-    // 按照价格排序 , 价格相同按照是否为special商品排序
-    sort(vec.begin(), vec.end(), [](SortVec a,SortVec b){
-        if(a.price != b.price) return stof(a.price) < stof(b.price);
-        if(a.isSpecial)return true;
-        if(b.isSpecial)return false;
-        return true;
-    });
-    
-    bool onlySpecial = true;
-    bool isBuyExpensive = false;
-    float maxAmount = getMaxAmount();
-    for(const auto& buyInfo : buyInfos){
-        
-        for(size_t i = 0; i < vec.size(); i++){
-            if(buyInfo != vec[i].id)continue;
-            if(!vec[i].isSpecial) onlySpecial = false;
-            if(stof(vec[i].price) == maxAmount) isBuyExpensive = true;
-        }
-        
-    }
-    
-    // 购买过最贵商品
-    if(isBuyExpensive)return UserBuyType::LotShopping;
-    // 仅购买Special商品
-    if(onlySpecial)return UserBuyType::LittleShopping;
-    // 正常购物
-    return UserBuyType::NormalShopping;
+    _buyInfos.push_back(commodityID);
 }
 
 void IAPUserData::clearBuyInfo(){
-    buyInfos.clear();
-}
-
-float IAPUserData::getMaxAmount(){
-    float maxn = 0.0;
-    
-    auto conf = IAPConf::getInstance();
-    vector<GoodsInfo> goodInfos;
-    conf->getAllGoods(goodInfos);
-    
-    std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
-    for(const auto& goodInfo : goodInfos){
-        std::smatch match;
-        if (std::regex_search(goodInfo.cost, match, re)) {
-            maxn = std::max(maxn, stof(match[0]));    // 提取价格
-        } else {
-            log("IAPUserData::getUserBuyType : 转换失败");
-        }
-    }
-    
-    return maxn;
+    _buyInfos.clear();
 }
 
-
-
 NS_IAP_END

+ 3 - 10
Classes/IAP/User/IAPUserData.hpp

@@ -13,12 +13,6 @@
 
 NS_IAP_BEGIN
 
-enum UserBuyType{
-    NoShopping,         // 无购买行为
-    LittleShopping,     // 仅购买特惠
-    NormalShopping,     // 正常消费
-    LotShopping         // 土豪
-};
 
 class IAPUserData {
 public:
@@ -30,20 +24,19 @@ public:
     // 参数说明 : 商品id
     void addBuyInfo(std::string commodityID);
     
-    // 获取用户类型
-    UserBuyType getUserBuyType();
+    // 获取用户购买记录
+    std::vector<std::string> getBuyInfos();
     
     // 清除所有购买信息
     void clearBuyInfo();
 
 private:
     
-    float getMaxAmount();
     
 private:
     static IAPUserData* _instance;
     
-    std::vector<std::string> buyInfos;    // 用户购买信息
+    std::vector<std::string> _buyInfos;    // 用户购买信息
 };
 
 NS_IAP_END