|
- 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 interface{}) 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]interface{}) error {
- num := SmsNumGetSmsNum(engine, args["sms_type"], args["uid"])
- ex := strings.Split(args["mobile"].(string), ",")
- if len(ex) == 0 {
- return errors.New("请输入手机号")
- }
- count := len(ex)
- contentLen := php2go.Ceil(float64(len(args["content"].(string))) / 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]interface{}{})
- if err != nil {
- return err
- }
- platformCount := gjson.Get(send, "smsNum").Int()
- if int(platformCount) < count*int(contentLen) {
- return errors.New("平台短信不足")
- }
- }
- where := make(map[string]interface{})
- param := []string{
- "content", //短信内容
- "mobile", //号码多个以英文”,”号分割,最大 2000 个
- "code", //扩展号 0-9 数字,超过 8 位自动截取前 8 位
- "ext", //自定义信息,状态报告时返回,可以为空
- }
- for _, v := range param {
- if args[v] != "" && args[v] != nil {
- where[v] = args[v].(string)
- }
- }
- send := ""
- var err error
- if args["type"] == "mob" { //mob
- if args["templateCode"] == "" {
- args["templateCode"] = "normal"
- }
- where = map[string]interface{}{
- "appkey": args["smsmsg_key"],
- "zone": args["zone"],
- "phone": args["mobile"],
- "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, "code").Int()
- if success != 20000 {
- return errors.New("发送失败")
- }
- }
- //存入记录
- ext := map[string]interface{}{
- "send": send,
- "post": where,
- }
- var record = model.SmsRecord{
- OrdId: "",
- Uid: int(zhios_third_party_utils.AnyToInt64(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",
- Fee: "",
- Ext: zhios_third_party_utils.SerializeStr(ext),
- SmsType: args["sms_type"].(string),
- Phone: args["mobile"].(string),
- }
- 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"])
- fmt.Println(sql)
- nativeString, err := db.QueryNativeString(engine, sql)
- fmt.Println(nativeString)
- fmt.Println(err)
- return nil
- }
- func SmsSendZy(engine *xorm.Engine, args map[string]interface{}) error {
- where := make(map[string]interface{})
- param := []string{
- "content", //短信内容
- "mobile", //号码多个以英文”,”号分割,最大 2000 个
- "code", //扩展号 0-9 数字,超过 8 位自动截取前 8 位
- "ext", //自定义信息,状态报告时返回,可以为空
- }
- for _, v := range param {
- if args[v] != "" && args[v] != nil {
- where[v] = args[v].(string)
- }
- }
- send := ""
- var err error
- if args["type"] == "mob" { //mob
- if args["templateCode"] == "" {
- args["templateCode"] = "normal"
- }
- where = map[string]interface{}{
- "appkey": args["smsmsg_key"],
- "zone": args["zone"],
- "phone": args["mobile"],
- "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, "code").Int()
- if success != 20000 {
- return errors.New("发送失败")
- }
- }
- return nil
- }
-
- func SmsApiSend(engine *xorm.Engine, method string, args map[string]interface{}) (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"].(string)))
- 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]interface{}) (string, error) {
- thisUrl := "https://webapi.sms.mob.com/sms/sendmsg"
- post, err := zhios_third_party_utils.CurlPost(thisUrl, args, nil)
- fmt.Println(string(post))
- fmt.Println(err)
- return string(post), err
- }
-
- func SMSMobCheck(args map[string]interface{}) (bool, error) {
- // https://www.mob.com/wiki/detailed/?wiki=SMSSDK_for_yanzhengmafuwuduanxiaoyanjiekou&id=23
- url := "https://webapi.sms.mob.com/sms/verify"
- fmt.Println(args)
- // 发送请求
- respBody, err := zhios_third_party_utils.CurlPost(url, args, nil)
- if err != nil {
- fmt.Println(err)
- return false, err
- }
- code := gjson.GetBytes(respBody, "status").Int()
- if code == 468 {
- return false, errors.New("验证码错误")
- }
- if code != 200 {
- return false, errors.New("验证码错误~")
- }
- return true, nil
- }
|