main.py 7.1 KB

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