telecom.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package http
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. "go-common/app/interface/main/app-wall/model/telecom"
  10. "go-common/library/ecode"
  11. log "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/metadata"
  14. )
  15. // ordersSync
  16. func telecomOrdersSync(c *bm.Context) {
  17. data, err := requestJSONTelecom(c.Request)
  18. if err != nil {
  19. telecomMessage(c, err.Error())
  20. return
  21. }
  22. switch v := data.(type) {
  23. case *telecom.TelecomOrderJson:
  24. if err = telecomSvc.InOrdersSync(c, metadata.String(c, metadata.RemoteIP), v); err != nil {
  25. log.Error("telecomSvc.InOrdersSync error (%v)", err)
  26. telecomMessage(c, err.Error())
  27. return
  28. }
  29. telecomMessage(c, "1")
  30. case *telecom.TelecomRechargeJson:
  31. if v == nil {
  32. telecomMessage(c, ecode.NothingFound.Error())
  33. return
  34. }
  35. if err = telecomSvc.InRechargeSync(c, metadata.String(c, metadata.RemoteIP), v.Detail); err != nil {
  36. log.Error("telecomSvc.InOrdersSync error (%v)", err)
  37. telecomMessage(c, err.Error())
  38. return
  39. }
  40. telecomMessage(c, "1")
  41. }
  42. }
  43. // telecomMsgSync
  44. func telecomMsgSync(c *bm.Context) {
  45. data, err := requestJSONTelecomMsg(c.Request)
  46. if err != nil {
  47. telecomMessage(c, err.Error())
  48. return
  49. }
  50. if err = telecomSvc.TelecomMessageSync(c, metadata.String(c, metadata.RemoteIP), data); err != nil {
  51. log.Error("telecomSvc.TelecomMessageSync error (%v)", err)
  52. telecomMessage(c, err.Error())
  53. return
  54. }
  55. telecomMessage(c, "1")
  56. }
  57. func telecomPay(c *bm.Context) {
  58. res := map[string]interface{}{}
  59. params := c.Request.Form
  60. phoneDES := params.Get("phone")
  61. phone, err := phoneDesToInt(phoneDES)
  62. if err != nil {
  63. log.Error("phoneDesToInt error(%v)", err)
  64. res["message"] = ""
  65. returnDataJSON(c, res, ecode.RequestErr)
  66. return
  67. }
  68. isRepeatOrderStr := params.Get("is_repeat_order")
  69. isRepeatOrder, err := strconv.Atoi(isRepeatOrderStr)
  70. if err != nil {
  71. log.Error("isRepeatOrder strconv.Atoi error(%v)", err)
  72. res["message"] = ""
  73. returnDataJSON(c, res, ecode.RequestErr)
  74. return
  75. }
  76. payChannelStr := params.Get("pay_channel")
  77. payChannel, err := strconv.Atoi(payChannelStr)
  78. if err != nil {
  79. log.Error("payChannel strconv.Atoi error(%v)", err)
  80. res["message"] = ""
  81. returnDataJSON(c, res, ecode.RequestErr)
  82. return
  83. }
  84. payActionStr := params.Get("pay_action")
  85. payAction, err := strconv.Atoi(payActionStr)
  86. if err != nil {
  87. log.Error("payAction strconv.Atoi error(%v)", err)
  88. res["message"] = ""
  89. returnDataJSON(c, res, ecode.RequestErr)
  90. return
  91. }
  92. orderIDStr := params.Get("orderid")
  93. orderID, _ := strconv.ParseInt(orderIDStr, 10, 64)
  94. data, msg, err := telecomSvc.TelecomPay(c, phone, isRepeatOrder, payChannel, payAction, orderID, metadata.String(c, metadata.RemoteIP))
  95. if err != nil {
  96. log.Error("telecomSvc.TelecomPay error(%v)", err)
  97. res["message"] = msg
  98. returnDataJSON(c, res, err)
  99. return
  100. }
  101. res["data"] = data
  102. returnDataJSON(c, res, nil)
  103. }
  104. func cancelRepeatOrder(c *bm.Context) {
  105. res := map[string]interface{}{}
  106. params := c.Request.Form
  107. phoneDES := params.Get("phone")
  108. phone, err := phoneDesToInt(phoneDES)
  109. if err != nil {
  110. log.Error("phoneDesToInt error(%v)", err)
  111. res["message"] = ""
  112. returnDataJSON(c, res, ecode.RequestErr)
  113. return
  114. }
  115. msg, err := telecomSvc.CancelRepeatOrder(c, phone)
  116. if err != nil {
  117. res["message"] = msg
  118. returnDataJSON(c, res, err)
  119. return
  120. }
  121. res["message"] = msg
  122. returnDataJSON(c, res, nil)
  123. }
  124. func orderList(c *bm.Context) {
  125. res := map[string]interface{}{}
  126. params := c.Request.Form
  127. phoneDES := params.Get("phone")
  128. phone, err := phoneDesToInt(phoneDES)
  129. if err != nil {
  130. log.Error("phoneDesToInt error(%v)", err)
  131. res["message"] = ""
  132. returnDataJSON(c, res, ecode.RequestErr)
  133. return
  134. }
  135. orderIDStr := params.Get("orderid")
  136. orderID, _ := strconv.ParseInt(orderIDStr, 10, 64)
  137. data, msg, err := telecomSvc.OrderList(c, orderID, phone)
  138. if err != nil {
  139. log.Error("telecomSvc.OrderList error(%v)", err)
  140. res["message"] = msg
  141. returnDataJSON(c, res, err)
  142. return
  143. }
  144. res["data"] = data
  145. returnDataJSON(c, res, nil)
  146. }
  147. func phoneFlow(c *bm.Context) {
  148. res := map[string]interface{}{}
  149. params := c.Request.Form
  150. phoneDES := params.Get("phone")
  151. phone, err := phoneDesToInt(phoneDES)
  152. if err != nil {
  153. log.Error("phoneDesToInt error(%v)", err)
  154. res["message"] = ""
  155. returnDataJSON(c, res, ecode.RequestErr)
  156. return
  157. }
  158. orderIDStr := params.Get("orderid")
  159. orderID, _ := strconv.ParseInt(orderIDStr, 10, 64)
  160. data, msg, err := telecomSvc.PhoneFlow(c, orderID, phone)
  161. if err != nil {
  162. log.Error("telecomSvc.PhoneFlow error(%v)", err)
  163. res["message"] = msg
  164. returnDataJSON(c, res, err)
  165. return
  166. }
  167. res["data"] = data
  168. returnDataJSON(c, res, nil)
  169. }
  170. func orderConsent(c *bm.Context) {
  171. res := map[string]interface{}{}
  172. params := c.Request.Form
  173. phoneDES := params.Get("phone")
  174. phone, err := phoneDesToInt(phoneDES)
  175. if err != nil {
  176. log.Error("phoneDesToInt error(%v)", err)
  177. res["message"] = ""
  178. returnDataJSON(c, res, ecode.RequestErr)
  179. return
  180. }
  181. captcha := params.Get("captcha")
  182. orderIDStr := params.Get("orderid")
  183. orderID, _ := strconv.ParseInt(orderIDStr, 10, 64)
  184. data, msg, err := telecomSvc.OrderConsent(c, phone, orderID, captcha)
  185. if err != nil {
  186. log.Error("telecomSvc.OrderConsent error(%v)", err)
  187. res["message"] = msg
  188. returnDataJSON(c, res, err)
  189. return
  190. }
  191. res["data"] = data
  192. returnDataJSON(c, res, nil)
  193. }
  194. func phoneSendSMS(c *bm.Context) {
  195. res := map[string]interface{}{}
  196. params := c.Request.Form
  197. phoneDES := params.Get("phone")
  198. phone, err := phoneDesToInt(phoneDES)
  199. if err != nil {
  200. log.Error("phoneDesToInt error(%v)", err)
  201. res["message"] = ""
  202. returnDataJSON(c, res, ecode.RequestErr)
  203. return
  204. }
  205. err = telecomSvc.PhoneSendSMS(c, phone)
  206. if err != nil {
  207. log.Error("telecomSvc.PhoneSendSMS error(%v)", err)
  208. res["code"] = err
  209. res["message"] = ""
  210. returnDataJSON(c, res, err)
  211. return
  212. }
  213. res["message"] = ""
  214. returnDataJSON(c, res, nil)
  215. }
  216. func phoneVerification(c *bm.Context) {
  217. res := map[string]interface{}{}
  218. params := c.Request.Form
  219. phoneDES := params.Get("phone")
  220. phone, err := phoneDesToInt(phoneDES)
  221. if err != nil {
  222. log.Error("phoneDesToInt error(%v)", err)
  223. res["message"] = ""
  224. returnDataJSON(c, res, ecode.RequestErr)
  225. return
  226. }
  227. captcha := params.Get("captcha")
  228. data, err, msg := telecomSvc.PhoneCode(c, phone, captcha, time.Now())
  229. if err != nil {
  230. log.Error("telecomSvc.PhoneCode error(%v)", err)
  231. res["message"] = msg
  232. returnDataJSON(c, res, err)
  233. return
  234. }
  235. res["data"] = data
  236. res["message"] = msg
  237. returnDataJSON(c, res, nil)
  238. }
  239. func orderState(c *bm.Context) {
  240. res := map[string]interface{}{}
  241. params := c.Request.Form
  242. orderIDStr := params.Get("orderid")
  243. orderID, err := strconv.ParseInt(orderIDStr, 10, 64)
  244. if err != nil {
  245. log.Error("orderID strconv.ParseInt error(%v)", err)
  246. res["message"] = ""
  247. returnDataJSON(c, res, ecode.RequestErr)
  248. return
  249. }
  250. data, msg, err := telecomSvc.OrderState(c, orderID)
  251. if err != nil {
  252. log.Error("telecomSvc.OrderState error(%v)", err)
  253. res["message"] = msg
  254. returnDataJSON(c, res, err)
  255. return
  256. }
  257. res["data"] = data
  258. res["message"] = msg
  259. returnDataJSON(c, res, nil)
  260. }
  261. // requestJSONTelecom
  262. func requestJSONTelecom(request *http.Request) (res interface{}, err error) {
  263. var (
  264. telecomOrder *telecom.TelecomOrderJson
  265. telecomRecharge *telecom.TelecomRechargeJson
  266. )
  267. body, err := ioutil.ReadAll(request.Body)
  268. if err != nil {
  269. log.Error("unicom_ioutil.ReadAll error (%v)", err)
  270. return
  271. }
  272. defer request.Body.Close()
  273. if len(body) == 0 {
  274. err = ecode.RequestErr
  275. return
  276. }
  277. log.Info("telecom orders json body(%s)", body)
  278. if err = json.Unmarshal(body, &telecomOrder); err == nil && telecomOrder != nil {
  279. if telecomOrder.ResultType != 2 {
  280. res = telecomOrder
  281. return
  282. }
  283. }
  284. if err = json.Unmarshal(body, &telecomRecharge); err == nil {
  285. res = telecomRecharge
  286. return
  287. }
  288. log.Error("telecom json.Unmarshal error (%v)", err)
  289. return
  290. }
  291. // requestJSONTelecomMsg
  292. func requestJSONTelecomMsg(request *http.Request) (res *telecom.TelecomMessageJSON, err error) {
  293. body, err := ioutil.ReadAll(request.Body)
  294. if err != nil {
  295. log.Error("unicom_ioutil.ReadAll error (%v)", err)
  296. return
  297. }
  298. defer request.Body.Close()
  299. if len(body) == 0 {
  300. err = ecode.RequestErr
  301. return
  302. }
  303. log.Info("telecom json msg body(%s)", body)
  304. if err = json.Unmarshal(body, &res); err != nil {
  305. log.Error("telecom Message json.Unmarshal error (%v)", err)
  306. return
  307. }
  308. return
  309. }
  310. // telecomMessage
  311. func telecomMessage(c *bm.Context, code string) {
  312. // response header
  313. c.Writer.Header().Set("Content-Type", "text; charset=UTF-8")
  314. c.Writer.Header().Set("Cache-Control", "no-cache")
  315. c.Writer.Header().Set("Connection", "keep-alive")
  316. c.Writer.Write([]byte(code))
  317. }
  318. // phoneDesToInt des to int
  319. func phoneDesToInt(phoneDes string) (phoneInt int, err error) {
  320. var (
  321. _aesKey = []byte("6b7e8b8a")
  322. )
  323. bs, err := base64.StdEncoding.DecodeString(phoneDes)
  324. if err != nil {
  325. log.Error("base64.StdEncoding.DecodeString(%s) error(%v)", phoneDes, err)
  326. err = ecode.RequestErr
  327. return
  328. }
  329. if bs, err = telecomSvc.DesDecrypt(bs, _aesKey); err != nil {
  330. log.Error("phone s.DesDecrypt error(%v)", err)
  331. return
  332. }
  333. var phoneStr string
  334. if len(bs) > 11 {
  335. phoneStr = string(bs[:11])
  336. } else {
  337. phoneStr = string(bs)
  338. }
  339. phoneInt, err = strconv.Atoi(phoneStr)
  340. if err != nil {
  341. log.Error("phoneDesToInt phoneStr:%v error(%v)", phoneStr, err)
  342. err = ecode.RequestErr
  343. return
  344. }
  345. return
  346. }