index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. let res = await getTaskResult({
  115. uuid
  116. })
  117. if (res.data.status != 2) {
  118. setTimeout(() => {
  119. this.getAiImg(uuid)
  120. }, 2500)
  121. } else {
  122. this.setData({
  123. loading: false,
  124. artistic: res.data.generated_imgs[0]
  125. })
  126. }
  127. },
  128. downloadImg() {
  129. wx.getSetting({
  130. success: (res) => {
  131. if (res.authSetting['scope.writePhotosAlbum']) {
  132. this.base64ToImg()
  133. } else {
  134. wx.authorize({
  135. scope: 'scope.writePhotosAlbum',
  136. success: () => {
  137. this.base64ToImg()
  138. },
  139. fail(res) {
  140. wx.showModal({
  141. title: '无法保存到相册',
  142. content: '点击右上角浮点按钮->设置,进行授权',
  143. confirmText: '我知道了',
  144. showCancel: false,
  145. })
  146. }
  147. })
  148. }
  149. }
  150. })
  151. },
  152. base64ToImg() {
  153. const base64 = this.data.artistic; //base64格式图片
  154. if (!base64 || this.data.loading) {
  155. return
  156. }
  157. const time = new Date().getTime();
  158. const imgPath = wx.env.USER_DATA_PATH + "/poster" + time + "" + ".png";
  159. //如果图片字符串不含要清空的前缀,可以不执行下行代码.
  160. const imageData = base64.replace(/^data:image\/\w+;base64,/, "");
  161. const file = wx.getFileSystemManager();
  162. file.writeFileSync(imgPath, imageData, "base64");
  163. wx.saveImageToPhotosAlbum({
  164. filePath: imgPath,
  165. success: async (res) => {
  166. wx.showModal({
  167. title: '照片已保存至相册',
  168. content: '快去分享给小伙伴吧',
  169. confirmText: '我知道了',
  170. showCancel: false,
  171. })
  172. await userEvent({
  173. action: 'AI_SAVE'
  174. })
  175. }
  176. })
  177. },
  178. onShareAppMessage() {
  179. userEvent({
  180. action: 'AI_SHARE'
  181. })
  182. return {
  183. title: '玩转AI头像',
  184. path: '/pages/index/index',
  185. }
  186. }
  187. })