receiver.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package notify
  15. import (
  16. "fmt"
  17. "regexp"
  18. "strings"
  19. "yunion.io/x/pkg/tristate"
  20. "yunion.io/x/onecloud/pkg/apis"
  21. )
  22. type ReceiverCreateInput struct {
  23. apis.VirtualResourceCreateInput
  24. apis.EnabledBaseResourceCreateInput
  25. // description: user id in keystone
  26. // example: adfb720ccdd34c638346ea4fa7a713a8
  27. UID string `json:"uid"`
  28. // description: user name in keystone
  29. // example: hello
  30. UName string `json:"uname"`
  31. // description: user email
  32. // example: example@gmail.com
  33. Email string `json:"email"`
  34. InternationalMobile SInternationalMobile `json:"international_mobile"`
  35. // description: enabled contact types for user
  36. // example: {"email", "mobile", "feishu", "dingtalk", "workwx"}
  37. EnabledContactTypes []string `json:"enabled_contact_types"`
  38. // force verified if admin create the records
  39. ForceVerified bool `json:"force_verified"`
  40. }
  41. type SInternationalMobile struct {
  42. // description: user mobile
  43. // example: 17812345678
  44. Mobile string `json:"mobile"`
  45. // description: area code of mobile
  46. // example: 86
  47. AreaCode string `json:"area_code"`
  48. }
  49. var (
  50. defaultAreaCode = "86"
  51. pareser = regexp.MustCompile(`\+(\d*) (.*)`)
  52. )
  53. func ParseInternationalMobile(mobile string) SInternationalMobile {
  54. matchs := pareser.FindStringSubmatch(mobile)
  55. if len(matchs) == 0 {
  56. if len(mobile) == 0 {
  57. return SInternationalMobile{}
  58. }
  59. return SInternationalMobile{
  60. AreaCode: defaultAreaCode,
  61. Mobile: mobile,
  62. }
  63. }
  64. return SInternationalMobile{
  65. Mobile: matchs[2],
  66. AreaCode: matchs[1],
  67. }
  68. }
  69. // 支持接受分机号(仅保留数字)
  70. func (im *SInternationalMobile) AcceptExtMobile() {
  71. im.Mobile = moveAreaCode(im.Mobile)
  72. im.Mobile = moveExtStr(im.Mobile)
  73. }
  74. // 对传入的手机号去除地区编号
  75. func moveAreaCode(mobile string) string {
  76. // 所有地区编号
  77. allArea := `852|283|282|281|280|269|268|267|266|265|264|263|262|261|260|259|258|257|256|255|254|253|252|251|250|249|248|247|246|245|244|243|242|241|240|239|238|237|236|235|234|233|232|231|230|229|228|227|226|225|224|223|222|221|220|219|218|217|216|215|214|213|212|211|210|98|95|94|93|92|91|90|86|84|82|81|66|65|64|63|62|61|60|58|57|56|55|54|53|52|51|49|48|47|46|45|44|43|41|40|39|36|34|33|32|31|30|27|20|7|1`
  78. temp := strings.Split(allArea, "|")
  79. for _, area := range temp {
  80. if strings.HasPrefix(mobile, "+"+area) {
  81. mobile = mobile[1+len(area):]
  82. break
  83. }
  84. }
  85. return mobile
  86. }
  87. // 对传入的手机号去除分机号后缀
  88. func moveExtStr(mobile string) string {
  89. // 定义正则表达式,只保留数字字符串,到不为数字时结束
  90. re := regexp.MustCompile(`^\d+`)
  91. // 匹配正则表达式并获取原号码
  92. match := re.FindStringSubmatch(mobile)
  93. if match != nil {
  94. mobile = match[0]
  95. } else {
  96. mobile = ""
  97. }
  98. return mobile
  99. }
  100. func (im SInternationalMobile) String() string {
  101. if len(im.Mobile) == 0 {
  102. return ""
  103. }
  104. if im.AreaCode == "" {
  105. return im.Mobile
  106. }
  107. return fmt.Sprintf("+%s %s", im.AreaCode, im.Mobile)
  108. }
  109. type ReceiverDetails struct {
  110. apis.VirtualResourceDetails
  111. SReceiver
  112. InternationalMobile SInternationalMobile `json:"international_mobile"`
  113. // description: enabled contact types for user
  114. // example: eamil, mobile, feishu, dingtalk, workwx
  115. EnabledContactTypes []string `json:"enabled_contact_types"`
  116. // description: verified info
  117. VerifiedInfos []VerifiedInfo `json:"verified_infos"`
  118. }
  119. type VerifiedInfo struct {
  120. ContactType string `json:"contact_type"`
  121. Verified bool `json:"verified"`
  122. Note string `json:"note"`
  123. }
  124. type ReceiverListInput struct {
  125. apis.VirtualResourceListInput
  126. apis.EnabledResourceBaseListInput
  127. UID string `json:"uid"`
  128. UName string `json:"uname"`
  129. EnabledContactType string `json:"enabled_contact_type"`
  130. VerifiedContactType string `json:"verified_contact_type"`
  131. ProjectDomainFilter bool `json:"project_domain_filter"`
  132. }
  133. type ReceiverUpdateInput struct {
  134. apis.VirtualResourceBaseUpdateInput
  135. // description: user email
  136. // example: example@gmail.com
  137. Email string `json:"email"`
  138. // swagger:ignore
  139. EnabledEmail tristate.TriState `json:"enabled_email"`
  140. // swagger:ignore
  141. VerifiedEmail tristate.TriState `json:"verified_email"`
  142. // swagger:ignore
  143. EnabledMobile tristate.TriState `json:"enabled_mobile"`
  144. // swagger:ignore
  145. VerifiedMobile tristate.TriState `json:"verified_mobile"`
  146. // swagger:ignore
  147. Mobile string `json:"mobile"`
  148. InternationalMobile SInternationalMobile `json:"international_mobile"`
  149. // description: enabled contacts for user
  150. // example: {"email", "mobile", "feishu", "dingtalk", "workwx"}
  151. EnabledContactTypes []string `json:"enabled_contact_types"`
  152. ForceVerified bool `json:"force_verified"`
  153. }
  154. type ReceiverTriggerVerifyInput struct {
  155. // description: contact type
  156. // required: true
  157. // example: email
  158. // enum: ["email","mobile"]
  159. ContactType string `json:"contact_type"`
  160. }
  161. type ReceiverVerifyInput struct {
  162. // description: Contact type
  163. // required: true
  164. // example: email
  165. // enum: ["email","mobile"]
  166. ContactType string `json:"contact_type"`
  167. // description: token user input
  168. // required: true
  169. // example: 123456
  170. Token string `json:"token"`
  171. }
  172. type ReceiverIntellijGetInput struct {
  173. // description: user id in keystone
  174. // required: true
  175. UserId string `json:"user_id"`
  176. // description: create if receiver with UserId does not exist
  177. // required: false
  178. CreateIfNo *bool `json:"create_if_no"`
  179. // description: scope
  180. // required: true
  181. Scope string `json:"scope"`
  182. }
  183. type ReceiverEnableContactTypeInput struct {
  184. EnabledContactTypes []string `json:"enabled_contact_types"`
  185. }
  186. type ReceiverGetSubscriptionOptions struct {
  187. Id string `json:"id"`
  188. ShowDisabled bool `json:"show_disabled"`
  189. }
  190. type SRoleContactInput struct {
  191. RoleIds []string `json:"role_ids"`
  192. Scope string `json:"scope"`
  193. ProjectDomainId string `json:"project_domain_id"`
  194. ProjectId string `json:"project_id"`
  195. }
  196. type SRoleContactOutput struct {
  197. ContactType []string `json:"contact_type"`
  198. }