hy_create_link.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //执行一个创建黑岩链接任务
  2. const CMD = {}
  3. const axios = require('axios');
  4. let get_hy_recharge_template = require('../src/api/hy/get_hy_recharge_template')
  5. let get_hy_paid_point = require('../src/api/hy/get_hy_paid_point')
  6. let get_hy_call_back_template = require('../src/api/hy/get_hy_call_back_template');
  7. const tools = require('../tools');
  8. const HttpClient = require('../src/HttpClient');
  9. const helper = require('../src/helper');
  10. const config = require('../etc/config.json')
  11. //卡点
  12. function get_paid_point(book_id,kadian_name,call_back){
  13. let chapter_num = parseInt(kadian_name)
  14. if(chapter_num==0||chapter_num>10){
  15. return call_back("")
  16. }
  17. get_hy_paid_point.get_hy_paid_point(book_id,(data)=>{
  18. if(chapter_num>=data.rows.length){
  19. call_back("")
  20. }else{
  21. call_back(""+chapter_num)
  22. }
  23. })
  24. }
  25. //回传
  26. function get_call_back(huichuan_name,call_back){
  27. get_hy_call_back_template.get_callback_template((data)=>{
  28. if(data.code==200){
  29. let is_find = false
  30. for (let index = 0; index < data.data.length; index++) {
  31. const obj = data.data[index];
  32. if(obj.value==huichuan_name){
  33. is_find = true
  34. call_back(obj.key)
  35. break
  36. }
  37. }
  38. if(!is_find){
  39. call_back('6')
  40. }
  41. }else{
  42. call_back('6')
  43. }
  44. })
  45. }
  46. function get_recharge_template(book_id,chongzhi_name,call_back){
  47. get_hy_recharge_template.get_recharge_template(book_id,(data)=>{
  48. if(data.code==200){
  49. let is_find = false
  50. for (let index = 0; index < data.data.length; index++) {
  51. const obj = data.data[index];
  52. if(obj.value==chongzhi_name){
  53. is_find = true
  54. call_back(obj.key)
  55. break
  56. }
  57. }
  58. if(!is_find){
  59. call_back('0')
  60. }
  61. }else{
  62. call_back('0')
  63. }
  64. })
  65. }
  66. CMD.runTask = function(data,main_info,PlatformInfo,call_back){
  67. let group_id = main_info.group_id
  68. if(group_id==null||group_id==""){
  69. group_id = "35"
  70. }
  71. let tg_link_config = JSON.parse(main_info.tg_link_config)
  72. let kadian_id = tg_link_config['kadian_id']
  73. let chongzhi_id = tg_link_config['chongzhi_id']
  74. let huichuan_id = tg_link_config['huichuan_id']
  75. let chongzhi_list = JSON.parse(PlatformInfo.chongzhi)
  76. let huichuan_list = JSON.parse(PlatformInfo.huichuan)
  77. let kadian_list = JSON.parse(PlatformInfo.kadian)
  78. let chongzhi_name = null
  79. let huichuan_name = null
  80. let kadian_name = null
  81. let tg_config = {
  82. chongzhi_key:'',
  83. huichuan_key:'',
  84. kadian_key:''
  85. }
  86. for (let index = 0; index < chongzhi_list.length; index++) {
  87. const cz_obj = chongzhi_list[index];
  88. if(cz_obj.id==chongzhi_id){
  89. chongzhi_name = cz_obj.name
  90. tg_config.chongzhi_key = cz_obj.value
  91. break
  92. }
  93. }
  94. for (let index = 0; index < huichuan_list.length; index++) {
  95. const hc_obj = huichuan_list[index];
  96. if(hc_obj.id==huichuan_id){
  97. huichuan_name = hc_obj.name
  98. tg_config.huichuan_key = hc_obj.value
  99. break
  100. }
  101. }
  102. for (let index = 0; index < kadian_list.length; index++) {
  103. const kd_obj = kadian_list[index];
  104. if(kd_obj.id==kadian_id){
  105. kadian_name = kd_obj.name
  106. break
  107. }
  108. }
  109. let create_hy_link = async () =>{
  110. let topUpTemplateId = tg_config.chongzhi_key; //充值模板
  111. let passBackTemplateId = tg_config.huichuan_key; //回传模板
  112. let paidPoint = tg_config.kadian_key; //卡点
  113. console.log("tg_config:",tg_config)
  114. try{
  115. let clinet = new HttpClient()
  116. const postData = {
  117. cmd:"create_link",
  118. name:data.product_name,
  119. bookId:data.product_id,
  120. topUpTemplateId:topUpTemplateId,
  121. passBackTemplateId:passBackTemplateId,
  122. paidPoint:paidPoint,
  123. token:await tools.getHyToken()
  124. };
  125. let t_data = {
  126. "name": postData.name,
  127. "bookId": postData.bookId,
  128. "passBackTemplateId": postData.passBackTemplateId,
  129. "adPassbackTemplateId": "",
  130. "topUpTemplateId": postData.topUpTemplateId,
  131. "mediaChannel": "1",
  132. "group": group_id,
  133. "paidPoint": postData.paidPoint,
  134. "site": PlatformInfo.mini_program_platform_id==config.wx?"7": "5"
  135. }
  136. let res = await clinet.otherPost( helper.getHyCreateLinkOpt(postData.token) ,t_data)
  137. if(res['code']==500){
  138. throw res
  139. }
  140. if(res['code']==10001){
  141. console.log("作品不存在:",data)
  142. throw res
  143. }
  144. if(res['code']!=200){
  145. throw res
  146. }
  147. let response = {
  148. "message": "create_link successfully",
  149. "received_data":{
  150. "appId":res["data"]["appId"],
  151. "launchPath":res["data"]["launchPath"],
  152. "fullPath":res["data"]["fullPath"],
  153. "wxMountLink":res["data"]["wxMountLink"]}
  154. }
  155. let params = response.received_data.fullPath.split('?')
  156. let start_page = params[0]
  157. let start_param = params[1]
  158. const linkId = start_param.match(/linkId=([^&]*)/)[1];
  159. data.promotion_id = linkId
  160. data.start_page = start_page
  161. data.start_param = start_param
  162. call_back(data)
  163. }catch(e){
  164. call_back(data,e)
  165. console.error('There was an error!', e);
  166. }
  167. }
  168. get_paid_point(data.book_id,kadian_name,(kadian_key)=>{
  169. tg_config.kadian_key = kadian_key
  170. create_hy_link()
  171. })
  172. }
  173. module.exports = CMD;