hy_create_link.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. if(tg_config.chongzhi_key==''){
  116. throw {msg:"充值id错误!"}
  117. }
  118. let clinet = new HttpClient()
  119. const postData = {
  120. cmd:"create_link",
  121. name:data.product_name,
  122. bookId:data.product_id,
  123. topUpTemplateId:topUpTemplateId,
  124. passBackTemplateId:passBackTemplateId,
  125. paidPoint:paidPoint,
  126. token:await tools.getHyToken()
  127. };
  128. let t_data = {
  129. "name": `${postData.name}_${main_info.id}`,
  130. "bookId": postData.bookId,
  131. "passBackTemplateId": postData.passBackTemplateId,
  132. "adPassbackTemplateId": "",
  133. "topUpTemplateId": postData.topUpTemplateId,
  134. "mediaChannel": "1",
  135. "group": group_id,
  136. "paidPoint": postData.paidPoint,
  137. "site": PlatformInfo.mini_program_platform_id==config.wx?"7": "5"
  138. }
  139. let res = await clinet.otherPost( helper.getHyCreateLinkOpt(postData.token) ,t_data)
  140. if(res['code']==500){
  141. throw res
  142. }
  143. if(res['code']==10001){
  144. console.log("作品不存在:",data)
  145. throw res
  146. }
  147. if(res['code']!=200){
  148. throw res
  149. }
  150. let response = {
  151. "message": "create_link successfully",
  152. "received_data":{
  153. "appId":res["data"]["appId"],
  154. "launchPath":res["data"]["launchPath"],
  155. "fullPath":res["data"]["fullPath"],
  156. "wxMountLink":res["data"]["wxMountLink"]}
  157. }
  158. let params = response.received_data.fullPath.split('?')
  159. let start_page = params[0]
  160. let start_param = params[1]
  161. const linkId = start_param.match(/linkId=([^&]*)/)[1];
  162. data.promotion_id = linkId
  163. data.start_page = start_page
  164. data.start_param = start_param
  165. call_back(data)
  166. }catch(e){
  167. call_back(data,e)
  168. console.error('There was an error!', e);
  169. }
  170. }
  171. get_paid_point(data.book_id,kadian_name,(kadian_key)=>{
  172. tg_config.kadian_key = kadian_key
  173. create_hy_link()
  174. })
  175. }
  176. module.exports = CMD;