main.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import os
  2. import flask
  3. import requests
  4. import json
  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. "git clone https://git.yishihui.com/weapp/longVideoFactory.git;"
  19. "cd longVideoFactory;"
  20. "rm -rf dist;"
  21. "git pull;"
  22. "git checkout "+branch+";"
  23. "git pull;"
  24. "n 8;"
  25. "cnpm install && npm install ;"
  26. "npm run build && npm run build:other;"
  27. )
  28. if os.system(command_str) == 0:
  29. res_json["code"] = 0
  30. res_json["msg"] = "打包成功"
  31. else:
  32. res_json["code"] = 1
  33. res_json["msg"] = "打包失败"
  34. # # 3.微信编译图片
  35. def create_preview(project,page):
  36. # 小程序 cli path for mac
  37. wx_cli ="/Applications/wechatwebdevtools.app/Contents/MacOS/cli"
  38. dist_path = os.path.join(current_path,'longVideoFactory/dist/'+project)
  39. code_img_path = os.path.join(current_path,'1.png')
  40. path = """ '{"pathName":"%s"}' """ % page
  41. os.system(wx_cli+ "open --project " + dist_path)
  42. os.system(wx_cli+ "reset-fileutils --project " + dist_path)
  43. f_read = os.popen(wx_cli+" preview --project "+dist_path+" --compile-condition "+path+" --qr-output "+code_img_path+" --qr-format image").read()
  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. @app.route('/send',methods=['POST'])
  137. def send():
  138. global send_status
  139. if send_status == True:
  140. res_json["code"] = 9
  141. res_json["msg"] = "当前有人打包,请稍后尝试"
  142. return res_json
  143. send_status = True
  144. res_json["code"] = 0
  145. res_json["msg"] = "成功"
  146. webhook_url = request.json["webhook"]
  147. branch = request.json["branch"]
  148. page = request.json["page"]
  149. project = request.json["project"]
  150. mode = request.json["mode"]
  151. project_name = request.json["project_name"]
  152. mode_name = request.json["mode_name"]
  153. # ------- 微信打包 ------
  154. # 1.小程序打包
  155. build_mini_program(branch)
  156. if res_json["code"] != 0:
  157. send_status = False
  158. return res_json
  159. # 2.修改环境
  160. edit_mode_by_file(project,mode)
  161. if res_json["code"] != 0:
  162. send_status = False
  163. return res_json
  164. # 3.微信编译图片
  165. create_preview(project,page)
  166. # -------- 飞书 --------
  167. # 1.获取签名
  168. if res_json["code"] != 0:
  169. send_status = False
  170. return res_json
  171. token = tenant_access_token_by_feishu()
  172. # # 2.获取imgKey
  173. if res_json["code"] != 0:
  174. send_status = False
  175. return res_json
  176. img_key = upload_img_by_feishu(token)
  177. # # 3.飞书发送消息
  178. if res_json["code"] != 0:
  179. send_status = False
  180. return res_json
  181. send_msg_by_feishu(token,img_key,webhook_url,project_name,mode_name)
  182. send_status = False
  183. return res_json
  184. @app.route("/getBranch")
  185. def get_branch():
  186. # 查看所有分支
  187. command_str= (
  188. "cd "+current_path+ ";"
  189. "git clone https://git.yishihui.com/weapp/longVideoFactory.git;"
  190. "cd longVideoFactory;"
  191. "git branch -a;"
  192. )
  193. f_read = os.popen(command_str).read()
  194. branch_list = []
  195. for item in f_read.split():
  196. if "->" not in item and "origin/master" not in item and "HEAD" not in item and "*" not in item:
  197. branch_list.append(item.replace("remotes/origin/",""))
  198. res = {"code":0,"data":branch_list}
  199. return res
  200. if __name__ == '__main__':
  201. app.run(host='0.0.0.0', port=7777, debug=False)