index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import {
  2. getLoraList,
  3. createAiImg,
  4. getTaskResult,
  5. } from '~/api/avatar'
  6. import {
  7. userEvent
  8. } from '~/api/global'
  9. Page({
  10. data: {
  11. templates: [],
  12. loading: false,
  13. active: '',
  14. artistic: '',
  15. original: ''
  16. },
  17. onLoad() {
  18. getLoraList().then(res => {
  19. this.setData({
  20. templates: res.loras,
  21. active: res.loras[0] ? res.loras[0].lora_id : ''
  22. })
  23. })
  24. userEvent({
  25. action: 'AI_LOGIN'
  26. })
  27. },
  28. async selectTemplate({
  29. currentTarget
  30. }) {
  31. if (this.data.loading) {
  32. return
  33. }
  34. this.setData({
  35. active: currentTarget.dataset.id
  36. })
  37. if (this.data.original) {
  38. this.createAiImg()
  39. }
  40. await userEvent({
  41. action: "AI_STYLE",
  42. targetContent: currentTarget.dataset.name
  43. })
  44. },
  45. uploadImg() {
  46. if (this.data.loading) {
  47. return
  48. }
  49. wx.chooseMedia({
  50. count: 1,
  51. mediaType: ['image'],
  52. sizeType: ['compressed'], // original 原图;compressed 压缩图
  53. sourceType: ['album', 'camera'],
  54. camera: 'back',
  55. success: (res) => {
  56. this.cropper = this.selectComponent("#cropper");
  57. this.cropper.init({
  58. imgPath: res.tempFiles[0].tempFilePath, //imgPath是需要裁剪图片的图片路径,只支持本地或临时路径
  59. success: (imgUrl) => {
  60. wx.getFileSystemManager().readFile({
  61. filePath: imgUrl,
  62. encoding: "base64",
  63. success: res => {
  64. //返回base64格式
  65. var base64Str = 'data:image/png' + ';base64,' + res.data
  66. this.setData({
  67. artistic: base64Str,
  68. original: base64Str
  69. })
  70. this.createAiImg()
  71. userEvent({
  72. action: "AI_PHOTO"
  73. })
  74. },
  75. fail: err => {
  76. console.log(err)
  77. }
  78. })
  79. },
  80. fail(error) {
  81. console.log(error) //有两种:cancel代表点击了叉,fail代表wx.canvasToTempFilePath生成图片失败
  82. }
  83. });
  84. }
  85. })
  86. },
  87. createAiImg() {
  88. if (!this.data.original || this.data.loading) {
  89. return
  90. }
  91. this.setData({
  92. loading: true
  93. })
  94. createAiImg({
  95. "width": 512, //生成图片宽度
  96. "height": 512, //生成图片高度
  97. "n_samples": 1, //生成图片数量
  98. "controlnet_input_image_base64": this.data.original, //控制图片base64编码
  99. "style_id": this.data.active //控制风格id
  100. }).then(res => {
  101. this.getAiImg(res.uuid)
  102. }).catch(() => {
  103. this.setData({
  104. loading: false
  105. })
  106. wx.showToast({
  107. title: '网络异常请重试',
  108. icon: 'none',
  109. duration: 2500
  110. })
  111. })
  112. },
  113. async getAiImg(uuid) {
  114. console.log(uuid, 'uuuid');
  115. let res = await getTaskResult({
  116. uuid
  117. })
  118. if (res.data.status != 2) {
  119. setTimeout(() => {
  120. this.getAiImg(uuid)
  121. }, 2500)
  122. } else {
  123. this.setData({
  124. loading: false,
  125. artistic: res.data.generated_imgs[0]
  126. })
  127. }
  128. },
  129. downloadImg() {
  130. wx.getSetting({
  131. success: (res) => {
  132. if (res.authSetting['scope.writePhotosAlbum']) {
  133. this.base64ToImg()
  134. } else {
  135. wx.authorize({
  136. scope: 'scope.writePhotosAlbum',
  137. success: () => {
  138. this.base64ToImg()
  139. },
  140. fail(res) {
  141. wx.showModal({
  142. title: '无法保存到相册',
  143. content: '点击右上角浮点按钮->设置,进行授权',
  144. confirmText: '我知道了',
  145. showCancel: false,
  146. })
  147. }
  148. })
  149. }
  150. }
  151. })
  152. },
  153. base64ToImg() {
  154. const base64 = this.data.artistic; //base64格式图片
  155. if (!base64 || this.data.loading) {
  156. return
  157. }
  158. const time = new Date().getTime();
  159. const imgPath = wx.env.USER_DATA_PATH + "/poster" + time + "" + ".png";
  160. //如果图片字符串不含要清空的前缀,可以不执行下行代码.
  161. const imageData = base64.replace(/^data:image\/\w+;base64,/, "");
  162. const file = wx.getFileSystemManager();
  163. file.writeFileSync(imgPath, imageData, "base64");
  164. wx.saveImageToPhotosAlbum({
  165. filePath: imgPath,
  166. success: async (res) => {
  167. wx.showModal({
  168. title: '照片已保存至相册',
  169. content: '快去分享给小伙伴吧',
  170. confirmText: '我知道了',
  171. showCancel: false,
  172. })
  173. await userEvent({
  174. action: 'AI_SAVE'
  175. })
  176. }
  177. })
  178. },
  179. onShareAppMessage() {
  180. userEvent({
  181. action: 'AI_SHARE'
  182. })
  183. return {
  184. title: '玩转AI头像',
  185. path: '/pages/index/index',
  186. }
  187. }
  188. })