jiaojiao7 4 年之前
父節點
當前提交
29dfb82238
共有 4 個文件被更改,包括 71 次插入42 次删除
  1. 4 7
      manage/user/user.go
  2. 8 5
      service/user.go
  3. 45 28
      static/templates/chooseProduct.html
  4. 14 2
      static/templates/projectList.html

+ 4 - 7
manage/user/user.go

@@ -5,7 +5,6 @@ import (
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
 	"io/ioutil"
-	"log"
 	"net/http"
 	"sfbase/global"
 	sutils "sfbase/utils"
@@ -88,21 +87,19 @@ func userProductChoose(c *gin.Context) {
 	}
 	var dataMap map[string]interface{}
 	json.Unmarshal([]byte(bs), &dataMap)
-	log.Println("获取前端参数:", dataMap)
+	//log.Println("获取前端参数:", dataMap)
 	appId := sutils.ObjToString(dataMap["appId"])
 	buyType := sutils.IntAll(dataMap["buyType"])
 	products := dataMap["products"]
 	productArr := make([]map[string]interface{}, 0)
 	if _products, ok := products.([]interface{}); ok {
 		productArr = sutils.ObjArrToMapArr(_products)
-		log.Println(productArr)
+		//log.Println(productArr)
 	}
-	log.Println("appid", appId)
-	log.Println(len(productArr), appId)
 	if appId != "" && len(productArr) > 0 {
 		status, haveProductId, err := service.CreateUserProduct(appId, productArr, buyType)
-		log.Println(status, haveProductId, err)
-		if status == 1 && err == nil {
+		//log.Println(status, haveProductId, err)
+		if status == 1 && len(err) == 0 {
 			if haveProductId != "" {
 				response.OkWithMessage("用户已购买过"+haveProductId+"产品,请去充值", c)
 			} else {

+ 8 - 5
service/user.go

@@ -19,7 +19,7 @@ import (
 	"gorm.io/gorm"
 )
 
-func CreateUserProduct(appId string, productArr []map[string]interface{}, buyType int) (status int, haveProductId string, err error) {
+func CreateUserProduct(appId string, productArr []map[string]interface{}, buyType int) (status int, haveProductId string, errArr []error) {
 	//取出用户锁
 	lock.MainLock.Lock()
 	userLock := lock.UserLockMap[appId]
@@ -28,6 +28,7 @@ func CreateUserProduct(appId string, productArr []map[string]interface{}, buyTyp
 	defer userLock.Unlock()
 	var errs error
 	haveProductId = "" //已经购买过的产品id
+	//var errArr = make([]error, 0)
 	for _, val := range productArr {
 		productId := utils.IntAll(val["productId"])
 		costModel := utils.IntAll(val["costModel"])
@@ -48,14 +49,16 @@ func CreateUserProduct(appId string, productArr []map[string]interface{}, buyTyp
 		//查询产品信息,获取购买时候产品单价、试用次数
 		err := db.GetSFISDB().Where("id = ?", productId).Find(&product).Error
 		if err != nil {
+			errArr = append(errArr, err)
 			global.Logger.Error("CreateUserProduct查询product表出错:", zap.Any("err:", err))
-			return 0, haveProductId, err
+			return 0, haveProductId, errArr
 		}
 		historyUnitPrice := product.UnitPrice
 		err = db.GetSFISDB().Where("product_id = ? and app_id = ?", productId, appId).Find(&userProductInfo).Error
 		if err != nil {
+			errArr = append(errArr, err)
 			global.Logger.Error("CreateUserProduct查询user_product表出错:", zap.Any("err:", err))
-			return 0, haveProductId, err
+			return 0, haveProductId, errArr
 		}
 		//用户第一次购买产品
 		if userProductInfo.ID == 0 {
@@ -121,6 +124,7 @@ func CreateUserProduct(appId string, productArr []map[string]interface{}, buyTyp
 				return nil
 			})
 			if errs != nil {
+				errArr = append(errArr, errs)
 				global.Logger.Info("用户已购买产品失败:", zap.Any("appId:", appId), zap.Any("productId:", productId))
 				continue
 			}
@@ -130,10 +134,9 @@ func CreateUserProduct(appId string, productArr []map[string]interface{}, buyTyp
 			continue
 		}
 	}
-	return 1, haveProductId, nil
+	return 1, haveProductId, errArr
 }
 
-
 func UserProductList(appId string, c *gin.Context) (userProduct []model.UserProduct, err error) {
 	//userProduct := []model.UserProduct{}
 	err = db.GetSFISDB().Where("app_id = ? ", appId).Find(&userProduct).Error

+ 45 - 28
static/templates/chooseProduct.html

@@ -11,42 +11,43 @@
     <table border="0" id="tableId">
         <thead>
         <tr class="tableTitle">
-            <th style="width: 20px"><input type="checkbox" name="item">
+            <th style="width: 10%"><input type="radio" name="item">
             </td></th>
-            <th style="width: 50px">序号</th>
-            <th style="width: 100px">产品名称</th>
-            <th style="width: 200px">url</th>
-            <th style="width: 100px">产品单价</th>
-            <th style="width: 100px">产品类型</th>
-            <th style="width: 100px">最小单位</th>
-            <th style="width: 100px">试用次数</th>
-            {{/*            <th style="width: 100px">开始时间</th>*/}}
-            {{/*            <th style="width: 100px">结束时间</th>*/}}
-            {{/*            <th style="width: 100px">结束时间</th>*/}}
+            <th style="width: 5%">序号</th>
+            <th style="width: 25%">产品名称</th>
+            <th style="width: 20%">url</th>
+            <th style="width: 10%">产品单价</th>
+            <th style="width: 10%">产品类型</th>
+            <th style="width: 10%">最小单位</th>
+            <th style="width: 10%">试用次数</th>
         </tr>
         </thead>
         <tbody class="box">
 
         </tbody>
     </table>
-    <div style="margin-top: 20px">
-        开始时间:<input id="startTime" type="text">
-        结束时间:<input id="endTime" type="text" name="" id=""><br>
-        扣费模式:<select id="costModel" style="width: 165px;height: 25px;margin-top: 10px">
+    <div class="productInfo" style="margin-top: 40px;margin-left: 20px">
+        <span>用户appid:</span><input id="appId" type="text"><br>
+        <span>开始时间:</span><input id="startTime" type="text">
+        <span>结束时间:</span><input id="endTime" type="text" name="" id=""><br>
+        <span>充值量:</span><input id="leftNum" type="text">
+        <span>扣费模式:</span>
+        <select id="costModel" style="width: 165px;height: 25px;margin-top: 10px">
             <option value="0">剩余量</option>
             <option value="1">余额</option>
         </select>
-        充值量:<input id="leftNum" type="text"><br>
-        充值金额:<input id="money" type="text">
-        产品状态:<select id="interfaceStatus" style="width: 165px;height: 25px;margin-top: 10px">
+        <br>
+        <span>充值金额:</span><input id="money" type="text">
+        <span>产品状态:</span>
+        <select id="interfaceStatus" style="width: 165px;height: 25px;margin-top: 10px">
             <option value="0">开启</option>
             <option value="1">关闭</option>
         </select><br>
-        每日调用次数:<input id="dayLimit" type="text">
-        每次获取数据量:<input id="oneLimit" type="text">
-        用户appid:<input id="appId" type="text">
+        <sapn>每日调用次数:</sapn>
+        <input id="dayLimit" type="text">
+        <span>每次获取数据量:</span><input id="oneLimit" type="text">
     </div>
-    <div style="margin-top: 10px">
+    <div style="margin-top: 10px;margin-left: 20px;">
         <button type="submit" onclick="submit()" style="width: 80px;height: 30px;">提交</button>
     </div>
 
@@ -56,6 +57,20 @@
 
     </body>
     </html>
+    <style>
+        .box tr td {
+            text-align: center;
+        }
+
+        .productInfo input {
+            margin-right: 30px;
+        }
+
+        .productInfo span {
+            width: 120px;
+        }
+
+    </style>
     <script>
         productList()
 
@@ -67,7 +82,7 @@
                 async: false,
                 success: function (r) {
                     if (r.code === 0) {
-                        console.log(r.data)
+                        // console.log(r.data)
                         var str = ""
                         for (var i = 0; i < r.data.length; i++) {
                             var item = r.data[i];
@@ -96,6 +111,7 @@
         }
 
         function submit() {
+            $(".result").html("");
             var productId = $("input[name='item']:checked").val();
             var startTime = $("#startTime").val();
             var endTime = $("#endTime").val();
@@ -135,13 +151,14 @@
                 async: false,
                 success: function (r) {
                     var a = JSON.stringify(r)
-                    $(".result").html(a);
-                    /*if (r.code == 0) {
-                        $(".result").html("请求成功!" + r.msg);
+                    if (r.code == 0) {
+                        window.location.reload();
+                        timeoutId = setTimeout(function () {
+                            $(".result").html(a);
+                        }, 300);
                     } else {
-                        $(".result").html("购买产品失败!" + r.msg);
+                        $(".result").html(a);
                     }
-*/
                 }
             })
         }

+ 14 - 2
static/templates/projectList.html

@@ -41,8 +41,20 @@
         }
     </style>
     <script>
-        var appid = "sfGSVYRQMAAgkGBAUBJg4f";
-        var key = "364xw909";
+        var LocString = String(window.document.location.href);
+
+        // 获取页面跳转传过来的参数
+        function GetQueryString(name) {
+            var rs = new RegExp("(^|)" + name + "=([^&]*)(&|$)", "gi").exec(LocString);
+            var tmp;
+            if (tmp = rs) {
+                return decodeURI(tmp[2])
+            }
+            return "";
+        }
+
+        var appid = GetQueryString("appId");
+        var key = GetQueryString("key");
 
         function submit(param, rType, url, hearders, str) {
             $.ajax({