diff --git a/db/offical/db_sms_num_list.go b/db/offical/db_sms_num_list.go new file mode 100644 index 0000000..4d7ecb6 --- /dev/null +++ b/db/offical/db_sms_num_list.go @@ -0,0 +1,15 @@ +package offical + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db/offical/model" + "xorm.io/xorm" +) + +func GetSmsNum(engine *xorm.Engine, smsType string, uid string) *model.SmsNumList { + var data model.SmsNumList + get, err := engine.Where("uid=? and type=?", uid, smsType).Get(&data) + if get == false || err != nil { + return nil + } + return &data +} diff --git a/db/offical/db_sys_cfg.go b/db/offical/db_sys_cfg.go new file mode 100644 index 0000000..139fefd --- /dev/null +++ b/db/offical/db_sys_cfg.go @@ -0,0 +1,15 @@ +package offical + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db/offical/model" + "xorm.io/xorm" +) + +func SysCfgByKey(engine *xorm.Engine, key string) string { + var data model.SysCfg + get, err := engine.Where("k=?", key).Get(&data) + if get == false || err != nil { + return "" + } + return data.V +} diff --git a/db/offical/model/sms_money.go b/db/offical/model/sms_money.go new file mode 100644 index 0000000..8a3d10d --- /dev/null +++ b/db/offical/model/sms_money.go @@ -0,0 +1,12 @@ +package model + +type SmsMoney struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Money float64 `json:"money" xorm:"default 0.00 comment('金额') DOUBLE(11,2)"` + Num int `json:"num" xorm:"default 0 comment('短信条数') INT(11)"` + Hide int `json:"hide" xorm:"default 0 comment('0否 1是') INT(1)"` + Sort int `json:"sort" xorm:"default 0 comment('排序') INT(11)"` + UpdateTime int `json:"update_time" xorm:"default 0 comment('修改时间') INT(11)"` + Time int `json:"time" xorm:"default 0 comment('创建时间') INT(11)"` + Type string `json:"type" xorm:"default '' comment('普通 putong 营销 yingxiao') VARCHAR(50)"` +} diff --git a/db/offical/model/sms_num_list.go b/db/offical/model/sms_num_list.go new file mode 100644 index 0000000..88b3e28 --- /dev/null +++ b/db/offical/model/sms_num_list.go @@ -0,0 +1,8 @@ +package model + +type SmsNumList struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Uid int `json:"uid" xorm:"default 0 comment('用户') INT(11)"` + Num int `json:"num" xorm:"default 0 comment('短信条数') INT(11)"` + Type string `json:"type" xorm:"default '' comment('普通 putong 营销 yingxiao') VARCHAR(50)"` +} diff --git a/db/offical/model/sms_record.go b/db/offical/model/sms_record.go new file mode 100644 index 0000000..e3ffd8b --- /dev/null +++ b/db/offical/model/sms_record.go @@ -0,0 +1,28 @@ +package model + +import ( + "time" +) + +type SmsRecord struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + OrdId string `json:"ord_id" xorm:"not null default '' comment('订单id') VARCHAR(100)"` + Uid int `json:"uid" xorm:"not null default 0 comment('主用户id') INT(11)"` + Phone int64 `json:"phone" xorm:"not null default 0 comment('主账号-手机号码') BIGINT(13)"` + Nickname string `json:"nickname" xorm:"not null default '' comment('主账号-昵称') VARCHAR(255)"` + Amount string `json:"amount" xorm:"not null default '0' comment('充值金额') VARCHAR(255)"` + CostPrice string `json:"cost_price" xorm:"not null default '0' comment('付费价格') VARCHAR(255)"` + Balance string `json:"balance" xorm:"not null default '0' comment('当前余额(充值完当前余额)') VARCHAR(255)"` + PayWay int `json:"pay_way" xorm:"not null default 1 comment('支付方式(1:支付宝 2:微信 3:余额)') TINYINT(1)"` + State int `json:"state" xorm:"not null default 0 comment('状态(0:待付款 1:已付款 2:付款失败)') TINYINT(2)"` + Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"` + CreateAt time.Time `json:"create_at" xorm:"not null default CURRENT_TIMESTAMP comment('创建时间') DATETIME"` + UpdateAt time.Time `json:"update_at" xorm:"default CURRENT_TIMESTAMP comment('更新时间') DATETIME"` + TradeNo string `json:"trade_no" xorm:"not null default '' comment('支付平台(支付宝/微信)订单号') VARCHAR(100)"` + Type int `json:"type" xorm:"default 0 comment('0收入 1支出') INT(1)"` + OrdType string `json:"ord_type" xorm:"comment('订单类型') VARCHAR(255)"` + SubUid int `json:"sub_uid" xorm:"default 0 INT(11)"` + Fee string `json:"fee" xorm:"VARCHAR(255)"` + Ext string `json:"ext" xorm:"TEXT"` + SmsType string `json:"sms_type" xorm:"VARCHAR(255)"` +} diff --git a/db/offical/model/sys_cfg.go b/db/offical/model/sys_cfg.go new file mode 100644 index 0000000..5508c9a --- /dev/null +++ b/db/offical/model/sys_cfg.go @@ -0,0 +1,7 @@ +package model + +type SysCfg struct { + K string `json:"k" xorm:"not null pk comment('键') VARCHAR(127)"` + V string `json:"v" xorm:"comment('值') TEXT"` + Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"` +} diff --git a/express/express.go b/express/express.go index e8db0bb..2997ccb 100644 --- a/express/express.go +++ b/express/express.go @@ -1,5 +1,7 @@ package express +import "fmt" + //余额 func Balance(mustParam map[string]string) (string, error) { data := make(map[string]interface{}) @@ -10,6 +12,8 @@ func Balance(mustParam map[string]string) (string, error) { //轨迹查询接口 func Trajectory(mustParam map[string]string, data map[string]interface{}) (string, error) { send, err := Send(mustParam, "3002", data) + fmt.Println(send) + fmt.Println(err) return send, err } diff --git a/sms/api.go b/sms/api.go new file mode 100644 index 0000000..930388a --- /dev/null +++ b/sms/api.go @@ -0,0 +1,162 @@ +package sms + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db" + "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db/offical" + "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db/offical/model" + zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils" + "errors" + "fmt" + "github.com/syyongx/php2go" + "github.com/tidwall/gjson" + "strings" + "time" + "xorm.io/xorm" +) + +//短信数量 +func SmsNumGetSmsNum(engine *xorm.Engine, smsType, uid string) int { + numData := offical.GetSmsNum(engine, smsType, uid) + num := 0 + if numData != nil { + num = numData.Num + } + if num < 0 { + num = 0 + } + return num +} + +//发送短信 +func SmsSend(engine *xorm.Engine, args map[string]string) error { + num := SmsNumGetSmsNum(engine, args["sms_type"], args["uid"]) + ex := strings.Split(args["mobile"], ",") + if len(ex) == 0 { + return errors.New("请输入手机号") + } + count := len(ex) + contentLen := php2go.Ceil(float64(len(args["content"])) / 4 / 70) + if args["type"] == "mob" { + contentLen = 1 + } + if num < count*int(contentLen) { + return errors.New("短信不足") + } + if args["type"] != "mob" { //联江短信数量 + send, err := SmsApiSend(engine, "api.v1.accountNum", map[string]string{}) + if err != nil { + return err + } + platformCount := gjson.Get(send, "data.smsNum").String() + if zhios_third_party_utils.StrToInt(platformCount) < count*int(contentLen) { + return errors.New("平台短信不足") + } + } + where := make(map[string]string) + param := []string{ + "content", //短信内容 + "mobile", //号码多个以英文”,”号分割,最大 2000 个 + "code", //扩展号 0-9 数字,超过 8 位自动截取前 8 位 + "ext", //自定义信息,状态报告时返回,可以为空 + } + for _, v := range param { + if args[v] != "" { + where[v] = args[v] + } + } + send := "" + var err error + if args["type"] == "mob" { //mob + where = map[string]string{ + "appkey": args["smsmsg_key"], + "zone": args["zone"], + "phone": args["phone"], + "templateCode": args["templateCode"], + } + send, err = SmsMobApiSend(where) + if err != nil { + return err + } + status := gjson.Get(send, "status").Int() + msg := gjson.Get(send, "error").String() + if status == 471 { + return errors.New("发送的ip不在白名单中") + } + if status == 406 { + return errors.New("appkey不存在") + } + if status != 200 { + return errors.New(msg) + } + } else { + send, err = SmsApiSend(engine, "api.v1.sms", where) + if err != nil { + return err + } + success := gjson.Get(send, "success").Int() + if success == 0 { + return errors.New("发送失败") + } + } + //存入记录 + ext := map[string]interface{}{ + "send": send, + "post": where, + } + var record = model.SmsRecord{ + OrdId: "", + Uid: zhios_third_party_utils.StrToInt(args["uid"]), + Amount: zhios_third_party_utils.IntToStr(count * int(contentLen)), + CostPrice: "", + Balance: zhios_third_party_utils.IntToStr(num - count*int(contentLen)), + PayWay: 0, + State: 1, + Memo: "发送短信", + CreateAt: time.Now(), + UpdateAt: time.Now(), + TradeNo: "", + Type: 1, + OrdType: "buy", + SubUid: zhios_third_party_utils.StrToInt(args["sub_uid"]), + Fee: "", + Ext: zhios_third_party_utils.SerializeStr(ext), + SmsType: args["sms_type"], + } + engine.InsertOne(&record) + sql := `UPDATE sms_num_list set num=num-%d WHERE uid=%s and type=%s;` + sql = fmt.Sprintf(sql, count*int(contentLen), args["uid"], args["sms_type"]) + db.QueryNativeString(engine, sql) + return nil +} + +func SmsApiSend(engine *xorm.Engine, method string, args map[string]string) (string, error) { + smsmsgKey := offical.SysCfgByKey(engine, "smsmsg_key") + smsmsgAccount := offical.SysCfgByKey(engine, "smsmsg_account") + smsWebHost := offical.SysCfgByKey(engine, "sms_web_host") + salesSmsmsgKey := offical.SysCfgByKey(engine, "sales_smsmsg_key") + salesSmsmsgAccount := offical.SysCfgByKey(engine, "sales_smsmsg_account") + + if args["sms_type"] == "yingxiao" { + smsmsgKey = salesSmsmsgKey + smsmsgAccount = salesSmsmsgAccount + } + thisUrl := smsWebHost + if thisUrl == "" { + thisUrl = "http://api.ljioe.cn/" + } + thisUrl += strings.ReplaceAll(method, ".", "/") + args["timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().Unix()) + args["account"] = smsmsgAccount + args["key"] = php2go.Md5(smsmsgKey + php2go.Md5(args["timestamp"])) + post, err := zhios_third_party_utils.CurlPost(thisUrl, zhios_third_party_utils.SerializeStr(args), nil) + fmt.Println(string(post)) + fmt.Println(err) + return string(post), err +} +func SmsMobApiSend(args map[string]string) (string, error) { + thisUrl := "https://webapi.sms.mob.com/sms/sendmsg" + post, err := zhios_third_party_utils.CurlPost(thisUrl, zhios_third_party_utils.SerializeStr(args), nil) + fmt.Println(string(post)) + fmt.Println(err) + return string(post), err +}