附近小店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

557 lines
15 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "encoding/json"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func StorePayInfo(c *gin.Context) {
  13. var arg map[string]string
  14. if err := c.ShouldBindJSON(&arg); err != nil {
  15. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  16. return
  17. }
  18. engine := MasterDb(c)
  19. store := db.GetStoreIdEg(engine, arg["store_id"])
  20. if store == nil {
  21. e.OutErr(c, 400, e.NewErr(400, "门店不存在"))
  22. return
  23. }
  24. res := map[string]string{
  25. "name": store.Name, "logo": store.Logo,
  26. }
  27. e.OutSuc(c, res, nil)
  28. return
  29. }
  30. func StorePayScan(c *gin.Context) {
  31. kind := c.DefaultQuery("type", "alipay") //1:支付宝 2:微信
  32. id, _ := c.GetQuery("id")
  33. if id == "" {
  34. e.OutErr(c, e.ERR_INVALID_ARGS, nil)
  35. return
  36. }
  37. engine := MasterDb(c)
  38. data := db.GetStorePayQrcodeById(engine, id)
  39. if data == nil {
  40. e.OutErr(c, 400, e.NewErr(400, "桌码不存在"))
  41. return
  42. }
  43. if data.IsUse != 1 {
  44. e.OutErr(c, 400, e.NewErr(400, "桌码不可用"))
  45. return
  46. }
  47. type RefundReq struct {
  48. StoreId string `json:"store_id" `
  49. StoreType string `json:"store_type" `
  50. AppId string `json:"app_id" `
  51. AppLogo string `json:"app_logo"`
  52. InviteCode string `json:"invite_code"`
  53. }
  54. store := db.GetStoreIdEg(engine, utils.IntToStr(data.Uid))
  55. if store == nil {
  56. e.OutErr(c, 400, e.NewErr(400, "门店不存在"))
  57. return
  58. }
  59. user, _ := db.UserProfileFindByID(engine, store.Uid)
  60. var appId = ""
  61. var appLogo = ""
  62. var inviteCode = user.InviteCode
  63. if kind != "alipay" {
  64. communityTeamStoreWxAppletId := db.SysCfgGet(c, "community_team_store_wx_applet_id")
  65. var wxApplet model.WxAppletList
  66. MasterDb(c).Where("id=?", communityTeamStoreWxAppletId).Get(&wxApplet)
  67. appId = wxApplet.OriginalAppId
  68. appLogo = wxApplet.AppletLogo
  69. }
  70. var result = RefundReq{
  71. StoreId: utils.IntToStr(data.Uid),
  72. AppId: appId,
  73. StoreType: utils.IntToStr(store.StoreType),
  74. InviteCode: inviteCode,
  75. AppLogo: appLogo,
  76. }
  77. e.OutSuc(c, result, nil)
  78. return
  79. }
  80. func StoreScan(c *gin.Context) {
  81. kind := c.DefaultQuery("type", "alipay") //1:支付宝 2:微信
  82. id, _ := c.GetQuery("id")
  83. if id == "" {
  84. e.OutErr(c, e.ERR_INVALID_ARGS, nil)
  85. return
  86. }
  87. engine := MasterDb(c)
  88. data := db.GetStoreQrcodeById(engine, id)
  89. if data == nil {
  90. e.OutErr(c, 400, e.NewErr(400, "桌码不存在"))
  91. return
  92. }
  93. if data.IsUse != 1 {
  94. e.OutErr(c, 400, e.NewErr(400, "桌码不可用"))
  95. return
  96. }
  97. type RefundReq struct {
  98. StoreId string `json:"store_id" `
  99. StoreType string `json:"store_type" `
  100. TableNumber string `json:"table_number" `
  101. AppId string `json:"app_id" `
  102. AppLogo string `json:"app_logo"`
  103. InviteCode string `json:"invite_code"`
  104. }
  105. store := db.GetStoreIdEg(engine, utils.IntToStr(data.Uid))
  106. if store == nil {
  107. e.OutErr(c, 400, e.NewErr(400, "门店不存在"))
  108. return
  109. }
  110. user, _ := db.UserProfileFindByID(engine, store.Uid)
  111. var appId = ""
  112. var appLogo = ""
  113. var inviteCode = user.InviteCode
  114. if kind != "alipay" && kind != "alipay_applet" {
  115. communityTeamStoreWxAppletId := db.SysCfgGet(c, "community_team_store_wx_applet_id")
  116. var wxApplet model.WxAppletList
  117. MasterDb(c).Where("id=?", communityTeamStoreWxAppletId).Get(&wxApplet)
  118. appId = wxApplet.OriginalAppId
  119. appLogo = wxApplet.AppletLogo
  120. }
  121. var result = RefundReq{
  122. StoreId: utils.IntToStr(data.Uid),
  123. TableNumber: data.TableNum,
  124. AppId: appId,
  125. StoreType: utils.IntToStr(store.StoreType),
  126. InviteCode: inviteCode,
  127. AppLogo: appLogo,
  128. }
  129. e.OutSuc(c, result, nil)
  130. return
  131. }
  132. func BankStore(c *gin.Context) {
  133. var arg map[string]string
  134. if err := c.ShouldBindJSON(&arg); err != nil {
  135. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  136. return
  137. }
  138. arg["store_type"] = "0"
  139. user, _ := GetDefaultUser(c, c.GetHeader("Authorization"))
  140. var store = make([]map[string]string, 0)
  141. if arg["cid"] == "2" || arg["is_like"] == "1" {
  142. store = db.GetStoreLike(MasterDb(c), arg)
  143. } else {
  144. store = db.GetStore(MasterDb(c), arg)
  145. }
  146. storeList := make([]map[string]interface{}, 0)
  147. for _, v := range store {
  148. km := v["km"]
  149. if utils.StrToFloat64(v["km"]) < 1 {
  150. v["km"] = utils.IntToStr(int(utils.StrToFloat64(v["km"])*1000)) + "m"
  151. } else {
  152. v["km"] = GetCommissionPrec(c, v["km"], "2", "1")
  153. v["km"] += "km"
  154. }
  155. if utils.StrToFloat64(km) <= 0 || utils.StrToFloat64(v["lat"]) == 0 || utils.StrToFloat64(v["lng"]) == 0 {
  156. v["km"] = "-"
  157. }
  158. label := make([]string, 0)
  159. if v["is_new"] == "1" {
  160. label = append(label, "新门店")
  161. }
  162. isWork := "1"
  163. if v["work_state"] == "1" {
  164. isWork = "0"
  165. }
  166. tmp := map[string]interface{}{
  167. "lat": v["lat"],
  168. "lng": v["lng"],
  169. "address": v["address"],
  170. "name": v["name"],
  171. "id": v["id"],
  172. "km": v["km"],
  173. "time_str": v["timer"],
  174. "uid": v["uid"],
  175. "phone": v["phone"],
  176. "logo": v["logo"],
  177. "is_work": isWork,
  178. "is_like": "0",
  179. "fan": "",
  180. "label": label,
  181. }
  182. if utils.StrToInt(v["fan"]) > 0 {
  183. tmp["fan"] = v["fan"]
  184. }
  185. if user != nil {
  186. count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, v["id"]).Count(&model.CommunityTeamStoreLike{})
  187. if count > 0 {
  188. tmp["is_like"] = "1"
  189. }
  190. }
  191. storeList = append(storeList, tmp)
  192. }
  193. e.OutSuc(c, storeList, nil)
  194. return
  195. }
  196. func BankNotice(c *gin.Context) {
  197. var arg map[string]string
  198. if err := c.ShouldBindJSON(&arg); err != nil {
  199. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  200. return
  201. }
  202. data := db.GetBankNotice(MasterDb(c), arg)
  203. list := make([]map[string]string, 0)
  204. if data != nil {
  205. for _, v := range *data {
  206. tmp := map[string]string{
  207. "id": utils.IntToStr(v.Id),
  208. "title": v.Title,
  209. "content": v.Content,
  210. "create_time": v.CreateTime.Format("2006-01-02 15:04"),
  211. }
  212. list = append(list, tmp)
  213. }
  214. }
  215. e.OutSuc(c, list, nil)
  216. return
  217. }
  218. func BankActivity(c *gin.Context) {
  219. var arg map[string]string
  220. if err := c.ShouldBindJSON(&arg); err != nil {
  221. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  222. return
  223. }
  224. list := make([]map[string]interface{}, 0)
  225. cate := db.GetActivityCate(MasterDb(c), arg["uid"])
  226. if cate != nil {
  227. ids := make([]int, 0)
  228. for _, v := range *cate {
  229. ids = append(ids, v.Id)
  230. }
  231. activityMap := make(map[int][]map[string]interface{})
  232. if len(ids) > 0 {
  233. activity := db.GetActivity(MasterDb(c), ids)
  234. if activity != nil {
  235. scheme, host := ImageBucket(c)
  236. for _, v := range *activity {
  237. _, ok := activityMap[v.Cid]
  238. if ok == false {
  239. activityMap[v.Cid] = make([]map[string]interface{}, 0)
  240. }
  241. var skip md.Skip
  242. json.Unmarshal(utils.MarshalJSONCamelCase2JsonSnakeCase(v.Skip), &skip)
  243. tmp := map[string]interface{}{
  244. "name": v.Name, "img": ImageFormatWithBucket(scheme, host, v.Img), "skip": skip,
  245. }
  246. activityMap[v.Cid] = append(activityMap[v.Cid], tmp)
  247. }
  248. }
  249. }
  250. for _, v := range *cate {
  251. tmpList := make([]map[string]interface{}, 0)
  252. _, ok := activityMap[v.Id]
  253. if ok {
  254. tmpList = activityMap[v.Id]
  255. }
  256. tmp := map[string]interface{}{
  257. "name": v.Name,
  258. "list": tmpList,
  259. }
  260. list = append(list, tmp)
  261. }
  262. }
  263. e.OutSuc(c, list, nil)
  264. return
  265. }
  266. func BankCardDetail(c *gin.Context) {
  267. var arg map[string]string
  268. if err := c.ShouldBindJSON(&arg); err != nil {
  269. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  270. return
  271. }
  272. detail := db.GetCardDetail(MasterDb(c), arg)
  273. if detail == nil {
  274. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  275. return
  276. }
  277. tmp := map[string]string{
  278. "id": utils.IntToStr(detail.Id),
  279. "title": detail.Title,
  280. "first_tip": detail.FirstTip,
  281. "second_tip": detail.SecondTip,
  282. "time_str": "",
  283. "content": detail.Content,
  284. "type": "0",
  285. }
  286. if tmp["time_str"] != "" {
  287. tmp["type"] = "1"
  288. }
  289. if detail.IsUse == 0 {
  290. tmp["type"] = "2"
  291. }
  292. e.OutSuc(c, tmp, nil)
  293. return
  294. }
  295. func BankCard(c *gin.Context) {
  296. var arg map[string]string
  297. if err := c.ShouldBindJSON(&arg); err != nil {
  298. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  299. return
  300. }
  301. list := make([]map[string]string, 0)
  302. data := db.GetCard(MasterDb(c), arg)
  303. if data != nil {
  304. scheme, host := ImageBucket(c)
  305. for _, v := range *data {
  306. tmp := map[string]string{
  307. "id": utils.IntToStr(v.Id),
  308. "title": v.Title,
  309. "first_tip": v.FirstTip,
  310. "second_tip": v.SecondTip,
  311. "time_str": "",
  312. "img": ImageFormatWithBucket(scheme, host, v.Img),
  313. "type": "0",
  314. }
  315. if tmp["time_str"] != "" {
  316. tmp["type"] = "1"
  317. }
  318. if v.IsUse == 0 {
  319. tmp["type"] = "2"
  320. }
  321. list = append(list, tmp)
  322. }
  323. }
  324. e.OutSuc(c, list, nil)
  325. return
  326. }
  327. func NewStore(c *gin.Context) {
  328. var arg map[string]string
  329. if err := c.ShouldBindJSON(&arg); err != nil {
  330. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  331. return
  332. }
  333. arg["store_type"] = "3"
  334. user, _ := GetDefaultUser(c, c.GetHeader("Authorization"))
  335. var store = make([]map[string]string, 0)
  336. if arg["cid"] == "2" {
  337. store = db.GetStoreLike(MasterDb(c), arg)
  338. } else {
  339. store = db.GetStore(MasterDb(c), arg)
  340. }
  341. storeList := make([]map[string]interface{}, 0)
  342. for _, v := range store {
  343. km := v["km"]
  344. if utils.StrToFloat64(v["km"]) < 1 {
  345. v["km"] = utils.IntToStr(int(utils.StrToFloat64(v["km"])*1000)) + "m"
  346. } else {
  347. v["km"] = GetCommissionPrec(c, v["km"], "2", "1")
  348. v["km"] += "km"
  349. }
  350. if utils.StrToFloat64(km) <= 0 || utils.StrToFloat64(v["lat"]) == 0 || utils.StrToFloat64(v["lng"]) == 0 {
  351. v["km"] = "-"
  352. }
  353. tmp := map[string]interface{}{
  354. "lat": v["lat"],
  355. "lng": v["lng"],
  356. "address": v["address"],
  357. "name": v["name"],
  358. "id": v["id"],
  359. "km": v["km"],
  360. "time_str": v["timer"],
  361. "uid": v["uid"],
  362. "store_id": v["uid"],
  363. "store_type": v["store_type"],
  364. "phone": v["phone"],
  365. "logo": v["logo"],
  366. "is_like": "0",
  367. "fan": "",
  368. }
  369. if user != nil {
  370. count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, v["id"]).Count(&model.CommunityTeamStoreLike{})
  371. if count > 0 {
  372. tmp["is_like"] = "1"
  373. }
  374. }
  375. storeList = append(storeList, tmp)
  376. }
  377. e.OutSuc(c, storeList, nil)
  378. return
  379. }
  380. func StoreLike(c *gin.Context) {
  381. var arg map[string]string
  382. if err := c.ShouldBindJSON(&arg); err != nil {
  383. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  384. return
  385. }
  386. arg["store_type"] = "0"
  387. storeId := GetStoreId(c)
  388. if utils.StrToInt(storeId) > 0 {
  389. arg["store_id"] = storeId
  390. storeData := db.GetStoreIdEg(MasterDb(c), storeId)
  391. if storeData.StoreType == 0 {
  392. arg["store_id"] = "0"
  393. }
  394. arg["store_type"] = utils.IntToStr(storeData.StoreType)
  395. }
  396. user, _ := GetDefaultUser(c, c.GetHeader("Authorization"))
  397. store := db.GetStoreLike(MasterDb(c), arg)
  398. storeList := make([]map[string]interface{}, 0)
  399. for _, v := range store {
  400. km := v["km"]
  401. if utils.StrToFloat64(v["km"]) < 1 {
  402. v["km"] = utils.IntToStr(int(utils.StrToFloat64(v["km"])*1000)) + "m"
  403. } else {
  404. v["km"] = GetCommissionPrec(c, v["km"], "2", "1")
  405. v["km"] += "km"
  406. }
  407. if utils.StrToFloat64(km) <= 0 || utils.StrToFloat64(v["lat"]) == 0 || utils.StrToFloat64(v["lng"]) == 0 {
  408. v["km"] = "-"
  409. }
  410. label := make([]string, 0)
  411. tmp := map[string]interface{}{
  412. "lat": v["lat"],
  413. "lng": v["lng"],
  414. "address": v["address"],
  415. "work_state": v["work_state"],
  416. "name": v["name"],
  417. "id": v["id"],
  418. "uid": v["uid"],
  419. "km": v["km"],
  420. "time_str": v["timer"],
  421. "phone": v["phone"],
  422. "label": label,
  423. "is_like": "0",
  424. }
  425. if user != nil {
  426. count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, v["id"]).Count(&model.CommunityTeamStoreLike{})
  427. if count > 0 {
  428. tmp["is_like"] = "1"
  429. }
  430. }
  431. storeList = append(storeList, tmp)
  432. }
  433. e.OutSuc(c, storeList, nil)
  434. return
  435. }
  436. func GetStoreId(c *gin.Context) string {
  437. storeId := c.GetHeader("store_id")
  438. if utils.StrToInt(storeId) == 0 {
  439. storeId = c.GetHeader("storeId")
  440. }
  441. if utils.StrToInt(storeId) == 0 {
  442. storeId = c.GetHeader("storeid")
  443. }
  444. if utils.StrToInt(storeId) == 0 {
  445. storeId = c.GetHeader("StoreId")
  446. }
  447. if utils.StrToInt(storeId) == 0 {
  448. storeId = c.GetHeader("Store_id")
  449. }
  450. return storeId
  451. }
  452. func Store(c *gin.Context) {
  453. var arg map[string]string
  454. if err := c.ShouldBindJSON(&arg); err != nil {
  455. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  456. return
  457. }
  458. arg["store_type"] = "0"
  459. storeId := GetStoreId(c)
  460. if utils.StrToInt(storeId) > 0 {
  461. arg["store_id"] = storeId
  462. storeData := db.GetStoreIdEg(MasterDb(c), storeId)
  463. if storeData.StoreType == 0 {
  464. arg["store_id"] = "0"
  465. }
  466. arg["store_type"] = utils.IntToStr(storeData.StoreType)
  467. }
  468. user, _ := GetDefaultUser(c, c.GetHeader("Authorization"))
  469. store := db.GetStore(MasterDb(c), arg)
  470. storeList := make([]map[string]interface{}, 0)
  471. for _, v := range store {
  472. km := v["km"]
  473. if utils.StrToFloat64(v["km"]) < 1 {
  474. v["km"] = utils.IntToStr(int(utils.StrToFloat64(v["km"])*1000)) + "m"
  475. } else {
  476. v["km"] = GetCommissionPrec(c, v["km"], "2", "1")
  477. v["km"] += "km"
  478. }
  479. if utils.StrToFloat64(km) <= 0 || utils.StrToFloat64(v["lat"]) == 0 || utils.StrToFloat64(v["lng"]) == 0 {
  480. v["km"] = "-"
  481. }
  482. label := make([]string, 0)
  483. tmp := map[string]interface{}{
  484. "lat": v["lat"],
  485. "lng": v["lng"],
  486. "address": v["address"],
  487. "work_state": v["work_state"],
  488. "name": v["name"],
  489. "id": v["id"],
  490. "uid": v["uid"],
  491. "km": v["km"],
  492. "time_str": v["timer"],
  493. "phone": v["phone"],
  494. "label": label,
  495. "is_like": "0",
  496. }
  497. if user != nil {
  498. count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, v["id"]).Count(&model.CommunityTeamStoreLike{})
  499. if count > 0 {
  500. tmp["is_like"] = "1"
  501. }
  502. }
  503. storeList = append(storeList, tmp)
  504. }
  505. e.OutSuc(c, storeList, nil)
  506. return
  507. }
  508. func StoreAddLike(c *gin.Context) {
  509. var arg map[string]string
  510. if err := c.ShouldBindJSON(&arg); err != nil {
  511. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  512. return
  513. }
  514. user := GetUser(c)
  515. count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, arg["id"]).Count(&model.CommunityTeamStoreLike{})
  516. if count > 0 {
  517. e.OutErr(c, 400, e.NewErr(400, "已收藏"))
  518. return
  519. }
  520. var data = model.CommunityTeamStoreLike{
  521. Uid: user.Info.Uid,
  522. StoreId: utils.StrToInt(arg["id"]),
  523. Time: time.Now(),
  524. }
  525. MasterDb(c).Insert(&data)
  526. store := db.GetStoreIdEg(MasterDb(c), arg["id"])
  527. store.Fan++
  528. MasterDb(c).Where("id=?", store.Id).Cols("fan").Update(store)
  529. e.OutSuc(c, "success", nil)
  530. return
  531. }
  532. func StoreCancelLike(c *gin.Context) {
  533. var arg map[string]string
  534. if err := c.ShouldBindJSON(&arg); err != nil {
  535. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  536. return
  537. }
  538. user := GetUser(c)
  539. MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, arg["id"]).Delete(&model.CommunityTeamStoreLike{})
  540. store := db.GetStoreIdEg(MasterDb(c), arg["id"])
  541. store.Fan--
  542. if store.Fan < 0 {
  543. store.Fan = 0
  544. }
  545. MasterDb(c).Where("id=?", store.Id).Cols("fan").Update(store)
  546. e.OutSuc(c, "success", nil)
  547. return
  548. }