main.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import os
  2. import requests
  3. import json
  4. import time
  5. from flask import Flask,request,redirect
  6. from flask_cors import CORS
  7. app = Flask(__name__,static_folder="static")
  8. CORS(app, resources=r'/*')
  9. # current_path
  10. current_path = os.getcwd()
  11. res_json = {"code":0,"msg":"成功"}
  12. send_status = {'a':False}
  13. # 1.小程序打包
  14. def build_mini_program(branch):
  15. res = False
  16. command_str = (
  17. "cd "+current_path+ ";"
  18. "cd longVideoFactory;"
  19. "rm -rf dist;"
  20. "git checkout "+branch+";"
  21. "git pull;"
  22. "cnpm install && npm install ;"
  23. "npm run build;"
  24. )
  25. if os.system(command_str) == 0:
  26. res_json["code"] = 0
  27. res_json["msg"] = "打包成功"
  28. else:
  29. res_json["code"] = 1
  30. res_json["msg"] = "打包失败"
  31. # # 3.微信编译图片
  32. def create_preview(project,page):
  33. # 小程序 cli path for mac
  34. wx_cli ="/Applications/wechatwebdevtools.app/Contents/MacOS/cli "
  35. dist_path = os.path.join(current_path,'longVideoFactory/dist/'+project)
  36. code_img_path = os.path.join(current_path,'1.png')
  37. path = """ '{"pathName":"%s"}' """ % page
  38. os.system(wx_cli+ " open --project " + dist_path)
  39. os.system(wx_cli+ " reset-fileutils --project " + dist_path)
  40. print(wx_cli+" preview --project "+dist_path+" --compile-condition "+path+" --qr-output "+code_img_path+" --qr-format image")
  41. f_read = os.popen(wx_cli+" preview --project "+dist_path+" --compile-condition "+path+" --qr-output "+code_img_path+" --qr-format image").read()
  42. os.system(wx_cli+ " quit")
  43. if "[error]" in f_read:
  44. res_json["code"] = 3
  45. res_json["msg"] = f_read
  46. else:
  47. res_json["code"] = 0
  48. res_json["msg"] = "微信小程序编译成功"
  49. # 修改环境文件
  50. def edit_mode_by_file(project,mode):
  51. path = os.path.join(current_path,"longVideoFactory/dist/"+project+"/network/volatileConfig.js")
  52. file_lines = []
  53. with open(path,"r") as f:
  54. for item in f.readlines():
  55. if "export const currentMode" in item:
  56. item = "export const currentMode = MODE."+mode+" \n"
  57. file_lines.append(item)
  58. f.close()
  59. with open(path,"w") as f2:
  60. f2.writelines(file_lines)
  61. f2.close()
  62. res_json["code"] = 0
  63. res_json["msg"] = "文件修改成功"
  64. def upload_img_by_feishu(token):
  65. key = ""
  66. url = "https://open.feishu.cn/open-apis/im/v1/images"
  67. files = {
  68. "image_type":(None,"message"),
  69. "image":('1.png',open(os.path.join(current_path,"1.png"),"rb"),"image/png")
  70. }
  71. headers = {
  72. "Authorization":"Bearer "+token,
  73. }
  74. res = requests.post(url,headers=headers,files=files).json()
  75. if res["code"] == 0:
  76. key = res["data"]["image_key"]
  77. res_json["code"] = 0
  78. else:
  79. res_json["code"] = 4
  80. res_json["msg"] = "飞书上传图片失败"
  81. return key
  82. def tenant_access_token_by_feishu():
  83. token = ""
  84. url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
  85. params = {
  86. "app_id":"cli_a1ec07458838d00c",
  87. "app_secret":"Ngm0kfaLkZnpM2T2DH7L8cM5hdAqY0hI"
  88. }
  89. res = requests.post(url,data=params).json()
  90. if res["code"] == 0:
  91. token = res["tenant_access_token"]
  92. res_json["msg"] = "获取飞书签名成功"
  93. res_json["code"] = 0
  94. else:
  95. res_json["code"] = 4
  96. res_json["msg"] = "获取飞书签名失败"
  97. return token
  98. def send_msg_by_feishu(token,img_key,url,project_name,mode_name):
  99. # webhook
  100. # webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/a924b6a5-86ef-4c76-a7e4-eb42056f70cc"
  101. webhook_url = url
  102. headers = {
  103. "Authorization":"Bearer "+token,
  104. "Content-Type": "application/json;charset=UTF-8"
  105. }
  106. params = {
  107. "msg_type": "post",
  108. "content": {
  109. "post": {
  110. "zh_cn": {
  111. "title": mode_name+':'+project_name+"小程序预览码",
  112. "content": [
  113. [
  114. {
  115. "tag": "img",
  116. "image_key": img_key,
  117. "width": 500,
  118. "height": 500
  119. }
  120. ]]
  121. }
  122. }
  123. }
  124. }
  125. res = requests.post(webhook_url,headers=headers,data=json.dumps(params)).json()
  126. if res["StatusCode"] == 0:
  127. res_json["code"] = 0
  128. res_json["msg"] = "飞书消息发送成功"
  129. else:
  130. res_json["code"] = 5
  131. res_json["msg"] = "飞书消息发送失败"
  132. @app.route('/')
  133. def func():
  134. # return 'Welcome to auto-mini-program-build-preview server. only can run on MacOS.'
  135. return redirect("static/dist/index.html")
  136. @app.route('/send',methods=['POST'])
  137. def send():
  138. global send_status
  139. try:
  140. if send_status == True:
  141. res_json["code"] = 9
  142. res_json["msg"] = "当前有人打包,请稍后尝试"
  143. return res_json
  144. send_status = True
  145. res_json["code"] = 0
  146. res_json["msg"] = "成功"
  147. webhook_url = request.json["webhook"]
  148. branch = request.json["branch"]
  149. page = request.json["page"]
  150. project = request.json["project"]
  151. mode = request.json["mode"]
  152. project_name = request.json["project_name"]
  153. mode_name = request.json["mode_name"]
  154. # ------- 微信打包 ------
  155. # 1.小程序打包
  156. build_mini_program(branch)
  157. if res_json["code"] != 0:
  158. send_status = False
  159. return res_json
  160. # 2.修改环境
  161. edit_mode_by_file(project,mode)
  162. if res_json["code"] != 0:
  163. send_status = False
  164. return res_json
  165. # 3.微信编译图片
  166. time.sleep(1)
  167. create_preview(project,page)
  168. # -------- 飞书 --------
  169. # 1.获取签名
  170. if res_json["code"] != 0:
  171. send_status = False
  172. return res_json
  173. token = tenant_access_token_by_feishu()
  174. # # 2.获取imgKey
  175. if res_json["code"] != 0:
  176. send_status = False
  177. return res_json
  178. img_key = upload_img_by_feishu(token)
  179. # # 3.飞书发送消息
  180. if res_json["code"] != 0:
  181. send_status = False
  182. return res_json
  183. send_msg_by_feishu(token,img_key,webhook_url,project_name,mode_name)
  184. send_status = False
  185. except:
  186. send_status = False
  187. return res_json
  188. @app.route("/getBranch")
  189. def get_branch():
  190. # 查看所有分支
  191. command_str= (
  192. "cd "+current_path+ ";"
  193. "git clone https://git.yishihui.com/weapp/longVideoFactory.git;"
  194. "cd longVideoFactory;"
  195. "git pull;"
  196. )
  197. os.system(command_str)
  198. command_str= (
  199. "cd "+current_path+ ";"
  200. "cd longVideoFactory;"
  201. "git branch -r;"
  202. )
  203. f_read = os.popen(command_str).read()
  204. branch_list = []
  205. for item in f_read.split():
  206. if "->" not in item and "HEAD" not in item and "*" not in item:
  207. branch_list.append(item.replace("origin/",""))
  208. res = {"code":0,"data":branch_list}
  209. return res
  210. if __name__ == '__main__':
  211. app.run(host='0.0.0.0', port=7777, debug=False)
  212. # token = tenant_access_token_by_feishu()
  213. # headers = {
  214. # "Authorization":"Bearer "+token,
  215. # "Content-Type": "application/json;charset=UTF-8"
  216. # }
  217. # url = "https://open.feishu.cn/open-apis/im/v1/chats"
  218. # res = requests.post(url,headers=headers).json()
  219. # print(res)
  220. # https://open.feishu.cn/open-apis/bot/v2/hook/a243326f-90cf-4a70-ba26-a6b202710971
  221. # https://open.feishu.cn/open-apis/bot/v2/hook/a924b6a5-86ef-4c76-a7e4-eb42056f70cc